carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostclaim
2
3import (
4 "errors"
5 "testing"
6)
7
8func TestRequireOpRefusesStale(t *testing.T) {
9 c := &Claim{Op: Op{Kind: KindDeleting, ID: "op_new"}}
10 if err := RequireOp("op_old")(c); !errors.Is(err, ErrStaleOp) {
11 t.Fatalf("stale op = %v", err)
12 }
13 if err := RequireOp("op_new")(c); err != nil {
14 t.Fatalf("current op = %v", err)
15 }
16 if err := RequireOp("")(c); !errors.Is(err, ErrStaleOp) {
17 t.Fatalf("empty id must never match: %v", err)
18 }
19}
20
21func TestStartGuard(t *testing.T) {
22 h := &Holder{Incarnation: "i"}
23 if err := StartGuard(&Claim{Holder: h, Op: Op{Kind: KindDeleting, ID: "x"}}, nil, nil, ""); !errors.Is(err, ErrWrongState) {
24 t.Fatalf("start over a running op = %v", err)
25 }
26 busy := &Claim{Holder: h, Op: Op{Kind: KindServing}, Obligations: []Obligation{{Kind: "park-replica", Region: "uk"}}}
27 if err := StartGuard(busy, []string{"uk"}, nil, ""); !errors.Is(err, ErrPendingChore) {
28 t.Fatalf("start over a pending chore = %v", err)
29 }
30 if err := StartGuard(busy, []string{"ie"}, nil, ""); err != nil {
31 t.Fatalf("start elsewhere = %v", err)
32 }
33}
34
35func TestStartGuardLocalBox(t *testing.T) {
36 busy := &Claim{Holder: &Holder{Incarnation: "i"}, Op: Op{Kind: KindServing},
37 Obligations: []Obligation{{Kind: "park-local", Box: "B0"}}}
38 for _, tc := range []struct {
39 name string
40 boxes []string
41 want error
42 }{
43 {name: "overlap", boxes: []string{"B0"}, want: ErrPendingChore},
44 {name: "elsewhere", boxes: []string{"B2"}},
45 {name: "no boxes"},
46 } {
47 t.Run(tc.name, func(t *testing.T) {
48 if err := StartGuard(busy, nil, tc.boxes, ""); !errors.Is(err, tc.want) {
49 t.Fatalf("start with boxes %v = %v, want %v", tc.boxes, err, tc.want)
50 }
51 })
52 }
53}
54