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 "context"
5 "os"
6 "strings"
7 "testing"
8
9 "github.com/carlosframework/platform/internal/instancectl"
10 "github.com/carlosframework/platform/internal/rootstate"
11)
12
13func TestPlan14UnitStartupIsolation(t *testing.T) {
14 b, in, shape := plan14UnitBindingFixture(t, "console.service", "", "")
15 shape.Program = strings.Repeat("a", 64)
16 b.canonical = func(context.Context, string) (claimUnitShape, error) { return shape, nil }
17 if _, err := b.establish(t.Context(), in.Host); err != nil {
18 t.Fatal(err)
19 }
20 d := claimUnitDispatch{bindings: b, shapes: func() (map[string]claimUnitShape, error) { return nil, os.ErrPermission }}
21 if unit, _, err := d.Placement("sibling.example"); err != nil || unit != instancectl.UnitName(instancectl.RoleRun, "sibling.example") {
22 t.Fatal("config corruption blocked ordinary cleanup", unit, err)
23 }
24 if _, err := d.forStart(t.Context(), in.Host); err == nil {
25 t.Fatal("corrupt config launched named unit")
26 }
27 if _, err := d.forStart(t.Context(), "sibling.example"); err == nil {
28 t.Fatal("corrupt config allowed an unverified ordinary launch")
29 }
30 d.shapes = func() (map[string]claimUnitShape, error) { return map[string]claimUnitShape{}, nil }
31 checked := false
32 d.effective = func(context.Context, claimUnitShape) error { checked = true; return nil }
33 if _, err := d.forStart(t.Context(), "sibling.example"); err != nil || !checked {
34 t.Fatal("verified ordinary launch refused", err, checked)
35 }
36}
37
38func TestPlan14ProductionUnitStartupIsolation(t *testing.T) {
39 for _, corrupt := range []bool{false, true} {
40 t.Run(map[bool]string{false: "bad-program", true: "corrupt-config"}[corrupt], func(t *testing.T) {
41 f, _, _ := generationLaunchFixture(t)
42 b, in, shape := plan14UnitBindingFixture(t, "console.service", "", "")
43 if _, err := b.establish(t.Context(), in.Host); err != nil {
44 t.Fatal(err)
45 }
46 oldOpen, oldMaterials, oldShapes := openClaimUnitBindings, openClaimUnitMaterializations, productionClaimUnitShapes
47 defer func() {
48 openClaimUnitBindings, openClaimUnitMaterializations, productionClaimUnitShapes = oldOpen, oldMaterials, oldShapes
49 }()
50 openClaimUnitBindings = func() (rootstate.Store, error) { return b.state, nil }
51 openClaimUnitMaterializations = func() (rootstate.Store, error) { return &rollbackDisk{dir: t.TempDir()}, nil }
52 productionClaimUnitShapes = func() (map[string]claimUnitShape, error) {
53 if corrupt {
54 return nil, os.ErrPermission
55 }
56 return map[string]claimUnitShape{in.Host: shape}, nil
57 }
58 base := instancectl.ClaimUnits{Systemctl: func(_ context.Context, args ...string) (string, error) {
59 if len(args) != 1 || args[0] != "daemon-reload" {
60 t.Fatal("startup touched a unit", args)
61 }
62 return "", nil
63 }}
64 f.root.Units = base
65 wrapped := ClaimRootWithLaunch{Launch: f.root}
66 wrapped.Root.Stop.Coordinator.Kernel = claimUnitStopKernel{Units: base}
67 a := &ClaimAssembly{}
68 if err := bindProductionClaimUnits(t.Context(), &wrapped, a); err != nil {
69 t.Fatal("one unit killed root startup", err)
70 }
71 defer a.Close(t.Context())
72 for _, units := range []ClaimLaunchUnits{wrapped.Launch.Units, wrapped.Root.Stop.Coordinator.Kernel.(claimUnitStopKernel).Units} {
73 p := units.(interface {
74 Placement(string) (string, string, error)
75 })
76 if unit, _, err := p.Placement("ordinary.example"); err != nil || unit != instancectl.UnitName(instancectl.RoleRun, "ordinary.example") {
77 t.Fatal("ordinary host blocked", unit, err)
78 }
79 if unit, _, err := p.Placement(in.Host); err != nil || unit != shape.Unit {
80 t.Fatal("retained cleanup placement lost", unit, err)
81 }
82 }
83 })
84 }
85}
86