]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
sync: run most tests which modify the host only in CI
authorSimon Ruderich <simon@ruderich.org>
Wed, 28 Apr 2021 10:26:18 +0000 (12:26 +0200)
committerSimon Ruderich <simon@ruderich.org>
Wed, 28 Apr 2021 10:31:35 +0000 (12:31 +0200)
This is especially important in case the user executes the tests as root
(no recommended but not prevented either). Permissions on paths like /
or /tmp which differ from those expected by the test could otherwise be
modified by the tests.

However, the end-to-end SSH tests which write /tmp/safcm-remote-$uid
(but no other paths) are still run so we get proper coverage of basic
features.

ci/run
cmd/safcm-remote/sync/files_test.go

diff --git a/ci/run b/ci/run
index 472502cb0fc43cd0fd11471d9e4c8c312e7ad5e5..45db771a7e9a40e71cfcc49cdb834a5c7db248ac 100755 (executable)
--- a/ci/run
+++ b/ci/run
@@ -12,6 +12,10 @@ if test $# -ne 0; then
     flags="$*"
 fi
 
+# Run additional tests in CI
+SAFCM_CI_RUN=1
+export SAFCM_CI_RUN
+
 make $flags
 make test $flags
 
index 879fefda156b8789d6b0818459a05b15eb3aa9ae..bd5eac9930c31b8978efe8060f491dd859b9d604 100644 (file)
@@ -53,10 +53,13 @@ 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
+               skip     bool
                req      safcm.MsgSyncReq
                prepare  func()
                triggers []string
@@ -74,6 +77,7 @@ func TestSyncFiles(t *testing.T) {
 
                {
                        "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{
                                        ".": {
@@ -461,6 +471,7 @@ func TestSyncFiles(t *testing.T) {
 
                {
                        "triggers: change middle",
+                       false,
                        safcm.MsgSyncReq{
                                Files: map[string]*safcm.File{
                                        ".": {
@@ -551,6 +562,7 @@ func TestSyncFiles(t *testing.T) {
 
                {
                        "triggers: change leaf",
+                       false,
                        safcm.MsgSyncReq{
                                Files: map[string]*safcm.File{
                                        ".": {
@@ -637,6 +649,7 @@ func TestSyncFiles(t *testing.T) {
 
                {
                        "triggers: multiple changes",
+                       false,
                        safcm.MsgSyncReq{
                                Files: map[string]*safcm.File{
                                        ".": {
@@ -738,6 +751,7 @@ func TestSyncFiles(t *testing.T) {
 
                {
                        "triggers: absolute paths",
+                       skipUnlessCiRun,
                        safcm.MsgSyncReq{
                                Files: map[string]*safcm.File{
                                        "/": {
@@ -772,16 +786,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,6 +828,10 @@ 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)