X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm%2Fsync_sync_test.go;h=f100f35125e125fa64884086698f4aac78448d3d;hb=6015bd22c34e8287b43b462facd5ad491296c8c2;hp=f6aafd52445e6664f4958ab82703736a51e93982;hpb=f2f2bc47e8729548f3c10117f7f008b547c4afc5;p=safcm%2Fsafcm.git diff --git a/cmd/safcm/sync_sync_test.go b/cmd/safcm/sync_sync_test.go index f6aafd5..f100f35 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -22,12 +22,10 @@ import ( "log" "os" "path/filepath" - "reflect" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" + "ruderich.org/simon/safcm/testutil" ) func TestHostSyncReq(t *testing.T) { @@ -61,6 +59,7 @@ func TestHostSyncReq(t *testing.T) { Groups: []string{ "all", "group", + "group3", "remove", "host1.example.org", }, @@ -95,7 +94,7 @@ func TestHostSyncReq(t *testing.T) { Mode: 0644, Uid: -1, Gid: -1, - Data: []byte("Welcome to Host ONE\n\n\n\n"), + Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"), }, "/etc/rc.local": &safcm.File{ OrigGroup: "group", @@ -137,13 +136,19 @@ func TestHostSyncReq(t *testing.T) { Services: []string{ "unbound", }, - Commands: []string{ - "echo command one", - "echo -n command two", + Commands: []*safcm.Command{ + { + OrigGroup: "group", + Cmd: "echo command one", + }, + { + OrigGroup: "group", + Cmd: "echo -n command two", + }, }, }, []string{ - "host1.example.org: 3 host groups: all group host1.example.org remove", + "host1.example.org: 3 host groups: all group group3 host1.example.org remove", "host1.example.org: 3 host group priorities (desc. order): host1.example.org", }, nil, @@ -159,6 +164,7 @@ func TestHostSyncReq(t *testing.T) { Groups: []string{ "all", "group", + "group3", "remove", "host1.example.org", }, @@ -193,7 +199,7 @@ func TestHostSyncReq(t *testing.T) { Mode: 0644, Uid: -1, Gid: -1, - Data: []byte("Welcome to Host ONE\n\n\n\n"), + Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"), }, "/etc/rc.local": &safcm.File{ OrigGroup: "group", @@ -235,9 +241,15 @@ func TestHostSyncReq(t *testing.T) { Services: []string{ "unbound", }, - Commands: []string{ - "echo command one", - "echo -n command two", + Commands: []*safcm.Command{ + { + OrigGroup: "group", + Cmd: "echo command one", + }, + { + OrigGroup: "group", + Cmd: "echo -n command two", + }, }, }, nil, @@ -419,71 +431,65 @@ func TestHostSyncReq(t *testing.T) { } for _, tc := range tests { - err = os.Chdir(filepath.Join(cwd, "testdata", tc.project)) - if err != nil { - t.Fatal(err) - } + t.Run(tc.name, func(t *testing.T) { + err = os.Chdir(filepath.Join(cwd, + "testdata", tc.project)) + if err != nil { + t.Fatal(err) + } - // `safcm fixperms` in case user has strict umask - log.SetOutput(io.Discard) - err := MainFixperms() - if err != nil { - t.Fatal(err) - } - log.SetOutput(os.Stderr) + // `safcm fixperms` in case user has strict umask + log.SetOutput(io.Discard) + err := MainFixperms() + if err != nil { + t.Fatal(err) + } + log.SetOutput(os.Stderr) - cfg, allHosts, allGroups, err := LoadBaseFiles() - if err != nil { - t.Fatal(err) - } - cfg.LogLevel = tc.level + cfg, allHosts, allGroups, err := LoadBaseFiles() + if err != nil { + t.Fatal(err) + } + cfg.LogLevel = tc.level - var events []string - ch := make(chan Event) - done := make(chan struct{}) - go func() { - for { - x, ok := <-ch - if !ok { - break + var events []string + ch := make(chan Event) + done := make(chan struct{}) + go func() { + for { + x, ok := <-ch + if !ok { + break + } + if x.ConnEvent.Type != 0 { + panic("unexpected ConnEvent") + } + events = append(events, + fmt.Sprintf("%s: %v %d %s", + x.Host.Name, + x.Error, x.Log.Level, + x.Log.Text)) } - if x.ConnEvent.Type != 0 { - panic("unexpected ConnEvent") - } - events = append(events, - fmt.Sprintf("%s: %v %d %s", - x.Host.Name, - x.Error, x.Log.Level, - x.Log.Text)) - } - done <- struct{}{} - }() + done <- struct{}{} + }() - s := &Sync{ - host: allHosts.Map[tc.host], - config: cfg, - allHosts: allHosts, - allGroups: allGroups, - events: ch, - } + s := &Sync{ + host: allHosts.Map[tc.host], + config: cfg, + allHosts: allHosts, + allGroups: allGroups, + events: ch, + } - res, err := s.hostSyncReq(tc.detected) - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("%s: res: %s", tc.name, - cmp.Diff(tc.exp, res)) - } - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("%s: err = %#v, want %#v", - tc.name, err, tc.expErr) - } + res, err := s.hostSyncReq(tc.detected) - close(ch) - <-done - if !reflect.DeepEqual(tc.expEvents, events) { - t.Errorf("%s: events: %s", tc.name, - cmp.Diff(tc.expEvents, events)) - } + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) + close(ch) + <-done + testutil.AssertEqual(t, "events", + events, tc.expEvents) + }) } }