]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm-remote/sync/commands_test.go
sync: shorten log messages by removing "info/sync remote:" prefix
[safcm/safcm.git] / cmd / safcm-remote / sync / commands_test.go
index aeeea32f0ce2233ad281f755312024e232beda8a..0bd0949b020b922976744ea9f85e3beacf381cee 100644 (file)
@@ -20,16 +20,19 @@ import (
        "io/fs"
        "os"
        "os/exec"
-       "reflect"
        "testing"
 
-       "github.com/google/go-cmp/cmp"
-
        "ruderich.org/simon/safcm"
+       "ruderich.org/simon/safcm/testutil"
 )
 
 func TestSyncCommands(t *testing.T) {
+       exe, err := os.Executable()
+       if err != nil {
+               t.Fatal(err)
+       }
        env := append(os.Environ(),
+               "SAFCM_HELPER="+exe,
                "SAFCM_GROUPS=all group1 group2 host.example.org",
                "SAFCM_GROUP_all=all",
                "SAFCM_GROUP_group1=group1",
@@ -51,7 +54,7 @@ func TestSyncCommands(t *testing.T) {
        }{
 
                // NOTE: Also update MsgSyncResp in safcm test cases when
-               // changing anything here!
+               // changing the MsgSyncResp struct!
 
                {
                        "successful command",
@@ -62,8 +65,11 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               OrigGroup: "group",
+                                               Cmd:       "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        nil,
@@ -79,8 +85,8 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_"`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr",
+                               `3: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
+                               "5: commands: command output:\nfake stdout/stderr",
                        },
                        safcm.MsgSyncResp{
                                CommandChanges: []safcm.CommandChange{
@@ -102,8 +108,11 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               OrigGroup: "group",
+                                               Cmd:       "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        nil,
@@ -131,8 +140,11 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo hi; false",
+                               Commands: []*safcm.Command{
+                                       {
+                                               OrigGroup: "group",
+                                               Cmd:       "echo hi; false",
+                                       },
                                },
                        },
                        nil,
@@ -148,8 +160,8 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo hi; false"`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr",
+                               `3: commands: running "/bin/sh" "-c" "echo hi; false" (group)`,
+                               "5: commands: command output:\nfake stdout/stderr",
                        },
                        safcm.MsgSyncResp{
                                CommandChanges: []safcm.CommandChange{
@@ -172,8 +184,11 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo hi; false",
+                               Commands: []*safcm.Command{
+                                       {
+                                               OrigGroup: "group",
+                                               Cmd:       "echo hi; false",
+                                       },
                                },
                        },
                        nil,
@@ -201,11 +216,20 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo first",
-                                       "echo second",
-                                       "false",
-                                       "echo third",
+                               Commands: []*safcm.Command{
+                                       {
+                                               OrigGroup: "group1",
+                                               Cmd:       "echo first",
+                                       }, {
+                                               OrigGroup: "group2",
+                                               Cmd:       "echo second",
+                                       }, {
+                                               OrigGroup: "group3",
+                                               Cmd:       "false",
+                                       }, {
+                                               OrigGroup: "group4",
+                                               Cmd:       "echo third",
+                                       },
                                },
                        },
                        nil,
@@ -247,11 +271,11 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo first"`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr first",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo second"`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr second",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "false"`,
+                               `3: commands: running "/bin/sh" "-c" "echo first" (group1)`,
+                               "5: commands: command output:\nfake stdout/stderr first",
+                               `3: commands: running "/bin/sh" "-c" "echo second" (group2)`,
+                               "5: commands: command output:\nfake stdout/stderr second",
+                               `3: commands: running "/bin/sh" "-c" "false" (group3)`,
                        },
                        safcm.MsgSyncResp{
                                CommandChanges: []safcm.CommandChange{
@@ -315,8 +339,11 @@ func TestSyncCommands(t *testing.T) {
                                                },
                                        },
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               OrigGroup: "group",
+                                               Cmd:       "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        []string{
@@ -361,12 +388,12 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ."`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr .",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger dir"`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr dir",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_"`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr",
+                               `3: commands: running "/bin/sh" "-c" "echo trigger ." (".")`,
+                               "5: commands: command output:\nfake stdout/stderr .",
+                               `3: commands: running "/bin/sh" "-c" "echo trigger dir" ("dir")`,
+                               "5: commands: command output:\nfake stdout/stderr dir",
+                               `3: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
+                               "5: commands: command output:\nfake stdout/stderr",
                        },
                        safcm.MsgSyncResp{
                                CommandChanges: []safcm.CommandChange{
@@ -431,8 +458,11 @@ func TestSyncCommands(t *testing.T) {
                                                },
                                        },
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               OrigGroup: "group",
+                                               Cmd:       "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        []string{
@@ -467,10 +497,10 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ."`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr .",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "false"`,
-                               "5: sync remote: commands: command output:\nfake stdout/stderr dir",
+                               `3: commands: running "/bin/sh" "-c" "echo trigger ." (".")`,
+                               "5: commands: command output:\nfake stdout/stderr .",
+                               `3: commands: running "/bin/sh" "-c" "false" ("dir")`,
+                               "5: commands: command output:\nfake stdout/stderr dir",
                        },
                        safcm.MsgSyncResp{
                                CommandChanges: []safcm.CommandChange{
@@ -492,31 +522,22 @@ func TestSyncCommands(t *testing.T) {
        }
 
        for _, tc := range tests {
-               s, res := prepareSync(tc.req, &testRunner{
-                       t:         t,
-                       name:      tc.name,
-                       expCmds:   tc.expCmds,
-                       resStdout: tc.stdout,
-                       resStderr: tc.stderr,
-                       resError:  tc.errors,
-               })
-               s.triggers = tc.triggers
+               t.Run(tc.name, func(t *testing.T) {
+                       s, res := prepareSync(tc.req, &testRunner{
+                               t:         t,
+                               expCmds:   tc.expCmds,
+                               resStdout: tc.stdout,
+                               resStderr: tc.stderr,
+                               resError:  tc.errors,
+                       })
+                       s.triggers = tc.triggers
 
-               err := s.syncCommands()
-               // 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)
-               }
-               dbg := res.Wait()
+                       err := s.syncCommands()
+                       testutil.AssertErrorEqual(t, "err", err, tc.expErr)
+                       dbg := res.Wait()
 
-               if !reflect.DeepEqual(tc.expResp, s.resp) {
-                       t.Errorf("%s: resp: %s", tc.name,
-                               cmp.Diff(tc.expResp, s.resp))
-               }
-               if !reflect.DeepEqual(tc.expDbg, dbg) {
-                       t.Errorf("%s: dbg: %s", tc.name,
-                               cmp.Diff(tc.expDbg, dbg))
-               }
+                       testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
+                       testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
+               })
        }
 }