forked from eBay/go-ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_switch_port_test.go
285 lines (236 loc) · 7.49 KB
/
logical_switch_port_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Copyright (c) 2019 eBay Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package goovn
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
PORT_TEST_LS1 = "LogicalSwitch1"
PORT_TEST_LSP1 = "LogicalSwitchPort1"
PORT_TEST_LSP2 = "LogicalSwitchPort2"
PORT_TEST_LSP3 = ""
PORT_TEST_LSP1DYNADDR1 = "a.b.c.d"
PORT_TEST_LSP2DYNADDR2 = ""
PORT_TEST_EXT_ID_MAC_KEY = "mac_addr"
PORT_TEST_EXT_ID_MAC = "00:01:02:03:04:05"
PORT_TEST_EXT_ID_MAC_2 = "01:02:03:05:05:06"
PORT_TEST_EXT_ID_IP_KEY = "ip_addr"
PORT_TEST_EXT_ID_IP = "169.254.1.1"
PORT_TEST_OPT_1_KEY = "foo1"
PORT_TEST_OPT_1_VAL = "bar1"
PORT_TEST_OPT_1_VAL_2 = "baz1"
PORT_TEST_OPT_2_KEY = "foo2"
PORT_TEST_OPT_2_VAL = "bar2"
)
func TestLogicalSwitchPortAPI(t *testing.T) {
ovndbapi := getOVNClient(DBNB)
// create Switch
t.Logf("Adding %s to OVN", PORT_TEST_LS1)
cmd, err := ovndbapi.LSAdd(PORT_TEST_LS1)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
ls, err := ovndbapi.LSGet(PORT_TEST_LS1)
if err != nil {
t.Fatal(err)
}
if ls[0].Name != PORT_TEST_LS1 {
t.Fatalf("ls not created %v", PORT_TEST_LS1)
}
// create logical switch port 1
cmd, err = ovndbapi.LSPAdd(PORT_TEST_LS1, PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
// create logical switch port 2
cmd, err = ovndbapi.LSPAdd(PORT_TEST_LS1, PORT_TEST_LSP2)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
// test LSPGet API
lsp1, err := ovndbapi.LSPGet(PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, lsp1.Name, PORT_TEST_LSP1)
//check for set/get dynamic addresses with non-empty string
cmd, err = ovndbapi.LSPSetDynamicAddresses(PORT_TEST_LSP1, PORT_TEST_LSP1DYNADDR1)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
// make sure the cache now shows the updated lsp object
lsp1, err = ovndbapi.LSPGet(PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, lsp1.DynamicAddresses, PORT_TEST_LSP1DYNADDR1)
dynAddr1, err := ovndbapi.LSPGetDynamicAddresses(PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, dynAddr1, PORT_TEST_LSP1DYNADDR1)
//check for Set/Get Dynamic Address with empty string
cmd, err = ovndbapi.LSPSetDynamicAddresses(PORT_TEST_LSP2, PORT_TEST_LSP2DYNADDR2)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
lsp2, err := ovndbapi.LSPGet(PORT_TEST_LSP2)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, lsp2.DynamicAddresses, PORT_TEST_LSP2DYNADDR2)
// test external id APIs
extIds, err := ovndbapi.LSPGetExternalIds(PORT_TEST_LSP1)
assert.Equal(t, extIds != nil, true)
t.Logf("Setting external ids with two keys")
extIds[PORT_TEST_EXT_ID_MAC_KEY] = PORT_TEST_EXT_ID_MAC
extIds[PORT_TEST_EXT_ID_IP_KEY] = PORT_TEST_EXT_ID_IP
cmd, err = ovndbapi.LSPSetExternalIds(PORT_TEST_LSP1, extIds)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
t.Logf("Validating the external ids are set correctly")
extIdsRet, err := ovndbapi.LSPGetExternalIds(PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
extIdMac, ok := extIdsRet[PORT_TEST_EXT_ID_MAC_KEY]
assert.Equal(t, ok, true)
assert.Equal(t, extIdMac, PORT_TEST_EXT_ID_MAC)
extIdIP, ok := extIdsRet[PORT_TEST_EXT_ID_IP_KEY]
assert.Equal(t, ok, true)
assert.Equal(t, extIdIP, PORT_TEST_EXT_ID_IP)
t.Logf("Validated the external ids are set correctly")
// update one of the existing keys, remove one key
// and make sure it's a complete update.
t.Logf("Validating that keys get clobbered by Set API correctly")
extIds[PORT_TEST_EXT_ID_MAC_KEY] = PORT_TEST_EXT_ID_MAC_2
delete(extIds, PORT_TEST_EXT_ID_IP_KEY)
t.Logf("Remove one of the external ids and verify the contents")
cmd, err = ovndbapi.LSPSetExternalIds(PORT_TEST_LSP1, extIds)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
extIdsRet, err = ovndbapi.LSPGetExternalIds(PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
t.Logf("Validating that only the MAC key is present in the external ids")
extIdMac, ok = extIdsRet[PORT_TEST_EXT_ID_MAC_KEY]
assert.Equal(t, ok, true)
assert.Equal(t, extIdMac, PORT_TEST_EXT_ID_MAC_2)
t.Logf("Validated that the MAC key's value has updated in the external ids")
// make sure IP key is not present in external ids
extIdIP, ok = extIdsRet[PORT_TEST_EXT_ID_IP_KEY]
assert.Equal(t, !ok, true)
t.Logf("Validated that the IP key is not present in the external ids")
// test options API
options, err := ovndbapi.LSPGetOptions(PORT_TEST_LSP1)
assert.Equal(t, options != nil, true)
t.Logf("Validating the options are set correctly")
options[PORT_TEST_OPT_1_KEY] = PORT_TEST_OPT_1_VAL
options[PORT_TEST_OPT_2_KEY] = PORT_TEST_OPT_2_VAL
cmd, err = ovndbapi.LSPSetOptions(PORT_TEST_LSP1, options)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
optionsRet, err := ovndbapi.LSPGetOptions(PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
option1, ok := optionsRet[PORT_TEST_OPT_1_KEY]
assert.Equal(t, ok, true)
assert.Equal(t, option1, PORT_TEST_OPT_1_VAL)
option2, ok := optionsRet[PORT_TEST_OPT_2_KEY]
assert.Equal(t, ok, true)
assert.Equal(t, option2, PORT_TEST_OPT_2_VAL)
t.Logf("Validated that multiple options are set correctly")
// update one of the existing keys, remove one key
// and make sure it's a complete update.
t.Logf("Validating that keys get clobbered by Set API correctly")
options[PORT_TEST_OPT_1_KEY] = PORT_TEST_OPT_1_VAL_2
delete(options, PORT_TEST_OPT_2_KEY)
t.Logf("Remove one of the options and verify the contents")
cmd, err = ovndbapi.LSPSetOptions(PORT_TEST_LSP1, options)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
optionsRet, err = ovndbapi.LSPGetOptions(PORT_TEST_LSP1)
if err != nil {
t.Fatal(err)
}
t.Logf("Validating that only the OPT_1 key is present in the options")
option1, ok = optionsRet[PORT_TEST_OPT_1_KEY]
assert.Equal(t, ok, true)
assert.Equal(t, option1, PORT_TEST_OPT_1_VAL_2)
t.Logf("Validated that the OPT_1 key's value has updated in the options")
// make sure IP key is not present in external ids
option2, ok = optionsRet[PORT_TEST_OPT_2_KEY]
assert.Equal(t, !ok, true)
t.Logf("Validated that the OPT_2 key is not present in the options")
//validate that setting fields on an empty LSP string gives a nil cmd and an error
cmd, err = ovndbapi.LSPSetDynamicAddresses(PORT_TEST_LSP3, PORT_TEST_LSP1DYNADDR1)
assert.Equal(t, cmd == nil, true)
assert.Equal(t, err == nil, false)
cmd, err = ovndbapi.LSPSetExternalIds(PORT_TEST_LSP3, extIds)
assert.Equal(t, cmd == nil, true)
assert.Equal(t, err == nil, false)
cmd, err = ovndbapi.LSDel(PORT_TEST_LS1)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
}