| 1 | package instancectl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestPlan11Rev2UnitFinalCheckAndSnapshot(t *testing.T) { |
| 14 | for _, deny := range []bool{false, true} { |
| 15 | t.Run(map[bool]string{true: "denied", false: "allowed"}[deny], func(t *testing.T) { |
| 16 | u, ctl, in, _ := claimUnitFixture(t) |
| 17 | u.UnitRoot, u.DataRoot = t.TempDir(), t.TempDir() |
| 18 | if err := os.Chmod(u.UnitRoot, 0700); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | launch := Launch{Schema: LaunchSchema, Host: in.Host, Argv: []string{LiveDir + "/" + in.Host, "--socket", "/run/app.sock", "--db", "/data/app.db"}, Env: []string{"TOKEN=secret"}} |
| 22 | checks := 0 |
| 23 | _, err := u.StartChecked(t.Context(), in, launch, func() error { |
| 24 | checks++ |
| 25 | source, dropin, e := u.launchPaths(in.Host) |
| 26 | if e != nil { |
| 27 | t.Fatal(e) |
| 28 | } |
| 29 | body, e := os.ReadFile(filepath.Join(source, "launch.json")) |
| 30 | if e != nil { |
| 31 | t.Fatal(e) |
| 32 | } |
| 33 | var got Launch |
| 34 | if e = json.Unmarshal(body, &got); e != nil || strings.Join(got.Env, ",") != "TOKEN=secret" { |
| 35 | t.Fatalf("snapshot %s %v", body, e) |
| 36 | } |
| 37 | body, e = os.ReadFile(filepath.Join(dropin, "60-claim-launch.conf")) |
| 38 | if e != nil || !strings.Contains(string(body), "BindReadOnlyPaths="+filepath.Join(source, "launch.json")) { |
| 39 | t.Fatalf("binding: %s %v", body, e) |
| 40 | } |
| 41 | if strings.Contains(ctl.joined(), "start ") { |
| 42 | t.Fatal("check ran after start") |
| 43 | } |
| 44 | if deny { |
| 45 | return errors.New("expired") |
| 46 | } |
| 47 | return nil |
| 48 | }) |
| 49 | if checks != 1 { |
| 50 | t.Fatalf("checks=%d err=%v", checks, err) |
| 51 | } |
| 52 | if deny { |
| 53 | if err == nil || strings.Contains(ctl.joined(), "start ") { |
| 54 | t.Fatal("denial launched") |
| 55 | } |
| 56 | return |
| 57 | } |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if err = u.Stop(context.Background(), in.Host); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | source, dropin, _ := u.launchPaths(in.Host) |
| 65 | for _, p := range []string{filepath.Join(source, "launch.json"), filepath.Join(dropin, "60-claim-launch.conf")} { |
| 66 | if _, e := os.Stat(p); !errors.Is(e, os.ErrNotExist) { |
| 67 | t.Fatalf("snapshot outlived stopped unit: %s %v", p, e) |
| 68 | } |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestPlan11Rev2UnitSameInvocation(t *testing.T) { |
| 75 | u, ctl, in, _ := claimUnitFixture(t) |
| 76 | s, err := u.Start(t.Context(), in) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | if err = u.Same(t.Context(), s); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | ctl.invocation = "replacement" |
| 84 | if err = u.Same(t.Context(), s); err == nil { |
| 85 | t.Fatal("replacement accepted") |
| 86 | } |
| 87 | ctl.invocation = s.InvocationID |
| 88 | ctl.pid++ |
| 89 | if err = u.Same(t.Context(), s); err == nil { |
| 90 | t.Fatal("replacement pid accepted") |
| 91 | } |
| 92 | } |
| 93 | |