blob: 2c39c53d14e0aa47bb141acd298d7fa8035c8118 [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 Rossiadc95bc2019-10-09 11:09:53 +020020 "github.com/stretchr/testify/assert"
21 "testing"
22)
23
Scott Baker4989fe92019-10-09 17:03:06 -070024func MakeServerTestConfig() *ServerConfig {
Daniele Rossiadc95bc2019-10-09 11:09:53 +020025 var routerPackage []RouterPackage
Daniele Rossiadc95bc2019-10-09 11:09:53 +020026
27 routerPackageConfig := RouterPackage{
28 Router: `json:"router"`,
29 Package: `json:"package"`,
30 }
31 routerPackage = append(routerPackage, routerPackageConfig)
32
33 serverConfig := ServerConfig{
34 Name: "grpc_command",
35 Port: 55555,
36 Addr: "127.0.0.1",
37 Type: "grpc",
38 Routers: routerPackage,
39 routers: make(map[string]*RouterConfig),
40 }
41 return &serverConfig
42
43}
44
45// Test creation of a new Server
46func TestServerInit(t *testing.T) {
47
Scott Baker4989fe92019-10-09 17:03:06 -070048 serverConfig := MakeServerTestConfig()
Daniele Rossiadc95bc2019-10-09 11:09:53 +020049
50 serv, err := newServer(serverConfig)
51
52 assert.NotNil(t, serv)
53 assert.Nil(t, err)
54
55}
56
57// Test creation of a new Server, error in Addr
58func TestServerInitWrongAddr(t *testing.T) {
59
Scott Baker4989fe92019-10-09 17:03:06 -070060 serverConfig := MakeServerTestConfig()
Daniele Rossiadc95bc2019-10-09 11:09:53 +020061 serverConfig.Addr = "127.300.1.1"
62
63 serv, err := newServer(serverConfig)
64
65 assert.Nil(t, serv)
66 assert.NotNil(t, err)
67}
68
69// Test creation of a new Server, error in Port
70func TestServerInitWrongPort(t *testing.T) {
71
Scott Baker4989fe92019-10-09 17:03:06 -070072 serverConfig := MakeServerTestConfig()
Daniele Rossiadc95bc2019-10-09 11:09:53 +020073 serverConfig.Port = 23
74
75 serv, err := newServer(serverConfig)
76
77 assert.Nil(t, serv)
78 assert.NotNil(t, err)
79}
80
81// Test creation of a new Server, error in Name
82func TestServerInitNoName(t *testing.T) {
83
Scott Baker4989fe92019-10-09 17:03:06 -070084 serverConfig := MakeServerTestConfig()
Daniele Rossiadc95bc2019-10-09 11:09:53 +020085 serverConfig.Name = ""
86
87 serv, err := newServer(serverConfig)
88
89 assert.Nil(t, serv)
90 assert.NotNil(t, err)
91}
92
93// Test creation of a new Server, error in Type
94func TestServerInitWrongType(t *testing.T) {
95
Scott Baker4989fe92019-10-09 17:03:06 -070096 serverConfig := MakeServerTestConfig()
Daniele Rossiadc95bc2019-10-09 11:09:53 +020097 serverConfig.Type = "xxx"
98
99 serv, err := newServer(serverConfig)
100
101 assert.Nil(t, serv)
102 assert.NotNil(t, err)
103}
104
105// Test creation of a new Server, error in Router
106func TestServerInitNoRouter(t *testing.T) {
107
Scott Baker4989fe92019-10-09 17:03:06 -0700108 serverConfig := MakeServerTestConfig()
Daniele Rossiadc95bc2019-10-09 11:09:53 +0200109 serverConfig.routers = nil
110
111 serv, err := newServer(serverConfig)
112
113 assert.Nil(t, serv)
114 assert.NotNil(t, err)
115}
116
117// Test creation of a new Server
118func TestServerInitHandler(t *testing.T) {
119
Scott Baker4989fe92019-10-09 17:03:06 -0700120 serverConfig := MakeServerTestConfig()
Daniele Rossiadc95bc2019-10-09 11:09:53 +0200121 serverConfig.Port = 55556
122
123 serv, err := newServer(serverConfig)
124
125 assert.NotNil(t, serv)
126 assert.Nil(t, err)
127
128}