Repository navigation
Expand file tree
/
Copy pathsystemd_test.go
More file actions
64 lines (53 loc) · 1.91 KB
/
Copy pathsystemd_test.go
File metadata and controls
64 lines (53 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fest
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
// SystemdBugFakeSystemctl writes a fake "systemctl" onto PATH. It keeps the set
// of enabled units in a state file so cache behaviour can be observed without
// root or a real systemd instance.
func SystemdBugFakeSystemctl(t *testing.T) string {
t.Helper()
dir := t.TempDir()
state := filepath.Join(dir, "state")
require.NoError(t, os.WriteFile(state, []byte("foo.service enabled\n"), 0644))
script := "#!/bin/sh\n" +
fmt.Sprintf("STATE=%q\n", state) +
"[ \"$1\" = \"--user\" ] && shift\n" +
"case \"$1\" in\n" +
" list-unit-files) [ -f \"$STATE\" ] && cat \"$STATE\" ;;\n" +
" enable)\n" +
" shift\n" +
" for u in \"$@\"; do\n" +
" case \"$u\" in --*) continue ;; esac\n" +
" printf '%s enabled\\n' \"$u\" >> \"$STATE\"\n" +
" done ;;\n" +
"esac\n" +
"exit 0\n"
require.NoError(t, os.WriteFile(filepath.Join(dir, "systemctl"), []byte(script), 0755))
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
return state
}
// TestSystemdBugInstallInvalidatesCache demonstrates that a systemd manager's
// memoized installed list is never invalidated by Install/Uninstall, so the
// "refresh after install" in syncPackages silently returns stale data.
func TestSystemdBugInstallInvalidatesCache(t *testing.T) {
SystemdBugFakeSystemctl(t)
mgr := &systemdManager{
resource: ResourceUserServices,
unitType: "service",
user: true,
wanted: &[]string{"foo", "bar"},
}
before, err := mgr.ListInstalled()
require.NoError(t, err)
require.ElementsMatch(t, []string{"foo"}, before)
require.NoError(t, mgr.Install([]string{"bar"}))
after, err := mgr.ListInstalled()
require.NoError(t, err)
require.ElementsMatch(t, []string{"foo", "bar"}, after,
"ListInstalled must reflect units enabled by Install; the installed list cache has to be invalidated by mutations")
}