1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024 Simon Ruderich
13 "ruderich.org/simon/safcm"
14 "ruderich.org/simon/safcm/testutil"
17 func TestSyncCommands(t *testing.T) {
18 exe, err := os.Executable()
22 env := append(os.Environ(),
24 "SAFCM_GROUPS=all group1 group2 host.example.org",
25 "SAFCM_GROUP_all=all",
26 "SAFCM_GROUP_group1=group1",
27 "SAFCM_GROUP_group2=group2",
28 "SAFCM_GROUP_host.example.org=host.example.org",
40 expResp safcm.MsgSyncResp
44 // NOTE: Also update MsgSyncResp in safcm test cases when
45 // changing the MsgSyncResp struct!
56 Commands: []*safcm.Command{
59 Cmd: "echo; env | grep SAFCM_",
64 [][]byte{[]byte("fake stdout/stderr")},
71 "echo; env | grep SAFCM_",
76 `3: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
77 "5: commands: command output:\nfake stdout/stderr",
80 CommandChanges: []safcm.CommandChange{
82 Command: "echo; env | grep SAFCM_",
83 Output: "fake stdout/stderr",
90 "successful command (dry-run)",
99 Commands: []*safcm.Command{
102 Cmd: "echo; env | grep SAFCM_",
113 CommandChanges: []safcm.CommandChange{
115 Command: "echo; env | grep SAFCM_",
131 Commands: []*safcm.Command{
134 Cmd: "echo hi; false",
139 [][]byte{[]byte("fake stdout/stderr")},
141 []error{fmt.Errorf("fake error")},
151 `3: commands: running "/bin/sh" "-c" "echo hi; false" (group)`,
152 "5: commands: command output:\nfake stdout/stderr",
155 CommandChanges: []safcm.CommandChange{
157 Command: "echo hi; false",
158 Output: "fake stdout/stderr",
163 fmt.Errorf("\"echo hi; false\" failed: fake error"),
166 "failed command (dry-run)",
175 Commands: []*safcm.Command{
178 Cmd: "echo hi; false",
189 CommandChanges: []safcm.CommandChange{
191 Command: "echo hi; false",
199 "multiple commands, abort on first failed",
207 Commands: []*safcm.Command{
225 []byte("fake stdout/stderr first"),
226 []byte("fake stdout/stderr second"),
237 fmt.Errorf("fake error"),
262 `3: commands: running "/bin/sh" "-c" "echo first" (group1)`,
263 "5: commands: command output:\nfake stdout/stderr first",
264 `3: commands: running "/bin/sh" "-c" "echo second" (group2)`,
265 "5: commands: command output:\nfake stdout/stderr second",
266 `3: commands: running "/bin/sh" "-c" "false" (group3)`,
269 CommandChanges: []safcm.CommandChange{
271 Command: "echo first",
272 Output: "fake stdout/stderr first",
275 Command: "echo second",
276 Output: "fake stdout/stderr second",
285 fmt.Errorf("\"false\" failed: fake error"),
297 Files: map[string]*safcm.File{
301 Mode: fs.ModeDir | 0700,
304 TriggerCommands: []string{
311 Mode: fs.ModeDir | 0755,
314 TriggerCommands: []string{
324 Data: []byte("content\n"),
325 TriggerCommands: []string{
326 "echo trigger dir/file",
330 Commands: []*safcm.Command{
333 Cmd: "echo; env | grep SAFCM_",
342 []byte("fake stdout/stderr ."),
343 []byte("fake stdout/stderr dir"),
344 []byte("fake stdout/stderr"),
374 "echo; env | grep SAFCM_",
379 `3: commands: running "/bin/sh" "-c" "echo trigger ." (".")`,
380 "5: commands: command output:\nfake stdout/stderr .",
381 `3: commands: running "/bin/sh" "-c" "echo trigger dir" ("dir")`,
382 "5: commands: command output:\nfake stdout/stderr dir",
383 `3: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
384 "5: commands: command output:\nfake stdout/stderr",
387 CommandChanges: []safcm.CommandChange{
389 Command: "echo trigger .",
391 Output: "fake stdout/stderr .",
394 Command: "echo trigger dir",
396 Output: "fake stdout/stderr dir",
399 Command: "echo; env | grep SAFCM_",
400 Output: "fake stdout/stderr",
416 Files: map[string]*safcm.File{
420 Mode: fs.ModeDir | 0700,
423 TriggerCommands: []string{
430 Mode: fs.ModeDir | 0755,
433 TriggerCommands: []string{
443 Data: []byte("content\n"),
444 TriggerCommands: []string{
445 "echo trigger dir/file",
449 Commands: []*safcm.Command{
452 Cmd: "echo; env | grep SAFCM_",
461 []byte("fake stdout/stderr ."),
462 []byte("fake stdout/stderr dir"),
470 fmt.Errorf("fake error"),
488 `3: commands: running "/bin/sh" "-c" "echo trigger ." (".")`,
489 "5: commands: command output:\nfake stdout/stderr .",
490 `3: commands: running "/bin/sh" "-c" "false" ("dir")`,
491 "5: commands: command output:\nfake stdout/stderr dir",
494 CommandChanges: []safcm.CommandChange{
496 Command: "echo trigger .",
498 Output: "fake stdout/stderr .",
503 Output: "fake stdout/stderr dir",
508 fmt.Errorf("\"false\" failed: fake error"),
512 for _, tc := range tests {
513 t.Run(tc.name, func(t *testing.T) {
514 s, res := prepareSync(tc.req, &testRunner{
517 resStdout: tc.stdout,
518 resStderr: tc.stderr,
521 s.triggers = tc.triggers
523 err := s.syncCommands()
524 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
527 testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
528 testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)