| 1 | package hostrelocate |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/hostclaim" |
| 10 | "github.com/carlosframework/platform/internal/hostexec" |
| 11 | ) |
| 12 | |
| 13 | type plan14TargetAcknowledgement struct{ *fakeBox } |
| 14 | |
| 15 | func (b plan14TargetAcknowledgement) Acknowledge(ctx context.Context, o hostclaim.Observed) (hostclaim.ExecutionAcknowledgement, error) { |
| 16 | if hostexec.PreinstallationParticipant(o.Claim, b.box, enrollment(b.box)) != nil { |
| 17 | return hostclaim.ExecutionAcknowledgement{}, ErrRefused |
| 18 | } |
| 19 | return b.fakeBox.Acknowledge(ctx, o) |
| 20 | } |
| 21 | |
| 22 | func TestPlan14PreinstallationTargetAcknowledgement(t *testing.T) { |
| 23 | for _, kind := range []string{"drain", "failover", "converted-landing"} { |
| 24 | t.Run(kind, func(t *testing.T) { |
| 25 | f := newFixture(t) |
| 26 | target, executor, next := "dst", "src", hostclaim.PhaseReleasing |
| 27 | if kind == "converted-landing" { |
| 28 | f.convertedLanding() |
| 29 | f.worker(nil) |
| 30 | target, executor, next = "spa", "none", hostclaim.PhasePinning |
| 31 | } else { |
| 32 | reason := hostclaim.ReasonDrain |
| 33 | if kind == "failover" { |
| 34 | f.terminate("src") |
| 35 | reason, executor, next = hostclaim.ReasonFailover, "none", hostclaim.PhasePinning |
| 36 | } |
| 37 | if _, err := f.w.Start(t.Context(), host, Request{Reason: reason, Source: "src", Target: target}); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | } |
| 41 | if kind == "drain" { |
| 42 | f.ack("src") |
| 43 | } |
| 44 | before := f.claim() |
| 45 | if before.Claim.Op.Phase != hostclaim.PhaseStarting || before.Claim.Op.Exec != executor || before.Claim.Op.Intent["target_box"] != target { |
| 46 | t.Fatalf("unexpected starting authority: %+v", before.Claim.Op) |
| 47 | } |
| 48 | box := &fakeBox{f: f, box: target} |
| 49 | p := Participant{Claims: f.s, Box: target, Enrollment: enrollment(target), Now: func() time.Time { return f.now }, Effects: plan14TargetAcknowledgement{box}} |
| 50 | for _, attack := range []string{"wrong phase", "unnamed box"} { |
| 51 | bad := *before.Claim |
| 52 | bad.Op = before.Claim.Op |
| 53 | bad.Op.Intent = map[string]string{} |
| 54 | for k, v := range before.Claim.Op.Intent { |
| 55 | bad.Op.Intent[k] = v |
| 56 | } |
| 57 | if attack == "wrong phase" { |
| 58 | bad.Op.Phase = hostclaim.PhaseRelocatingProbing |
| 59 | bad.Op.Exec = target |
| 60 | } else { |
| 61 | bad.Op.Intent["target_box"] = "elsewhere" |
| 62 | } |
| 63 | if _, err := (plan14TargetAcknowledgement{box}).Acknowledge(t.Context(), hostclaim.Observed{Host: host, Claim: &bad}); !errors.Is(err, ErrRefused) { |
| 64 | t.Fatalf("%s: %v", attack, err) |
| 65 | } |
| 66 | } |
| 67 | if after := f.claim(); after.ETag != before.ETag || box.opened != 0 || len(box.effects) != 0 || len(f.leases) != 0 { |
| 68 | t.Fatal("refusal had effects") |
| 69 | } |
| 70 | if _, err := p.Step(t.Context(), host); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if o := f.worker(nil); o.Claim.Op.Phase != next || o.Claim.Op.Exec != executor { |
| 74 | t.Fatalf("named-target acknowledgement did not advance without executor handoff: %+v", o.Claim.Op) |
| 75 | } |
| 76 | if box.opened != 1 || len(box.effects) != 0 { |
| 77 | t.Fatal("acknowledgement performed installation effects") |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |