]> 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 3c47c1757c6adf5f199dc4342a692127da263e03..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{
@@ -508,21 +523,11 @@ func TestSyncCommands(t *testing.T) {
                        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)
-                       }
+                       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)
                })
        }
 }