|
| 1 | +package recorder |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/spf13/afero" |
| 8 | + |
| 9 | + "github.com/bpineau/katafygio/config" |
| 10 | + "github.com/bpineau/katafygio/pkg/event" |
| 11 | + "github.com/bpineau/katafygio/pkg/log" |
| 12 | +) |
| 13 | + |
| 14 | +func newNotif(action event.Action, key string) *event.Notification { |
| 15 | + return &event.Notification{ |
| 16 | + Action: action, |
| 17 | + Key: key, |
| 18 | + Kind: "foo", |
| 19 | + Object: "bar", |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestRecorder(t *testing.T) { |
| 24 | + appFs = afero.NewMemMapFs() |
| 25 | + |
| 26 | + evt := event.New() |
| 27 | + |
| 28 | + conf := &config.KfConfig{ |
| 29 | + Logger: log.New("info", "", "test"), |
| 30 | + LocalDir: "/tmp/ktest", // fake dir (in memory fs provided by Afero) |
| 31 | + ResyncIntv: 60, |
| 32 | + } |
| 33 | + |
| 34 | + rec := New(conf, evt).Start() |
| 35 | + |
| 36 | + evt.Send(newNotif(event.Upsert, "foo1")) |
| 37 | + evt.Send(newNotif(event.Upsert, "foo2")) |
| 38 | + evt.Send(newNotif(event.Delete, "foo1")) |
| 39 | + |
| 40 | + rec.Stop() // to flush ongoing fs operations |
| 41 | + |
| 42 | + exist, _ := afero.Exists(appFs, conf.LocalDir+"/foo-foo2.yaml") |
| 43 | + if !exist { |
| 44 | + t.Error("foo-foo2.yaml should exist; upsert event didn't propagate") |
| 45 | + } |
| 46 | + |
| 47 | + exist, _ = afero.Exists(appFs, conf.LocalDir+"/foo-foo1.yaml") |
| 48 | + if exist { |
| 49 | + t.Error("foo-foo1.yaml shouldn't exist, delete event didn't propagate") |
| 50 | + } |
| 51 | + |
| 52 | + rogue := conf.LocalDir + "/roguefile.yaml" |
| 53 | + _ = afero.WriteFile(appFs, rogue, []byte{42}, 0600) |
| 54 | + _ = afero.WriteFile(appFs, rogue+".txt", []byte{42}, 0600) |
| 55 | + rec.deleteObsoleteFiles() |
| 56 | + |
| 57 | + exist, _ = afero.Exists(appFs, rogue) |
| 58 | + if exist { |
| 59 | + t.Errorf("%s file should have been garbage collected", rogue) |
| 60 | + } |
| 61 | + |
| 62 | + exist, _ = afero.Exists(appFs, rogue+".txt") |
| 63 | + if !exist { |
| 64 | + t.Errorf("garbage collection should only touch .yaml files") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestDryRunRecorder(t *testing.T) { |
| 69 | + appFs = afero.NewMemMapFs() |
| 70 | + |
| 71 | + conf := &config.KfConfig{ |
| 72 | + Logger: log.New("info", "", "test"), |
| 73 | + LocalDir: "/tmp/ktest", |
| 74 | + ResyncIntv: 60, |
| 75 | + } |
| 76 | + |
| 77 | + conf.DryRun = true |
| 78 | + dryevt := event.New() |
| 79 | + dryrec := New(conf, dryevt).Start() |
| 80 | + dryevt.Send(newNotif(event.Upsert, "foo3")) |
| 81 | + dryevt.Send(newNotif(event.Upsert, "foo4")) |
| 82 | + dryevt.Send(newNotif(event.Delete, "foo4")) |
| 83 | + dryrec.Stop() |
| 84 | + |
| 85 | + exist, _ := afero.Exists(appFs, conf.LocalDir+"/foo-foo3.yaml") |
| 86 | + if exist { |
| 87 | + t.Error("foo-foo3.yaml was created but we're in dry-run mode") |
| 88 | + } |
| 89 | + |
| 90 | + rogue := conf.LocalDir + "/roguefile.yaml" |
| 91 | + _ = afero.WriteFile(appFs, rogue, []byte{42}, 0600) |
| 92 | + dryrec.deleteObsoleteFiles() |
| 93 | + |
| 94 | + exist, _ = afero.Exists(appFs, rogue) |
| 95 | + if !exist { |
| 96 | + t.Errorf("garbage collection shouldn't remove files in dry-run mode") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// testing behavior on fs errors (we shouldn't block the program) |
| 101 | +func TestFailingFSRecorder(t *testing.T) { |
| 102 | + appFs = afero.NewMemMapFs() |
| 103 | + |
| 104 | + evt := event.New() |
| 105 | + |
| 106 | + conf := &config.KfConfig{ |
| 107 | + Logger: log.New("info", "", "test"), |
| 108 | + LocalDir: "/tmp/ktest", // fake dir (in memory fs provided by Afero) |
| 109 | + ResyncIntv: 60, |
| 110 | + } |
| 111 | + |
| 112 | + rec := New(conf, evt).Start() |
| 113 | + |
| 114 | + _ = afero.WriteFile(appFs, conf.LocalDir+"/foo.yaml", []byte{42}, 0600) |
| 115 | + |
| 116 | + // switching to failing (read-only) filesystem |
| 117 | + appFs = afero.NewReadOnlyFs(appFs) |
| 118 | + |
| 119 | + err := rec.save("foo", "bar") |
| 120 | + if err == nil { |
| 121 | + t.Error("save should return an error in case of failure") |
| 122 | + } |
| 123 | + |
| 124 | + // shouldn't panic in case of failures |
| 125 | + rec.deleteObsoleteFiles() |
| 126 | + |
| 127 | + // shouldn't block (the controllers event loop will retry anyway) |
| 128 | + ch := make(chan struct{}) |
| 129 | + go func() { |
| 130 | + evt.Send(newNotif(event.Upsert, "foo3")) |
| 131 | + evt.Send(newNotif(event.Upsert, "foo4")) |
| 132 | + ch <- struct{}{} |
| 133 | + }() |
| 134 | + |
| 135 | + select { |
| 136 | + case <-ch: |
| 137 | + case <-time.After(5 * time.Second): |
| 138 | + t.Error("recorder shouldn't block in case of fs failure") |
| 139 | + } |
| 140 | + |
| 141 | + // back to normal operations |
| 142 | + rec.Stop() // just to flush ongoing ops before switch filesystem |
| 143 | + appFs = afero.NewMemMapFs() |
| 144 | + rec.Start() |
| 145 | + evt.Send(newNotif(event.Upsert, "foo2")) |
| 146 | + rec.Stop() // flush ongoing ops |
| 147 | + |
| 148 | + exist, _ := afero.Exists(appFs, conf.LocalDir+"/foo-foo2.yaml") |
| 149 | + if !exist { |
| 150 | + t.Error("foo-foo2.yaml should exist; recorder should recover from fs failures") |
| 151 | + } |
| 152 | +} |
0 commit comments