//go:build linux package hostexec import ( "context" "encoding/json" "os" "testing" "github.com/carlosframework/platform/internal/rootparticipation" ) func TestPlan14ExecutionServingView(t *testing.T) { for _, scenario := range []string{"empty", "settled", "active", "lost", "missing census", "incomplete census", "wrong migration", "wrong enrollment", "update", "wrong kind", "participation only", "dead peer"} { t.Run(scenario, func(t *testing.T) { b, disk, remote, _, q, peer := brokerFixture(t) if scenario == "settled" || scenario == "active" || scenario == "lost" { out, err := b.Execute(t.Context(), peer, q) if err != nil { t.Fatal(err) } if scenario == "settled" { settle := q settle.Operation, settle.ID, settle.Token = "executionSettle", "settle", out.Token if _, err := b.Execute(t.Context(), peer, settle); err != nil { t.Fatal(err) } } else if scenario == "lost" { r, _ := b.read(q.Host) if err := b.deny(t.Context(), r, nil); err != nil { t.Fatal(err) } } } census := rootparticipation.Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 1, Complete: true} if scenario == "incomplete census" { census.Complete = false } if scenario == "wrong migration" { census.Migration++ } if scenario == "wrong enrollment" { census.Enrollment = "other" } if scenario != "missing census" { raw, _ := json.Marshal(census) if err := disk.Write("census.json", raw); err != nil { t.Fatal(err) } } if scenario == "update" { if err := disk.Write("update.json", []byte(`{}`)); err != nil { t.Fatal(err) } } original := b.cfg.Authority originalQ := q b.cfg.Authority = func(ctx context.Context, _ ExecutionRequest, p ControllerProcess) (ExecutionAuthority, error) { a, err := original(ctx, originalQ, p) a.Kind, a.Phase = "serving", "" if scenario == "wrong kind" { a.Kind = "landing" } if scenario == "participation only" { a.ParticipationOnly = true } return a, err } if scenario == "dead peer" { b.cfg.Alive = func(context.Context, ControllerProcess) error { return os.ErrNotExist } } q.Operation, q.ID, q.Token, q.Op = "executionView", "serving-view", "", "" writes, acquisitions := disk.writes, remote.calls out, err := b.Execute(t.Context(), peer, q) if scenario == "empty" || scenario == "settled" || scenario == "participation only" { if err != nil || out.Status != "serving" || out.Host != q.Host || out.Stamp != q.Stamp || out.Session != q.Session || out.Token != "" || out.SafeUntil != 0 || out.LeaseETag != "" || !out.Expires.IsZero() { t.Fatalf("serving view: %+v %v", out, err) } } else if err == nil { t.Fatalf("%s granted serving: %+v", scenario, out) } if disk.writes != writes || remote.calls != acquisitions { t.Fatal("serving view wrote participation or acquired a lease") } }) } } func TestPlan14ExecutionServingCannotAcquire(t *testing.T) { b, _, remote, _, q, peer := brokerFixture(t) original := b.cfg.Authority b.cfg.Authority = func(ctx context.Context, q ExecutionRequest, p ControllerProcess) (ExecutionAuthority, error) { a, err := original(ctx, q, p) a.Kind = "serving" return a, err } if _, err := b.Execute(t.Context(), peer, q); err == nil || remote.calls != 0 { t.Fatalf("serving acquired an operation lease: err=%v acquisitions=%d", err, remote.calls) } }