package host import ( "context" "errors" "os" "path/filepath" "testing" "time" "golang.org/x/sys/unix" "github.com/carlosframework/platform/internal/release" ) // Settings plan 4, Astra round 2 finding 2 (ruling R-CARRY-FENCE): the // agent's carry (buildStates.carryRequest) takes the carry fence only once // the row's move hold is effective, and holds it across the carry, so a root // publication in flight lands before the carry reads the file and is // carried. // rootHoldsFence takes root's side of host's fence as root's publication // does (the lock file created, flock exclusive) and returns its release. func rootHoldsFence(t *testing.T, optDir, host string) func() { t.Helper() if err := os.MkdirAll(filepath.Join(optDir, "consumed"), 0o755); err != nil { t.Fatal(err) } f, err := os.OpenFile(release.ConsumedLockPath(optDir, host), os.O_RDWR|os.O_CREATE, 0o644) if err != nil { t.Fatal(err) } if err := unix.Flock(int(f.Fd()), unix.LOCK_EX); err != nil { t.Fatal(err) } return func() { _ = unix.Flock(int(f.Fd()), unix.LOCK_UN); _ = f.Close() } } // Root holds the fence and is about to publish R1 completed, failed; the // cut, its row held moving, asks for the carry: it waits for root, then // carries the record root published. func TestTheCarryWaitsForRootsPublicationAndCarriesIt(t *testing.T) { t.Parallel() b, s, reg := testBuildStates(t) ctx := context.Background() if err := put(t, b, ctx, astraHost, release.BuildState{LastExecuted: "A", Resolved: &release.ResolvedRequest{ID: 1, Target: "T"}}); err != nil { t.Fatal(err) } if err := reg.SetStatus(astraHost, "moving"); err != nil { t.Fatal(err) } unlock := rootHoldsFence(t, b.optDir, astraHost) done := make(chan error, 1) go func() { done <- b.carryRequest(ctx, astraHost, "hib", "inst/"+astraHost) }() select { case err := <-done: unlock() t.Fatalf("the carry ran while root held the fence: %v", err) case <-time.After(200 * time.Millisecond): } writeConsumed(t, b, release.Consumed{Resolved: &release.ResolvedRequest{ID: 1, Target: "T"}, Completed: 1, CompletedFailed: true}) unlock() select { case err := <-done: if err != nil { t.Fatal(err) } case <-time.After(10 * time.Second): t.Fatal("the carry never took the fence") } st, _, err := getState(ctx, s, release.BuildStateKey("inst/"+astraHost), astraHost) if err != nil || st == nil || st.Completed != 1 || !st.CompletedFailed || st.LastExecuted != "A" { t.Fatalf("state %+v, %v; want R1 completed failed carried, A untouched", st, err) } } // A carry for a row the move does not hold is refused: root could still // publish after it. Nothing is written. func TestTheCarryRefusesARowNoMoveHolds(t *testing.T) { b, s, _ := testBuildStates(t) ctx := context.Background() writeConsumed(t, b, release.Consumed{Resolved: &release.ResolvedRequest{ID: 1, Target: "T"}, Completed: 1, CompletedFailed: true}) if err := b.carryRequest(ctx, astraHost, "hib", "inst/"+astraHost); err == nil { t.Fatal("a carry for a row no move holds went ahead") } if _, _, err := s.Get(ctx, release.BuildStateKey("inst/"+astraHost)); !errors.Is(err, os.ErrNotExist) { t.Fatalf("the refused carry wrote: %v", err) } } // No consumed directory: no record to carry and no lock to take; the agent // creates nothing there. func TestTheCarryWithNoConsumedDirectoryTakesNoLock(t *testing.T) { b, _, reg := testBuildStates(t) if err := reg.SetStatus(astraHost, "moving"); err != nil { t.Fatal(err) } if err := b.carryRequest(context.Background(), astraHost, "hib", "inst/"+astraHost); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(b.optDir, "consumed")); !errors.Is(err, os.ErrNotExist) { t.Fatalf("the carry created the consumed directory: %v", err) } }