package host import ( "github.com/carlosframework/platform/internal/cgroup" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/hostwrite" "github.com/carlosframework/platform/internal/instancectl" "github.com/carlosframework/platform/internal/provision" "strings" "testing" "time" ) func TestPlan11Rev2RootRefusesInventedAttempt(t *testing.T) { for _, field := range []string{"id", "invocation", "inputs", "missing", "settled"} { t.Run(field, func(t *testing.T) { f := newLaunchFixture(t, servingOp()) switch field { case "id": f.req.Reservation.ID = "invented:main" case "invocation": f.req.Reservation.InvocationID = "invented" case "inputs": f.req.Reservation.Inputs = "invented" case "missing": f.claims.set(func(c *hostclaim.Claim) { c.Holder.WakeRetry = nil }) case "settled": f.claims.set(func(c *hostclaim.Claim) { c.Holder.WakeRetry = &hostclaim.WakeRetry{EpisodeID: "old", State: "open"} }) } if _, err := f.root.Serve(t.Context(), f.peer, f.req); err == nil { t.Fatal("root launched without its authoritative attempt") } f.noReservation(t, field) }) } } func TestPlan11Rev2RootQuietPeriod(t *testing.T) { f := newLaunchFixture(t, servingOp()) f.claims.set(func(c *hostclaim.Claim) { c.QuietAfter = map[string]time.Time{"eu": time.Now().Add(time.Hour)} }) if _, err := f.root.Serve(t.Context(), f.peer, f.req); err == nil { t.Fatal("root launched during the regional quiet period") } f.noReservation(t, "quiet") } type rev2SlowKernel struct { *launchKernel calls int } func (k *rev2SlowKernel) Observe(s instancectl.ClaimUnitStarted) (ClaimLaunchObserved, error) { k.calls++ if k.calls < 4 { return ClaimLaunchObserved{ProcessStart: k.observed.ProcessStart, Group: k.observed.Group}, nil } return k.launchKernel.Observe(s) } func TestPlan11Rev2SlowValidStart(t *testing.T) { f := newLaunchFixture(t, servingOp()) k := &rev2SlowKernel{launchKernel: f.kernel} f.root.Kernel = k if _, err := f.root.Serve(t.Context(), f.peer, f.req); err != nil { t.Fatalf("slow valid start was killed: %v", err) } starts, stops := f.units.counts() if starts != 1 || stops != 0 || k.calls != 4 { t.Fatalf("starts=%d stops=%d observations=%d", starts, stops, k.calls) } } func TestPlan11Rev2ShapeBeforeReservation(t *testing.T) { host := "shop.example" a := &ClaimRoleAssembly{Root: fakeLaunchRoot{protocol: hostbox.RoleLaunchProtocol}, ManagedRoots: map[string]string{"data": "/data"}} 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"}} for _, args := range [][]string{{"--db", in.DBPath}, {"--socket", "/run/other.sock", "--db", in.DBPath}, {"--socket", provision.NestedSocket(host), "--db", in.DBPath, "--exec", "/bin/sh"}} { in.Args = args if err := a.PreflightLaunch(host, in); err == nil { t.Fatalf("malformed shape passed prerequisites: %v", args) } } }