1 // Copyright (C) 2021 Simon Ruderich
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
29 ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
30 "ruderich.org/simon/safcm/testutil"
33 func TestSyncSshEndToEnd(t *testing.T) {
34 cwd, err := os.Getwd()
41 // Needs different options in sshd_config
42 if runtime.GOOS == "openbsd" {
46 sshDir := cwd + "/testdata/ssh"
47 sshCmd := exec.Command("/usr/sbin/sshd",
48 "-D", // stay in foreground
49 "-e", // write messages to stderr instead of syslog
50 "-f", sshDir+"/sshd/sshd_config"+suffix,
51 "-h", sshDir+"/sshd/ssh_host_key",
52 "-o", "AuthorizedKeysFile="+sshDir+"/ssh/authorized_keys",
54 sshCmd.Stderr = os.Stderr
59 defer sshCmd.Process.Kill()
61 // Wait until SSH server is ready (up to 30 seconds)
62 for i := 0; i < 30; i++ {
63 conn, err := net.Dial("tcp", "127.0.0.1:29327")
68 time.Sleep(time.Second)
71 err = os.Chdir(sshDir + "/project")
76 ft.CreateDirectoryExists("no-changes.example.org", 0755)
77 ft.CreateDirectoryExists("no-changes.example.org/files", 0755)
78 ft.CreateDirectoryExists("no-changes.example.org/files/etc", 0755)
79 ft.CreateDirectoryExists("no-changes.example.org/files/tmp", 0755)
81 noChangePermissions := `
86 if runtime.GOOS == "openbsd" || runtime.GOOS == "freebsd" {
87 noChangePermissions = `
93 ft.CreateFile("no-changes.example.org/permissions.yaml",
94 noChangePermissions, 0644)
96 skipUnlessCiRun := len(os.Getenv("SAFCM_CI_RUN")) == 0
111 []string{"no-settings.example.org"},
112 `<LOG>[info] [no-settings.example.org] remote helper upload in progress
113 <LOG>[info] [no-settings.example.org] no changes
118 "no settings (no helper upload)",
121 []string{"no-settings.example.org"},
122 `<LOG>[info] [no-settings.example.org] no changes
127 "no settings (error)",
130 []string{"-log", "error", "no-settings.example.org"},
135 "no settings (verbose)",
138 []string{"-log", "verbose", "no-settings.example.org"},
139 `<LOG>[info] [no-settings.example.org] remote helper upload in progress
140 <LOG>[verbose] [no-settings.example.org] host groups: all <DET> <DET> no-settings.example.org
141 <LOG>[verbose] [no-settings.example.org] host group priorities (descending): no-settings.example.org
142 <LOG>[info] [no-settings.example.org] no changes
147 "no settings (debug2)",
150 []string{"-log", "debug2", "no-settings.example.org"},
151 `<LOG>[info] [no-settings.example.org] remote helper upload in progress
152 <LOG>[verbose] [no-settings.example.org] host groups: all <DET> <DET> no-settings.example.org
153 <LOG>[verbose] [no-settings.example.org] host group priorities (descending): no-settings.example.org
154 <LOG>[info] [no-settings.example.org] no changes
159 // NOTE: We use -n on regular runs to prevent changing
160 // anything important on the host when running as root!
163 "no changes (dry-run)",
166 []string{"-n", "no-changes.example.org"},
167 `<LOG>[info] [no-changes.example.org] remote helper upload in progress
168 <LOG>[info] [no-changes.example.org] no changes
173 "no changes (dry-run, debug2)",
176 []string{"-n", "-log", "debug2", "no-changes.example.org"},
177 `<LOG>[info] [no-changes.example.org] remote helper upload in progress
178 <LOG>[verbose] [no-changes.example.org] host groups: all <DET> <DET> no-changes.example.org
179 <LOG>[verbose] [no-changes.example.org] host group priorities (descending): no-changes.example.org
180 <LOG>[debug] [no-changes.example.org] sync remote: files: "/" (no-changes.example.org): unchanged
181 <LOG>[debug] [no-changes.example.org] sync remote: files: "/etc" (no-changes.example.org): unchanged
182 <LOG>[debug] [no-changes.example.org] sync remote: files: "/tmp" (no-changes.example.org): unchanged
183 <LOG>[info] [no-changes.example.org] no changes
191 []string{"no-changes.example.org"},
192 `<LOG>[info] [no-changes.example.org] remote helper upload in progress
193 <LOG>[info] [no-changes.example.org] no changes
198 "no changes (debug2)",
201 []string{"-log", "debug2", "no-changes.example.org"},
202 `<LOG>[info] [no-changes.example.org] remote helper upload in progress
203 <LOG>[verbose] [no-changes.example.org] host groups: all <DET> <DET> no-changes.example.org
204 <LOG>[verbose] [no-changes.example.org] host group priorities (descending): no-changes.example.org
205 <LOG>[debug] [no-changes.example.org] sync remote: files: "/" (no-changes.example.org): unchanged
206 <LOG>[debug] [no-changes.example.org] sync remote: files: "/etc" (no-changes.example.org): unchanged
207 <LOG>[debug] [no-changes.example.org] sync remote: files: "/tmp" (no-changes.example.org): unchanged
208 <LOG>[info] [no-changes.example.org] no changes
214 "no effect commands (dry-run)",
217 []string{"-n", "no-effect-commands.example.org"},
218 `<LOG>[info] [no-effect-commands.example.org] remote helper upload in progress
219 <LOG>[info] [no-effect-commands.example.org]
220 executed 2 command(s): (dry-run)
221 "echo this is a command"
227 "no effect commands (dry-run)",
230 []string{"-n", "-log", "debug2", "no-effect-commands.example.org"},
231 `<LOG>[info] [no-effect-commands.example.org] remote helper upload in progress
232 <LOG>[verbose] [no-effect-commands.example.org] host groups: all <DET> <DET> no-effect-commands.example.org
233 <LOG>[verbose] [no-effect-commands.example.org] host group priorities (descending): no-effect-commands.example.org
234 <LOG>[info] [no-effect-commands.example.org]
235 executed 2 command(s): (dry-run)
236 "echo this is a command"
242 "no effect commands",
245 []string{"no-effect-commands.example.org"},
246 `<LOG>[info] [no-effect-commands.example.org] remote helper upload in progress
247 <LOG>[info] [no-effect-commands.example.org]
248 executed 2 command(s):
249 "echo this is a command":
256 "no effect commands (debug2)",
259 []string{"-log", "debug2", "no-effect-commands.example.org"},
260 `<LOG>[info] [no-effect-commands.example.org] remote helper upload in progress
261 <LOG>[verbose] [no-effect-commands.example.org] host groups: all <DET> <DET> no-effect-commands.example.org
262 <LOG>[verbose] [no-effect-commands.example.org] host group priorities (descending): no-effect-commands.example.org
263 <LOG>[verbose] [no-effect-commands.example.org] sync remote: commands: running "/bin/sh" "-c" "echo this is a command" (no-effect-commands.example.org)
264 <LOG>[debug2] [no-effect-commands.example.org] sync remote: commands: command output:
266 <LOG>[verbose] [no-effect-commands.example.org] sync remote: commands: running "/bin/sh" "-c" "true" (no-effect-commands.example.org)
267 <LOG>[info] [no-effect-commands.example.org]
268 executed 2 command(s):
269 "echo this is a command":
277 "no effect commands failing (dry-run)",
280 []string{"-n", "no-effect-commands-failing.example.org"},
281 `<LOG>[info] [no-effect-commands-failing.example.org] remote helper upload in progress
282 <LOG>[info] [no-effect-commands-failing.example.org]
283 executed 2 command(s): (dry-run)
284 "echo this is a command"
285 "echo failing; false"
290 "no effect commands failing (dry-run)",
293 []string{"-n", "-log", "debug2", "no-effect-commands-failing.example.org"},
294 `<LOG>[info] [no-effect-commands-failing.example.org] remote helper upload in progress
295 <LOG>[verbose] [no-effect-commands-failing.example.org] host groups: all <DET> <DET> no-effect-commands-failing.example.org
296 <LOG>[verbose] [no-effect-commands-failing.example.org] host group priorities (descending): no-effect-commands-failing.example.org
297 <LOG>[info] [no-effect-commands-failing.example.org]
298 executed 2 command(s): (dry-run)
299 "echo this is a command"
300 "echo failing; false"
305 "no effect commands failing",
308 []string{"no-effect-commands-failing.example.org"},
309 `<LOG>[info] [no-effect-commands-failing.example.org] remote helper upload in progress
310 <LOG>[info] [no-effect-commands-failing.example.org]
311 executed 2 command(s):
312 "echo this is a command":
314 "echo failing; false", failed: "exit status 1":
316 <LOG>[error] [no-effect-commands-failing.example.org] sync remote: commands: "echo failing; false" failed: exit status 1
318 fmt.Errorf("exit status 1"),
321 "no effect commands failing (debug2)",
324 []string{"-log", "debug2", "no-effect-commands-failing.example.org"},
325 `<LOG>[info] [no-effect-commands-failing.example.org] remote helper upload in progress
326 <LOG>[verbose] [no-effect-commands-failing.example.org] host groups: all <DET> <DET> no-effect-commands-failing.example.org
327 <LOG>[verbose] [no-effect-commands-failing.example.org] host group priorities (descending): no-effect-commands-failing.example.org
328 <LOG>[verbose] [no-effect-commands-failing.example.org] sync remote: commands: running "/bin/sh" "-c" "echo this is a command" (no-effect-commands-failing.example.org)
329 <LOG>[debug2] [no-effect-commands-failing.example.org] sync remote: commands: command output:
331 <LOG>[verbose] [no-effect-commands-failing.example.org] sync remote: commands: running "/bin/sh" "-c" "echo failing; false" (no-effect-commands-failing.example.org)
332 <LOG>[debug2] [no-effect-commands-failing.example.org] sync remote: commands: command output:
334 <LOG>[info] [no-effect-commands-failing.example.org]
335 executed 2 command(s):
336 "echo this is a command":
338 "echo failing; false", failed: "exit status 1":
340 <LOG>[error] [no-effect-commands-failing.example.org] sync remote: commands: "echo failing; false" failed: exit status 1
342 fmt.Errorf("exit status 1"),
346 remotePath := fmt.Sprintf("/tmp/safcm-remote-%d", os.Getuid())
348 logRegexp := regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} `)
349 detectedRegexp := regexp.MustCompile(`detected_\S+`)
351 for _, tc := range tests {
352 t.Run(tc.name, func(t *testing.T) {
354 os.Remove(remotePath)
357 args := append([]string{"sync",
358 "-sshconfig", sshDir + "/ssh/ssh_config",
360 cmd := exec.Command("../../../../../safcm", args...)
361 out, err := cmd.CombinedOutput()
364 for _, x := range strings.Split(string(out), "\n") {
365 // Strip parts which change on each run (LOG)
366 // or depending on the system (DET)
367 x = logRegexp.ReplaceAllString(x, "<LOG>")
368 x = detectedRegexp.ReplaceAllString(x, "<DET>")
371 res := strings.Join(tmp, "\n")
373 testutil.AssertEqual(t, "res", res, tc.exp)
374 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
378 os.Remove(remotePath)