From b167de3096863ade86b5f4aae051d080861c45a7 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 14 Jul 2019 18:04:31 +0200 Subject: [PATCH] filters: add some basic test cases for DatasetMapFilter --- daemon/filters/fsmapfilter_test.go | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 daemon/filters/fsmapfilter_test.go diff --git a/daemon/filters/fsmapfilter_test.go b/daemon/filters/fsmapfilter_test.go new file mode 100644 index 0000000..4a3b418 --- /dev/null +++ b/daemon/filters/fsmapfilter_test.go @@ -0,0 +1,89 @@ +package filters + +import ( + "github.com/zrepl/zrepl/zfs" +) + +import "testing" + +func TestDatasetMapFilter(t *testing.T) { + + type testCase struct { + name string + filter map[string]string + checkPass map[string]bool // each entry is checked to match the filter's `pass` return value + } + + tcs := []testCase{ + { + "default_no_match", + map[string]string{}, + map[string]bool{ + "": false, + "foo": false, + "zroot": false, + }, + }, + { + "more_specific_path_has_precedence", + map[string]string{ + "tank<": "ok", + "tank/tmp<": "!", + "tank/home/x<": "!", + "tank/home/x/1": "ok", + }, + map[string]bool{ + "zroot": false, + "tank": true, + "tank/tmp": false, + "tank/tmp/foo": false, + "tank/home/x": false, + "tank/home/y": true, + "tank/home/x/1": true, + "tank/home/x/2": false, + }, + }, + { + "precedence_of_specific_over_subtree_wildcard_on_same_path", + map[string]string{ + "tank/home/bob": "ok", + "tank/home/bob<": "!", + }, + map[string]bool{ + "tank/home/bob": true, + "tank/home/bob/downloads": false, + }, + }, + } + + for tc := range tcs { + t.Run(tcs[tc].name, func(t *testing.T) { + c := tcs[tc] + f := NewDatasetMapFilter(len(c.filter), true) + for p, a := range c.filter { + err := f.Add(p, a) + if err != nil { + t.Fatalf("incorrect filter spec: %s", err) + } + } + for p, checkPass := range c.checkPass { + zp, err := zfs.NewDatasetPath(p) + if err != nil { + t.Fatalf("incorrect path spec: %s", err) + } + pass, err := f.Filter(zp) + if err != nil { + t.Fatalf("unexpected filter error: %s", err) + } + ok := pass == checkPass + failstr := "OK" + if !ok { + failstr = "FAIL" + t.Fail() + } + t.Logf("%-40q %5v (exp=%v act=%v)", p, failstr, checkPass, pass) + } + }) + } + +}