Daniele Rossi | a64c3bd | 2019-10-22 17:00:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | // Unit Test Backend manager that handles redundant connections per backend |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "github.com/stretchr/testify/assert" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | func makeBackendClusterConfig(numBackends int, numConnections int) *BackendClusterConfig { |
| 28 | |
| 29 | var backends []BackendConfig |
| 30 | for backendIndex := 0; backendIndex < numBackends; backendIndex++ { |
| 31 | var connections []ConnectionConfig |
| 32 | for connectionIndex := 0; connectionIndex < numConnections; connectionIndex++ { |
| 33 | connectionConfig := ConnectionConfig{ |
| 34 | Name: fmt.Sprintf("ro_vcore%d%d", backendIndex, connectionIndex+1), |
| 35 | Addr: "foo", |
| 36 | Port: "123", |
| 37 | } |
| 38 | connections = append(connections, connectionConfig) |
| 39 | } |
| 40 | |
| 41 | backendConfig := BackendConfig{ |
| 42 | Name: fmt.Sprintf("ro_vcore%d", backendIndex), |
| 43 | Type: BackendSingleServer, |
| 44 | Connections: connections, |
| 45 | } |
| 46 | |
| 47 | backends = append(backends, backendConfig) |
| 48 | } |
| 49 | |
| 50 | backendClusterConfig := BackendClusterConfig{ |
| 51 | Name: "ro_vcore", |
| 52 | Backends: backends, |
| 53 | } |
| 54 | return &backendClusterConfig |
| 55 | } |
| 56 | |
| 57 | func TestNewBackendCluster(t *testing.T) { |
| 58 | backends := makeBackendClusterConfig(1, 1) |
| 59 | cluster, err := newBackendCluster(backends) |
| 60 | assert.NotNil(t, cluster) |
| 61 | assert.Nil(t, err) |
| 62 | } |
| 63 | |
| 64 | func TestNewBackendClusterTwoOne(t *testing.T) { |
| 65 | backends := makeBackendClusterConfig(2, 1) |
| 66 | cluster, err := newBackendCluster(backends) |
| 67 | assert.NotNil(t, cluster) |
| 68 | assert.Nil(t, err) |
| 69 | } |
| 70 | |
| 71 | func TestNewBackendClusterNoName(t *testing.T) { |
| 72 | backends := makeBackendClusterConfig(1, 1) |
| 73 | backends.Name = "" |
| 74 | backend, err := newBackendCluster(backends) |
| 75 | assert.Nil(t, backend) |
| 76 | assert.NotNil(t, err, "A backend cluster must have a name") |
| 77 | } |
| 78 | |
| 79 | func TestNewBackendClusterBackendsNoName(t *testing.T) { |
| 80 | backends := makeBackendClusterConfig(1, 1) |
| 81 | backends.Backends[0].Name = "" |
| 82 | backend, err := newBackendCluster(backends) |
| 83 | assert.Nil(t, backend) |
| 84 | assert.NotNil(t, err, "A backend cluster must have a name") |
| 85 | } |
| 86 | |
| 87 | func TestNewBackendClusterBackendsTwoNoName(t *testing.T) { |
| 88 | backends := makeBackendClusterConfig(2, 1) |
| 89 | //backends.Backends[0].Name = "" |
| 90 | backends.Backends[1].Name = "" |
| 91 | backend, err := newBackendCluster(backends) |
| 92 | assert.Nil(t, backend) |
| 93 | assert.NotNil(t, err, "A backend cluster must have a name") |
| 94 | } |