-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathmanager_test.go
390 lines (344 loc) · 10.4 KB
/
manager_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package ipam
import (
"errors"
"fmt"
"net"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/platform"
"github.com/Azure/azure-container-networking/store"
"github.com/Azure/azure-container-networking/testutils"
)
var (
anyInterface = "any"
anyPriority = 42
// Pools and addresses used by tests.
subnet1 = net.IPNet{IP: net.IPv4(10, 0, 1, 0), Mask: net.IPv4Mask(255, 255, 255, 0)}
addr11 = net.IPv4(10, 0, 1, 1)
addr12 = net.IPv4(10, 0, 1, 2)
addr13 = net.IPv4(10, 0, 1, 3)
subnet2 = net.IPNet{IP: net.IPv4(10, 0, 2, 0), Mask: net.IPv4Mask(255, 255, 255, 0)}
addr21 = net.IPv4(10, 0, 2, 1)
addr22 = net.IPv4(10, 0, 2, 2)
addr23 = net.IPv4(10, 0, 2, 3)
subnet3 = net.IPNet{IP: net.IPv4(10, 0, 3, 0), Mask: net.IPv4Mask(255, 255, 255, 0)}
addr31 = net.IPv4(10, 0, 3, 1)
addr32 = net.IPv4(10, 0, 3, 2)
addr33 = net.IPv4(10, 0, 3, 3)
)
// createAddressManager creates an address manager with a simple test configuration.
func createAddressManager(options map[string]interface{}) (AddressManager, error) {
var config common.PluginConfig
am, err := NewAddressManager()
if err != nil {
return nil, err
}
if err := am.Initialize(&config, false, options); err != nil {
return nil, err
}
if err := setupTestAddressSpace(am); err != nil {
return nil, err
}
return am, nil
}
// dumpAddressManager dumps the contents of an address manager.
func dumpAddressManager(am AddressManager) {
amImpl := am.(*addressManager)
fmt.Printf("AddressManager:%+v\n", amImpl)
for sk, sv := range amImpl.AddrSpaces {
fmt.Printf("AddressSpace %v:%+v\n", sk, sv)
for pk, pv := range sv.Pools {
fmt.Printf("\tPool %v:%+v\n", pk, pv)
for ak, av := range pv.Addresses {
fmt.Printf("\t\tAddress %v:%+v\n", ak, av)
}
}
}
}
// setupTestAddressSpace creates a simple address space used by various tests.
func setupTestAddressSpace(am AddressManager) error {
anyInterface := "any"
anyPriority := 42
amImpl := am.(*addressManager)
// Configure an empty global address space.
globalAs, err := amImpl.newAddressSpace(GlobalDefaultAddressSpaceId, GlobalScope)
if err != nil {
return err
}
if err := amImpl.setAddressSpace(globalAs); err != nil {
return err
}
// Configure a local address space.
localAs, err := amImpl.newAddressSpace(LocalDefaultAddressSpaceId, LocalScope)
if err != nil {
return err
}
// Add subnet1 with addresses addr11 and addr12.
ap, err := localAs.newAddressPool(anyInterface, anyPriority, &subnet1)
ap.newAddressRecord(&addr11)
ap.newAddressRecord(&addr12)
ap.newAddressRecord(&addr13)
ap.newAddressRecord(&addr22)
ap.newAddressRecord(&addr32)
// Add subnet2 with addr21.
ap, err = localAs.newAddressPool(anyInterface, anyPriority, &subnet2)
ap.newAddressRecord(&addr21)
amImpl.setAddressSpace(localAs)
return nil
}
// cleanupTestAddressSpace deletes any existing address spaces.
func cleanupTestAddressSpace(am AddressManager) error {
amImpl := am.(*addressManager)
// Configure an empty local address space.
localAs, err := amImpl.newAddressSpace(LocalDefaultAddressSpaceId, LocalScope)
if err != nil {
return err
}
if err := amImpl.setAddressSpace(localAs); err != nil {
return err
}
// Configure an empty global address space.
globalAs, err := amImpl.newAddressSpace(GlobalDefaultAddressSpaceId, GlobalScope)
if err != nil {
return err
}
if err := amImpl.setAddressSpace(globalAs); err != nil {
return err
}
return nil
}
//
// Address manager tests.
//
func TestManager(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Manager Suite")
}
var _ = Describe("Test Manager", func() {
Describe("Test Initialize", func() {
Context("When store is nil", func() {
It("Initialize return nil", func() {
var config common.PluginConfig
config.Store = nil
options := map[string]interface{}{}
options[common.OptEnvironment] = ""
am, err := NewAddressManager()
Expect(am).NotTo(BeNil())
Expect(err).NotTo(HaveOccurred())
err = am.Initialize(&config, false, options)
Expect(err).To(BeNil())
})
})
Context("When restore key not found", func() {
It("Initialize return nil", func() {
var config common.PluginConfig
storeMock := &testutils.KeyValueStoreMock{}
storeMock.ReadError = store.ErrKeyNotFound
config.Store = storeMock
options := map[string]interface{}{}
options[common.OptEnvironment] = ""
am, err := NewAddressManager()
Expect(am).NotTo(BeNil())
Expect(err).NotTo(HaveOccurred())
err = am.Initialize(&config, false, options)
Expect(err).To(BeNil())
})
})
Context("When restore return error", func() {
It("Initialize return error", func() {
var config common.PluginConfig
storeMock := &testutils.KeyValueStoreMock{}
storeMock.ReadError = errors.New("Error")
config.Store = storeMock
options := map[string]interface{}{}
options[common.OptEnvironment] = ""
am, err := NewAddressManager()
Expect(am).NotTo(BeNil())
Expect(err).NotTo(HaveOccurred())
err = am.Initialize(&config, false, options)
Expect(err).To(HaveOccurred())
})
})
Context("When StartSource fail", func() {
It("Initialize return error", func() {
var config common.PluginConfig
options := map[string]interface{}{}
options[common.OptEnvironment] = "Invalid"
am, err := NewAddressManager()
Expect(am).NotTo(BeNil())
Expect(err).NotTo(HaveOccurred())
err = am.Initialize(&config, false, options)
Expect(err).To(HaveOccurred())
})
})
})
Describe("Test restore", func() {
Context("When store is nil", func() {
It("restore return nil", func() {
am := &addressManager{
AddrSpaces: make(map[string]*addressSpace),
}
err := am.restore(false)
Expect(err).To(BeNil())
})
})
Context("Test Populate pointers", func() {
It("Should build addrsByID successfully", func() {
am := &addressManager{
AddrSpaces: make(map[string]*addressSpace),
}
p := platform.NewExecClient(nil)
timeReboot, _ := p.GetLastRebootTime()
am.store = &testutils.KeyValueStoreMock{
ModificationTime: timeReboot.Add(time.Hour),
}
ap := &addressPool{
Id: "ap-test",
RefCount: 1,
Addresses: make(map[string]*addressRecord),
}
ap.Addresses["ar-test"] = &addressRecord{
ID: "ar-test",
InUse: true,
}
as := &addressSpace{
Id: "as-test",
Pools: make(map[string]*addressPool),
}
as.Pools["ap-test"] = ap
am.AddrSpaces["as-test"] = as
err := am.restore(false)
Expect(err).To(BeNil())
as = am.AddrSpaces["as-test"]
ap = as.Pools["ap-test"]
ar := ap.addrsByID["ar-test"]
Expect(ar.ID).To(Equal("ar-test"))
Expect(ap.RefCount).To(Equal(1))
Expect(ar.InUse).To(BeTrue())
})
})
Context("When GetModificationTime return error", func() {
It("Should not clear the RefCount and InUse", func() {
am := &addressManager{
AddrSpaces: make(map[string]*addressSpace),
}
am.store = &testutils.KeyValueStoreMock{
GetModificationTimeError: errors.New("Error"),
}
ap := &addressPool{
Id: "ap-test",
RefCount: 1,
Addresses: make(map[string]*addressRecord),
}
ap.Addresses["ar-test"] = &addressRecord{
ID: "ar-test",
InUse: true,
}
as := &addressSpace{
Id: "as-test",
Pools: make(map[string]*addressPool),
}
as.Pools["ap-test"] = ap
am.AddrSpaces["as-test"] = as
err := am.restore(false)
Expect(err).To(BeNil())
as = am.AddrSpaces["as-test"]
ap = as.Pools["ap-test"]
ar := ap.addrsByID["ar-test"]
Expect(ar.ID).To(Equal("ar-test"))
Expect(ap.RefCount).To(Equal(1))
Expect(ar.InUse).To(BeTrue())
})
})
})
Describe("Test save", func() {
Context("When store is nill", func() {
It("Should return nil", func() {
am := &addressManager{}
err := am.save()
Expect(err).NotTo(HaveOccurred())
})
})
})
Describe("Test StartSource", func() {
Context("When environment is azure", func() {
It("Should return azure source", func() {
am := &addressManager{}
options := map[string]interface{}{}
options[common.OptEnvironment] = common.OptEnvironmentAzure
err := am.StartSource(options)
Expect(err).NotTo(HaveOccurred())
Expect(am.source).NotTo(BeNil())
})
})
Context("When environment is mas", func() {
It("Should return mas", func() {
am := &addressManager{}
options := map[string]interface{}{}
options[common.OptEnvironment] = common.OptEnvironmentMAS
err := am.StartSource(options)
Expect(err).NotTo(HaveOccurred())
Expect(am.source).NotTo(BeNil())
})
})
Context("When environment is null", func() {
It("Should return null source", func() {
am := &addressManager{}
options := map[string]interface{}{}
options[common.OptEnvironment] = "null"
err := am.StartSource(options)
Expect(err).NotTo(HaveOccurred())
Expect(am.source).NotTo(BeNil())
})
})
Context("When environment is nil", func() {
It("Should return nil", func() {
am := &addressManager{}
options := map[string]interface{}{}
options[common.OptEnvironment] = ""
err := am.StartSource(options)
Expect(err).NotTo(HaveOccurred())
Expect(am.source).To(BeNil())
})
})
Context("When environment is nil", func() {
It("Should return nil", func() {
am := &addressManager{}
options := map[string]interface{}{}
options[common.OptEnvironment] = "Invalid"
err := am.StartSource(options)
Expect(err).To(HaveOccurred())
Expect(am.source).To(BeNil())
})
})
})
Describe("Test GetDefaultAddressSpaces", func() {
Context("When local and global are nil", func() {
It("Should return empty string", func() {
am := &addressManager{
AddrSpaces: make(map[string]*addressSpace),
}
localId, globalId := am.GetDefaultAddressSpaces()
Expect(localId).To(BeEmpty())
Expect(globalId).To(BeEmpty())
})
})
Context("When local and global are nil", func() {
It("Should return empty string", func() {
am := &addressManager{
AddrSpaces: make(map[string]*addressSpace),
}
am.AddrSpaces[LocalDefaultAddressSpaceId] = &addressSpace{Id: "localId"}
am.AddrSpaces[GlobalDefaultAddressSpaceId] = &addressSpace{Id: "globalId"}
localId, globalId := am.GetDefaultAddressSpaces()
Expect(localId).To(Equal("localId"))
Expect(globalId).To(Equal("globalId"))
})
})
})
})