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 | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 20 | "github.com/phayes/freeport" |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 21 | "github.com/stretchr/testify/assert" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 25 | func MakeServerTestConfig() (*ServerConfig, error) { |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 26 | var routerPackage []RouterPackage |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 27 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 28 | freePort, errP := freeport.GetFreePort() |
| 29 | |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 30 | 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 Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 38 | Port: uint(freePort), |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 39 | Addr: "127.0.0.1", |
| 40 | Type: "grpc", |
| 41 | Routers: routerPackage, |
| 42 | routers: make(map[string]*RouterConfig), |
| 43 | } |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 44 | return &serverConfig, errP |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 45 | |
| 46 | } |
| 47 | |
| 48 | // Test creation of a new Server |
| 49 | func TestServerInit(t *testing.T) { |
| 50 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 51 | serverConfig, errConf := MakeServerTestConfig() |
| 52 | assert.NotNil(t, serverConfig) |
| 53 | assert.Nil(t, errConf) |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 54 | |
| 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 |
| 63 | func TestServerInitWrongAddr(t *testing.T) { |
| 64 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 65 | serverConfig, errConf := MakeServerTestConfig() |
| 66 | assert.NotNil(t, serverConfig) |
| 67 | assert.Nil(t, errConf) |
| 68 | |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 69 | 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 |
| 78 | func TestServerInitWrongPort(t *testing.T) { |
| 79 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 80 | serverConfig, errConf := MakeServerTestConfig() |
| 81 | assert.NotNil(t, serverConfig) |
| 82 | assert.Nil(t, errConf) |
| 83 | |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 84 | 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 |
| 93 | func TestServerInitNoName(t *testing.T) { |
| 94 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 95 | serverConfig, errConf := MakeServerTestConfig() |
| 96 | assert.NotNil(t, serverConfig) |
| 97 | assert.Nil(t, errConf) |
| 98 | |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 99 | 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 |
| 108 | func TestServerInitWrongType(t *testing.T) { |
| 109 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 110 | serverConfig, errConf := MakeServerTestConfig() |
| 111 | assert.NotNil(t, serverConfig) |
| 112 | assert.Nil(t, errConf) |
| 113 | |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 114 | serverConfig.Type = "xxx" |
| 115 | |
| 116 | serv, err := newServer(serverConfig) |
| 117 | |
| 118 | assert.Nil(t, serv) |
| 119 | assert.NotNil(t, err) |
| 120 | } |
| 121 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 122 | // Test creation of a new Server, error in Routers |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 123 | func TestServerInitNoRouter(t *testing.T) { |
| 124 | |
Daniele Rossi | 17007a4 | 2019-10-17 16:21:52 +0200 | [diff] [blame^] | 125 | serverConfig, errConf := MakeServerTestConfig() |
| 126 | assert.NotNil(t, serverConfig) |
| 127 | assert.Nil(t, errConf) |
| 128 | |
| 129 | serverConfig.Routers = nil |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 130 | |
| 131 | serv, err := newServer(serverConfig) |
| 132 | |
| 133 | assert.Nil(t, serv) |
| 134 | assert.NotNil(t, err) |
| 135 | } |