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 ( |
| 20 | "fmt" |
| 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "github.com/stretchr/testify/assert" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
Hardik Windlass | 9f949c9 | 2019-10-10 06:39:24 +0000 | [diff] [blame] | 27 | log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": 1}) |
Daniele Rossi | adc95bc | 2019-10-09 11:09:53 +0200 | [diff] [blame] | 28 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 29 | } |
| 30 | |
| 31 | func MakeServerTestConfig(numBackends int, numConnections int) *ServerConfig { |
| 32 | |
| 33 | var routerPackage []RouterPackage |
| 34 | var backends []BackendConfig |
| 35 | for backendIndex := 0; backendIndex < numBackends; backendIndex++ { |
| 36 | var connections []ConnectionConfig |
| 37 | for connectionIndex := 0; connectionIndex < numConnections; connectionIndex++ { |
| 38 | connectionConfig := ConnectionConfig{ |
| 39 | Name: fmt.Sprintf("ro_vcore%d%d", backendIndex, connectionIndex+1), |
| 40 | Addr: "foo", |
| 41 | Port: "123", |
| 42 | } |
| 43 | connections = append(connections, connectionConfig) |
| 44 | } |
| 45 | |
| 46 | backendConfig := BackendConfig{ |
| 47 | Name: fmt.Sprintf("ro_vcore%d", backendIndex), |
| 48 | Type: BackendSingleServer, |
| 49 | Connections: connections, |
| 50 | } |
| 51 | |
| 52 | backends = append(backends, backendConfig) |
| 53 | } |
| 54 | |
| 55 | routerPackageConfig := RouterPackage{ |
| 56 | Router: `json:"router"`, |
| 57 | Package: `json:"package"`, |
| 58 | } |
| 59 | routerPackage = append(routerPackage, routerPackageConfig) |
| 60 | |
| 61 | serverConfig := ServerConfig{ |
| 62 | Name: "grpc_command", |
| 63 | Port: 55555, |
| 64 | Addr: "127.0.0.1", |
| 65 | Type: "grpc", |
| 66 | Routers: routerPackage, |
| 67 | routers: make(map[string]*RouterConfig), |
| 68 | } |
| 69 | return &serverConfig |
| 70 | |
| 71 | } |
| 72 | |
| 73 | // Test creation of a new Server |
| 74 | func TestServerInit(t *testing.T) { |
| 75 | |
| 76 | serverConfig := MakeServerTestConfig(1, 1) |
| 77 | |
| 78 | serv, err := newServer(serverConfig) |
| 79 | |
| 80 | assert.NotNil(t, serv) |
| 81 | assert.Nil(t, err) |
| 82 | |
| 83 | } |
| 84 | |
| 85 | // Test creation of a new Server, error in Addr |
| 86 | func TestServerInitWrongAddr(t *testing.T) { |
| 87 | |
| 88 | serverConfig := MakeServerTestConfig(1, 1) |
| 89 | serverConfig.Addr = "127.300.1.1" |
| 90 | |
| 91 | serv, err := newServer(serverConfig) |
| 92 | |
| 93 | assert.Nil(t, serv) |
| 94 | assert.NotNil(t, err) |
| 95 | } |
| 96 | |
| 97 | // Test creation of a new Server, error in Port |
| 98 | func TestServerInitWrongPort(t *testing.T) { |
| 99 | |
| 100 | serverConfig := MakeServerTestConfig(1, 1) |
| 101 | serverConfig.Port = 23 |
| 102 | |
| 103 | serv, err := newServer(serverConfig) |
| 104 | |
| 105 | assert.Nil(t, serv) |
| 106 | assert.NotNil(t, err) |
| 107 | } |
| 108 | |
| 109 | // Test creation of a new Server, error in Name |
| 110 | func TestServerInitNoName(t *testing.T) { |
| 111 | |
| 112 | serverConfig := MakeServerTestConfig(1, 1) |
| 113 | serverConfig.Name = "" |
| 114 | |
| 115 | serv, err := newServer(serverConfig) |
| 116 | |
| 117 | assert.Nil(t, serv) |
| 118 | assert.NotNil(t, err) |
| 119 | } |
| 120 | |
| 121 | // Test creation of a new Server, error in Type |
| 122 | func TestServerInitWrongType(t *testing.T) { |
| 123 | |
| 124 | serverConfig := MakeServerTestConfig(1, 1) |
| 125 | serverConfig.Type = "xxx" |
| 126 | |
| 127 | serv, err := newServer(serverConfig) |
| 128 | |
| 129 | assert.Nil(t, serv) |
| 130 | assert.NotNil(t, err) |
| 131 | } |
| 132 | |
| 133 | // Test creation of a new Server, error in Router |
| 134 | func TestServerInitNoRouter(t *testing.T) { |
| 135 | |
| 136 | serverConfig := MakeServerTestConfig(1, 1) |
| 137 | serverConfig.routers = nil |
| 138 | |
| 139 | serv, err := newServer(serverConfig) |
| 140 | |
| 141 | assert.Nil(t, serv) |
| 142 | assert.NotNil(t, err) |
| 143 | } |
| 144 | |
| 145 | // Test creation of a new Server |
| 146 | func TestServerInitHandler(t *testing.T) { |
| 147 | |
| 148 | serverConfig := MakeServerTestConfig(1, 1) |
| 149 | serverConfig.Port = 55556 |
| 150 | |
| 151 | serv, err := newServer(serverConfig) |
| 152 | |
| 153 | assert.NotNil(t, serv) |
| 154 | assert.Nil(t, err) |
| 155 | |
| 156 | } |