]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm-remote/sync/services_systemd_test.go
sync: shorten log messages by removing "info/sync remote:" prefix
[safcm/safcm.git] / cmd / safcm-remote / sync / services_systemd_test.go
index df226fc85e00fae40dc06834e25deae3a1bce5e5..5aa36cd7bc38019eac8d947d374876cf05bf4f7b 100644 (file)
@@ -19,12 +19,10 @@ import (
        "bytes"
        "fmt"
        "os/exec"
-       "reflect"
        "testing"
 
-       "github.com/google/go-cmp/cmp"
-
        "ruderich.org/simon/safcm"
+       "ruderich.org/simon/safcm/testutil"
 )
 
 func TestSyncServicesSystemd(t *testing.T) {
@@ -41,7 +39,7 @@ func TestSyncServicesSystemd(t *testing.T) {
        }{
 
                // NOTE: Also update MsgSyncResp in safcm test cases when
-               // changing anything here!
+               // changing the MsgSyncResp struct!
 
                {
                        "no service change necessary",
@@ -77,10 +75,9 @@ LoadError=
                                Stderr: &bytes.Buffer{},
                        }},
                        []string{
-                               "4: sync remote: services: detected systemd",
-                               "4: sync remote: services: checking service-one service-two",
-                               `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
-                               `5: sync remote: services: command stdout:
+                               "4: services: checking service-one service-two (systemd detected)",
+                               `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
+                               `5: services: command stdout:
 ActiveState=active
 UnitFileState=enabled
 LoadError=
@@ -128,10 +125,9 @@ LoadError= ""
                                Stderr: &bytes.Buffer{},
                        }},
                        []string{
-                               "4: sync remote: services: detected systemd",
-                               "4: sync remote: services: checking service-one service-two",
-                               `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
-                               `5: sync remote: services: command stdout:
+                               "4: services: checking service-one service-two (systemd detected)",
+                               `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
+                               `5: services: command stdout:
 ActiveState=active
 UnitFileState=enabled
 LoadError= ""
@@ -179,10 +175,9 @@ LoadError=
                                Stderr: &bytes.Buffer{},
                        }},
                        []string{
-                               "4: sync remote: services: detected systemd",
-                               "4: sync remote: services: checking service-does-not-exist service-two",
-                               `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-does-not-exist" "service-two"`,
-                               `5: sync remote: services: command stdout:
+                               "4: services: checking service-does-not-exist service-two (systemd detected)",
+                               `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-does-not-exist" "service-two"`,
+                               `5: services: command stdout:
 ActiveState=inactive
 UnitFileState=
 LoadError=org.freedesktop.systemd1.NoSuchUnit "Unit service-does-not-exist.service not found."
@@ -226,7 +221,7 @@ LoadError=
                                nil,
                                nil,
                                nil,
-                               []byte(`fake stderr`),
+                               []byte("fake stderr"),
                        },
                        []error{nil, nil, nil, nil},
                        []*exec.Cmd{&exec.Cmd{
@@ -274,10 +269,9 @@ LoadError=
                                Stderr: &bytes.Buffer{},
                        }},
                        []string{
-                               "4: sync remote: services: detected systemd",
-                               "4: sync remote: services: checking service-one service-two service-three",
-                               `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
-                               `5: sync remote: services: command stdout:
+                               "4: services: checking service-one service-two service-three (systemd detected)",
+                               `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
+                               `5: services: command stdout:
 ActiveState=inactive
 UnitFileState=enabled
 LoadError=
@@ -290,12 +284,12 @@ ActiveState=failed
 UnitFileState=disabled
 LoadError=
 `,
-                               `4: sync remote: services: running "/bin/systemctl" "daemon-reload"`,
-                               "3: sync remote: services: starting service-one service-three",
-                               `4: sync remote: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
-                               "3: sync remote: services: enabling service-two service-three",
-                               `4: sync remote: services: running "/bin/systemctl" "enable" "--" "service-two" "service-three"`,
-                               "5: sync remote: services: command stderr:\nfake stderr",
+                               `4: services: running "/bin/systemctl" "daemon-reload"`,
+                               "3: services: starting service-one service-three",
+                               `4: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
+                               "3: services: enabling service-two service-three",
+                               `4: services: running "/bin/systemctl" "enable" "--" "service-two" "service-three"`,
+                               "5: services: command stderr:\nfake stderr",
                        },
                        safcm.MsgSyncResp{
                                ServiceChanges: []safcm.ServiceChange{
@@ -358,10 +352,9 @@ LoadError=
                                Stderr: &bytes.Buffer{},
                        }},
                        []string{
-                               "4: sync remote: services: detected systemd",
-                               "4: sync remote: services: checking service-one service-two service-three",
-                               `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
-                               `5: sync remote: services: command stdout:
+                               "4: services: checking service-one service-two service-three (systemd detected)",
+                               `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
+                               `5: services: command stdout:
 ActiveState=inactive
 UnitFileState=enabled
 LoadError=
@@ -423,7 +416,7 @@ LoadError=
                        [][]byte{
                                nil,
                                nil,
-                               []byte(`fake stderr`),
+                               []byte("fake stderr"),
                        },
                        []error{
                                nil,
@@ -464,10 +457,9 @@ LoadError=
                                Stderr: &bytes.Buffer{},
                        }},
                        []string{
-                               "4: sync remote: services: detected systemd",
-                               "4: sync remote: services: checking service-one service-two service-three",
-                               `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
-                               `5: sync remote: services: command stdout:
+                               "4: services: checking service-one service-two service-three (systemd detected)",
+                               `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
+                               `5: services: command stdout:
 ActiveState=inactive
 UnitFileState=enabled
 LoadError=
@@ -480,10 +472,10 @@ ActiveState=failed
 UnitFileState=disabled
 LoadError=
 `,
-                               `4: sync remote: services: running "/bin/systemctl" "daemon-reload"`,
-                               "3: sync remote: services: starting service-one service-three",
-                               `4: sync remote: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
-                               "5: sync remote: services: command stderr:\nfake stderr",
+                               `4: services: running "/bin/systemctl" "daemon-reload"`,
+                               "3: services: starting service-one service-three",
+                               `4: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
+                               "5: services: command stderr:\nfake stderr",
                        },
                        safcm.MsgSyncResp{
                                ServiceChanges: []safcm.ServiceChange{
@@ -508,30 +500,20 @@ LoadError=
 
        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,
-               })
-
-               err := s.syncServicesSystemd()
-               // 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()
-
-               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))
-               }
+                       s, res := prepareSync(tc.req, &testRunner{
+                               t:         t,
+                               expCmds:   tc.expCmds,
+                               resStdout: tc.stdout,
+                               resStderr: tc.stderr,
+                               resError:  tc.errors,
+                       })
+
+                       err := s.syncServicesSystemd()
+                       testutil.AssertErrorEqual(t, "err", err, tc.expErr)
+                       dbg := res.Wait()
+
+                       testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
+                       testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)
                })
        }
 }