| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "github.com/carlosframework/platform/internal/cgroup" |
| 5 | "github.com/carlosframework/platform/internal/hostbox" |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | "github.com/carlosframework/platform/internal/hostwrite" |
| 8 | "github.com/carlosframework/platform/internal/instancectl" |
| 9 | "github.com/carlosframework/platform/internal/provision" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | func TestPlan11Rev2RootRefusesInventedAttempt(t *testing.T) { |
| 16 | for _, field := range []string{"id", "invocation", "inputs", "missing", "settled"} { |
| 17 | t.Run(field, func(t *testing.T) { |
| 18 | f := newLaunchFixture(t, servingOp()) |
| 19 | switch field { |
| 20 | case "id": |
| 21 | f.req.Reservation.ID = "invented:main" |
| 22 | case "invocation": |
| 23 | f.req.Reservation.InvocationID = "invented" |
| 24 | case "inputs": |
| 25 | f.req.Reservation.Inputs = "invented" |
| 26 | case "missing": |
| 27 | f.claims.set(func(c *hostclaim.Claim) { c.Holder.WakeRetry = nil }) |
| 28 | case "settled": |
| 29 | f.claims.set(func(c *hostclaim.Claim) { c.Holder.WakeRetry = &hostclaim.WakeRetry{EpisodeID: "old", State: "open"} }) |
| 30 | } |
| 31 | if _, err := f.root.Serve(t.Context(), f.peer, f.req); err == nil { |
| 32 | t.Fatal("root launched without its authoritative attempt") |
| 33 | } |
| 34 | f.noReservation(t, field) |
| 35 | }) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestPlan11Rev2RootQuietPeriod(t *testing.T) { |
| 40 | f := newLaunchFixture(t, servingOp()) |
| 41 | f.claims.set(func(c *hostclaim.Claim) { c.QuietAfter = map[string]time.Time{"eu": time.Now().Add(time.Hour)} }) |
| 42 | if _, err := f.root.Serve(t.Context(), f.peer, f.req); err == nil { |
| 43 | t.Fatal("root launched during the regional quiet period") |
| 44 | } |
| 45 | f.noReservation(t, "quiet") |
| 46 | } |
| 47 | |
| 48 | type rev2SlowKernel struct { |
| 49 | *launchKernel |
| 50 | calls int |
| 51 | } |
| 52 | |
| 53 | func (k *rev2SlowKernel) Observe(s instancectl.ClaimUnitStarted) (ClaimLaunchObserved, error) { |
| 54 | k.calls++ |
| 55 | if k.calls < 4 { |
| 56 | return ClaimLaunchObserved{ProcessStart: k.observed.ProcessStart, Group: k.observed.Group}, nil |
| 57 | } |
| 58 | return k.launchKernel.Observe(s) |
| 59 | } |
| 60 | |
| 61 | func TestPlan11Rev2SlowValidStart(t *testing.T) { |
| 62 | f := newLaunchFixture(t, servingOp()) |
| 63 | k := &rev2SlowKernel{launchKernel: f.kernel} |
| 64 | f.root.Kernel = k |
| 65 | if _, err := f.root.Serve(t.Context(), f.peer, f.req); err != nil { |
| 66 | t.Fatalf("slow valid start was killed: %v", err) |
| 67 | } |
| 68 | starts, stops := f.units.counts() |
| 69 | if starts != 1 || stops != 0 || k.calls != 4 { |
| 70 | t.Fatalf("starts=%d stops=%d observations=%d", starts, stops, k.calls) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestPlan11Rev2ShapeBeforeReservation(t *testing.T) { |
| 75 | host := "shop.example" |
| 76 | a := &ClaimRoleAssembly{Root: fakeLaunchRoot{protocol: hostbox.RoleLaunchProtocol}, ManagedRoots: map[string]string{"data": "/data"}} |
| 77 | in := ClaimLaunchInputs{Roles: []string{"main"}, DBPath: provision.NestedDBPath("/data", host), Limit: cgroup.TierDefault, Executable: "/opt/carlos/versions/v1/app", ExecutableSHA256: strings.Repeat("a", 64), Identity: hostwrite.Identity{Config: "config", Release: "release"}, Env: []string{"CARLOS_ADMIN_TOKEN=test"}} |
| 78 | for _, args := range [][]string{{"--db", in.DBPath}, {"--socket", "/run/other.sock", "--db", in.DBPath}, {"--socket", provision.NestedSocket(host), "--db", in.DBPath, "--exec", "/bin/sh"}} { |
| 79 | in.Args = args |
| 80 | if err := a.PreflightLaunch(host, in); err == nil { |
| 81 | t.Fatalf("malformed shape passed prerequisites: %v", args) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |