X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm-remote%2Fsync%2Ffiles_test.go;h=57d27c7fa8e043b06543387a1a15a720fcdb0755;hb=b088bf0b7e872d0e6ba230d4e4b79625af4fb17e;hp=879fefda156b8789d6b0818459a05b15eb3aa9ae;hpb=7c4e5d64e25acec7209a54a15836b4481c450416;p=safcm%2Fsafcm.git diff --git a/cmd/safcm-remote/sync/files_test.go b/cmd/safcm-remote/sync/files_test.go index 879fefd..57d27c7 100644 --- a/cmd/safcm-remote/sync/files_test.go +++ b/cmd/safcm-remote/sync/files_test.go @@ -53,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{ ".": { @@ -157,6 +161,7 @@ func TestSyncFiles(t *testing.T) { { "basic: no change", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -211,6 +216,7 @@ func TestSyncFiles(t *testing.T) { { "invalid File: user", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -234,6 +240,7 @@ func TestSyncFiles(t *testing.T) { }, { "invalid File: group", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -265,6 +272,7 @@ func TestSyncFiles(t *testing.T) { // 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{ "/": { @@ -306,6 +314,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: no change", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -369,6 +378,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: change root", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -405,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) }, @@ -461,6 +468,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: change middle", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -551,6 +559,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: change leaf", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -637,6 +646,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: multiple changes", + false, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ ".": { @@ -738,6 +748,7 @@ func TestSyncFiles(t *testing.T) { { "triggers: absolute paths", + skipUnlessCiRun, safcm.MsgSyncReq{ Files: map[string]*safcm.File{ "/": { @@ -772,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", @@ -823,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) } @@ -841,9 +847,12 @@ func TestSyncFiles(t *testing.T) { s, res := prepareSync(tc.req, &testRunner{ t: t, }) - s.setDefaults() + err = s.setDefaults() + if err != nil { + t.Fatal(err) + } - err := s.syncFiles() + err = s.syncFiles() testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() // Remove random file names from result @@ -860,7 +869,7 @@ func TestSyncFiles(t *testing.T) { testutil.AssertEqual(t, "resp", s.resp, tc.expResp) testutil.AssertEqual(t, "triggers", - s.triggers, tc.triggers) + s.triggers, tc.expTriggers) }) } @@ -908,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 @@ -2230,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", }, }, }, @@ -2286,7 +2295,7 @@ file Gid: gid, }, DataDiff: `@@ -1,2 +1,2 @@ -- +- +content `, @@ -2346,7 +2355,7 @@ file }, DataDiff: `@@ -1,2 +1,2 @@ -content -+ ++ `, }, @@ -2365,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) } @@ -2381,13 +2390,16 @@ 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) + err = s.syncFile(tc.file, &changed) testutil.AssertErrorEqual(t, "err", err, tc.expErr) dbg := res.Wait() // Remove random file names from result