| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "github.com/carlosframework/platform/internal/hostbox" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestPlan11RootWriterClosureSurvivesAdmissionRefusal(t *testing.T) { |
| 15 | f := newLaunchFixture(t, servingOp()) |
| 16 | service, err := hostbox.NewJournalService(f.journal) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | root := ClaimRootHTTP{Journals: service, Stop: ClaimStopHTTP{Coordinator: ClaimStopCoordinator{Guard: f.root.Guard}}, RequireNewWork: func(context.Context) error { return errors.New("no new work") }} |
| 21 | root.Reconcile.Box = f.target.Box |
| 22 | root.Reconcile.Enrollment = f.target.Enrollment |
| 23 | var q hostbox.JournalRequest |
| 24 | remote, _ := hostbox.RemoteJournals(func(_ context.Context, prepared hostbox.JournalRequest) (hostbox.JournalResponse, error) { |
| 25 | q = prepared |
| 26 | return hostbox.JournalResponse{V: 1, ID: prepared.ID}, nil |
| 27 | }) |
| 28 | if err := remote.CloseWriters(t.Context(), f.target); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | raw, err := json.Marshal(q) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | wrong := f.peer |
| 36 | wrong.ControllerID = "other-controller" |
| 37 | w := httptest.NewRecorder() |
| 38 | root.serveJournal(w, httptest.NewRequest(http.MethodPost, "/claim/journal", bytes.NewReader(raw)), wrong) |
| 39 | got, err := f.journal.Read(t.Context(), f.target) |
| 40 | if err != nil || got.WritersClosed { |
| 41 | t.Fatal("foreign controller changed the closure") |
| 42 | } |
| 43 | w = httptest.NewRecorder() |
| 44 | root.serveJournal(w, httptest.NewRequest(http.MethodPost, "/claim/journal", bytes.NewReader(raw)), f.peer) |
| 45 | var out hostbox.JournalResponse |
| 46 | if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &out) != nil || out.Error != "" { |
| 47 | t.Fatalf("closure blocked by new-work gate: %d %s", w.Code, w.Body.String()) |
| 48 | } |
| 49 | got, err = f.journal.Read(t.Context(), f.target) |
| 50 | if err != nil || !got.WritersClosed { |
| 51 | t.Fatal("closure not recorded") |
| 52 | } |
| 53 | } |
| 54 | |