github.imxd.top/hashicorp/consul@v1.4.5/agent/consul/state/autopilot_test.go (about) 1 package state 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/consul/agent/consul/autopilot" 9 "github.com/pascaldekloe/goe/verify" 10 ) 11 12 func TestStateStore_Autopilot(t *testing.T) { 13 s := testStateStore(t) 14 15 expected := &autopilot.Config{ 16 CleanupDeadServers: true, 17 LastContactThreshold: 5 * time.Second, 18 MaxTrailingLogs: 500, 19 ServerStabilizationTime: 100 * time.Second, 20 RedundancyZoneTag: "az", 21 DisableUpgradeMigration: true, 22 UpgradeVersionTag: "build", 23 } 24 25 if err := s.AutopilotSetConfig(0, expected); err != nil { 26 t.Fatal(err) 27 } 28 29 idx, config, err := s.AutopilotConfig() 30 if err != nil { 31 t.Fatal(err) 32 } 33 if idx != 0 { 34 t.Fatalf("bad: %d", idx) 35 } 36 if !reflect.DeepEqual(expected, config) { 37 t.Fatalf("bad: %#v, %#v", expected, config) 38 } 39 } 40 41 func TestStateStore_AutopilotCAS(t *testing.T) { 42 s := testStateStore(t) 43 44 expected := &autopilot.Config{ 45 CleanupDeadServers: true, 46 } 47 48 if err := s.AutopilotSetConfig(0, expected); err != nil { 49 t.Fatal(err) 50 } 51 if err := s.AutopilotSetConfig(1, expected); err != nil { 52 t.Fatal(err) 53 } 54 55 // Do a CAS with an index lower than the entry 56 ok, err := s.AutopilotCASConfig(2, 0, &autopilot.Config{ 57 CleanupDeadServers: false, 58 }) 59 if ok || err != nil { 60 t.Fatalf("expected (false, nil), got: (%v, %#v)", ok, err) 61 } 62 63 // Check that the index is untouched and the entry 64 // has not been updated. 65 idx, config, err := s.AutopilotConfig() 66 if err != nil { 67 t.Fatal(err) 68 } 69 if idx != 1 { 70 t.Fatalf("bad: %d", idx) 71 } 72 if !config.CleanupDeadServers { 73 t.Fatalf("bad: %#v", config) 74 } 75 76 // Do another CAS, this time with the correct index 77 ok, err = s.AutopilotCASConfig(2, 1, &autopilot.Config{ 78 CleanupDeadServers: false, 79 }) 80 if !ok || err != nil { 81 t.Fatalf("expected (true, nil), got: (%v, %#v)", ok, err) 82 } 83 84 // Make sure the config was updated 85 idx, config, err = s.AutopilotConfig() 86 if err != nil { 87 t.Fatal(err) 88 } 89 if idx != 2 { 90 t.Fatalf("bad: %d", idx) 91 } 92 if config.CleanupDeadServers { 93 t.Fatalf("bad: %#v", config) 94 } 95 } 96 97 func TestStateStore_Autopilot_Snapshot_Restore(t *testing.T) { 98 s := testStateStore(t) 99 before := &autopilot.Config{ 100 CleanupDeadServers: true, 101 } 102 if err := s.AutopilotSetConfig(99, before); err != nil { 103 t.Fatal(err) 104 } 105 106 snap := s.Snapshot() 107 defer snap.Close() 108 109 after := &autopilot.Config{ 110 CleanupDeadServers: false, 111 } 112 if err := s.AutopilotSetConfig(100, after); err != nil { 113 t.Fatal(err) 114 } 115 116 snapped, err := snap.Autopilot() 117 if err != nil { 118 t.Fatalf("err: %s", err) 119 } 120 verify.Values(t, "", before, snapped) 121 122 s2 := testStateStore(t) 123 restore := s2.Restore() 124 if err := restore.Autopilot(snapped); err != nil { 125 t.Fatalf("err: %s", err) 126 } 127 restore.Commit() 128 129 idx, res, err := s2.AutopilotConfig() 130 if err != nil { 131 t.Fatalf("err: %s", err) 132 } 133 if idx != 99 { 134 t.Fatalf("bad index: %d", idx) 135 } 136 verify.Values(t, "", before, res) 137 }