Merge the delete-procedure fixtures (signal stop and dependent snapshots)

This commit is contained in:
pingqiu committed 2026-11-26 09:07:17 -08:00
commit db01d4fc28
1 file changed
+184 -34
@@ -3,7 +3,9 @@
package main_test
import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
@@ -22,6 +24,113 @@ func (env *handoffEnv) volumeDelete(t *testing.T, request string) (string, strin
"--master", env.h.masterAddr, "--request-id", request)
}
// deletePastPrintedHorizon follows volume-lifecycle steps 2-4. The first
// refusal proves the lease is still live; the timestamp only schedules
// retries. The CLI verdict decides success. All retries keep the same id and
// the first T+5s deadline, even if another lease appears (which fails the test).
func (env *handoffEnv) deletePastPrintedHorizon(t *testing.T, volume, request string, requireLive bool) (string, string, int) {
t.Helper()
call := func(ctx context.Context) (string, string, int) {
out, errOut, code, err := runCLI(ctx, env.h, "ops", "chunk", "volume-delete", "--volume", volume,
"--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath,
"--master", env.h.masterAddr, "--request-id", request)
if err != nil {
t.Fatalf("volume-delete %s: %v", volume, err)
}
return out, errOut, code
}
first, stop := context.WithTimeout(context.Background(), 15*time.Second)
defer stop()
out, errOut, code := call(first)
until, err := deleteLeaseHorizon(out, code)
if err != nil {
if requireLive {
t.Fatalf("first delete must refuse the live lease: %v; %s %s", err, out, errOut)
}
return out, errOut, code
}
if wait := time.Until(until); wait < 0 || wait > 90*time.Second {
t.Fatalf("first refusal horizon must be upcoming and within 90s, got %s: %s", until, out)
}
ctx, cancel := context.WithDeadline(context.Background(), until.Add(5*time.Second))
defer cancel()
timer := time.NewTimer(time.Until(until))
defer timer.Stop()
for {
select {
case <-ctx.Done():
t.Fatalf("delete did not resolve by original horizon + 5s: %s %s", out, errOut)
case <-timer.C:
}
out, errOut, code = call(ctx)
next, err := deleteLeaseHorizon(out, code)
if err != nil {
return out, errOut, code
}
if !next.Equal(until) {
t.Fatalf("lease horizon changed from %s to %s; refusing to widen deadline", until, next)
}
timer.Reset(100 * time.Millisecond)
}
}
// deleteLeaseHorizon accepts only the documented typed CLI refusal, never
// a matching stderr fragment or a successful exit with an error-shaped body.
func deleteLeaseHorizon(out string, code int) (time.Time, error) {
var body struct {
Code string `json:"code"`
Message string `json:"message"`
}
if code != 1 {
return time.Time{}, fmt.Errorf("exit %d is not refusal", code)
}
if err := json.Unmarshal([]byte(out), &body); err != nil {
return time.Time{}, err
}
if body.Code != "not_admitted" {
return time.Time{}, fmt.Errorf("code %q is not not_admitted", body.Code)
}
_, rest, ok := strings.Cut(body.Message, "live until ")
if !ok {
return time.Time{}, fmt.Errorf("missing lease horizon")
}
fields := strings.Fields(rest)
if len(fields) == 0 {
return time.Time{}, fmt.Errorf("empty lease horizon")
}
return time.Parse(time.RFC3339Nano, strings.TrimSuffix(fields[0], ":"))
}
// The CLI error body, rather than a substring in combined output, is the
// authority for retry timing. Every refusal class stays distinguishable.
func TestDeleteLeaseHorizonRequiresTypedRefusal(t *testing.T) {
const valid = `{"code":"not_admitted","message":"workspace delete: live until 2026-11-26T13:00:00.125Z: lease expiry plus clock-skew margin has not elapsed"}`
for _, tc := range []struct {
name, out string
code int
ok bool
}{
{"typed horizon", valid, 1, true},
{"successful exit cannot refuse", valid, 0, false},
{"usage failure", valid, 2, false},
{"plain stderr is not evidence", "not_admitted: live until 2026-11-26T13:00:00.125Z", 1, false},
{"dependent refusal", `{"code":"snapshot_has_dependents","message":"snapshot s1"}`, 1, false},
{"uncertain lease", `{"code":"not_admitted","message":"restart required"}`, 1, false},
{"missing time", `{"code":"not_admitted","message":"live until "}`, 1, false},
{"invalid time", `{"code":"not_admitted","message":"live until tomorrow: refused"}`, 1, false},
} {
t.Run(tc.name, func(t *testing.T) {
until, err := deleteLeaseHorizon(tc.out, tc.code)
if (err == nil) != tc.ok {
t.Fatalf("horizon %s error %v, want valid=%v", until, err, tc.ok)
}
if tc.ok && until.Format(time.RFC3339Nano) != "2026-11-26T13:00:00.125Z" {
t.Fatalf("wrong horizon %s", until)
}
})
}
}
// requireDeleteCompleted checks step 4's documented state:"deleted" result
// (volume-lifecycle.md, "Delete a volume", step 4). Retries use the same
// request id and must complete again; the CLI reports no replayed flag.
@@ -62,18 +171,18 @@ func (env *handoffEnv) requireDeletedForGood(t *testing.T, request string) {
}
}
// TestProcedureDeleteAVolume: the page's steps. A volume with a snapshot is
// sealed, revoked (blockvolume sends the ACK and exits 0 by itself) and its
// ACK seen; volume-delete is refused while the snapshot exists, with nothing
// deleted; the snapshot is deleted; volume-delete then answers deleted,
// `chunk show` shows HEAD deleted and the pool is empty, and a rerun with
// the same request id completes again.
// TestProcedureDeleteAVolume: ACK does not bypass the lease horizon; a
// restored-volume dependent blocks deletion, while ordinary snapshots do not.
// Refusals preserve HEAD/pool; completion empties the pool and replays by id.
func TestProcedureDeleteAVolume(t *testing.T) {
env := startHandoffEnv(t, 1)
env := startHandoffEnvWith(t, 1, []string{"v2"})
holder := env.start(t, "blockvolume-s1", "s1")
e := env.adopt(t, holder, "s1")
env.writeBlock(t, "s1", 0, pattern("delete-a-volume!"))
env.snapshot(t, "s1", "v1", "s1")
env.snapshot(t, "s1", "v1", "plain")
env.h.command(t, "ops", "chunk", "restore", "--master", env.h.masterAddr, "--source", "v1",
"--snapshot", "s1", "--target", "v2", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath)
g := env.seal(t, "s1", e)
env.revoke(t, e, g)
@@ -83,53 +192,94 @@ func TestProcedureDeleteAVolume(t *testing.T) {
env.waitAck(t, e, g)
state0, n0 := env.headShown(t)
out, errOut, code := env.volumeDelete(t, "del-v1")
if code == 0 || !strings.Contains(out+errOut, "has snapshots (s1)") {
t.Fatalf("volume-delete with a snapshot: exit %d %s %s; want refused, naming s1", code, out, errOut)
out, errOut, code := env.deletePastPrintedHorizon(t, "v1", "del-v1", true)
var refusal struct {
Code string `json:"code"`
Message string `json:"message"`
}
if err := json.Unmarshal([]byte(out), &refusal); err != nil || code != 1 || refusal.Code != "snapshot_has_dependents" || !strings.Contains(refusal.Message, `snapshot "s1"`) {
t.Fatalf("volume-delete with a dependent: exit %d %s %s; want typed snapshot_has_dependents naming s1", code, out, errOut)
}
if state, n := env.headShown(t); state != "detached" || state != state0 || n != n0 || n == 0 {
t.Fatalf("after the refused delete HEAD is %q with %d chunks; want it as before (%q, %d chunks): nothing deleted", state, n, state0, n0)
}
env.deleteSnapshot(t, "v1", "s1")
// v2 never started; delete it through the public API (and its own horizon,
// if the master granted a line). This releases s1's dependent.
out, errOut, code = env.deletePastPrintedHorizon(t, "v2", "del-v2", false)
requireDeleteCompleted(t, out, errOut, code)
if state, n := env.headOf(t, "v2"), env.poolObjects(t, "v2"); !strings.Contains(state, `"state":"deleted"`) || n != 0 {
t.Fatalf("restored target not deleted: HEAD %s pool %d", state, n)
}
// Both snapshots still exist, now without dependents: plain snapshots
// must be removed by the volume delete itself, not by this fixture.
snapshots, _ := env.h.command(t, "ops", "chunk", "snapshots", "--master", env.h.masterAddr,
"--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath)
var names []struct {
Name string `json:"name"`
Dependents []string `json:"dependents"`
}
if err := json.Unmarshal([]byte(snapshots), &names); err != nil {
t.Fatal(err)
}
seen := map[string]bool{}
for _, snap := range names {
if len(snap.Dependents) != 0 {
t.Fatalf("dependent not released: %s", snapshots)
}
seen[snap.Name] = true
}
if !seen["s1"] || !seen["plain"] {
t.Fatalf("plain snapshots disappeared before volume-delete: %s", snapshots)
}
out, errOut, code = env.volumeDelete(t, "del-v1")
requireDeleteCompleted(t, out, errOut, code)
env.requireDeletedForGood(t, "del-v1")
}
// TestProcedureDeleteAfterASignalStop: the page's recovery. The volume is
// sealed and blockvolume is then stopped with SIGTERM, so the ACK is
// skipped: volume-delete is refused (live authority requires a clean
// revocation ACK) and nothing is deleted. blockvolume is started again with
// the same flags and local root; it is revoked and sends the ACK (exit 0 by
// itself); volume-delete then completes, HEAD is deleted with no chunks, and
// a rerun with the same request id completes again.
// TestProcedureDeleteAfterASignalStop: a sealed holder stopped by SIGTERM
// stays down. The first delete refuses the live lease and changes nothing;
// the same request completes after its printed horizon without a restart or
// ACK. A retry completes again, and a late start cannot claim or serve.
func TestProcedureDeleteAfterASignalStop(t *testing.T) {
env := startHandoffEnv(t, 1)
holder := env.start(t, "blockvolume-s1", "s1")
e := env.adopt(t, holder, "s1")
env.writeBlock(t, "s1", 0, pattern("delete-after-sig"))
g := env.seal(t, "s1", e)
env.seal(t, "s1", e)
holder.term(t, 30*time.Second)
stopped := func() {
t.Helper()
select {
case <-holder.done:
default:
t.Fatal("signal-stopped holder is still running")
}
if listening(env.hosts["s1"].status) {
t.Fatal("signal-stopped holder listened again before deletion")
}
}
stopped()
state0, n0 := env.headShown(t)
out, errOut, code := env.volumeDelete(t, "del-v1")
if code == 0 || !strings.Contains(out+errOut, "live authority requires clean revocation ACK") {
t.Fatalf("volume-delete after a signal stop: exit %d %s %s; want refused for the missing ACK", code, out, errOut)
if _, err := deleteLeaseHorizon(out, code); err != nil {
t.Fatalf("volume-delete after signal stop: exit %d %s %s; want typed live-until refusal: %v", code, out, errOut, err)
}
if state, n := env.headShown(t); state != "detached" || state != state0 || n != n0 || n == 0 {
t.Fatalf("after the refused delete HEAD is %q with %d chunks; want it as before (%q, %d chunks): nothing deleted", state, n, state0, n0)
t.Fatalf("refused delete changed HEAD/pool: %q %d; before %q %d", state, n, state0, n0)
}
again := env.start(t, "blockvolume-s1-again", "s1")
poll(t, 60*time.Second, "the restarted holder never listened on its status address", func() bool {
return listening(env.hosts["s1"].status)
})
env.revoke(t, e, g)
if err := again.wait(60 * time.Second); err != nil {
t.Fatalf("the restarted s1 after the revocation: %v; want a clean exit by itself", err)
}
env.waitAck(t, e, g)
out, errOut, code = env.volumeDelete(t, "del-v1")
stopped()
out, errOut, code = env.deletePastPrintedHorizon(t, "v1", "del-v1", true)
requireDeleteCompleted(t, out, errOut, code)
stopped()
env.requireDeletedForGood(t, "del-v1")
late := env.start(t, "blockvolume-s1-late", "s1")
err := late.wait(60 * time.Second)
served := late.has("blockvolume: backend admits I/O: starting the frontends") || volumeFrontendReady(env.hosts["s1"].status, "v1")
keepFullLog(t, late)
if err == nil || strings.Contains(fmt.Sprint(err), "still running") || served {
t.Fatalf("late start after deletion must exit without serving: exit=%v served=%v", err, served)
}
if state, n := env.headShown(t); state != "deleted" || n != 0 {
t.Fatalf("late start changed deleted HEAD/pool: %q %d", state, n)
}
}