Skip to content

Commit 5cb0335

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

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path/filepath"
1212
"reflect"
1313
"strings"
14+
"time"
1415

1516
"github.com/vishvananda/netlink"
1617

@@ -236,6 +237,36 @@ var _ = Describe("ocicni operations", func() {
236237
ocicni.Shutdown()
237238
})
238239

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

0 commit comments

Comments
 (0)