| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/carlosframework/platform/internal/cgroup" |
| 17 | "github.com/carlosframework/platform/internal/edge" |
| 18 | "github.com/carlosframework/platform/internal/instancectl" |
| 19 | "github.com/carlosframework/platform/internal/registry" |
| 20 | "github.com/carlosframework/platform/internal/release" |
| 21 | ) |
| 22 | |
| 23 | // stageVersion writes <OptDir>/versions/<ver>/app with body and points |
| 24 | // host's live link at it. Each version's body differs, so each has its own |
| 25 | // digest. |
| 26 | func stageVersion(t *testing.T, b *ExecBackend, host, ver, body string) string { |
| 27 | t.Helper() |
| 28 | bin := filepath.Join(b.OptDir, "versions", ver, "app") |
| 29 | if err := os.MkdirAll(filepath.Dir(bin), 0o755); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if err := os.WriteFile(bin, []byte("#!/bin/sh\n# "+ver+"\n"+body), 0o755); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | repointLiveLink(t, b, host, bin) |
| 36 | return bin |
| 37 | } |
| 38 | |
| 39 | // repointLiveLink is root's repoint: live/<host> names target from now on. |
| 40 | func repointLiveLink(t *testing.T, b *ExecBackend, host, target string) { |
| 41 | t.Helper() |
| 42 | if err := os.MkdirAll(b.liveDir(), 0o755); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | tmp := b.liveLink(host) + ".tmp" |
| 46 | os.Remove(tmp) |
| 47 | if err := os.Symlink(target, tmp); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if err := os.Rename(tmp, b.liveLink(host)); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // servingBody is a staged build that records the path it was exec'd by |
| 56 | // ($0) in marker, then becomes the test binary's fake app (TestMain), so it |
| 57 | // binds --socket and answers the health wait. |
| 58 | func servingBody(t *testing.T, marker string) string { |
| 59 | t.Helper() |
| 60 | self, err := os.Executable() |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | t.Setenv("ACTIVATOR_FAKE_APP", "1") |
| 65 | return "printf '%s' \"$0\" > '" + marker + "'\nexec '" + self + "' \"$@\"\n" |
| 66 | } |
| 67 | |
| 68 | // Settings spec §4.2: the identity is captured per route. A unit row on a |
| 69 | // SplitBackend has none, whatever its backend could do; an exec row gets |
| 70 | // the exec half. A bare ExecBackend answers for itself, and a backend |
| 71 | // without the capability answers nil. |
| 72 | func TestIdentityIsPerRoute(t *testing.T) { |
| 73 | exec := testExecBackend(t) |
| 74 | // The unit half is an ExecBackend too, so a nil for the unit row is the |
| 75 | // row's answer, not an accident of its backend's type. |
| 76 | split := &SplitBackend{Exec: exec, Unit: testExecBackend(t)} |
| 77 | unitRow := registry.Route{Host: "u.example.com", Backing: registry.BackingUnit} |
| 78 | execRow := registry.Route{Host: "e.example.com"} |
| 79 | if ib := identityFor(split, unitRow); ib != nil { |
| 80 | t.Fatalf("a unit row got an identity backend: %T", ib) |
| 81 | } |
| 82 | if ib := identityFor(split, execRow); ib != identityBackend(exec) { |
| 83 | t.Fatalf("an exec row got %v, want the exec half", ib) |
| 84 | } |
| 85 | if ib := identityFor(exec, execRow); ib != identityBackend(exec) { |
| 86 | t.Fatalf("a bare ExecBackend answered %v for itself", ib) |
| 87 | } |
| 88 | if ib := identityFor(&fakeBackend{}, execRow); ib != nil { |
| 89 | t.Fatalf("a backend without the capability answered %T", ib) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // A cold default instance has no live link until the Materialize hook |
| 94 | // stages one. LiveIdentity runs the hook first and captures what it |
| 95 | // materialized, read once. |
| 96 | func TestLiveIdentityMaterializesFirst(t *testing.T) { |
| 97 | b := testExecBackend(t) |
| 98 | host := "cold.example.com" |
| 99 | var bin string |
| 100 | calls := 0 |
| 101 | b.Materialize = func(_ context.Context, h string) error { |
| 102 | calls++ |
| 103 | bin = stageVersion(t, b, h, "v1", "exit 0\n") |
| 104 | return nil |
| 105 | } |
| 106 | id, err := b.LiveIdentity(context.Background(), registry.Route{Host: host}) |
| 107 | if err != nil { |
| 108 | t.Fatalf("LiveIdentity: %v", err) |
| 109 | } |
| 110 | want, _ := release.FileSHA256(bin) |
| 111 | if calls != 1 || id.Version != "v1" || id.StagedPath != bin || id.Digest != want { |
| 112 | t.Fatalf("materialized %d times, identity %+v; want v1 at %s (%s)", calls, id, bin, want) |
| 113 | } |
| 114 | // A link that exists is not materialized again. |
| 115 | if _, err := b.LiveIdentity(context.Background(), registry.Route{Host: host}); err != nil || calls != 1 { |
| 116 | t.Fatalf("second capture: %v, materialized %d times", err, calls) |
| 117 | } |
| 118 | // A materialize failure is the capture's failure. |
| 119 | b.Materialize = func(context.Context, string) error { return errors.New("controld: 502") } |
| 120 | if _, err := b.LiveIdentity(context.Background(), registry.Route{Host: "other.example.com"}); err == nil || !strings.Contains(err.Error(), "502") { |
| 121 | t.Fatalf("a failed materialize = %v", err) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Flat, generations off: StartGenExe execs the recorded file by path on the |
| 126 | // canonical socket, though root repointed the link after the capture. The |
| 127 | // handle names that build, its path and digest, and the host's cache is |
| 128 | // purged as on any wake. A copy whose bytes changed starts nothing. |
| 129 | func TestStartGenExeFlatExecsTheRecordedFile(t *testing.T) { |
| 130 | requireUnixSockets(t) |
| 131 | b := testExecBackend(t) |
| 132 | b.RunDirOverride = shortRunDir(t) |
| 133 | cache := edge.NewCache(edge.DefaultCacheMaxEntries, edge.DefaultCacheMaxBodyBytes) |
| 134 | b.Cache = cache |
| 135 | host := "flat.example.com" |
| 136 | marker := filepath.Join(t.TempDir(), "argv0") |
| 137 | a := stageVersion(t, b, host, "vA", servingBody(t, marker)) |
| 138 | rt := flatGenRoute(b, host) |
| 139 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 140 | defer cancel() |
| 141 | id, err := b.LiveIdentity(ctx, rt) |
| 142 | if err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | stageVersion(t, b, host, "vB", servingBody(t, marker)) // root's repoint lands after the record |
| 146 | |
| 147 | stale := edge.CacheKey{Host: host, Method: "GET", Path: "/"} |
| 148 | cache.Put(stale, "", &edge.CachedResponse{StatusCode: 200, Header: http.Header{}, Body: []byte("old"), StoredAt: time.Now(), TTL: time.Minute}) |
| 149 | |
| 150 | proc, g, err := b.StartGenExe(ctx, rt, 3, false, id) |
| 151 | if err != nil { |
| 152 | t.Fatalf("StartGenExe: %v", err) |
| 153 | } |
| 154 | defer proc.Stop(context.Background()) //nolint:errcheck |
| 155 | if got := waitForFile(t, marker, 2*time.Second); got != a { |
| 156 | t.Fatalf("execd %q, want the recorded %q", got, a) |
| 157 | } |
| 158 | // No TierOf here, so the build's tier is GenTier's fallback, the default. |
| 159 | want := Gen{N: 3, Socket: rt.Socket, Control: rt.Socket + ".control", Build: "vA", StagedPath: a, Digest: id.Digest, Tier: cgroup.TierDefault} |
| 160 | if g != want { |
| 161 | t.Fatalf("handle = %+v, want %+v", g, want) |
| 162 | } |
| 163 | if _, ok := cache.GetFor(stale, httptest.NewRequest("GET", "http://"+host+"/", nil)); ok { |
| 164 | t.Fatal("the host's cache survived the spawn") |
| 165 | } |
| 166 | stopCtx, stopCancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 167 | defer stopCancel() |
| 168 | if err := proc.Stop(stopCtx); err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | |
| 172 | os.Remove(marker) |
| 173 | bad := id |
| 174 | bad.Digest = strings.Repeat("0", 64) |
| 175 | p, _, err := b.StartGenExe(ctx, rt, 4, false, bad) |
| 176 | if !errors.Is(err, release.ErrStagedCopyChanged) || p != nil { |
| 177 | t.Fatalf("a changed copy = %v, %v; want ErrStagedCopyChanged and no process", p, err) |
| 178 | } |
| 179 | time.Sleep(200 * time.Millisecond) |
| 180 | if _, err := os.Stat(marker); !os.IsNotExist(err) { |
| 181 | t.Fatal("a refused identity ran something") |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Flat, generations on: the recorded file binds generation n's own socket, |
| 186 | // as StartGen does. |
| 187 | func TestStartGenExeFlatPerGenBindsTheGenerationSocket(t *testing.T) { |
| 188 | requireUnixSockets(t) |
| 189 | b := testExecBackend(t) |
| 190 | b.RunDirOverride = shortRunDir(t) |
| 191 | host := "pergen.example.com" |
| 192 | marker := filepath.Join(t.TempDir(), "argv0") |
| 193 | a := stageVersion(t, b, host, "vA", servingBody(t, marker)) |
| 194 | rt := flatGenRoute(b, host) |
| 195 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 196 | defer cancel() |
| 197 | id, err := b.LiveIdentity(ctx, rt) |
| 198 | if err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | stageVersion(t, b, host, "vB", servingBody(t, marker)) |
| 202 | proc, g, err := b.StartGenExe(ctx, rt, 9, true, id) |
| 203 | if err != nil { |
| 204 | t.Fatalf("StartGenExe: %v", err) |
| 205 | } |
| 206 | defer proc.Stop(context.Background()) //nolint:errcheck |
| 207 | own := genSocketPathIn(filepath.Join(b.RunDirOverride, "g"), host, 9) |
| 208 | if g.Socket != own || g.Control != own+".control" || !isSocket(own) { |
| 209 | t.Fatalf("handle = %+v, want generation 9's bound socket %s", g, own) |
| 210 | } |
| 211 | if got := waitForFile(t, marker, 2*time.Second); got != a { |
| 212 | t.Fatalf("execd %q, want the recorded %q", got, a) |
| 213 | } |
| 214 | if _, err := os.Lstat(rt.Socket); !os.IsNotExist(err) { |
| 215 | t.Fatalf("the canonical path was bound with generations on (lstat: %v)", err) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // A foreign row keeps its canonical socket whatever perGen says, as |
| 220 | // StartStaged and StartGen keep it, and execs the recorded file there. |
| 221 | func TestStartGenExeForeignRunsOnTheCanonicalSocket(t *testing.T) { |
| 222 | requireUnixSockets(t) |
| 223 | b := testExecBackend(t) |
| 224 | host := "foreign.example.com" |
| 225 | marker := filepath.Join(t.TempDir(), "argv0") |
| 226 | a := stageVersion(t, b, host, "vA", servingBody(t, marker)) |
| 227 | rt := registry.Route{Host: host, Kind: "instance", DBPath: filepath.Join(b.DataDir, host+".db"), Socket: tmpSock(t)} |
| 228 | if l, _ := b.Layout(rt); l != LayoutForeign { |
| 229 | t.Fatalf("fixture layout = %v, want LayoutForeign", l) |
| 230 | } |
| 231 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 232 | defer cancel() |
| 233 | id, err := b.LiveIdentity(ctx, rt) |
| 234 | if err != nil { |
| 235 | t.Fatal(err) |
| 236 | } |
| 237 | stageVersion(t, b, host, "vB", servingBody(t, marker)) |
| 238 | proc, g, err := b.StartGenExe(ctx, rt, 2, true, id) |
| 239 | if err != nil { |
| 240 | t.Fatalf("StartGenExe: %v", err) |
| 241 | } |
| 242 | defer proc.Stop(context.Background()) //nolint:errcheck |
| 243 | if g.Socket != rt.Socket || g.Build != "vA" { |
| 244 | t.Fatalf("handle = %+v, want vA on the canonical %s", g, rt.Socket) |
| 245 | } |
| 246 | if got := waitForFile(t, marker, 2*time.Second); got != a { |
| 247 | t.Fatalf("execd %q, want the recorded %q", got, a) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Flat seed: SeedExe runs the recorded file, though the link moved after |
| 252 | // the capture, and Seed still runs the link. A changed copy seeds nothing. |
| 253 | func TestSeedExeFlatRunsTheRecordedFile(t *testing.T) { |
| 254 | b := testExecBackend(t) |
| 255 | host := "seedexe.example.com" |
| 256 | marker := filepath.Join(t.TempDir(), "argv0") |
| 257 | record := "printf '%s' \"$0\" > '" + marker + "'\n" |
| 258 | a := stageVersion(t, b, host, "vA", record) |
| 259 | rt := registry.Route{Host: host, Kind: "instance", Seed: `["seed"]`} |
| 260 | id, err := b.LiveIdentity(context.Background(), rt) |
| 261 | if err != nil { |
| 262 | t.Fatal(err) |
| 263 | } |
| 264 | bPath := stageVersion(t, b, host, "vB", record) |
| 265 | if err := b.SeedExe(context.Background(), rt, id); err != nil { |
| 266 | t.Fatalf("SeedExe: %v", err) |
| 267 | } |
| 268 | if got := waitForFile(t, marker, time.Second); got != a { |
| 269 | t.Fatalf("seeded with %q, want the recorded %q", got, a) |
| 270 | } |
| 271 | |
| 272 | os.Remove(marker) |
| 273 | bad := id |
| 274 | bad.Digest = strings.Repeat("0", 64) |
| 275 | if err := b.SeedExe(context.Background(), rt, bad); !errors.Is(err, release.ErrStagedCopyChanged) { |
| 276 | t.Fatalf("a changed copy = %v, want ErrStagedCopyChanged", err) |
| 277 | } |
| 278 | if _, err := os.Stat(marker); !os.IsNotExist(err) { |
| 279 | t.Fatal("a refused identity seeded") |
| 280 | } |
| 281 | |
| 282 | if err := b.Seed(context.Background(), rt); err != nil { |
| 283 | t.Fatalf("Seed: %v", err) |
| 284 | } |
| 285 | if got := waitForFile(t, marker, time.Second); got != b.liveLink(host) { |
| 286 | t.Fatalf("Seed ran %q, want the live link (today's, which resolves to %s)", got, bPath) |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // identityControl is a root control channel that can name an executable |
| 291 | // for the original templates (StagedExeControl) and for a generation |
| 292 | // (GenControl). Each verb is recorded with the file it named. |
| 293 | type identityControl struct{ *fakeControl } |
| 294 | |
| 295 | func exeLine(verb, host, what string, x instancectl.StagedExe) string { |
| 296 | return fmt.Sprintf("%s %s %s %s %s %s", verb, host, what, x.Path, x.Version, x.Digest) |
| 297 | } |
| 298 | |
| 299 | func (c identityControl) StartExe(_ context.Context, host string, role instancectl.Role, max int64, x instancectl.StagedExe) (string, error) { |
| 300 | c.rec(exeLine("startexe", host, string(role), x)) |
| 301 | c.rec(fmt.Sprintf("tier startexe %d", max)) |
| 302 | if c.onStart != nil { |
| 303 | c.onStart() |
| 304 | } |
| 305 | return c.inv, c.startErr |
| 306 | } |
| 307 | func (c identityControl) SeedExe(_ context.Context, host string, max int64, x instancectl.StagedExe) (instancectl.SeedResponse, error) { |
| 308 | c.rec(exeLine("seedexe", host, "seed", x)) |
| 309 | c.rec(fmt.Sprintf("tier seedexe %d", max)) |
| 310 | return c.seed, nil |
| 311 | } |
| 312 | func (c identityControl) StartGen(_ context.Context, host string, n uint64, _ int64) (string, error) { |
| 313 | c.rec(fmt.Sprintf("startgen %s %d", host, n)) |
| 314 | return "", errors.New("test: the live-link generation start was used") |
| 315 | } |
| 316 | func (c identityControl) StartGenExe(_ context.Context, host string, n uint64, max int64, x instancectl.StagedExe) (string, error) { |
| 317 | c.rec(exeLine("startgenexe", host, fmt.Sprint(n), x)) |
| 318 | c.rec(fmt.Sprintf("tier startgenexe %d", max)) |
| 319 | if c.onStart != nil { |
| 320 | c.onStart() |
| 321 | } |
| 322 | return c.inv, c.startErr |
| 323 | } |
| 324 | func (c identityControl) StartSidecarExe(_ context.Context, host string, _ int64, x instancectl.StagedExe) (string, error) { |
| 325 | c.rec(exeLine("startsidecarexe", host, "sidecar", x)) |
| 326 | return c.inv, nil |
| 327 | } |
| 328 | func (c identityControl) StopGen(_ context.Context, host string, n uint64, kill bool) error { |
| 329 | c.rec(fmt.Sprintf("stopgen %s %d kill=%v", host, n, kill)) |
| 330 | return nil |
| 331 | } |
| 332 | func (c identityControl) ResetGen(context.Context, string, uint64) error { return nil } |
| 333 | func (c identityControl) Parent(context.Context, string, int64) error { return nil } |
| 334 | |
| 335 | // noPlainVerbs fails when ctl saw a start or seed that names no executable: |
| 336 | // the live link's layout. |
| 337 | func noPlainVerbs(t *testing.T, ctl *fakeControl) { |
| 338 | t.Helper() |
| 339 | for _, line := range strings.Split(ctl.joined(), "\n") { |
| 340 | if strings.HasPrefix(line, "start ") || strings.HasPrefix(line, "seed ") || strings.HasPrefix(line, "startgen ") { |
| 341 | t.Errorf("a live-link verb was used: %q in\n%s", line, ctl.joined()) |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Nested: StartGenExe asks root for the recorded file, by path, version and |
| 347 | // digest, after root's repoint of the live link to B. With generations off |
| 348 | // that is the original run unit (generation 0); with them on, generation n. |
| 349 | // Never a plain Start or StartGen. |
| 350 | func TestNestedStartExeNamesTheRecordedFile(t *testing.T) { |
| 351 | t.Parallel() |
| 352 | for _, tc := range []struct { |
| 353 | name string |
| 354 | gens bool |
| 355 | n uint64 |
| 356 | want func(host string, x instancectl.StagedExe) string |
| 357 | }{ |
| 358 | {"generation 0", false, 1, func(h string, x instancectl.StagedExe) string { return exeLine("startexe", h, "run", x) }}, |
| 359 | {"generation n", true, 6, func(h string, x instancectl.StagedExe) string { return exeLine("startgenexe", h, "6", x) }}, |
| 360 | } { |
| 361 | t.Run(tc.name, func(t *testing.T) { |
| 362 | ctl := &fakeControl{inv: "inv1"} |
| 363 | unit := &fakeUnit{} |
| 364 | b, rt := nestedBackend(t, ctl, unit) |
| 365 | b.Control = identityControl{ctl} |
| 366 | b.GenContainment = tc.gens |
| 367 | a := stageVersion(t, b, rt.Host, "vA", "exit 0\n") |
| 368 | id, err := b.LiveIdentity(context.Background(), rt) |
| 369 | if err != nil { |
| 370 | t.Fatal(err) |
| 371 | } |
| 372 | stageVersion(t, b, rt.Host, "vB", "exit 0\n") |
| 373 | sock := rt.Socket |
| 374 | if tc.gens { |
| 375 | sock = nestedGenSocketIn(b.pathsFor(rt).runDir, tc.n) |
| 376 | } |
| 377 | var stopServing func() |
| 378 | ctl.onStart = func() { stopServing = serveOK(t, sock) } |
| 379 | defer func() { |
| 380 | if stopServing != nil { |
| 381 | stopServing() |
| 382 | } |
| 383 | }() |
| 384 | unit.set("active", "inv1", true, true) |
| 385 | p, g, err := b.StartGenExe(context.Background(), rt, tc.n, tc.gens, id) |
| 386 | if err != nil { |
| 387 | t.Fatalf("StartGenExe: %v", err) |
| 388 | } |
| 389 | x := instancectl.StagedExe{Path: a, Version: "vA", Digest: id.Digest} |
| 390 | if !strings.Contains(ctl.joined(), tc.want(rt.Host, x)) { |
| 391 | t.Fatalf("root was asked:\n%s\nwant %q", ctl.joined(), tc.want(rt.Host, x)) |
| 392 | } |
| 393 | noPlainVerbs(t, ctl) |
| 394 | if g.Build != "vA" || g.StagedPath != a || g.Digest != id.Digest || g.Socket != sock { |
| 395 | t.Fatalf("handle = %+v", g) |
| 396 | } |
| 397 | unit.set("inactive", "inv1", false, false) |
| 398 | waitFor(t, "the unit to exit", p.Exited) |
| 399 | }) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Nested seed: SeedExe asks root to run the seed unit on the recorded file, |
| 404 | // not a plain Seed on the link root moved to B. A changed copy asks root |
| 405 | // for nothing. |
| 406 | func TestNestedSeedExeNamesTheRecordedFile(t *testing.T) { |
| 407 | ctl := &fakeControl{seed: instancectl.SeedResponse{InvocationID: "s1", Result: "success", Status: "0", Code: "exited"}} |
| 408 | b, rt := nestedBackend(t, ctl, &fakeUnit{}) |
| 409 | b.Control = identityControl{ctl} |
| 410 | rt.Seed = `["seed"]` |
| 411 | a := stageVersion(t, b, rt.Host, "vA", "exit 0\n") |
| 412 | id, err := b.LiveIdentity(context.Background(), rt) |
| 413 | if err != nil { |
| 414 | t.Fatal(err) |
| 415 | } |
| 416 | stageVersion(t, b, rt.Host, "vB", "exit 0\n") |
| 417 | if err := b.SeedExe(context.Background(), rt, id); err != nil { |
| 418 | t.Fatalf("SeedExe: %v", err) |
| 419 | } |
| 420 | x := instancectl.StagedExe{Path: a, Version: "vA", Digest: id.Digest} |
| 421 | if want := exeLine("seedexe", rt.Host, "seed", x); !strings.Contains(ctl.joined(), want) { |
| 422 | t.Fatalf("root was asked:\n%s\nwant %q", ctl.joined(), want) |
| 423 | } |
| 424 | noPlainVerbs(t, ctl) |
| 425 | |
| 426 | ctl.mu.Lock() |
| 427 | ctl.calls = nil |
| 428 | ctl.mu.Unlock() |
| 429 | bad := id |
| 430 | bad.Digest = strings.Repeat("0", 64) |
| 431 | if err := b.SeedExe(context.Background(), rt, bad); !errors.Is(err, release.ErrStagedCopyChanged) { |
| 432 | t.Fatalf("a changed copy = %v, want ErrStagedCopyChanged", err) |
| 433 | } |
| 434 | if ctl.joined() != "" { |
| 435 | t.Fatalf("a refused identity reached root:\n%s", ctl.joined()) |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // repointingGate is g's gate followed, on every answer, by root's repoint of |
| 440 | // host's live link at to: the window between a declined start's gate and |
| 441 | // the moment what it starts is bound or exec'd. |
| 442 | func repointingGate(t *testing.T, b *ExecBackend, host, to string, g *liveGate) func(registry.Route, string) error { |
| 443 | t.Helper() |
| 444 | return func(rt registry.Route, build string) error { |
| 445 | err := g.gate(rt, build) |
| 446 | repointLiveLink(t, b, host, to) |
| 447 | return err |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // declinedPinFixture is a nested backend whose root channel names |
| 452 | // executables, with v1 and v2 staged and the live link at v1. It returns |
| 453 | // v1's identity and v2's path. |
| 454 | func declinedPinFixture(t *testing.T, ctl *fakeControl, unit *fakeUnit) (*ExecBackend, registry.Route, instancectl.StagedExe, string) { |
| 455 | t.Helper() |
| 456 | b, rt := nestedBackend(t, ctl, unit) |
| 457 | b.Control = identityControl{ctl} |
| 458 | v1 := stageVersion(t, b, rt.Host, "v1", "exit 0\n") |
| 459 | v2 := stageVersion(t, b, rt.Host, "v2", "exit 0\n") |
| 460 | repointLiveLink(t, b, rt.Host, v1) |
| 461 | sum, err := release.FileSHA256(v1) |
| 462 | if err != nil { |
| 463 | t.Fatal(err) |
| 464 | } |
| 465 | return b, rt, instancectl.StagedExe{Path: v1, Version: "v1", Digest: sum}, v2 |
| 466 | } |
| 467 | |
| 468 | // Task 5 (readiness 2, recon A §1): a declined box's nested live-link start |
| 469 | // pins the link once. The gate is asked about the build the link names, and |
| 470 | // root is asked to bind exactly that file (StartExe at generation 0, |
| 471 | // StartGenExe at generation n), so root's repoint of the link at v2 after |
| 472 | // the gate answered still runs v1. Never a plain Start or StartGen, which |
| 473 | // would bind whatever the link names when root lays the unit out. |
| 474 | func TestADeclinedNestedLiveLinkStartRunsThePinnedFile(t *testing.T) { |
| 475 | t.Parallel() |
| 476 | for _, tc := range []struct { |
| 477 | name string |
| 478 | gens bool |
| 479 | want func(host string, x instancectl.StagedExe) string |
| 480 | }{ |
| 481 | {"generation 0", false, func(h string, x instancectl.StagedExe) string { return exeLine("startexe", h, "run", x) }}, |
| 482 | {"generation n", true, func(h string, x instancectl.StagedExe) string { return exeLine("startgenexe", h, "6", x) }}, |
| 483 | } { |
| 484 | t.Run(tc.name, func(t *testing.T) { |
| 485 | ctl := &fakeControl{inv: "inv1"} |
| 486 | unit := &fakeUnit{} |
| 487 | b, rt, x, v2 := declinedPinFixture(t, ctl, unit) |
| 488 | b.GenContainment = tc.gens |
| 489 | g := &liveGate{refuse: map[string]bool{"v2": true}} |
| 490 | b.LiveExecGate = repointingGate(t, b, rt.Host, v2, g) |
| 491 | sock := rt.Socket |
| 492 | if tc.gens { |
| 493 | sock = nestedGenSocketIn(b.pathsFor(rt).runDir, 6) |
| 494 | } |
| 495 | var stopServing func() |
| 496 | ctl.onStart = func() { stopServing = serveOK(t, sock) } |
| 497 | defer func() { |
| 498 | if stopServing != nil { |
| 499 | stopServing() |
| 500 | } |
| 501 | }() |
| 502 | unit.set("active", "inv1", true, true) |
| 503 | var ( |
| 504 | p Proc |
| 505 | ver string |
| 506 | err error |
| 507 | ) |
| 508 | if tc.gens { |
| 509 | var gen Gen |
| 510 | p, gen, err = b.StartGen(context.Background(), rt, 6, true) |
| 511 | ver = gen.Build |
| 512 | } else { |
| 513 | p, ver, err = b.Start(context.Background(), rt) |
| 514 | } |
| 515 | if err != nil { |
| 516 | t.Fatalf("start: %v", err) |
| 517 | } |
| 518 | if !strings.Contains(ctl.joined(), tc.want(rt.Host, x)) { |
| 519 | t.Fatalf("root was asked:\n%s\nwant %q", ctl.joined(), tc.want(rt.Host, x)) |
| 520 | } |
| 521 | noPlainVerbs(t, ctl) |
| 522 | if got := g.builds(); !slices.Equal(got, []string{"v1"}) { |
| 523 | t.Fatalf("gate asked %v, want [v1]", got) |
| 524 | } |
| 525 | if ver != "v1" { |
| 526 | t.Fatalf("version %q, want the pinned v1", ver) |
| 527 | } |
| 528 | unit.set("inactive", "inv1", false, false) |
| 529 | waitFor(t, "the unit to exit", p.Exited) |
| 530 | }) |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // The same for the legacy sidecar's nested live-link start: root is asked |
| 535 | // to bind the pinned v1 (StartSidecarExe), never a plain sidecar start. |
| 536 | func TestADeclinedNestedSidecarStartRunsThePinnedFile(t *testing.T) { |
| 537 | t.Parallel() |
| 538 | ctl := &fakeControl{inv: "inv1"} |
| 539 | unit := &fakeUnit{} |
| 540 | b, rt, x, v2 := declinedPinFixture(t, ctl, unit) |
| 541 | g := &liveGate{refuse: map[string]bool{"v2": true}} |
| 542 | b.LiveExecGate = repointingGate(t, b, rt.Host, v2, g) |
| 543 | unit.set("active", "inv1", true, true) |
| 544 | p, err := b.StartSidecar(context.Background(), rt) |
| 545 | if err != nil { |
| 546 | t.Fatalf("StartSidecar: %v", err) |
| 547 | } |
| 548 | if want := exeLine("startsidecarexe", rt.Host, "sidecar", x); !strings.Contains(ctl.joined(), want) { |
| 549 | t.Fatalf("root was asked:\n%s\nwant %q", ctl.joined(), want) |
| 550 | } |
| 551 | noPlainVerbs(t, ctl) |
| 552 | if got := g.builds(); !slices.Equal(got, []string{"v1"}) { |
| 553 | t.Fatalf("gate asked %v, want [v1]", got) |
| 554 | } |
| 555 | unit.set("inactive", "inv1", false, false) |
| 556 | waitFor(t, "the sidecar unit to exit", p.Exited) |
| 557 | } |
| 558 | |
| 559 | // A declined box's live link outside the version store names no build the |
| 560 | // gate could judge or root could validate: every nested live-link start |
| 561 | // (run, sidecar, seed) is refused in recordBuild's words, the gate is asked |
| 562 | // nothing, and root is asked for nothing. |
| 563 | func TestADeclinedLinkOutsideTheStoreAsksRootForNothing(t *testing.T) { |
| 564 | ctl := &fakeControl{inv: "inv1", seed: instancectl.SeedResponse{InvocationID: "s1", Result: "success", Status: "0", Code: "exited"}} |
| 565 | b, rt := nestedBackend(t, ctl, &fakeUnit{}) |
| 566 | b.Control = identityControl{ctl} |
| 567 | rt.Seed = `["seed"]` |
| 568 | hand := filepath.Join(t.TempDir(), "app") |
| 569 | if err := os.WriteFile(hand, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil { |
| 570 | t.Fatal(err) |
| 571 | } |
| 572 | repointLiveLink(t, b, rt.Host, hand) |
| 573 | g := &liveGate{} |
| 574 | b.LiveExecGate = g.gate |
| 575 | if _, _, err := b.Start(context.Background(), rt); !errors.Is(err, errBuildStateUnwritten) { |
| 576 | t.Fatalf("Start = %v, want errBuildStateUnwritten", err) |
| 577 | } |
| 578 | if _, err := b.StartSidecar(context.Background(), rt); !errors.Is(err, errBuildStateUnwritten) { |
| 579 | t.Fatalf("StartSidecar = %v, want errBuildStateUnwritten", err) |
| 580 | } |
| 581 | if err := b.Seed(context.Background(), rt); !errors.Is(err, errBuildStateUnwritten) { |
| 582 | t.Fatalf("Seed = %v, want errBuildStateUnwritten", err) |
| 583 | } |
| 584 | if got := ctl.joined(); got != "" { |
| 585 | t.Fatalf("root was asked:\n%s", got) |
| 586 | } |
| 587 | if got := g.builds(); len(got) != 0 { |
| 588 | t.Fatalf("gate asked %v about a link that names no build", got) |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // Task 5 (recon A §2): a declined box's flat seed on the live link pins the |
| 593 | // link once and execs the gated file by path, so root's repoint at v2 after |
| 594 | // the gate answered still seeds with v1. A held build seeds nothing: no |
| 595 | // process runs and no database is created. |
| 596 | func TestADeclinedFlatSeedRunsThePinnedFile(t *testing.T) { |
| 597 | t.Parallel() |
| 598 | b := testExecBackend(t) |
| 599 | host := "seedpin.example.com" |
| 600 | marker := filepath.Join(t.TempDir(), "argv0") |
| 601 | // $0 into marker, then the database the seed was handed (--db, $3). |
| 602 | body := "printf '%s' \"$0\" > '" + marker + "'\n: > \"$3\"\n" |
| 603 | v1 := stageVersion(t, b, host, "v1", body) |
| 604 | v2 := stageVersion(t, b, host, "v2", body) |
| 605 | repointLiveLink(t, b, host, v1) |
| 606 | rt := registry.Route{Host: host, Kind: "instance", Seed: `["seed"]`} |
| 607 | db := b.pathsFor(rt).dbPath |
| 608 | g := &liveGate{refuse: map[string]bool{"v2": true}} |
| 609 | b.LiveExecGate = repointingGate(t, b, host, v2, g) |
| 610 | if err := b.Seed(context.Background(), rt); err != nil { |
| 611 | t.Fatalf("Seed: %v", err) |
| 612 | } |
| 613 | if got := waitForFile(t, marker, time.Second); got != v1 { |
| 614 | t.Fatalf("seeded with %q, want the pinned %q", got, v1) |
| 615 | } |
| 616 | if got := g.builds(); !slices.Equal(got, []string{"v1"}) { |
| 617 | t.Fatalf("gate asked %v, want [v1]", got) |
| 618 | } |
| 619 | if _, err := os.Stat(db); err != nil { |
| 620 | t.Fatalf("the seed's database: %v", err) |
| 621 | } |
| 622 | |
| 623 | // The link names held v2 now. A fresh instance seeds nothing. |
| 624 | os.Remove(marker) |
| 625 | if err := removeSeedDB(db); err != nil { |
| 626 | t.Fatal(err) |
| 627 | } |
| 628 | held := &liveGate{refuse: map[string]bool{"v2": true}} |
| 629 | b.LiveExecGate = held.gate |
| 630 | if err := b.Seed(context.Background(), rt); !errors.Is(err, errLiveHeld) { |
| 631 | t.Fatalf("Seed onto held v2 = %v, want the gate's refusal", err) |
| 632 | } |
| 633 | time.Sleep(100 * time.Millisecond) |
| 634 | if _, err := os.Stat(marker); !os.IsNotExist(err) { |
| 635 | t.Fatal("held v2 ran as the seed") |
| 636 | } |
| 637 | if _, err := os.Stat(db); !os.IsNotExist(err) { |
| 638 | t.Fatalf("a refused seed left a database: %v", err) |
| 639 | } |
| 640 | if got := held.builds(); !slices.Equal(got, []string{"v2"}) { |
| 641 | t.Fatalf("gate asked %v, want [v2]", got) |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // The nested seed: root is asked to run the seed unit on the pinned v1 |
| 646 | // (SeedExe), never a plain Seed of the link. A held build asks root for |
| 647 | // nothing. |
| 648 | func TestADeclinedNestedSeedRunsThePinnedFile(t *testing.T) { |
| 649 | ctl := &fakeControl{seed: instancectl.SeedResponse{InvocationID: "s1", Result: "success", Status: "0", Code: "exited"}} |
| 650 | b, rt, x, v2 := declinedPinFixture(t, ctl, &fakeUnit{}) |
| 651 | rt.Seed = `["seed"]` |
| 652 | g := &liveGate{refuse: map[string]bool{"v2": true}} |
| 653 | b.LiveExecGate = repointingGate(t, b, rt.Host, v2, g) |
| 654 | if err := b.Seed(context.Background(), rt); err != nil { |
| 655 | t.Fatalf("Seed: %v", err) |
| 656 | } |
| 657 | if want := exeLine("seedexe", rt.Host, "seed", x); !strings.Contains(ctl.joined(), want) { |
| 658 | t.Fatalf("root was asked:\n%s\nwant %q", ctl.joined(), want) |
| 659 | } |
| 660 | noPlainVerbs(t, ctl) |
| 661 | if got := g.builds(); !slices.Equal(got, []string{"v1"}) { |
| 662 | t.Fatalf("gate asked %v, want [v1]", got) |
| 663 | } |
| 664 | |
| 665 | ctl.mu.Lock() |
| 666 | ctl.calls = nil |
| 667 | ctl.mu.Unlock() |
| 668 | held := &liveGate{refuse: map[string]bool{"v2": true}} |
| 669 | b.LiveExecGate = held.gate |
| 670 | if err := b.Seed(context.Background(), rt); !errors.Is(err, errLiveHeld) { |
| 671 | t.Fatalf("Seed onto held v2 = %v, want the gate's refusal", err) |
| 672 | } |
| 673 | if got := ctl.joined(); got != "" { |
| 674 | t.Fatalf("a held seed reached root:\n%s", got) |
| 675 | } |
| 676 | if got := held.builds(); !slices.Equal(got, []string{"v2"}) { |
| 677 | t.Fatalf("gate asked %v, want [v2]", got) |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // BuildIdentity names build's staged copy of rt's app at the path root's |
| 682 | // validation expects (VersionPath under the row's account) and hashes it |
| 683 | // now. An invalid version or app, or a missing copy, names nothing. The |
| 684 | // SplitBackend forwards it for exec rows and refuses unit rows. |
| 685 | func TestBuildIdentityNamesTheStagedCopy(t *testing.T) { |
| 686 | b := testExecBackend(t) |
| 687 | rt := registry.Route{Host: "own.example.com", App: "blog"} |
| 688 | path := release.VersionPath(b.OptDir, release.RouteAccount(rt), "v1", "blog") |
| 689 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 690 | t.Fatal(err) |
| 691 | } |
| 692 | if err := os.WriteFile(path, []byte("#!/bin/sh\n# v1\n"), 0o755); err != nil { |
| 693 | t.Fatal(err) |
| 694 | } |
| 695 | sum, err := release.FileSHA256(path) |
| 696 | if err != nil { |
| 697 | t.Fatal(err) |
| 698 | } |
| 699 | want := release.StagedIdentity{Version: "v1", StagedPath: path, Digest: sum} |
| 700 | if id, err := b.BuildIdentity(rt, "v1"); err != nil || id != want { |
| 701 | t.Fatalf("BuildIdentity = %+v, %v; want %+v", id, err, want) |
| 702 | } |
| 703 | for _, bad := range []string{"", "..", "../v1", "v1/blog", "v9"} { |
| 704 | if id, err := b.BuildIdentity(rt, bad); err == nil { |
| 705 | t.Errorf("BuildIdentity(%q) = %+v, want a refusal", bad, id) |
| 706 | } |
| 707 | } |
| 708 | for _, app := range []string{"", "..", "a/b"} { |
| 709 | bad := rt |
| 710 | bad.App = app |
| 711 | if id, err := b.BuildIdentity(bad, "v1"); err == nil { |
| 712 | t.Errorf("BuildIdentity for app %q = %+v, want a refusal", app, id) |
| 713 | } |
| 714 | } |
| 715 | split := &SplitBackend{Exec: b, Unit: testExecBackend(t)} |
| 716 | if id, err := split.BuildIdentity(rt, "v1"); err != nil || id != want { |
| 717 | t.Fatalf("SplitBackend.BuildIdentity = %+v, %v; want %+v", id, err, want) |
| 718 | } |
| 719 | unitRow := rt |
| 720 | unitRow.Backing = registry.BackingUnit |
| 721 | if id, err := split.BuildIdentity(unitRow, "v1"); err == nil { |
| 722 | t.Fatalf("a unit row's BuildIdentity = %+v, want a refusal", id) |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // A control channel that cannot name an executable for the original |
| 727 | // template refuses a generation-0 start that names one. It never drops the |
| 728 | // file and binds the live link instead. |
| 729 | func TestStartRoleRefusesAnExeTheControlCannotName(t *testing.T) { |
| 730 | b := testExecBackend(t) |
| 731 | ctl := &fakeControl{inv: "inv"} |
| 732 | b.Control = ctl |
| 733 | x := instancectl.StagedExe{Path: "/opt/carlos/versions/v2/blog", Version: "v2", Digest: strings.Repeat("a", 64)} |
| 734 | if _, err := b.startRole(context.Background(), registry.Route{Host: "n.example.com"}, instancectl.RoleRun, 0, &x, cgroup.Limit{}); err == nil { |
| 735 | t.Fatal("a generation-0 start with an executable the control cannot name was accepted") |
| 736 | } |
| 737 | if ctl.joined() != "" { |
| 738 | t.Fatalf("root was asked:\n%s", ctl.joined()) |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | // tierFollowsTheLink is production's tier shape for a test backend: b.Tier |
| 743 | // reads the manifest of whatever the live link names when it is asked, and |
| 744 | // TierOf reads one named version's. vA declares children (1 GiB); every |
| 745 | // other build is the default (512 MiB). |
| 746 | func tierFollowsTheLink(b *ExecBackend, vA string) { |
| 747 | b.TierOf = func(_ registry.Route, v string) cgroup.Limit { |
| 748 | if v == vA { |
| 749 | return cgroup.TierChildren |
| 750 | } |
| 751 | return cgroup.TierDefault |
| 752 | } |
| 753 | b.Tier = func(rt registry.Route) cgroup.Limit { |
| 754 | v, _ := b.LiveVersion(rt.Host) |
| 755 | return b.TierOf(rt, v) |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | const childrenMax = "1073741824" // cgroup.TierChildren.MemoryMax |
| 760 | |
| 761 | // Ruling R-TIER: the recorded build runs under its own tier, not the tier |
| 762 | // of whatever root repointed the live link to after the record. A declares |
| 763 | // children (1 GiB), B the default (512 MiB), and the link names B by the |
| 764 | // time each spawn and seed runs. Flat, the limit lands on the host cgroup |
| 765 | // (flag off) or generation n's child (flag on); nested, it is the memory_max |
| 766 | // root is sent. The handle carries A's tier. |
| 767 | func TestStartGenExeAndSeedExeUseTheRecordedBuildsTier(t *testing.T) { |
| 768 | flat := func(t *testing.T, gens bool) (*ExecBackend, registry.Route, release.StagedIdentity, string) { |
| 769 | const host = "tier.example.com" |
| 770 | b, parent := delegatedBackend(t, host) |
| 771 | b.RunDirOverride = shortRunDir(t) |
| 772 | b.GenContainment = gens |
| 773 | if gens { |
| 774 | cgroup.FakePlacementForTest(t) // a temp dir is no cgroupfs, and a generation is never moved by PID |
| 775 | } |
| 776 | tierFollowsTheLink(b, "vA") |
| 777 | marker := filepath.Join(t.TempDir(), "argv0") |
| 778 | stageVersion(t, b, host, "vA", servingBody(t, marker)) |
| 779 | rt := flatGenRoute(b, host) |
| 780 | id, err := b.LiveIdentity(context.Background(), rt) |
| 781 | if err != nil { |
| 782 | t.Fatal(err) |
| 783 | } |
| 784 | stageVersion(t, b, host, "vB", servingBody(t, marker)) |
| 785 | if got := b.tier(rt); got != cgroup.TierDefault { |
| 786 | t.Fatalf("fixture: the live tier is %v, want B's default", got) |
| 787 | } |
| 788 | return b, rt, id, parent |
| 789 | } |
| 790 | readMax := func(t *testing.T, path string) string { |
| 791 | t.Helper() |
| 792 | got, err := os.ReadFile(path) |
| 793 | if err != nil { |
| 794 | t.Fatal(err) |
| 795 | } |
| 796 | return string(got) |
| 797 | } |
| 798 | t.Run("flat, generations off", func(t *testing.T) { |
| 799 | requireUnixSockets(t) |
| 800 | b, rt, id, parent := flat(t, false) |
| 801 | proc, g, err := b.StartGenExe(context.Background(), rt, 3, false, id) |
| 802 | if err != nil { |
| 803 | t.Fatalf("StartGenExe: %v", err) |
| 804 | } |
| 805 | defer proc.Stop(context.Background()) //nolint:errcheck |
| 806 | if got := readMax(t, filepath.Join(parent, "memory.max")); got != childrenMax || g.Tier != cgroup.TierChildren { |
| 807 | t.Fatalf("host cgroup memory.max = %s, handle tier %v; want A's 1 GiB", got, g.Tier) |
| 808 | } |
| 809 | }) |
| 810 | t.Run("flat, generations on", func(t *testing.T) { |
| 811 | requireUnixSockets(t) |
| 812 | b, rt, id, parent := flat(t, true) |
| 813 | proc, g, err := b.StartGenExe(context.Background(), rt, 9, true, id) |
| 814 | if err != nil { |
| 815 | t.Fatalf("StartGenExe: %v", err) |
| 816 | } |
| 817 | defer proc.Stop(context.Background()) //nolint:errcheck |
| 818 | if got := readMax(t, filepath.Join(parent, "g9", "memory.max")); got != childrenMax || g.Tier != cgroup.TierChildren { |
| 819 | t.Fatalf("g9 memory.max = %s, handle tier %v; want A's 1 GiB", got, g.Tier) |
| 820 | } |
| 821 | }) |
| 822 | t.Run("flat seed", func(t *testing.T) { |
| 823 | b, rt, id, parent := flat(t, false) |
| 824 | rt.Seed = `["seed"]` |
| 825 | // A's script execs the fake app, which exits 2 with no --socket: |
| 826 | // the seed's verdict is not this test's, its cgroup limit is. |
| 827 | _ = b.SeedExe(context.Background(), rt, id) |
| 828 | if got := readMax(t, filepath.Join(parent, "memory.max")); got != childrenMax { |
| 829 | t.Fatalf("host cgroup memory.max after the seed = %s, want A's 1 GiB", got) |
| 830 | } |
| 831 | }) |
| 832 | for _, tc := range []struct { |
| 833 | name string |
| 834 | gens bool |
| 835 | want string |
| 836 | }{ |
| 837 | {"nested, generation 0", false, "tier startexe " + childrenMax}, |
| 838 | {"nested, generation n", true, "tier startgenexe " + childrenMax}, |
| 839 | } { |
| 840 | t.Run(tc.name, func(t *testing.T) { |
| 841 | ctl := &fakeControl{inv: "inv1"} |
| 842 | unit := &fakeUnit{} |
| 843 | b, rt := nestedBackend(t, ctl, unit) |
| 844 | b.Control = identityControl{ctl} |
| 845 | b.GenContainment = tc.gens |
| 846 | tierFollowsTheLink(b, "vA") |
| 847 | stageVersion(t, b, rt.Host, "vA", "exit 0\n") |
| 848 | id, err := b.LiveIdentity(context.Background(), rt) |
| 849 | if err != nil { |
| 850 | t.Fatal(err) |
| 851 | } |
| 852 | stageVersion(t, b, rt.Host, "vB", "exit 0\n") |
| 853 | sock := rt.Socket |
| 854 | if tc.gens { |
| 855 | sock = nestedGenSocketIn(b.pathsFor(rt).runDir, 6) |
| 856 | } |
| 857 | var stopServing func() |
| 858 | ctl.onStart = func() { stopServing = serveOK(t, sock) } |
| 859 | defer func() { |
| 860 | if stopServing != nil { |
| 861 | stopServing() |
| 862 | } |
| 863 | }() |
| 864 | unit.set("active", "inv1", true, true) |
| 865 | p, g, err := b.StartGenExe(context.Background(), rt, 6, tc.gens, id) |
| 866 | if err != nil { |
| 867 | t.Fatalf("StartGenExe: %v", err) |
| 868 | } |
| 869 | if !strings.Contains(ctl.joined(), tc.want) || g.Tier != cgroup.TierChildren { |
| 870 | t.Fatalf("root was asked:\n%s\nwant %q; handle tier %v", ctl.joined(), tc.want, g.Tier) |
| 871 | } |
| 872 | unit.set("inactive", "inv1", false, false) |
| 873 | waitFor(t, "the unit to exit", p.Exited) |
| 874 | }) |
| 875 | } |
| 876 | t.Run("nested seed", func(t *testing.T) { |
| 877 | ctl := &fakeControl{seed: instancectl.SeedResponse{InvocationID: "s1", Result: "success", Status: "0", Code: "exited"}} |
| 878 | b, rt := nestedBackend(t, ctl, &fakeUnit{}) |
| 879 | b.Control = identityControl{ctl} |
| 880 | tierFollowsTheLink(b, "vA") |
| 881 | rt.Seed = `["seed"]` |
| 882 | stageVersion(t, b, rt.Host, "vA", "exit 0\n") |
| 883 | id, err := b.LiveIdentity(context.Background(), rt) |
| 884 | if err != nil { |
| 885 | t.Fatal(err) |
| 886 | } |
| 887 | stageVersion(t, b, rt.Host, "vB", "exit 0\n") |
| 888 | if err := b.SeedExe(context.Background(), rt, id); err != nil { |
| 889 | t.Fatalf("SeedExe: %v", err) |
| 890 | } |
| 891 | if want := "tier seedexe " + childrenMax; !strings.Contains(ctl.joined(), want) { |
| 892 | t.Fatalf("root was asked:\n%s\nwant %q", ctl.joined(), want) |
| 893 | } |
| 894 | }) |
| 895 | } |
| 896 | |
| 897 | // A control channel that cannot name the recorded file refuses a nested |
| 898 | // generation-0 start before root is asked anything. That is proven not to |
| 899 | // have run: never start-uncertain, so it cannot become a failure record. |
| 900 | func TestAMissingStagedExeCapabilityIsNeverMayHaveRun(t *testing.T) { |
| 901 | ctl := &fakeControl{inv: "inv1"} |
| 902 | b, rt := nestedBackend(t, ctl, &fakeUnit{}) |
| 903 | stageVersion(t, b, rt.Host, "vA", "exit 0\n") |
| 904 | id, err := b.LiveIdentity(context.Background(), rt) |
| 905 | if err != nil { |
| 906 | t.Fatal(err) |
| 907 | } |
| 908 | proc, _, err := b.StartGenExe(context.Background(), rt, 1, false, id) |
| 909 | if !errors.Is(err, errNoStagedExeControl) { |
| 910 | t.Fatalf("StartGenExe = %v, want errNoStagedExeControl", err) |
| 911 | } |
| 912 | if startMayHaveRun(proc, err) { |
| 913 | t.Fatalf("a start root was never asked for reads as may-have-run: %v", err) |
| 914 | } |
| 915 | if ctl.joined() != "" { |
| 916 | t.Fatalf("root was asked:\n%s", ctl.joined()) |
| 917 | } |
| 918 | } |
| 919 | |