Skip to content

Commit 8a37657

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

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,36 @@ var _ = Describe("ocicni operations", func() {
236236
ocicni.Shutdown()
237237
})
238238

239+
It("should monitor the conf dir for changes", func() {
240+
_, _, err := writeConfig(tmpDir, "5-notdefault.conf", "notdefault", "myplugin", "0.3.1")
241+
Expect(err).NotTo(HaveOccurred())
242+
_, _, err = writeConfig(tmpDir, "10-test.conf", "test", "myplugin", "0.3.1")
243+
Expect(err).NotTo(HaveOccurred())
244+
245+
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, false, "/opt/cni/bin")
246+
Expect(err).NotTo(HaveOccurred())
247+
Expect(ocicni.Status()).NotTo(HaveOccurred())
248+
249+
// Ensure the default network is the one we expect
250+
tmp := ocicni.(*cniNetworkPlugin)
251+
net := tmp.getDefaultNetwork()
252+
Expect(net.name).To(Equal("test"))
253+
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
254+
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
255+
256+
_, _, err = writeConfig(tmpDir, "10-test.conf", "update", "testplugin", "0.3.1")
257+
Expect(err).NotTo(HaveOccurred())
258+
259+
// Ensure the default network is the updated one
260+
tmp = ocicni.(*cniNetworkPlugin)
261+
net = tmp.getDefaultNetwork()
262+
Expect(net.name).To(Equal("update"))
263+
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
264+
Expect(net.config.Plugins[0].Network.Type).To(Equal("testplugin"))
265+
266+
ocicni.Shutdown()
267+
})
268+
239269
It("finds an asynchronously written default network configuration", func() {
240270
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, true, "/opt/cni/bin")
241271
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)