blob: edd584e0aaa0aaee6f92042b52a5920aaadae67a [file] [log] [blame]
gerardo.laurenzi1453fd92019-10-11 07:44:31 +00001/*
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
17package afrouter
18
19// Unit Test Backend manager that handles redundant connections per backend
20
21import (
22 "fmt"
23 "github.com/stretchr/testify/assert"
24 "testing"
25)
26
27func makeBackend() *BackendConfig {
28 var connections []ConnectionConfig
29 connectionConfig := ConnectionConfig{
30 Name: fmt.Sprintf("ro_vcore%d%d", 0, 1),
31 Addr: "foo",
32 Port: "123",
33 }
34 connections = append(connections, connectionConfig)
35
36 backendConfig := BackendConfig{
37 Name: fmt.Sprintf("ro_vcore%d", 0),
38 Type: BackendSingleServer,
39 Connections: connections,
40 }
41 return &backendConfig
42}
43
44func makeBackendCluster(numBackends int, numConnections int) *BackendClusterConfig {
45
46 var backends []BackendConfig
47 for backendIndex := 0; backendIndex < numBackends; backendIndex++ {
48 var connections []ConnectionConfig
49 for connectionIndex := 0; connectionIndex < numConnections; connectionIndex++ {
50 connectionConfig := ConnectionConfig{
51 Name: fmt.Sprintf("ro_vcore%d%d", backendIndex, connectionIndex+1),
52 Addr: "foo",
53 Port: "123",
54 }
55 connections = append(connections, connectionConfig)
56 }
57
58 backendConfig := BackendConfig{
59 Name: fmt.Sprintf("ro_vcore%d", backendIndex),
60 Type: BackendSingleServer,
61 Connections: connections,
62 }
63
64 backends = append(backends, backendConfig)
65 }
66
67 backendClusterConfig := BackendClusterConfig{
68 Name: "ro_vcore",
69 Backends: backends,
70 }
71 return &backendClusterConfig
72}
73
74func TestBackend(t *testing.T) {
75
76 backend, err := newBackend(makeBackend(), "Cluster")
77 assert.NotNil(t, backend)
78 assert.Nil(t, err)
79}
80
81func TestBackendInvalidType(t *testing.T) {
82 conf := makeBackend()
83 conf.Type = BackendUndefined
84 backend, err := newBackend(conf, "Cluster")
85 assert.Nil(t, backend)
86 assert.NotNil(t, err, "Backend Invalid Type Undefined")
87}
88
89func TestBackendAssociationLocationMissing(t *testing.T) {
90 conf := makeBackend()
91 conf.Type = BackendActiveActive
92 conf.Association.Location = AssociationLocationUndefined
93 backend, err := newBackend(conf, "Cluster")
94 assert.Nil(t, backend)
95 assert.NotNil(t, err, "An association location must be provided if the backend "+
96 "type is active/active for backend in cluster ")
97}
98
99func TestBackendAssociationFieldMissing(t *testing.T) {
100 conf := makeBackend()
101 conf.Association.Location = AssociationLocationProtobuf
102 conf.Association.Field = ""
103 backend, err := newBackend(conf, "Cluster")
104 assert.Nil(t, backend)
105 assert.NotNil(t, err, "An association field must be provided if the backend "+
106 "type is active/active and the location is set to protobuf "+
107 "for backend in cluster")
108}
109
110func TestBackendAssociationStrategyMissing(t *testing.T) {
111 conf := makeBackend()
112 conf.Association.Strategy = AssociationStrategyUndefined
113 conf.Type = BackendActiveActive
114 backend, err := newBackend(conf, "Cluster")
115 assert.Nil(t, backend)
116 assert.NotNil(t, err, "An association strategy must be provided if the backend "+
117 "type is active/active")
118}
119
120func TestBackendAssociationKeyMissing(t *testing.T) {
121 conf := makeBackend()
122 conf.Association.Key = ""
123 conf.Association.Location = AssociationLocationHeader
124 backend, err := newBackend(conf, "Cluster")
125 assert.Nil(t, backend)
126 assert.NotNil(t, err, "An association key must be provided if the backend "+
127 "type is active/active and the location is set to header "+
128 "for backend in cluster")
129}
130func TestBackendWithMoreConnections(t *testing.T) {
131 conf := makeBackendCluster(1, 4)
132 conf.Backends[0].Association.Location = AssociationLocationHeader
133 conf.Backends[0].Association.Key = "BackendKey"
134 backend, err := newBackend(&conf.Backends[0], "Cluster")
135
136 assert.Nil(t, backend)
137 assert.NotNil(t, err, "Only one connection must be specified if the association "+
138 "strategy is not set to 'active_active'")
139
140}
141func TestBackendNoConnections(t *testing.T) {
142 confZero := makeBackendCluster(1, 0)
143 confZero.Backends[0].Association.Location = AssociationLocationHeader
144 confZero.Backends[0].Association.Key = "BackendKey"
145 backendNoConn, err := newBackend(&confZero.Backends[0], "Cluster")
146
147 assert.Nil(t, backendNoConn)
148 assert.NotNil(t, err, "A connection must have a name for backend in cluster")
149}