"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) {
"group2",
"host.example.org",
},
- Commands: []string{
- "echo; env | grep SAFCM_",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo; env | grep SAFCM_",
+ },
},
},
nil,
Env: env,
}},
[]string{
- `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
"5: sync remote: commands: command output:\nfake stdout/stderr",
},
safcm.MsgSyncResp{
"group2",
"host.example.org",
},
- Commands: []string{
- "echo; env | grep SAFCM_",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo; env | grep SAFCM_",
+ },
},
},
nil,
"group2",
"host.example.org",
},
- Commands: []string{
- "echo hi; false",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo hi; false",
+ },
},
},
nil,
Env: env,
}},
[]string{
- `3: sync remote: commands: running "/bin/sh" "-c" "echo hi; false"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "echo hi; false" (group)`,
"5: sync remote: commands: command output:\nfake stdout/stderr",
},
safcm.MsgSyncResp{
"group2",
"host.example.org",
},
- Commands: []string{
- "echo hi; false",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo hi; false",
+ },
},
},
nil,
"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,
Env: env,
}},
[]string{
- `3: sync remote: commands: running "/bin/sh" "-c" "echo first"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "echo first" (group1)`,
"5: sync remote: commands: command output:\nfake stdout/stderr first",
- `3: sync remote: commands: running "/bin/sh" "-c" "echo second"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "echo second" (group2)`,
"5: sync remote: commands: command output:\nfake stdout/stderr second",
- `3: sync remote: commands: running "/bin/sh" "-c" "false"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "false" (group3)`,
},
safcm.MsgSyncResp{
CommandChanges: []safcm.CommandChange{
},
},
},
- Commands: []string{
- "echo; env | grep SAFCM_",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo; env | grep SAFCM_",
+ },
},
},
[]string{
Env: env,
}},
[]string{
- `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ."`,
+ `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"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger dir" ("dir")`,
"5: sync remote: commands: command output:\nfake stdout/stderr dir",
- `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
"5: sync remote: commands: command output:\nfake stdout/stderr",
},
safcm.MsgSyncResp{
},
},
},
- Commands: []string{
- "echo; env | grep SAFCM_",
+ Commands: []*safcm.Command{
+ {
+ OrigGroup: "group",
+ Cmd: "echo; env | grep SAFCM_",
+ },
},
},
[]string{
Env: env,
}},
[]string{
- `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ."`,
+ `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"`,
+ `3: sync remote: commands: running "/bin/sh" "-c" "false" ("dir")`,
"5: sync remote: commands: command output:\nfake stdout/stderr dir",
},
safcm.MsgSyncResp{
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)
})
}
}