X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm-remote%2Fsync%2Ffiles_test.go;h=57d27c7fa8e043b06543387a1a15a720fcdb0755;hb=b088bf0b7e872d0e6ba230d4e4b79625af4fb17e;hp=e57401e3ada3c13b5d3e0edca9b37f06ab19d306;hpb=4299c5d0946ecfa034e9eefe6b16e50bab94a820;p=safcm%2Fsafcm.git diff --git a/cmd/safcm-remote/sync/files_test.go b/cmd/safcm-remote/sync/files_test.go index e57401e..57d27c7 100644 --- a/cmd/safcm-remote/sync/files_test.go +++ b/cmd/safcm-remote/sync/files_test.go @@ -21,14 +21,12 @@ import ( "math/rand" "os" "path/filepath" - "reflect" "regexp" "testing" - "github.com/google/go-cmp/cmp" - "ruderich.org/simon/safcm" ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest" + "ruderich.org/simon/safcm/testutil" ) var randFilesRegexp = regexp.MustCompile(`\d+"$`) @@ -55,27 +53,31 @@ func TestSyncFiles(t *testing.T) { } user, uid, group, gid := ft.CurrentUserAndGroup() + skipUnlessCiRun := len(os.Getenv("SAFCM_CI_RUN")) == 0 + tmpTestFilePath := "/tmp/safcm-sync-files-test-file" tests := []struct { - name string - req safcm.MsgSyncReq - prepare func() - triggers []string - expFiles []ft.File - expResp safcm.MsgSyncResp - expDbg []string - expErr error + name string + skip bool + req safcm.MsgSyncReq + prepare func() + expTriggers []string + expFiles []ft.File + expResp safcm.MsgSyncResp + expDbg []string + expErr error }{ // NOTE: Also update MsgSyncResp in safcm test cases when - // changing anything here! + // changing the MsgSyncResp struct! // See TestSyncFile() for most file related tests. This // function only tests the overall results and triggers. { "basic: create", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -159,6 +161,7 @@ func TestSyncFiles(t *testing.T) { { "basic: no change", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -213,6 +216,7 @@ func TestSyncFiles(t *testing.T) { { "invalid File: user", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -236,6 +240,7 @@ func TestSyncFiles(t *testing.T) { }, { "invalid File: group", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -263,43 +268,32 @@ func TestSyncFiles(t *testing.T) { // don't want to modify the running system. Use this // test (and the one below for triggers) as a basic // check that absolute paths work. + // + // Use numeric IDs as not all systems use root/root; + // for example BSDs use root/wheel. "absolute paths: no change", + skipUnlessCiRun, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ "/": { Path: "/", Mode: fs.ModeDir | 0755, - User: "root", - Uid: -1, - Group: "root", - Gid: -1, + Uid: 0, + Gid: 0, OrigGroup: "group", }, "/etc": { Path: "/etc", Mode: fs.ModeDir | 0755, - User: "root", - Uid: -1, - Group: "root", - Gid: -1, + Uid: 0, + Gid: 0, OrigGroup: "group", }, "/tmp": { Path: "/tmp", Mode: fs.ModeDir | 0777 | fs.ModeSticky, - User: "root", - Uid: -1, - Group: "root", - Gid: -1, - OrigGroup: "group", - }, - "/var/tmp": { - Path: "/var/tmp", - Mode: fs.ModeDir | 0777 | fs.ModeSticky, - User: "root", - Uid: -1, - Group: "root", - Gid: -1, + Uid: 0, + Gid: 0, OrigGroup: "group", }, }, @@ -314,13 +308,13 @@ func TestSyncFiles(t *testing.T) { `4: sync remote: files: "/" (group): unchanged`, `4: sync remote: files: "/etc" (group): unchanged`, `4: sync remote: files: "/tmp" (group): unchanged`, - `4: sync remote: files: "/var/tmp" (group): unchanged`, }, nil, }, { "triggers: no change", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -384,6 +378,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: change root", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -420,10 +415,7 @@ func TestSyncFiles(t *testing.T) { }, }, func() { - err = os.Chmod(".", 0750) - if err != nil { - panic(err) - } + ft.CreateDirectoryExists(".", 0750) ft.CreateDirectory("dir", 0755) ft.CreateFile("dir/file", "content\n", 0644) }, @@ -476,6 +468,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: change middle", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -566,6 +559,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: change leaf", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -652,6 +646,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: multiple changes", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -753,15 +748,14 @@ func TestSyncFiles(t *testing.T) { { "triggers: absolute paths", + skipUnlessCiRun, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ "/": { Path: "/", Mode: fs.ModeDir | 0755, - User: "root", - Uid: -1, - Group: "root", - Gid: -1, + Uid: 0, + Gid: 0, OrigGroup: "group", TriggerCommands: []string{ "echo trigger /", @@ -770,10 +764,8 @@ func TestSyncFiles(t *testing.T) { "/tmp": { Path: "/tmp", Mode: fs.ModeDir | 0777 | fs.ModeSticky, - User: "root", - Uid: -1, - Group: "root", - Gid: -1, + Uid: 0, + Gid: 0, OrigGroup: "group", TriggerCommands: []string{ "echo trigger /tmp", @@ -791,16 +783,7 @@ func TestSyncFiles(t *testing.T) { }, }, }, - func() { - // This is slightly racy but the file name - // should be rare enough that this isn't an - // issue - _, err := os.Stat(tmpTestFilePath) - if err == nil { - t.Fatalf("%q exists, aborting", - tmpTestFilePath) - } - }, + nil, []string{ "/", "/tmp", @@ -842,9 +825,13 @@ func TestSyncFiles(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { + if tc.skip { + t.SkipNow() + } + // Create separate test directory for each test case path := filepath.Join(cwd, "testdata", "files-"+tc.name) - err = os.Mkdir(path, 0700) + err := os.Mkdir(path, 0700) if err != nil { t.Fatal(err) } @@ -860,41 +847,29 @@ func TestSyncFiles(t *testing.T) { s, res := prepareSync(tc.req, &testRunner{ t: t, }) - s.setDefaults() - - err := s.syncFiles() - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) + err = s.setDefaults() + if err != nil { + t.Fatal(err) } + + err = s.syncFiles() + testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() // Remove random file names from result for i, x := range dbg { dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`) } - if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("dbg: %s", - cmp.Diff(tc.expDbg, dbg)) - } + testutil.AssertEqual(t, "dbg", dbg, tc.expDbg) files, err := ft.WalkDir(path) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("files: %s", - cmp.Diff(tc.expFiles, files)) - } + testutil.AssertEqual(t, "files", files, tc.expFiles) - if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("resp: %s", - cmp.Diff(tc.expResp, s.resp)) - } - if !reflect.DeepEqual(tc.triggers, s.triggers) { - t.Errorf("triggers: %s", - cmp.Diff(tc.triggers, s.triggers)) - } + testutil.AssertEqual(t, "resp", s.resp, tc.expResp) + testutil.AssertEqual(t, "triggers", + s.triggers, tc.expTriggers) }) } @@ -942,7 +917,7 @@ func TestSyncFile(t *testing.T) { }{ // NOTE: Also update MsgSyncResp in safcm test cases when - // changing anything here! + // changing the MsgSyncResp struct! // TODO: Add tests for chown and run them only as root @@ -2264,7 +2239,7 @@ file Group: group, Gid: gid, }, - DataDiff: "Binary files differ, cannot show diff", + DataDiff: "Binary files differ (3 -> 4 bytes), cannot show diff", }, }, }, @@ -2320,7 +2295,7 @@ file Gid: gid, }, DataDiff: `@@ -1,2 +1,2 @@ -- +- +content `, @@ -2380,7 +2355,7 @@ file }, DataDiff: `@@ -1,2 +1,2 @@ -content -+ ++ `, }, @@ -2399,7 +2374,7 @@ file t.Run(tc.name, func(t *testing.T) { // Create separate test directory for each test case path := filepath.Join(cwd, "testdata", "file-"+tc.name) - err = os.Mkdir(path, 0700) + err := os.Mkdir(path, 0700) if err != nil { t.Fatal(err) } @@ -2415,45 +2390,32 @@ file s, res := prepareSync(tc.req, &testRunner{ t: t, }) - s.setDefaults() + err = s.setDefaults() + if err != nil { + t.Fatal(err) + } // Deterministic temporary symlink names rand.Seed(0) var changed bool - err := s.syncFile(tc.file, &changed) - // Ugly but the simplest way to compare errors (including nil) - if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) { - t.Errorf("err = %#v, want %#v", - err, tc.expErr) - } + err = s.syncFile(tc.file, &changed) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() // Remove random file names from result for i, x := range dbg { dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`) } - if !reflect.DeepEqual(tc.expDbg, dbg) { - t.Errorf("dbg: %s", - cmp.Diff(tc.expDbg, dbg)) - } + testutil.AssertEqual(t, "dbg", dbg, tc.expDbg) files, err := ft.WalkDir(path) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(tc.expFiles, files) { - t.Errorf("files: %s", - cmp.Diff(tc.expFiles, files)) - } + testutil.AssertEqual(t, "files", files, tc.expFiles) - if tc.expChanged != changed { - t.Errorf("changed = %#v, want %#v", - changed, tc.expChanged) - } - if !reflect.DeepEqual(tc.expResp, s.resp) { - t.Errorf("resp: %s", - cmp.Diff(tc.expResp, s.resp)) - } + testutil.AssertEqual(t, "changed", changed, tc.expChanged) + testutil.AssertEqual(t, "resp", s.resp, tc.expResp) }) }