carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "bytes"
5 "os"
6 "path/filepath"
7 "testing"
8)
9
10// plan9aOddHostEnv enables the claim but carries an unrelated line that
11// readClaimUnitSettings cannot parse.
12var plan9aOddHostEnv = []string{
13 "CARLOS_HOST_CLAIM=1\nCARLOS_OTHER=\"unterminated\n",
14 "CARLOS_HOST_CLAIM=1\nCARLOS_OTHER=a\\\n",
15 "CARLOS_HOST_CLAIM='1\n",
16}
17
18func plan9aEnabledControl(t *testing.T) []byte {
19 t.Helper()
20 legacy, err := unitsFS.ReadFile("units/" + claimControlUnit)
21 if err != nil {
22 t.Fatal(err)
23 }
24 return claimUnitContent(claimControlUnit, legacy, claimUnitSettings{enabled: true, data: "/data", opt: "/opt/carlos"})
25}
26
27func plan9aWriteFile(t *testing.T, path string, body []byte) {
28 t.Helper()
29 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
30 t.Fatal(err)
31 }
32 if err := os.WriteFile(path, body, 0644); err != nil {
33 t.Fatal(err)
34 }
35}
36
37// 6b-N2: system apply on an enabled box leaves the installed control unit
38// alone, unrestarted, when host.env cannot be parsed.
39func TestPlan9AApplyKeepsEnabledControlUnitOnHostEnvError(t *testing.T) {
40 for _, env := range plan9aOddHostEnv {
41 t.Run(env, func(t *testing.T) {
42 f := newApplyFixture(t, "stable", "v1.0.0")
43 plan9aWriteFile(t, filepath.Join(f.a.etcDir, "carlos", "host.env"), []byte(env))
44 if _, err := readClaimUnitSettings(filepath.Join(f.a.etcDir, "carlos", "host.env")); err == nil {
45 t.Fatal("precondition: host.env parsed")
46 }
47 installed := plan9aEnabledControl(t)
48 live := filepath.Join(f.a.etcDir, "systemd", "system", claimControlUnit)
49 plan9aWriteFile(t, live, installed)
50 stage := t.TempDir()
51 if err := bootstrap(stage, nil); err != nil {
52 t.Fatal(err)
53 }
54 if err := f.a.installUnits(t.Context(), stage, nil); err != nil {
55 t.Fatal(err)
56 }
57 if got, _ := os.ReadFile(live); !bytes.Equal(got, installed) {
58 t.Fatalf("enabled control unit flipped:\n%s", got)
59 }
60 if f.called("systemctl try-restart " + claimControlUnit) {
61 t.Fatal("control unit restarted")
62 }
63 })
64 }
65}
66
67// 6b-N2: the live bootstrap likewise keeps an enabled control unit.
68func TestPlan9ABootstrapKeepsEnabledControlUnitOnHostEnvError(t *testing.T) {
69 for _, env := range plan9aOddHostEnv {
70 t.Run(env, func(t *testing.T) {
71 root := t.TempDir()
72 plan9aWriteFile(t, filepath.Join(root, "etc", "carlos", "host.env"), []byte(env))
73 installed := plan9aEnabledControl(t)
74 live := filepath.Join(root, "etc", "systemd", "system", claimControlUnit)
75 plan9aWriteFile(t, live, installed)
76 if err := bootstrap(root, nil); err != nil {
77 t.Fatal(err)
78 }
79 if got, _ := os.ReadFile(live); !bytes.Equal(got, installed) {
80 t.Fatalf("enabled control unit flipped:\n%s", got)
81 }
82 })
83 }
84}
85
86// Flag-off stays main's: an installed unit without the claim environment
87// is rendered from the template exactly as before, whatever host.env holds.
88func TestPlan9AFlagOffControlUnitUnchangedOnHostEnvError(t *testing.T) {
89 t.Parallel()
90 legacy, err := unitsFS.ReadFile("units/" + claimControlUnit)
91 if err != nil {
92 t.Fatal(err)
93 }
94 for name, installed := range map[string][]byte{"absent": nil, "template": legacy, "older": []byte("[Unit]\nDescription=older template\n")} {
95 t.Run(name, func(t *testing.T) {
96 f := newApplyFixture(t, "stable", "v1.0.0")
97 plan9aWriteFile(t, filepath.Join(f.a.etcDir, "carlos", "host.env"), []byte(plan9aOddHostEnv[0]))
98 live := filepath.Join(f.a.etcDir, "systemd", "system", claimControlUnit)
99 if installed != nil {
100 plan9aWriteFile(t, live, installed)
101 }
102 stage := t.TempDir()
103 if err := bootstrap(stage, nil); err != nil {
104 t.Fatal(err)
105 }
106 if err := f.a.installUnits(t.Context(), stage, nil); err != nil {
107 t.Fatal(err)
108 }
109 if got, _ := os.ReadFile(live); !bytes.Equal(got, legacy) {
110 t.Fatal("flag-off apply did not render main's unit")
111 }
112 root := t.TempDir()
113 plan9aWriteFile(t, filepath.Join(root, "etc", "carlos", "host.env"), []byte(plan9aOddHostEnv[0]))
114 rootLive := filepath.Join(root, "etc", "systemd", "system", claimControlUnit)
115 if installed != nil {
116 plan9aWriteFile(t, rootLive, installed)
117 }
118 if err := bootstrap(root, nil); err != nil {
119 t.Fatal(err)
120 }
121 if got, _ := os.ReadFile(rootLive); !bytes.Equal(got, legacy) {
122 t.Fatal("flag-off bootstrap did not render main's unit")
123 }
124 })
125 }
126}
127