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/>.
24 "ruderich.org/simon/safcm"
25 "ruderich.org/simon/safcm/testutil"
28 func TestSyncServicesSystemd(t *testing.T) {
37 expResp safcm.MsgSyncResp
41 // NOTE: Also update MsgSyncResp in safcm test cases when
42 // changing the MsgSyncResp struct!
45 "no service change necessary",
53 []byte(`ActiveState=active
64 []*exec.Cmd{&exec.Cmd{
65 Path: "/bin/systemctl",
69 "--property=ActiveState,UnitFileState,LoadError",
74 Stdout: &bytes.Buffer{},
75 Stderr: &bytes.Buffer{},
78 "4: sync remote: services: checking service-one service-two (systemd detected)",
79 `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
80 `5: sync remote: services: command stdout:
95 "no service change necessary (older systemd)",
103 []byte(`ActiveState=active
104 UnitFileState=enabled
108 UnitFileState=enabled
114 []*exec.Cmd{&exec.Cmd{
115 Path: "/bin/systemctl",
119 "--property=ActiveState,UnitFileState,LoadError",
124 Stdout: &bytes.Buffer{},
125 Stderr: &bytes.Buffer{},
128 "4: sync remote: services: checking service-one service-two (systemd detected)",
129 `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
130 `5: sync remote: services: command stdout:
132 UnitFileState=enabled
136 UnitFileState=enabled
148 "service-does-not-exist",
153 []byte(`ActiveState=inactive
155 LoadError=org.freedesktop.systemd1.NoSuchUnit "Unit service-does-not-exist.service not found."
158 UnitFileState=enabled
164 []*exec.Cmd{&exec.Cmd{
165 Path: "/bin/systemctl",
169 "--property=ActiveState,UnitFileState,LoadError",
171 "service-does-not-exist",
174 Stdout: &bytes.Buffer{},
175 Stderr: &bytes.Buffer{},
178 "4: sync remote: services: checking service-does-not-exist service-two (systemd detected)",
179 `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-does-not-exist" "service-two"`,
180 `5: sync remote: services: command stdout:
183 LoadError=org.freedesktop.systemd1.NoSuchUnit "Unit service-does-not-exist.service not found."
186 UnitFileState=enabled
191 fmt.Errorf("systemd unit \"service-does-not-exist\" not found"),
195 "start/enable service",
204 []byte(`ActiveState=inactive
205 UnitFileState=enabled
209 UnitFileState=disabled
213 UnitFileState=disabled
224 []byte("fake stderr"),
226 []error{nil, nil, nil, nil},
227 []*exec.Cmd{&exec.Cmd{
228 Path: "/bin/systemctl",
232 "--property=ActiveState,UnitFileState,LoadError",
238 Stdout: &bytes.Buffer{},
239 Stderr: &bytes.Buffer{},
241 Path: "/bin/systemctl",
246 Stdout: &bytes.Buffer{},
247 Stderr: &bytes.Buffer{},
249 Path: "/bin/systemctl",
257 Stdout: &bytes.Buffer{},
258 Stderr: &bytes.Buffer{},
260 Path: "/bin/systemctl",
268 Stdout: &bytes.Buffer{},
269 Stderr: &bytes.Buffer{},
272 "4: sync remote: services: checking service-one service-two service-three (systemd detected)",
273 `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
274 `5: sync remote: services: command stdout:
276 UnitFileState=enabled
280 UnitFileState=disabled
284 UnitFileState=disabled
287 `4: sync remote: services: running "/bin/systemctl" "daemon-reload"`,
288 "3: sync remote: services: starting service-one service-three",
289 `4: sync remote: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
290 "3: sync remote: services: enabling service-two service-three",
291 `4: sync remote: services: running "/bin/systemctl" "enable" "--" "service-two" "service-three"`,
292 "5: sync remote: services: command stderr:\nfake stderr",
295 ServiceChanges: []safcm.ServiceChange{
305 Name: "service-three",
315 "start/enable service (dry-run)",
325 []byte(`ActiveState=inactive
326 UnitFileState=enabled
330 UnitFileState=disabled
334 UnitFileState=disabled
340 []*exec.Cmd{&exec.Cmd{
341 Path: "/bin/systemctl",
345 "--property=ActiveState,UnitFileState,LoadError",
351 Stdout: &bytes.Buffer{},
352 Stderr: &bytes.Buffer{},
355 "4: sync remote: services: checking service-one service-two service-three (systemd detected)",
356 `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
357 `5: sync remote: services: command stdout:
359 UnitFileState=enabled
363 UnitFileState=disabled
367 UnitFileState=disabled
372 ServiceChanges: []safcm.ServiceChange{
382 Name: "service-three",
392 "start/enable service (error)",
401 []byte(`ActiveState=inactive
402 UnitFileState=enabled
406 UnitFileState=disabled
410 UnitFileState=disabled
419 []byte("fake stderr"),
424 fmt.Errorf("fake error"),
426 []*exec.Cmd{&exec.Cmd{
427 Path: "/bin/systemctl",
431 "--property=ActiveState,UnitFileState,LoadError",
437 Stdout: &bytes.Buffer{},
438 Stderr: &bytes.Buffer{},
440 Path: "/bin/systemctl",
445 Stdout: &bytes.Buffer{},
446 Stderr: &bytes.Buffer{},
448 Path: "/bin/systemctl",
456 Stdout: &bytes.Buffer{},
457 Stderr: &bytes.Buffer{},
460 "4: sync remote: services: checking service-one service-two service-three (systemd detected)",
461 `4: sync remote: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
462 `5: sync remote: services: command stdout:
464 UnitFileState=enabled
468 UnitFileState=disabled
472 UnitFileState=disabled
475 `4: sync remote: services: running "/bin/systemctl" "daemon-reload"`,
476 "3: sync remote: services: starting service-one service-three",
477 `4: sync remote: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
478 "5: sync remote: services: command stderr:\nfake stderr",
481 ServiceChanges: []safcm.ServiceChange{
491 Name: "service-three",
497 fmt.Errorf(`"/bin/systemctl" "start" "--" "service-one" "service-three" failed: fake error; stdout: "", stderr: "fake stderr"`),
501 for _, tc := range tests {
502 t.Run(tc.name, func(t *testing.T) {
503 s, res := prepareSync(tc.req, &testRunner{
506 resStdout: tc.stdout,
507 resStderr: tc.stderr,
511 err := s.syncServicesSystemd()
512 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
515 testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
516 testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)