| 1 | package moves |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func stoppedWith(ok map[string]bool) StoppedReport { |
| 10 | r := StoppedReport{V: 1, Steps: map[string]Check{}} |
| 11 | for id, pass := range ok { |
| 12 | if pass { |
| 13 | r.Steps[id] = Check{OK: true} |
| 14 | } else { |
| 15 | r.Steps[id] = Check{Reason: "no"} |
| 16 | } |
| 17 | } |
| 18 | return r |
| 19 | } |
| 20 | |
| 21 | func TestStoppedReportTreeStoppedNeedsStepsOneToFour(t *testing.T) { |
| 22 | all := map[string]bool{StepTree: true, StepStray: true, StepFDs: true, StepLease: true} |
| 23 | if r := stoppedWith(all); !r.TreeStopped() || r.Complete() { |
| 24 | t.Fatalf("steps 1 to 4 OK: TreeStopped %v Complete %v, want true false", r.TreeStopped(), r.Complete()) |
| 25 | } |
| 26 | for _, step := range []string{StepTree, StepStray, StepFDs, StepLease} { |
| 27 | failed := map[string]bool{StepTree: true, StepStray: true, StepFDs: true, StepLease: true, StepReplica: true} |
| 28 | failed[step] = false |
| 29 | if r := stoppedWith(failed); r.TreeStopped() || r.Complete() { |
| 30 | t.Fatalf("%s failed but TreeStopped %v Complete %v", step, r.TreeStopped(), r.Complete()) |
| 31 | } |
| 32 | missing := map[string]bool{StepTree: true, StepStray: true, StepFDs: true, StepLease: true, StepReplica: true} |
| 33 | delete(missing, step) |
| 34 | if r := stoppedWith(missing); r.TreeStopped() || r.Complete() { |
| 35 | t.Fatalf("%s never ran but TreeStopped %v Complete %v", step, r.TreeStopped(), r.Complete()) |
| 36 | } |
| 37 | } |
| 38 | if (StoppedReport{}).TreeStopped() || (StoppedReport{}).Complete() { |
| 39 | t.Fatal("the zero report passed") |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestStoppedReportCompleteNeedsTheReplicaAndEveryStep(t *testing.T) { |
| 44 | all := map[string]bool{StepTree: true, StepStray: true, StepFDs: true, StepLease: true, StepReplica: true} |
| 45 | if r := stoppedWith(all); !r.Complete() || !r.TreeStopped() { |
| 46 | t.Fatalf("every step OK: Complete %v TreeStopped %v", r.Complete(), r.TreeStopped()) |
| 47 | } |
| 48 | all[StepReplica] = false |
| 49 | if r := stoppedWith(all); r.Complete() || !r.TreeStopped() { |
| 50 | t.Fatalf("replica failed: Complete %v TreeStopped %v, want false true", r.Complete(), r.TreeStopped()) |
| 51 | } |
| 52 | // A step this version does not know about still has to pass: Complete |
| 53 | // fails closed on anything a newer writer recorded as failed. |
| 54 | all[StepReplica] = true |
| 55 | all["later"] = false |
| 56 | if r := stoppedWith(all); r.Complete() { |
| 57 | t.Fatal("a failed extra step passed Complete") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestStoppedReportWireNames(t *testing.T) { |
| 62 | r := StoppedReport{V: 1, Role: "source", Box: "b", Incarnation: "i", ClaimGen: 7, Host: "h.example.com", OpID: "op", |
| 63 | Steps: map[string]Check{StepTree: {OK: true}}, Stray: []int{9}, FinalTXID: 12, |
| 64 | DBSHA256: "d", FilesPlain: "f", TimersSHA256: "t"} |
| 65 | raw, err := json.Marshal(r) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | for _, name := range []string{`"v":1`, `"role":"source"`, `"box":"b"`, `"incarnation":"i"`, `"claim_gen":7`, |
| 70 | `"host":"h.example.com"`, `"op_id":"op"`, `"steps":{"tree":{"ok":true,"reason":""}}`, `"stray":[9]`, |
| 71 | `"final_txid":12`, `"db_sha256":"d"`, `"files_plain":"f"`, `"timers_sha256":"t"`, `"checked_at":`} { |
| 72 | if !strings.Contains(string(raw), name) { |
| 73 | t.Fatalf("%s missing from %s", name, raw) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |