gerardo.laurenzi | bc18182 | 2019-10-28 09:09:53 +0000 | [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/phayes/freeport" |
| 22 | "github.com/stretchr/testify/assert" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
| 26 | func makeConfig(numBackends int, numConnections int) Configuration { |
| 27 | |
| 28 | var backends []BackendConfig |
| 29 | var slbackendClusterConfig []BackendClusterConfig |
| 30 | var slRouterConfig []RouterConfig |
| 31 | |
| 32 | for backendIndex := 0; backendIndex < numBackends; backendIndex++ { |
| 33 | var connections []ConnectionConfig |
| 34 | for connectionIndex := 0; connectionIndex < numConnections; connectionIndex++ { |
| 35 | connectionConfig := ConnectionConfig{ |
| 36 | Name: fmt.Sprintf("ro_vcore%d%d", backendIndex, connectionIndex+1), |
| 37 | Addr: "foo", |
| 38 | Port: "123", |
| 39 | } |
| 40 | connections = append(connections, connectionConfig) |
| 41 | } |
| 42 | |
| 43 | backendConfig := BackendConfig{ |
| 44 | Name: fmt.Sprintf("ro_vcore%d", backendIndex), |
| 45 | Type: BackendSingleServer, |
| 46 | Connections: connections, |
| 47 | } |
| 48 | |
| 49 | backends = append(backends, backendConfig) |
| 50 | } |
| 51 | |
| 52 | backendClusterConfig := BackendClusterConfig{ |
| 53 | Name: "ro_vcore", |
| 54 | Backends: backends, |
| 55 | } |
| 56 | |
| 57 | slbackendClusterConfig = append(slbackendClusterConfig, backendClusterConfig) |
| 58 | |
| 59 | routeConfig := RouteConfig{ |
| 60 | Name: "read_only", |
| 61 | Type: RouteTypeRoundRobin, |
| 62 | Association: AssociationRoundRobin, |
| 63 | BackendCluster: "ro_vcore", |
| 64 | backendCluster: &backendClusterConfig, |
| 65 | Methods: []string{"ListDevicePorts"}, |
| 66 | } |
| 67 | |
| 68 | routerConfig := RouterConfig{ |
| 69 | Name: "vcore", |
| 70 | ProtoService: "VolthaService", |
| 71 | ProtoPackage: "voltha", |
| 72 | Routes: []RouteConfig{routeConfig}, |
| 73 | ProtoFile: TEST_PROTOFILE, |
| 74 | } |
| 75 | |
| 76 | slRouterConfig = append(slRouterConfig, routerConfig) |
| 77 | |
| 78 | port1, _ := freeport.GetFreePort() |
| 79 | apiConfig := ApiConfig{ |
| 80 | Addr: "127.0.0.1", |
| 81 | Port: uint(port1), |
| 82 | } |
| 83 | |
| 84 | cfile := "config_file" |
| 85 | loglevel := 0 |
| 86 | glog := false |
| 87 | |
| 88 | conf := Configuration{ |
| 89 | InstanceID: "1", |
| 90 | ConfigFile: &cfile, |
| 91 | LogLevel: &loglevel, |
| 92 | GrpcLog: &glog, |
| 93 | BackendClusters: slbackendClusterConfig, |
| 94 | Routers: slRouterConfig, |
| 95 | Api: apiConfig, |
| 96 | } |
| 97 | return conf |
| 98 | } |
| 99 | |
| 100 | func makeProxy(numBackends int, numConnections int) (*ArouterProxy, error) { |
| 101 | |
| 102 | conf := makeConfig(3, 2) |
| 103 | arouter, err := NewArouterProxy(&conf) |
| 104 | return arouter, err |
| 105 | } |
| 106 | |
| 107 | func TestNewApi(t *testing.T) { |
| 108 | |
| 109 | port, _ := freeport.GetFreePort() |
| 110 | |
| 111 | apiConfig := ApiConfig{ |
| 112 | Addr: "127.0.0.1", |
| 113 | Port: uint(port), |
| 114 | } |
| 115 | arouter, err := makeProxy(3, 2) |
| 116 | assert.NotNil(t, arouter, "Error the proxy confiiguration fails!") |
| 117 | assert.Nil(t, err) |
| 118 | |
| 119 | ar, err := newApi(&apiConfig, arouter) |
| 120 | |
| 121 | assert.NotNil(t, ar, "NewApi Fails!") |
| 122 | assert.Nil(t, err) |
| 123 | } |
| 124 | |
| 125 | func TestNewApiWrongSocket(t *testing.T) { |
| 126 | port, _ := freeport.GetFreePort() |
| 127 | apiConfig := ApiConfig{ |
| 128 | Addr: "256.300.1.83", |
| 129 | Port: uint(port), |
| 130 | } |
| 131 | arouter, err := makeProxy(3, 2) |
| 132 | assert.NotNil(t, arouter, "Error the proxy configuration fails!") |
| 133 | assert.Nil(t, err) |
| 134 | ar, err := newApi(&apiConfig, arouter) |
| 135 | |
| 136 | assert.Nil(t, ar, "Accepted a wrong IP") |
| 137 | assert.NotNil(t, err) |
| 138 | |
| 139 | apiConfig = ApiConfig{ |
| 140 | Addr: "127.0.0.1", |
| 141 | Port: 68000, |
| 142 | } |
| 143 | ar, err = newApi(&apiConfig, arouter) |
| 144 | assert.Nil(t, ar, "Accepted a wrong port") |
| 145 | assert.NotNil(t, err) |
| 146 | } |
| 147 | |
| 148 | func TestGetBackend(t *testing.T) { |
| 149 | port, _ := freeport.GetFreePort() |
| 150 | apiConfig := ApiConfig{ |
| 151 | Addr: "127.0.0.1", |
| 152 | Port: uint(port), |
| 153 | } |
| 154 | arouter, _ := makeProxy(3, 2) |
| 155 | |
| 156 | ar, _ := newApi(&apiConfig, arouter) |
| 157 | |
| 158 | backends := makeBackendClusterConfig(5, 1) |
| 159 | cluster, err := newBackendCluster(backends) |
| 160 | assert.NotNil(t, cluster) |
| 161 | assert.Nil(t, err) |
| 162 | |
| 163 | for backendIndex := 0; backendIndex < 5; backendIndex++ { |
| 164 | backend, errb := ar.getBackend(cluster, fmt.Sprintf("ro_vcore%d", backendIndex)) |
| 165 | assert.NotNil(t, backend) |
| 166 | assert.Nil(t, errb) |
| 167 | } |
| 168 | |
| 169 | backend, errb := ar.getBackend(cluster, "ro_vcore5") |
| 170 | assert.Nil(t, backend) |
| 171 | assert.NotNil(t, errb) |
| 172 | } |
| 173 | |
| 174 | func TestGetConnection(t *testing.T) { |
| 175 | port, _ := freeport.GetFreePort() |
| 176 | apiConfig := ApiConfig{ |
| 177 | Addr: "127.0.0.1", |
| 178 | Port: uint(port), |
| 179 | } |
| 180 | arouter, _ := makeProxy(3, 2) |
| 181 | |
| 182 | ar, _ := newApi(&apiConfig, arouter) |
| 183 | |
| 184 | backend, err := newBackend(makeBackend(), "Cluster") |
| 185 | assert.NotNil(t, backend) |
| 186 | assert.Nil(t, err) |
| 187 | cc, _ := ar.getConnection(backend, "ro_vcore01") |
| 188 | assert.NotNil(t, cc, "Error connection name not found") |
| 189 | |
| 190 | cc2, err2 := ar.getConnection(backend, "wrongConnectionName") |
| 191 | assert.Nil(t, cc2) |
| 192 | assert.NotNil(t, err2) |
| 193 | } |