Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 20 | "github.com/stretchr/testify/assert" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 24 | func MakeServerTestConfig() *ServerConfig { |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 25 | var routerPackage []RouterPackage |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 26 | |
| 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 |
| 46 | func TestServerInit(t *testing.T) { |
| 47 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 48 | serverConfig := MakeServerTestConfig() |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 49 | |
| 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 |
| 58 | func TestServerInitWrongAddr(t *testing.T) { |
| 59 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 60 | serverConfig := MakeServerTestConfig() |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 61 | 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 |
| 70 | func TestServerInitWrongPort(t *testing.T) { |
| 71 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 72 | serverConfig := MakeServerTestConfig() |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 73 | 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 |
| 82 | func TestServerInitNoName(t *testing.T) { |
| 83 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 84 | serverConfig := MakeServerTestConfig() |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 85 | 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 |
| 94 | func TestServerInitWrongType(t *testing.T) { |
| 95 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 96 | serverConfig := MakeServerTestConfig() |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 97 | 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 |
| 106 | func TestServerInitNoRouter(t *testing.T) { |
| 107 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 108 | serverConfig := MakeServerTestConfig() |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 109 | 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 |
| 118 | func TestServerInitHandler(t *testing.T) { |
| 119 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 120 | serverConfig := MakeServerTestConfig() |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 121 | serverConfig.Port = 55556 |
| 122 | |
| 123 | serv, err := newServer(serverConfig) |
| 124 | |
| 125 | assert.NotNil(t, serv) |
| 126 | assert.Nil(t, err) |
| 127 | |
| 128 | } |