-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
kubernetes-backup | ||
dist/* | ||
katafygio | ||
profile.cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package recorder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
|
||
"github.com/bpineau/katafygio/config" | ||
"github.com/bpineau/katafygio/pkg/event" | ||
"github.com/bpineau/katafygio/pkg/log" | ||
) | ||
|
||
func newNotif(action event.Action, key string) *event.Notification { | ||
return &event.Notification{ | ||
Action: action, | ||
Key: key, | ||
Kind: "foo", | ||
Object: "bar", | ||
} | ||
} | ||
|
||
func TestRecorder(t *testing.T) { | ||
appFs = afero.NewMemMapFs() | ||
|
||
evt := event.New() | ||
|
||
conf := &config.KfConfig{ | ||
Logger: log.New("info", "", "test"), | ||
LocalDir: "/tmp/ktest", // fake dir (in memory fs provided by Afero) | ||
ResyncIntv: 60, | ||
} | ||
|
||
rec := New(conf, evt).Start() | ||
|
||
evt.Send(newNotif(event.Upsert, "foo1")) | ||
evt.Send(newNotif(event.Upsert, "foo2")) | ||
evt.Send(newNotif(event.Delete, "foo1")) | ||
|
||
rec.Stop() | ||
|
||
exist, _ := afero.Exists(appFs, conf.LocalDir+"/foo-foo2.yaml") | ||
if !exist { | ||
t.Error("foo-foo2.yaml should exist; upsert event didn't propagate") | ||
} | ||
|
||
exist, _ = afero.Exists(appFs, conf.LocalDir+"/foo-foo1.yaml") | ||
if exist { | ||
t.Error("foo-foo1.yaml shouldn't exist, delete event didn't propagate") | ||
} | ||
|
||
rogue := conf.LocalDir + "/roguefile.yaml" | ||
_ = afero.WriteFile(appFs, rogue, []byte{42}, 0600) | ||
_ = afero.WriteFile(appFs, rogue+".txt", []byte{42}, 0600) | ||
rec.deleteObsoleteFiles() | ||
|
||
exist, _ = afero.Exists(appFs, rogue) | ||
if exist { | ||
t.Errorf("%s file should have been garbage collected", rogue) | ||
} | ||
|
||
exist, _ = afero.Exists(appFs, rogue+".txt") | ||
if !exist { | ||
t.Errorf("garbage collection should only touch .yaml files") | ||
} | ||
} | ||
|
||
func TestDryRunRecorder(t *testing.T) { | ||
appFs = afero.NewMemMapFs() | ||
|
||
conf := &config.KfConfig{ | ||
Logger: log.New("info", "", "test"), | ||
LocalDir: "/tmp/ktest", | ||
ResyncIntv: 60, | ||
} | ||
|
||
conf.DryRun = true | ||
dryevt := event.New() | ||
dryrec := New(conf, dryevt).Start() | ||
dryevt.Send(newNotif(event.Upsert, "foo3")) | ||
dryevt.Send(newNotif(event.Upsert, "foo4")) | ||
dryevt.Send(newNotif(event.Delete, "foo4")) | ||
dryrec.Stop() | ||
|
||
exist, _ := afero.Exists(appFs, conf.LocalDir+"/foo-foo3.yaml") | ||
if exist { | ||
t.Error("foo-foo3.yaml was created but we're in dry-run mode") | ||
} | ||
|
||
rogue := conf.LocalDir + "/roguefile.yaml" | ||
_ = afero.WriteFile(appFs, rogue, []byte{42}, 0600) | ||
dryrec.deleteObsoleteFiles() | ||
|
||
exist, _ = afero.Exists(appFs, rogue) | ||
if !exist { | ||
t.Errorf("garbage collection shouldn't remove files in dry-run mode") | ||
} | ||
} |