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