package hostclaim import ( "errors" "testing" ) func TestRequireOpRefusesStale(t *testing.T) { c := &Claim{Op: Op{Kind: KindDeleting, ID: "op_new"}} if err := RequireOp("op_old")(c); !errors.Is(err, ErrStaleOp) { t.Fatalf("stale op = %v", err) } if err := RequireOp("op_new")(c); err != nil { t.Fatalf("current op = %v", err) } if err := RequireOp("")(c); !errors.Is(err, ErrStaleOp) { t.Fatalf("empty id must never match: %v", err) } } func TestStartGuard(t *testing.T) { h := &Holder{Incarnation: "i"} if err := StartGuard(&Claim{Holder: h, Op: Op{Kind: KindDeleting, ID: "x"}}, nil, nil, ""); !errors.Is(err, ErrWrongState) { t.Fatalf("start over a running op = %v", err) } busy := &Claim{Holder: h, Op: Op{Kind: KindServing}, Obligations: []Obligation{{Kind: "park-replica", Region: "uk"}}} if err := StartGuard(busy, []string{"uk"}, nil, ""); !errors.Is(err, ErrPendingChore) { t.Fatalf("start over a pending chore = %v", err) } if err := StartGuard(busy, []string{"ie"}, nil, ""); err != nil { t.Fatalf("start elsewhere = %v", err) } } func TestStartGuardLocalBox(t *testing.T) { busy := &Claim{Holder: &Holder{Incarnation: "i"}, Op: Op{Kind: KindServing}, Obligations: []Obligation{{Kind: "park-local", Box: "B0"}}} for _, tc := range []struct { name string boxes []string want error }{ {name: "overlap", boxes: []string{"B0"}, want: ErrPendingChore}, {name: "elsewhere", boxes: []string{"B2"}}, {name: "no boxes"}, } { t.Run(tc.name, func(t *testing.T) { if err := StartGuard(busy, nil, tc.boxes, ""); !errors.Is(err, tc.want) { t.Fatalf("start with boxes %v = %v, want %v", tc.boxes, err, tc.want) } }) } }