Skip to content

Commit 07d40a6

Browse files
committed
refactor: add test to verify update event handle
Signed-off-by: Amar Prakash Pandey <[email protected]>
1 parent f2749dc commit 07d40a6

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

pkg/ocicni/ocicni.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ func (plugin *cniNetworkPlugin) monitorConfDir(start *sync.WaitGroup) {
155155
logrus.Infof("CNI monitoring event %v", event)
156156

157157
var defaultDeleted bool
158-
createWriteRename := (event.Op&fsnotify.Create == fsnotify.Create ||
158+
createWriteRename := event.Op&fsnotify.Create == fsnotify.Create ||
159159
event.Op&fsnotify.Write == fsnotify.Write ||
160-
event.Op&fsnotify.Rename > 0)
160+
event.Op&fsnotify.Rename == fsnotify.Rename
161161
if event.Op&fsnotify.Remove == fsnotify.Remove {
162162
// Care about the event if the default network
163163
// was just deleted

pkg/ocicni/ocicni_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,68 @@ var _ = Describe("ocicni operations", func() {
293293
ocicni.Shutdown()
294294
})
295295

296+
It("should monitor the net conf dir for changes when default network is not specified", func() {
297+
_, _, err := writeConfig(tmpDir, "5-notdefault.conf", "notdefault", "myplugin", "0.3.1")
298+
Expect(err).NotTo(HaveOccurred())
299+
_, _, err = writeConfig(tmpDir, "10-test.conf", "test", "myplugin", "0.3.1")
300+
Expect(err).NotTo(HaveOccurred())
301+
302+
ocicni, err := initCNI(&fakeExec{}, "", "", tmpDir, true, "/opt/cni/bin")
303+
Expect(err).NotTo(HaveOccurred())
304+
Expect(ocicni.Status()).NotTo(HaveOccurred())
305+
306+
// Ensure the default network is the one we expect
307+
tmp := ocicni.(*cniNetworkPlugin)
308+
net := tmp.getDefaultNetwork()
309+
Expect(net.name).To(Equal("test"))
310+
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
311+
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
312+
313+
// If a CNI config file is updated, default network name should be reloaded real-time
314+
_, _, err = writeConfig(tmpDir, "10-test.conf", "secondary", "testplugin", "0.3.1")
315+
Expect(err).NotTo(HaveOccurred())
316+
317+
Consistently(ocicni.Status, 5).Should(Succeed())
318+
319+
net = tmp.getDefaultNetwork()
320+
Expect(net.name).To(Equal("secondary"))
321+
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
322+
Expect(net.config.Plugins[0].Network.Type).To(Equal("testplugin"))
323+
324+
ocicni.Shutdown()
325+
})
326+
327+
It("should monitor the net conf dir for changes when default network is specified", func() {
328+
_, _, err := writeConfig(tmpDir, "5-notdefault.conf", "notdefault", "myplugin", "0.3.1")
329+
Expect(err).NotTo(HaveOccurred())
330+
_, _, err = writeConfig(tmpDir, "10-test.conf", "test", "myplugin", "0.3.1")
331+
Expect(err).NotTo(HaveOccurred())
332+
333+
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, true, "/opt/cni/bin")
334+
Expect(err).NotTo(HaveOccurred())
335+
Expect(ocicni.Status()).NotTo(HaveOccurred())
336+
337+
// Ensure the default network is the one we expect
338+
tmp := ocicni.(*cniNetworkPlugin)
339+
net := tmp.getDefaultNetwork()
340+
Expect(net.name).To(Equal("test"))
341+
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
342+
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
343+
344+
// If a CNI config file is updated, default network name should be reloaded real-time
345+
_, _, err = writeConfig(tmpDir, "10-test.conf", "test", "testplugin", "0.3.1")
346+
Expect(err).NotTo(HaveOccurred())
347+
348+
Consistently(ocicni.Status, 5).Should(Succeed())
349+
350+
net = tmp.getDefaultNetwork()
351+
Expect(net.name).To(Equal("test"))
352+
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
353+
Expect(net.config.Plugins[0].Network.Type).To(Equal("testplugin"))
354+
355+
ocicni.Shutdown()
356+
})
357+
296358
It("finds and refinds an asynchronously written default network configuration", func() {
297359
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, true, "/opt/cni/bin")
298360
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)