1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024 Simon Ruderich
12 "ruderich.org/simon/safcm"
13 "ruderich.org/simon/safcm/testutil"
16 func TestSyncServicesSystemd(t *testing.T) {
25 expResp safcm.MsgSyncResp
29 // NOTE: Also update MsgSyncResp in safcm test cases when
30 // changing the MsgSyncResp struct!
33 "no service change necessary",
41 []byte(`ActiveState=active
53 Path: "/bin/systemctl",
57 "--property=ActiveState,UnitFileState,LoadError",
62 Stdout: &bytes.Buffer{},
63 Stderr: &bytes.Buffer{},
66 "4: services: checking service-one service-two (systemd detected)",
67 `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
68 `5: services: command stdout:
83 "no service change necessary (older systemd)",
91 []byte(`ActiveState=active
103 Path: "/bin/systemctl",
107 "--property=ActiveState,UnitFileState,LoadError",
112 Stdout: &bytes.Buffer{},
113 Stderr: &bytes.Buffer{},
116 "4: services: checking service-one service-two (systemd detected)",
117 `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two"`,
118 `5: services: command stdout:
120 UnitFileState=enabled
124 UnitFileState=enabled
136 "service-does-not-exist",
141 []byte(`ActiveState=inactive
143 LoadError=org.freedesktop.systemd1.NoSuchUnit "Unit service-does-not-exist.service not found."
146 UnitFileState=enabled
153 Path: "/bin/systemctl",
157 "--property=ActiveState,UnitFileState,LoadError",
159 "service-does-not-exist",
162 Stdout: &bytes.Buffer{},
163 Stderr: &bytes.Buffer{},
166 "4: services: checking service-does-not-exist service-two (systemd detected)",
167 `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-does-not-exist" "service-two"`,
168 `5: services: command stdout:
171 LoadError=org.freedesktop.systemd1.NoSuchUnit "Unit service-does-not-exist.service not found."
174 UnitFileState=enabled
179 fmt.Errorf("systemd unit \"service-does-not-exist\" not found"),
183 "start/enable service",
192 []byte(`ActiveState=inactive
193 UnitFileState=enabled
197 UnitFileState=disabled
201 UnitFileState=disabled
212 []byte("fake stderr"),
214 []error{nil, nil, nil, nil},
216 Path: "/bin/systemctl",
220 "--property=ActiveState,UnitFileState,LoadError",
226 Stdout: &bytes.Buffer{},
227 Stderr: &bytes.Buffer{},
229 Path: "/bin/systemctl",
234 Stdout: &bytes.Buffer{},
235 Stderr: &bytes.Buffer{},
237 Path: "/bin/systemctl",
245 Stdout: &bytes.Buffer{},
246 Stderr: &bytes.Buffer{},
248 Path: "/bin/systemctl",
256 Stdout: &bytes.Buffer{},
257 Stderr: &bytes.Buffer{},
260 "4: services: checking service-one service-two service-three (systemd detected)",
261 `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
262 `5: services: command stdout:
264 UnitFileState=enabled
268 UnitFileState=disabled
272 UnitFileState=disabled
275 `4: services: running "/bin/systemctl" "daemon-reload"`,
276 "3: services: starting service-one service-three",
277 `4: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
278 "3: services: enabling service-two service-three",
279 `4: services: running "/bin/systemctl" "enable" "--" "service-two" "service-three"`,
280 "5: services: command stderr:\nfake stderr",
283 ServiceChanges: []safcm.ServiceChange{
293 Name: "service-three",
303 "start/enable service (dry-run)",
313 []byte(`ActiveState=inactive
314 UnitFileState=enabled
318 UnitFileState=disabled
322 UnitFileState=disabled
329 Path: "/bin/systemctl",
333 "--property=ActiveState,UnitFileState,LoadError",
339 Stdout: &bytes.Buffer{},
340 Stderr: &bytes.Buffer{},
343 "4: services: checking service-one service-two service-three (systemd detected)",
344 `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
345 `5: services: command stdout:
347 UnitFileState=enabled
351 UnitFileState=disabled
355 UnitFileState=disabled
360 ServiceChanges: []safcm.ServiceChange{
370 Name: "service-three",
380 "start/enable service (error)",
389 []byte(`ActiveState=inactive
390 UnitFileState=enabled
394 UnitFileState=disabled
398 UnitFileState=disabled
407 []byte("fake stderr"),
412 fmt.Errorf("fake error"),
415 Path: "/bin/systemctl",
419 "--property=ActiveState,UnitFileState,LoadError",
425 Stdout: &bytes.Buffer{},
426 Stderr: &bytes.Buffer{},
428 Path: "/bin/systemctl",
433 Stdout: &bytes.Buffer{},
434 Stderr: &bytes.Buffer{},
436 Path: "/bin/systemctl",
444 Stdout: &bytes.Buffer{},
445 Stderr: &bytes.Buffer{},
448 "4: services: checking service-one service-two service-three (systemd detected)",
449 `4: services: running "/bin/systemctl" "show" "--property=ActiveState,UnitFileState,LoadError" "--" "service-one" "service-two" "service-three"`,
450 `5: services: command stdout:
452 UnitFileState=enabled
456 UnitFileState=disabled
460 UnitFileState=disabled
463 `4: services: running "/bin/systemctl" "daemon-reload"`,
464 "3: services: starting service-one service-three",
465 `4: services: running "/bin/systemctl" "start" "--" "service-one" "service-three"`,
466 "5: services: command stderr:\nfake stderr",
469 ServiceChanges: []safcm.ServiceChange{
479 Name: "service-three",
485 fmt.Errorf(`"/bin/systemctl" "start" "--" "service-one" "service-three" failed: fake error; stdout: "", stderr: "fake stderr"`),
489 for _, tc := range tests {
490 t.Run(tc.name, func(t *testing.T) {
491 s, res := prepareSync(tc.req, &testRunner{
494 resStdout: tc.stdout,
495 resStderr: tc.stderr,
499 err := s.syncServicesSystemd()
500 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
503 testutil.AssertEqual(t, "resp", s.resp, tc.expResp)
504 testutil.AssertEqual(t, "dbg", dbg, tc.expDbg)