package host import ( "bytes" "os" "path/filepath" "testing" ) // plan9aOddHostEnv enables the claim but carries an unrelated line that // readClaimUnitSettings cannot parse. var plan9aOddHostEnv = []string{ "CARLOS_HOST_CLAIM=1\nCARLOS_OTHER=\"unterminated\n", "CARLOS_HOST_CLAIM=1\nCARLOS_OTHER=a\\\n", "CARLOS_HOST_CLAIM='1\n", } func plan9aEnabledControl(t *testing.T) []byte { t.Helper() legacy, err := unitsFS.ReadFile("units/" + claimControlUnit) if err != nil { t.Fatal(err) } return claimUnitContent(claimControlUnit, legacy, claimUnitSettings{enabled: true, data: "/data", opt: "/opt/carlos"}) } func plan9aWriteFile(t *testing.T, path string, body []byte) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, body, 0644); err != nil { t.Fatal(err) } } // 6b-N2: system apply on an enabled box leaves the installed control unit // alone, unrestarted, when host.env cannot be parsed. func TestPlan9AApplyKeepsEnabledControlUnitOnHostEnvError(t *testing.T) { for _, env := range plan9aOddHostEnv { t.Run(env, func(t *testing.T) { f := newApplyFixture(t, "stable", "v1.0.0") plan9aWriteFile(t, filepath.Join(f.a.etcDir, "carlos", "host.env"), []byte(env)) if _, err := readClaimUnitSettings(filepath.Join(f.a.etcDir, "carlos", "host.env")); err == nil { t.Fatal("precondition: host.env parsed") } installed := plan9aEnabledControl(t) live := filepath.Join(f.a.etcDir, "systemd", "system", claimControlUnit) plan9aWriteFile(t, live, installed) stage := t.TempDir() if err := bootstrap(stage, nil); err != nil { t.Fatal(err) } if err := f.a.installUnits(t.Context(), stage, nil); err != nil { t.Fatal(err) } if got, _ := os.ReadFile(live); !bytes.Equal(got, installed) { t.Fatalf("enabled control unit flipped:\n%s", got) } if f.called("systemctl try-restart " + claimControlUnit) { t.Fatal("control unit restarted") } }) } } // 6b-N2: the live bootstrap likewise keeps an enabled control unit. func TestPlan9ABootstrapKeepsEnabledControlUnitOnHostEnvError(t *testing.T) { for _, env := range plan9aOddHostEnv { t.Run(env, func(t *testing.T) { root := t.TempDir() plan9aWriteFile(t, filepath.Join(root, "etc", "carlos", "host.env"), []byte(env)) installed := plan9aEnabledControl(t) live := filepath.Join(root, "etc", "systemd", "system", claimControlUnit) plan9aWriteFile(t, live, installed) if err := bootstrap(root, nil); err != nil { t.Fatal(err) } if got, _ := os.ReadFile(live); !bytes.Equal(got, installed) { t.Fatalf("enabled control unit flipped:\n%s", got) } }) } } // Flag-off stays main's: an installed unit without the claim environment // is rendered from the template exactly as before, whatever host.env holds. func TestPlan9AFlagOffControlUnitUnchangedOnHostEnvError(t *testing.T) { t.Parallel() legacy, err := unitsFS.ReadFile("units/" + claimControlUnit) if err != nil { t.Fatal(err) } for name, installed := range map[string][]byte{"absent": nil, "template": legacy, "older": []byte("[Unit]\nDescription=older template\n")} { t.Run(name, func(t *testing.T) { f := newApplyFixture(t, "stable", "v1.0.0") plan9aWriteFile(t, filepath.Join(f.a.etcDir, "carlos", "host.env"), []byte(plan9aOddHostEnv[0])) live := filepath.Join(f.a.etcDir, "systemd", "system", claimControlUnit) if installed != nil { plan9aWriteFile(t, live, installed) } stage := t.TempDir() if err := bootstrap(stage, nil); err != nil { t.Fatal(err) } if err := f.a.installUnits(t.Context(), stage, nil); err != nil { t.Fatal(err) } if got, _ := os.ReadFile(live); !bytes.Equal(got, legacy) { t.Fatal("flag-off apply did not render main's unit") } root := t.TempDir() plan9aWriteFile(t, filepath.Join(root, "etc", "carlos", "host.env"), []byte(plan9aOddHostEnv[0])) rootLive := filepath.Join(root, "etc", "systemd", "system", claimControlUnit) if installed != nil { plan9aWriteFile(t, rootLive, installed) } if err := bootstrap(root, nil); err != nil { t.Fatal(err) } if got, _ := os.ReadFile(rootLive); !bytes.Equal(got, legacy) { t.Fatal("flag-off bootstrap did not render main's unit") } }) } }