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 "encoding/json"
6 "errors"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "testing"
11 "time"
12
13 "github.com/carlosframework/platform/internal/hostbox"
14 "github.com/carlosframework/platform/internal/hostclaim"
15 "github.com/carlosframework/platform/internal/hostguard"
16 "github.com/carlosframework/platform/internal/hostwrite"
17 "github.com/carlosframework/platform/internal/registry"
18 "github.com/carlosframework/platform/internal/release"
19 "github.com/carlosframework/platform/internal/rootparticipation"
20)
21
22type restartRequestProcessFixture struct {
23 Unit rollbackProcessFixture
24 Claim *hostclaim.Claim
25 Installations, Journal string
26}
27
28func TestPlan14RootRestartProductionCrash(t *testing.T) {
29 t.Parallel()
30 if boundary := os.Getenv("PLAN14_REQUEST_CRASH"); boundary != "" {
31 dir := os.Getenv("PLAN14_REQUEST_DIR")
32 f := newRestartConsumptionFixture(t)
33 if err := f.run(t); err != nil {
34 t.Fatal(err)
35 }
36 f.request(t, release.RestartScopeHost, observedHost)
37 storeDir := filepath.Join(dir, "deployment")
38 store := release.NewDirStore(storeDir)
39 keys, err := f.e.store.List(t.Context(), "")
40 if err != nil {
41 t.Fatal(err)
42 }
43 for _, key := range keys {
44 raw, _, err := f.e.store.Get(t.Context(), key)
45 if err != nil {
46 t.Fatal(err)
47 }
48 if _, err := store.Put(t.Context(), key, raw, ""); err != nil {
49 t.Fatal(err)
50 }
51 }
52 crash := func() {
53 state := restartRequestProcessFixture{Unit: rollbackProcessFixture{Props: f.u.props, Exe: f.u.exe, Opt: f.u.opt, Bake: f.e.bake, Store: storeDir, Effects: f.u.effects}, Claim: f.claim,
54 Installations: f.p.source.state.(*rollbackDisk).dir, Journal: f.p.journal.Store.(*rollbackDisk).dir}
55 raw, err := json.Marshal(state)
56 if err != nil {
57 t.Fatal(err)
58 }
59 if err := os.WriteFile(filepath.Join(dir, "fixture.json"), raw, 0600); err != nil {
60 t.Fatal(err)
61 }
62 os.Exit(74)
63 }
64 disk := f.p.journal.Store.(*rollbackDisk)
65 classify := func(raw []byte) string {
66 var r struct {
67 Pending *release.RestartIntent
68 ScopeID string `json:"scope_id"`
69 }
70 if json.Unmarshal(raw, &r) != nil {
71 return ""
72 }
73 if r.Pending == nil {
74 if r.ScopeID != release.RestartScopeNone {
75 return "consumed"
76 }
77 return "baseline"
78 }
79 if r.Pending.Targets[0].After != "" {
80 return "completed"
81 }
82 if r.Pending.Targets[0].Before != "" {
83 return "intent"
84 }
85 return "pending"
86 }
87 disk.afterWrite = func(_ string, raw []byte) {
88 if classify(raw) == boundary {
89 crash()
90 }
91 }
92 if boundary == "intent-visible" {
93 disk.beforeWrite = func(name string, raw []byte) {
94 if classify(raw) != "intent" {
95 return
96 }
97 file, err := os.CreateTemp(disk.dir, "visible-")
98 if err != nil {
99 t.Fatal(err)
100 }
101 if _, err = file.Write(raw); err != nil {
102 t.Fatal(err)
103 }
104 if err = file.Sync(); err != nil {
105 t.Fatal(err)
106 }
107 if err = file.Close(); err != nil {
108 t.Fatal(err)
109 }
110 if err = os.Rename(file.Name(), filepath.Join(disk.dir, name)); err != nil {
111 t.Fatal(err)
112 }
113 // Exit before syncing the parent of the visible rename.
114 crash()
115 }
116 }
117 f.u.afterEffect = func() {
118 if boundary == "effect" {
119 crash()
120 }
121 }
122 _ = f.run(t)
123 t.Fatal("crash boundary not reached")
124 }
125 for _, boundary := range []string{"pending", "intent", "intent-visible", "effect", "completed", "consumed"} {
126 t.Run(boundary, func(t *testing.T) {
127 dir := t.TempDir()
128 ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
129 defer cancel()
130 cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestPlan14RootRestartProductionCrash$", "-test.count=1")
131 cmd.Dir = dir
132 cmd.Env = append(os.Environ(), "TMPDIR="+dir, "PLAN14_REQUEST_DIR="+dir, "PLAN14_REQUEST_CRASH="+boundary, "CARLOS_HOST_CLAIM=")
133 out, err := cmd.CombinedOutput()
134 var exit *exec.ExitError
135 if !errors.As(err, &exit) || exit.ExitCode() != 74 {
136 t.Fatalf("child: %v\n%s", err, out)
137 }
138 raw, err := os.ReadFile(filepath.Join(dir, "fixture.json"))
139 if err != nil {
140 t.Fatal(err)
141 }
142 var saved restartRequestProcessFixture
143 if err = json.Unmarshal(raw, &saved); err != nil {
144 t.Fatal(err)
145 }
146 u := &plan14RestartUnits{props: saved.Unit.Props, exe: saved.Unit.Exe, opt: saved.Unit.Opt, effects: saved.Unit.Effects}
147 store := release.NewDirStore(saved.Unit.Store)
148 state := &rollbackDisk{dir: saved.Installations}
149 guard := hostguard.Guard{DataDir: t.TempDir(), LockDir: t.TempDir(), Root: hostguard.NewRootAuthority(state), Claims: rootViewClaimReader{obs: hostclaim.Observed{Host: observedHost, ETag: "claim", Claim: saved.Claim}}}
150 source := &proofBindingSource{store: store, sys: u, optDir: u.opt, openFile: os.Open, unitDirs: []string{filepath.Dir(u.props.FragmentPath)}, healthy: func(acct, app, version, digest string) *release.HealthyBuild {
151 for _, b := range release.ReadHealthy(saved.Unit.Bake, acct, app) {
152 if b.Version == version && b.SHA256 == digest {
153 return &b
154 }
155 }
156 return nil
157 }}
158 now, sleep := settleClock()
159 p := rootRestartRequests{journal: &release.RootRestarts{Store: &rollbackDisk{dir: saved.Journal}}, source: rootRestartRequestSource{state: state, store: store, census: func() (rootparticipation.Census, error) {
160 return rootparticipation.Census{Box: "box", Enrollment: "en", Migration: 1, Complete: true}, nil
161 }},
162 sys: u, optDir: u.opt, getenv: func(string) string { return "5s" }, checks: rootRestartChecks{host: source.forHost, ready: func(registry.Route) func() error { return func() error { return nil } }, now: now, sleep: sleep},
163 writes: hostwrite.Config{Enabled: true, Selector: hostbox.WorkSource{Guard: guard, Box: "box", Region: "ie", Now: time.Now, Policy: func(_ context.Context, c *hostclaim.Claim, s hostbox.Selection, _ hostwrite.Identity) error {
164 return hostbox.Serving(c, s)
165 }, Local: func(context.Context, *hostclaim.Claim, hostbox.Selection) error { return nil }}}}
166 err = p.run(t.Context())
167 if boundary == "intent" || boundary == "intent-visible" {
168 if err == nil || len(u.effects) != 0 {
169 t.Fatalf("ambiguous replay: %v %v", err, u.effects)
170 }
171 u.props.InvocationID += "reconciled"
172 u.props.MainPID++
173 if err = p.run(t.Context()); err != nil || len(u.effects) != 0 {
174 t.Fatalf("reconcile: %v %v", err, u.effects)
175 }
176 } else if err != nil || len(u.effects) != 1 {
177 t.Fatalf("resume: %v %v", err, u.effects)
178 }
179 count := len(u.effects)
180 if err = p.run(t.Context()); err != nil || len(u.effects) != count {
181 t.Fatalf("replay: %v %v", err, u.effects)
182 }
183 })
184 }
185}
186