]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
tests: end-to-end: add test with changes in /tmp
authorSimon Ruderich <simon@ruderich.org>
Sat, 1 Nov 2025 09:19:32 +0000 (10:19 +0100)
committerSimon Ruderich <simon@ruderich.org>
Sat, 1 Nov 2025 09:29:44 +0000 (10:29 +0100)
The test is careful not to affect real data on the system.

12 files changed:
.gitignore
Makefile
cmd/safcm/main_sync_test.go
cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/dir/.gitignore [new file with mode: 0644]
cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/executable [new file with mode: 0755]
cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/file [new file with mode: 0644]
cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/symlink [new symlink]
cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/template [new file with mode: 0644]
cmd/safcm/testdata/ssh/project/changes.example.org/templates.yaml [new file with mode: 0644]
cmd/safcm/testdata/ssh/project/groups.yaml
cmd/safcm/testdata/ssh/project/hosts.yaml
cmd/safcm/testdata/ssh/ssh/ssh_config

index 8e87b2f84d9438df5b9930c690e6dd29c7ddf88e..6a45e082f4bbe647b56ffcb39552069d869edad5 100644 (file)
@@ -1,4 +1,5 @@
 /cmd/safcm/remote/
+/cmd/safcm/testdata/ssh/project/changes.example.org/permissions.yaml
 /cmd/safcm/testdata/ssh/project/no-changes.example.org/
 /cmd/safcm/testdata/ssh/ssh/authorized_keys
 /cmd/safcm/testdata/ssh/ssh/id_ed25519
index 759e7031eb4e516160adf21e0554fa086c7c1970..321de8048731b79c37addc2f4089430ce868b0fe 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -30,6 +30,7 @@ clean:
        rm -f cmd/safcm/testdata/ssh/ssh/known_hosts
        rm -f cmd/safcm/testdata/ssh/sshd/ssh_host_key
        rm -f cmd/safcm/testdata/ssh/sshd/ssh_host_key.pub
+       rm -f cmd/safcm/testdata/ssh/project/changes.example.org/permissions.yaml
        rm -rf cmd/safcm/testdata/ssh/project/no-changes.example.org/
 
 .PHONY: all test clean safcm
index dbab3169189cf3acf098ba76aea428c50ac4036d..c02b8816eb1e1e71ce476f2ca53b6936ac97abdd 100644 (file)
@@ -5,6 +5,7 @@ package main_test
 
 import (
        "fmt"
+       "io/fs"
        "net"
        "os"
        "os/exec"
@@ -24,6 +25,21 @@ func TestSyncSshEndToEnd(t *testing.T) {
                t.Fatal(err)
        }
 
+       var userStr, groupStr string
+       {
+               u, uid, g, gid := ft.CurrentUserAndGroup()
+               userStr = fmt.Sprintf("%s(%d)", u, uid)
+               groupStr = fmt.Sprintf("%s(%d)", g, gid)
+       }
+
+       changesPath := "/tmp/kjn8W3OR4LFwo4Iw7YLY5"
+       _, err = os.Stat(changesPath)
+       if err == nil {
+               // Very unlikely, but just in case
+               t.Fatalf("%q already exists, aborting to prevent data loss",
+                       changesPath)
+       }
+
        var suffix string
        // Needs different options in sshd_config
        if runtime.GOOS == "openbsd" {
@@ -77,6 +93,23 @@ func TestSyncSshEndToEnd(t *testing.T) {
        ft.CreateFile("no-changes.example.org/permissions.yaml",
                noChangePermissions, 0644)
 
+       changesPermissions := `
+/: 0755 root root
+/tmp: 1777 root root
+/tmp/kjn8W3OR4LFwo4Iw7YLY5/file: 0604
+/tmp/kjn8W3OR4LFwo4Iw7YLY5/template: 0400
+`
+       if runtime.GOOS == "openbsd" || runtime.GOOS == "freebsd" {
+               changesPermissions = `
+/: 0755 root wheel
+/tmp: 1777 root wheel
+/tmp/kjn8W3OR4LFwo4Iw7YLY5/file: 0604
+/tmp/kjn8W3OR4LFwo4Iw7YLY5/template: 0400
+`
+       }
+       ft.CreateFile("changes.example.org/permissions.yaml",
+               changesPermissions, 0644)
+
        ciRun := len(os.Getenv("SAFCM_CI_RUN")) != 0
        isRoot := os.Getuid() == 0
        skipUnlessHarmless := !(ciRun || !isRoot)
@@ -192,6 +225,33 @@ func TestSyncSshEndToEnd(t *testing.T) {
                        nil,
                },
 
+               {
+                       "changes",
+                       skipUnlessHarmless,
+                       false,
+                       []string{"changes.example.org"},
+                       `<LOG>[info]    [changes.example.org] 
+changed 7 file(s):
+"/tmp/kjn8W3OR4LFwo4Iw7YLY5": created, dir, USER GROUP, 0755
+"/tmp/kjn8W3OR4LFwo4Iw7YLY5/dir": created, dir, USER GROUP, 0755
+"/tmp/kjn8W3OR4LFwo4Iw7YLY5/dir/.gitignore": created, file, USER GROUP, 0644
+"/tmp/kjn8W3OR4LFwo4Iw7YLY5/executable": created, file, USER GROUP, 0755
+"/tmp/kjn8W3OR4LFwo4Iw7YLY5/file": created, file, USER GROUP, 0604
+"/tmp/kjn8W3OR4LFwo4Iw7YLY5/symlink": created, symlink, USER GROUP, 0777
+"/tmp/kjn8W3OR4LFwo4Iw7YLY5/template": created, file, USER GROUP, 0400
+`,
+                       nil,
+               },
+               {
+                       "changes (up-to-date)",
+                       skipUnlessHarmless,
+                       false,
+                       []string{"changes.example.org"},
+                       `<LOG>[info]    [changes.example.org] no changes
+`,
+                       nil,
+               },
+
                {
                        "no effect commands (dry-run)",
                        false,
@@ -358,16 +418,69 @@ executed 2 command(s):
                                tmp = append(tmp, x)
                        }
                        res := strings.Join(tmp, "\n")
+                       // Adapt output to current user/group
+                       tc.exp = strings.ReplaceAll(tc.exp, "USER", userStr)
+                       tc.exp = strings.ReplaceAll(tc.exp, "GROUP", groupStr)
 
                        testutil.AssertEqual(t, "res", res, tc.exp)
                        testutil.AssertErrorEqual(t, "err", err, tc.expErr)
                })
        }
 
+       if !skipUnlessHarmless {
+               t.Run("check result of changes.example.org", func(t *testing.T) {
+                       exp := []ft.File{
+                               {
+                                       Path: ".",
+                                       Mode: fs.ModeDir | 0755,
+                               },
+                               {
+                                       Path: "dir",
+                                       Mode: fs.ModeDir | 0755,
+                               },
+                               {
+                                       Path: "dir/.gitignore",
+                                       Mode: 0644,
+                                       Data: []byte{},
+                               },
+                               {
+                                       Path: "executable",
+                                       Mode: 0755,
+                                       Data: []byte("#!/bin/sh\n\necho 'Hello World!'\n"),
+                               },
+                               {
+                                       Path: "file",
+                                       Mode: 0604,
+                                       Data: []byte("just\na\nsmall\ntext\nfile\n"),
+                               },
+                               {
+                                       Path: "symlink",
+                                       Mode: fs.ModeSymlink | 0777,
+                                       Data: []byte("file"),
+                               },
+                               {
+                                       Path: "template",
+                                       Mode: 0400,
+                                       Data: []byte("\n>> changes\n\n"),
+                               },
+                       }
+
+                       files, err := ft.WalkDir(changesPath)
+                       if err != nil {
+                               t.Fatal(err)
+                       }
+                       testutil.AssertEqual(t, "files", files, exp)
+               })
+       }
+
        err = os.RemoveAll(remotePath)
        if err != nil {
                t.Fatal(err)
        }
+       err = os.RemoveAll(changesPath)
+       if err != nil {
+               t.Fatal(err)
+       }
 }
 
 // vi: set noet ts=4 sw=4 sts=4:
diff --git a/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/dir/.gitignore b/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/dir/.gitignore
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/executable b/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/executable
new file mode 100755 (executable)
index 0000000..97cca98
--- /dev/null
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+echo 'Hello World!'
diff --git a/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/file b/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/file
new file mode 100644 (file)
index 0000000..1e25e25
--- /dev/null
@@ -0,0 +1,5 @@
+just
+a
+small
+text
+file
diff --git a/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/symlink b/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/symlink
new file mode 120000 (symlink)
index 0000000..1a010b1
--- /dev/null
@@ -0,0 +1 @@
+file
\ No newline at end of file
diff --git a/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/template b/cmd/safcm/testdata/ssh/project/changes.example.org/files/tmp/kjn8W3OR4LFwo4Iw7YLY5/template
new file mode 100644 (file)
index 0000000..e3be637
--- /dev/null
@@ -0,0 +1,3 @@
+{{if .InGroup "changes"}}
+>> changes
+{{end}}
diff --git a/cmd/safcm/testdata/ssh/project/changes.example.org/templates.yaml b/cmd/safcm/testdata/ssh/project/changes.example.org/templates.yaml
new file mode 100644 (file)
index 0000000..b8b8097
--- /dev/null
@@ -0,0 +1 @@
+- /tmp/kjn8W3OR4LFwo4Iw7YLY5/template
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..ee6e00c9928a3545ca7a22705b823881ddd5e964 100644 (file)
@@ -0,0 +1,2 @@
+changes:
+  - changes.example.org
index 34a858f22edfc6ae9e3ddf2623c5bd0d647113fa..e0af9d755d4886f23d223088f5c050fbf322b220 100644 (file)
@@ -1,4 +1,5 @@
-- name: no-settings.example.org
+- name: changes.example.org
 - name: no-changes.example.org
-- name: no-effect-commands.example.org
 - name: no-effect-commands-failing.example.org
+- name: no-effect-commands.example.org
+- name: no-settings.example.org
index a3394e37c789498724c12c723c6201d85763ff5d..6784ad49a0939fc960b2869c044668b5ec561a8c 100644 (file)
@@ -2,6 +2,6 @@ IdentityFile ../ssh/id_ed25519
 IdentitiesOnly yes
 UserKnownHostsFile ../ssh/known_hosts
 
-Host no-settings.example.org no-changes.example.org no-effect-commands.example.org no-effect-commands-failing.example.org
+Host changes.example.org no-changes.example.org no-effect-commands-failing.example.org no-effect-commands.example.org no-settings.example.org
     Hostname 127.0.0.1
     Port 29327