]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm-remote/sync/files_test.go
tests: use subtests
[safcm/safcm.git] / cmd / safcm-remote / sync / files_test.go
index 22daa63fae92537239b21cb3fab375f7329ab313..32130e928abbad99c77ed139fbc48373c28c1f32 100644 (file)
@@ -20,66 +20,17 @@ import (
        "io/fs"
        "math/rand"
        "os"
-       "os/user"
        "path/filepath"
        "reflect"
        "regexp"
-       "strconv"
-       "syscall"
        "testing"
 
        "github.com/google/go-cmp/cmp"
 
        "ruderich.org/simon/safcm"
+       ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
 )
 
-type File struct {
-       Path string
-       Mode fs.FileMode
-       Data []byte
-}
-
-func walkDir(basePath string) ([]File, error) {
-       var res []File
-       err := filepath.WalkDir(basePath, func(path string, d fs.DirEntry, err error) error {
-               if err != nil {
-                       return err
-               }
-               info, err := d.Info()
-               if err != nil {
-                       return err
-               }
-               rel, err := filepath.Rel(basePath, path)
-               if err != nil {
-                       return err
-               }
-
-               f := File{
-                       Path: rel,
-                       Mode: info.Mode(),
-               }
-               if f.Mode.Type() == 0 {
-                       x, err := os.ReadFile(path)
-                       if err != nil {
-                               return err
-                       }
-                       f.Data = x
-               } else if f.Mode.Type() == fs.ModeSymlink {
-                       x, err := os.Readlink(path)
-                       if err != nil {
-                               return err
-                       }
-                       f.Data = []byte(x)
-               }
-               res = append(res, f)
-               return nil
-       })
-       if err != nil {
-               return nil, err
-       }
-       return res, nil
-}
-
 var randFilesRegexp = regexp.MustCompile(`\d+"$`)
 
 func TestSyncFiles(t *testing.T) {
@@ -98,11 +49,11 @@ func TestSyncFiles(t *testing.T) {
                t.Fatal(err)
        }
 
-       root := File{
+       root := ft.File{
                Path: ".",
                Mode: fs.ModeDir | 0700,
        }
-       user, uid, group, gid := currentUserAndGroup()
+       user, uid, group, gid := ft.CurrentUserAndGroup()
 
        tmpTestFilePath := "/tmp/safcm-sync-files-test-file"
 
@@ -111,7 +62,7 @@ func TestSyncFiles(t *testing.T) {
                req      safcm.MsgSyncReq
                prepare  func()
                triggers []string
-               expFiles []File
+               expFiles []ft.File
                expResp  safcm.MsgSyncResp
                expDbg   []string
                expErr   error
@@ -153,7 +104,7 @@ func TestSyncFiles(t *testing.T) {
                        },
                        nil,
                        nil,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -235,11 +186,11 @@ func TestSyncFiles(t *testing.T) {
                                },
                        },
                        func() {
-                               createDirectory("dir", 0755)
-                               createFile("dir/file", "content\n", 0644)
+                               ft.CreateDirectory("dir", 0755)
+                               ft.CreateFile("dir/file", "content\n", 0644)
                        },
                        nil,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -276,7 +227,7 @@ func TestSyncFiles(t *testing.T) {
                        },
                        nil,
                        nil,
-                       []File{
+                       []ft.File{
                                root,
                        },
                        safcm.MsgSyncResp{},
@@ -299,7 +250,7 @@ func TestSyncFiles(t *testing.T) {
                        },
                        nil,
                        nil,
-                       []File{
+                       []ft.File{
                                root,
                        },
                        safcm.MsgSyncResp{},
@@ -355,7 +306,7 @@ func TestSyncFiles(t *testing.T) {
                        },
                        nil,
                        nil,
-                       []File{
+                       []ft.File{
                                root,
                        },
                        safcm.MsgSyncResp{},
@@ -406,11 +357,11 @@ func TestSyncFiles(t *testing.T) {
                                },
                        },
                        func() {
-                               createDirectory("dir", 0755)
-                               createFile("dir/file", "content\n", 0644)
+                               ft.CreateDirectory("dir", 0755)
+                               ft.CreateFile("dir/file", "content\n", 0644)
                        },
                        nil,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -473,13 +424,13 @@ func TestSyncFiles(t *testing.T) {
                                if err != nil {
                                        panic(err)
                                }
-                               createDirectory("dir", 0755)
-                               createFile("dir/file", "content\n", 0644)
+                               ft.CreateDirectory("dir", 0755)
+                               ft.CreateFile("dir/file", "content\n", 0644)
                        },
                        []string{
                                ".",
                        },
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -561,14 +512,14 @@ func TestSyncFiles(t *testing.T) {
                                },
                        },
                        func() {
-                               createDirectory("dir", 0750)
-                               createFile("dir/file", "content\n", 0644)
+                               ft.CreateDirectory("dir", 0750)
+                               ft.CreateFile("dir/file", "content\n", 0644)
                        },
                        []string{
                                ".",
                                "dir",
                        },
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -651,14 +602,14 @@ func TestSyncFiles(t *testing.T) {
                                },
                        },
                        func() {
-                               createDirectory("dir", 0755)
+                               ft.CreateDirectory("dir", 0755)
                        },
                        []string{
                                ".",
                                "dir",
                                "dir/file",
                        },
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -742,7 +693,7 @@ func TestSyncFiles(t *testing.T) {
                                "dir",
                                "dir/file",
                        },
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -856,7 +807,7 @@ func TestSyncFiles(t *testing.T) {
                                // Don't use variable for more robust test
                                "/tmp/safcm-sync-files-test-file",
                        },
-                       []File{
+                       []ft.File{
                                root,
                        },
                        safcm.MsgSyncResp{
@@ -890,6 +841,7 @@ func TestSyncFiles(t *testing.T) {
        }
 
        for _, tc := range tests {
+               t.Run(tc.name, func(t *testing.T) {
                // Create separate test directory for each test case
                path := filepath.Join(cwd, "testdata", "files-"+tc.name)
                err = os.Mkdir(path, 0700)
@@ -907,15 +859,14 @@ func TestSyncFiles(t *testing.T) {
 
                s, res := prepareSync(tc.req, &testRunner{
                        t:    t,
-                       name: tc.name,
                })
                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("%s: err = %#v, want %#v",
-                               tc.name, err, tc.expErr)
+                       t.Errorf("err = %#v, want %#v",
+                               err, tc.expErr)
                }
                dbg := res.Wait()
                // Remove random file names from result
@@ -923,27 +874,28 @@ func TestSyncFiles(t *testing.T) {
                        dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`)
                }
                if !reflect.DeepEqual(tc.expDbg, dbg) {
-                       t.Errorf("%s: dbg: %s", tc.name,
+                       t.Errorf("dbg: %s",
                                cmp.Diff(tc.expDbg, dbg))
                }
 
-               files, err := walkDir(path)
+               files, err := ft.WalkDir(path)
                if err != nil {
                        t.Fatal(err)
                }
                if !reflect.DeepEqual(tc.expFiles, files) {
-                       t.Errorf("%s: files: %s", tc.name,
+                       t.Errorf("files: %s",
                                cmp.Diff(tc.expFiles, files))
                }
 
                if !reflect.DeepEqual(tc.expResp, s.resp) {
-                       t.Errorf("%s: resp: %s", tc.name,
+                       t.Errorf("resp: %s",
                                cmp.Diff(tc.expResp, s.resp))
                }
                if !reflect.DeepEqual(tc.triggers, s.triggers) {
-                       t.Errorf("%s: triggers: %s", tc.name,
+                       t.Errorf("triggers: %s",
                                cmp.Diff(tc.triggers, s.triggers))
                }
+               })
        }
 
        os.Remove(tmpTestFilePath)
@@ -971,11 +923,11 @@ func TestSyncFile(t *testing.T) {
                t.Fatal(err)
        }
 
-       root := File{
+       root := ft.File{
                Path: ".",
                Mode: fs.ModeDir | 0700,
        }
-       user, uid, group, gid := currentUserAndGroup()
+       user, uid, group, gid := ft.CurrentUserAndGroup()
 
        tests := []struct {
                name       string
@@ -983,7 +935,7 @@ func TestSyncFile(t *testing.T) {
                file       *safcm.File
                prepare    func()
                expChanged bool
-               expFiles   []File
+               expFiles   []ft.File
                expResp    safcm.MsgSyncResp
                expDbg     []string
                expErr     error
@@ -1009,7 +961,7 @@ func TestSyncFile(t *testing.T) {
                        },
                        nil,
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -1055,7 +1007,7 @@ func TestSyncFile(t *testing.T) {
                        },
                        nil,
                        true,
-                       []File{root},
+                       []ft.File{root},
                        safcm.MsgSyncResp{
                                FileChanges: []safcm.FileChange{
                                        {
@@ -1091,10 +1043,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", "content\n", 0644)
+                               ft.CreateFile("file", "content\n", 0644)
                        },
                        false,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -1123,10 +1075,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", "content\n", 0644)
+                               ft.CreateFile("file", "content\n", 0644)
                        },
                        false,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -1153,10 +1105,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", "content\n", 0755)
+                               ft.CreateFile("file", "content\n", 0755)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -1206,10 +1158,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", "old content\n", 0644)
+                               ft.CreateFile("file", "old content\n", 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -1267,7 +1219,7 @@ func TestSyncFile(t *testing.T) {
                        },
                        nil,
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "link",
@@ -1310,10 +1262,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile(".link8717895732742165505", "", 0600)
+                               ft.CreateFile(".link8717895732742165505", "", 0600)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: ".link8717895732742165505",
@@ -1365,7 +1317,7 @@ func TestSyncFile(t *testing.T) {
                        },
                        nil,
                        true,
-                       []File{root},
+                       []ft.File{root},
                        safcm.MsgSyncResp{
                                FileChanges: []safcm.FileChange{
                                        {
@@ -1401,10 +1353,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createSymlink("link", "target")
+                               ft.CreateSymlink("link", "target")
                        },
                        false,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "link",
@@ -1431,10 +1383,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createSymlink("link", "old-target")
+                               ft.CreateSymlink("link", "old-target")
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "link",
@@ -1490,7 +1442,7 @@ func TestSyncFile(t *testing.T) {
                        },
                        nil,
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -1535,7 +1487,7 @@ func TestSyncFile(t *testing.T) {
                        },
                        nil,
                        true,
-                       []File{root},
+                       []ft.File{root},
                        safcm.MsgSyncResp{
                                FileChanges: []safcm.FileChange{
                                        {
@@ -1570,10 +1522,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createDirectory("dir", 0755)
+                               ft.CreateDirectory("dir", 0755)
                        },
                        false,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -1598,10 +1550,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createDirectory("dir", 0500|fs.ModeSticky)
+                               ft.CreateDirectory("dir", 0500|fs.ModeSticky)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "dir",
@@ -1650,10 +1602,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("path", "content\n", 0644)
+                               ft.CreateFile("path", "content\n", 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -1708,10 +1660,10 @@ func TestSyncFile(t *testing.T) {
                                Data:      []byte("target"),
                        },
                        func() {
-                               createFile("path", "content\n", 0644)
+                               ft.CreateFile("path", "content\n", 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -1766,10 +1718,10 @@ func TestSyncFile(t *testing.T) {
                                Data:      []byte("content\n"),
                        },
                        func() {
-                               createSymlink("path", "target")
+                               ft.CreateSymlink("path", "target")
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -1823,10 +1775,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createSymlink("path", "target")
+                               ft.CreateSymlink("path", "target")
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -1881,10 +1833,10 @@ func TestSyncFile(t *testing.T) {
                                Data:      []byte("content\n"),
                        },
                        func() {
-                               createDirectory("path", 0777)
+                               ft.CreateDirectory("path", 0777)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -1935,10 +1887,10 @@ func TestSyncFile(t *testing.T) {
                                Data:      []byte("target"),
                        },
                        func() {
-                               createDirectory("path", 0777)
+                               ft.CreateDirectory("path", 0777)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -1989,10 +1941,10 @@ func TestSyncFile(t *testing.T) {
                                Data:      []byte("content\n"),
                        },
                        func() {
-                               createFifo("path", 0666)
+                               ft.CreateFifo("path", 0666)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -2042,10 +1994,10 @@ func TestSyncFile(t *testing.T) {
                                Data:      []byte("target"),
                        },
                        func() {
-                               createFifo("path", 0666)
+                               ft.CreateFifo("path", 0666)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -2094,10 +2046,10 @@ func TestSyncFile(t *testing.T) {
                                OrigGroup: "group",
                        },
                        func() {
-                               createFifo("path", 0666)
+                               ft.CreateFifo("path", 0666)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -2148,10 +2100,10 @@ func TestSyncFile(t *testing.T) {
                                Data:      []byte("target"),
                        },
                        func() {
-                               createFile("path", "target", 0644)
+                               ft.CreateFile("path", "target", 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "path",
@@ -2211,14 +2163,14 @@ file
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", `this
+                               ft.CreateFile("file", `this
 is
 file
 !
 `, 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -2283,10 +2235,10 @@ file
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", "\x00\x01\x02", 0644)
+                               ft.CreateFile("file", "\x00\x01\x02", 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -2338,10 +2290,10 @@ file
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", "\x00\x01\x02", 0644)
+                               ft.CreateFile("file", "\x00\x01\x02", 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -2397,10 +2349,10 @@ file
                                OrigGroup: "group",
                        },
                        func() {
-                               createFile("file", "content\n", 0644)
+                               ft.CreateFile("file", "content\n", 0644)
                        },
                        true,
-                       []File{
+                       []ft.File{
                                root,
                                {
                                        Path: "file",
@@ -2444,6 +2396,7 @@ file
        }
 
        for _, tc := range tests {
+               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)
@@ -2461,7 +2414,6 @@ file
 
                s, res := prepareSync(tc.req, &testRunner{
                        t:    t,
-                       name: tc.name,
                })
                s.setDefaults()
 
@@ -2472,8 +2424,8 @@ file
                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("%s: err = %#v, want %#v",
-                               tc.name, err, tc.expErr)
+                       t.Errorf("err = %#v, want %#v",
+                               err, tc.expErr)
                }
                dbg := res.Wait()
                // Remove random file names from result
@@ -2481,27 +2433,28 @@ file
                        dbg[i] = randFilesRegexp.ReplaceAllString(x, `RND"`)
                }
                if !reflect.DeepEqual(tc.expDbg, dbg) {
-                       t.Errorf("%s: dbg: %s", tc.name,
+                       t.Errorf("dbg: %s",
                                cmp.Diff(tc.expDbg, dbg))
                }
 
-               files, err := walkDir(path)
+               files, err := ft.WalkDir(path)
                if err != nil {
                        t.Fatal(err)
                }
                if !reflect.DeepEqual(tc.expFiles, files) {
-                       t.Errorf("%s: files: %s", tc.name,
+                       t.Errorf("files: %s",
                                cmp.Diff(tc.expFiles, files))
                }
 
                if tc.expChanged != changed {
-                       t.Errorf("%s: changed = %#v, want %#v",
-                               tc.name, changed, tc.expChanged)
+                       t.Errorf("changed = %#v, want %#v",
+                               changed, tc.expChanged)
                }
                if !reflect.DeepEqual(tc.expResp, s.resp) {
-                       t.Errorf("%s: resp: %s", tc.name,
+                       t.Errorf("resp: %s",
                                cmp.Diff(tc.expResp, s.resp))
                }
+               })
        }
 
        if !t.Failed() {
@@ -2511,62 +2464,3 @@ file
                }
        }
 }
-
-// Helper functions
-
-func createFile(path string, data string, mode fs.FileMode) {
-       err := os.WriteFile(path, []byte(data), 0644)
-       if err != nil {
-               panic(err)
-       }
-       err = os.Chmod(path, mode)
-       if err != nil {
-               panic(err)
-       }
-}
-func createSymlink(path string, data string) {
-       err := os.Symlink(data, path)
-       if err != nil {
-               panic(err)
-       }
-}
-func createDirectory(path string, mode fs.FileMode) {
-       err := os.Mkdir(path, 0700)
-       if err != nil {
-               panic(err)
-       }
-       err = os.Chmod(path, mode)
-       if err != nil {
-               panic(err)
-       }
-}
-func createFifo(path string, mode fs.FileMode) {
-       err := syscall.Mkfifo(path, 0600)
-       if err != nil {
-               panic(err)
-       }
-       err = os.Chmod(path, mode)
-       if err != nil {
-               panic(err)
-       }
-}
-
-func currentUserAndGroup() (string, int, string, int) {
-       u, err := user.Current()
-       if err != nil {
-               panic(err)
-       }
-       g, err := user.LookupGroupId(u.Gid)
-       if err != nil {
-               panic(err)
-       }
-       uid, err := strconv.Atoi(u.Uid)
-       if err != nil {
-               panic(err)
-       }
-       gid, err := strconv.Atoi(g.Gid)
-       if err != nil {
-               panic(err)
-       }
-       return u.Username, uid, g.Name, gid
-}