blob: cfabbd38f65a43a572d884065fc53c3db5cca626 [file] [log] [blame]
Daniele Rossiadc95bc2019-10-09 11:09:53 +02001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package afrouter
18
19import (
Daniele Rossi17007a42019-10-17 16:21:52 +020020 "github.com/phayes/freeport"
Daniele Rossiadc95bc2019-10-09 11:09:53 +020021 "github.com/stretchr/testify/assert"
22 "testing"
23)
24
Daniele Rossi17007a42019-10-17 16:21:52 +020025func MakeServerTestConfig() (*ServerConfig, error) {
Daniele Rossiadc95bc2019-10-09 11:09:53 +020026 var routerPackage []RouterPackage
Daniele Rossiadc95bc2019-10-09 11:09:53 +020027
Daniele Rossi17007a42019-10-17 16:21:52 +020028 freePort, errP := freeport.GetFreePort()
29
Daniele Rossiadc95bc2019-10-09 11:09:53 +020030 routerPackageConfig := RouterPackage{
31 Router: `json:"router"`,
32 Package: `json:"package"`,
33 }
34 routerPackage = append(routerPackage, routerPackageConfig)
35
36 serverConfig := ServerConfig{
37 Name: "grpc_command",
Daniele Rossi17007a42019-10-17 16:21:52 +020038 Port: uint(freePort),
Daniele Rossiadc95bc2019-10-09 11:09:53 +020039 Addr: "127.0.0.1",
40 Type: "grpc",
41 Routers: routerPackage,
42 routers: make(map[string]*RouterConfig),
43 }
Daniele Rossi17007a42019-10-17 16:21:52 +020044 return &serverConfig, errP
Daniele Rossiadc95bc2019-10-09 11:09:53 +020045
46}
47
48// Test creation of a new Server
49func TestServerInit(t *testing.T) {
50
Daniele Rossi17007a42019-10-17 16:21:52 +020051 serverConfig, errConf := MakeServerTestConfig()
52 assert.NotNil(t, serverConfig)
53 assert.Nil(t, errConf)
Daniele Rossiadc95bc2019-10-09 11:09:53 +020054
55 serv, err := newServer(serverConfig)
56
57 assert.NotNil(t, serv)
58 assert.Nil(t, err)
59
60}
61
62// Test creation of a new Server, error in Addr
63func TestServerInitWrongAddr(t *testing.T) {
64
Daniele Rossi17007a42019-10-17 16:21:52 +020065 serverConfig, errConf := MakeServerTestConfig()
66 assert.NotNil(t, serverConfig)
67 assert.Nil(t, errConf)
68
Daniele Rossiadc95bc2019-10-09 11:09:53 +020069 serverConfig.Addr = "127.300.1.1"
70
71 serv, err := newServer(serverConfig)
72
73 assert.Nil(t, serv)
74 assert.NotNil(t, err)
75}
76
77// Test creation of a new Server, error in Port
78func TestServerInitWrongPort(t *testing.T) {
79
Daniele Rossi17007a42019-10-17 16:21:52 +020080 serverConfig, errConf := MakeServerTestConfig()
81 assert.NotNil(t, serverConfig)
82 assert.Nil(t, errConf)
83
Daniele Rossiadc95bc2019-10-09 11:09:53 +020084 serverConfig.Port = 23
85
86 serv, err := newServer(serverConfig)
87
88 assert.Nil(t, serv)
89 assert.NotNil(t, err)
90}
91
92// Test creation of a new Server, error in Name
93func TestServerInitNoName(t *testing.T) {
94
Daniele Rossi17007a42019-10-17 16:21:52 +020095 serverConfig, errConf := MakeServerTestConfig()
96 assert.NotNil(t, serverConfig)
97 assert.Nil(t, errConf)
98
Daniele Rossiadc95bc2019-10-09 11:09:53 +020099 serverConfig.Name = ""
100
101 serv, err := newServer(serverConfig)
102
103 assert.Nil(t, serv)
104 assert.NotNil(t, err)
105}
106
107// Test creation of a new Server, error in Type
108func TestServerInitWrongType(t *testing.T) {
109
Daniele Rossi17007a42019-10-17 16:21:52 +0200110 serverConfig, errConf := MakeServerTestConfig()
111 assert.NotNil(t, serverConfig)
112 assert.Nil(t, errConf)
113
Daniele Rossiadc95bc2019-10-09 11:09:53 +0200114 serverConfig.Type = "xxx"
115
116 serv, err := newServer(serverConfig)
117
118 assert.Nil(t, serv)
119 assert.NotNil(t, err)
120}
121
Daniele Rossi17007a42019-10-17 16:21:52 +0200122// Test creation of a new Server, error in Routers
Daniele Rossiadc95bc2019-10-09 11:09:53 +0200123func TestServerInitNoRouter(t *testing.T) {
124
Daniele Rossi17007a42019-10-17 16:21:52 +0200125 serverConfig, errConf := MakeServerTestConfig()
126 assert.NotNil(t, serverConfig)
127 assert.Nil(t, errConf)
128
129 serverConfig.Routers = nil
Daniele Rossiadc95bc2019-10-09 11:09:53 +0200130
131 serv, err := newServer(serverConfig)
132
133 assert.Nil(t, serv)
134 assert.NotNil(t, err)
135}