]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm-remote/sync/commands_test.go
safcm: use Command struct instead of string to run commands
[safcm/safcm.git] / cmd / safcm-remote / sync / commands_test.go
index eb0cc0ba050df313cdd29d59155792fdbb079f13..ba7f4a2e89ec47e16fec11f88f41aad4577a2617 100644 (file)
@@ -20,12 +20,10 @@ 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) {
@@ -67,8 +65,10 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               Cmd: "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        nil,
@@ -107,8 +107,10 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               Cmd: "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        nil,
@@ -136,8 +138,10 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo hi; false",
+                               Commands: []*safcm.Command{
+                                       {
+                                               Cmd: "echo hi; false",
+                                       },
                                },
                        },
                        nil,
@@ -177,8 +181,10 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo hi; false",
+                               Commands: []*safcm.Command{
+                                       {
+                                               Cmd: "echo hi; false",
+                                       },
                                },
                        },
                        nil,
@@ -206,11 +212,16 @@ func TestSyncCommands(t *testing.T) {
                                        "group2",
                                        "host.example.org",
                                },
-                               Commands: []string{
-                                       "echo first",
-                                       "echo second",
-                                       "false",
-                                       "echo third",
+                               Commands: []*safcm.Command{
+                                       {
+                                               Cmd: "echo first",
+                                       }, {
+                                               Cmd: "echo second",
+                                       }, {
+                                               Cmd: "false",
+                                       }, {
+                                               Cmd: "echo third",
+                                       },
                                },
                        },
                        nil,
@@ -320,8 +331,10 @@ func TestSyncCommands(t *testing.T) {
                                                },
                                        },
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               Cmd: "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        []string{
@@ -436,8 +449,10 @@ func TestSyncCommands(t *testing.T) {
                                                },
                                        },
                                },
-                               Commands: []string{
-                                       "echo; env | grep SAFCM_",
+                               Commands: []*safcm.Command{
+                                       {
+                                               Cmd: "echo; env | grep SAFCM_",
+                                       },
                                },
                        },
                        []string{
@@ -498,31 +513,21 @@ func TestSyncCommands(t *testing.T) {
 
        for _, tc := range tests {
                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
+                       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("err = %#v, want %#v",
-                               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("resp: %s",
-                               cmp.Diff(tc.expResp, s.resp))
-               }
-               if !reflect.DeepEqual(tc.expDbg, dbg) {
-                       t.Errorf("dbg: %s",
-                               cmp.Diff(tc.expDbg, dbg))
-               }
+                       testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
+                       testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
                })
        }
 }