github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/state/autopilot_test.go (about) 1 package state 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/nomad/nomad/structs" 9 ) 10 11 func TestStateStore_Autopilot(t *testing.T) { 12 s := testStateStore(t) 13 14 expected := &structs.AutopilotConfig{ 15 CleanupDeadServers: true, 16 LastContactThreshold: 5 * time.Second, 17 MaxTrailingLogs: 500, 18 MinQuorum: 3, 19 ServerStabilizationTime: 100 * time.Second, 20 EnableRedundancyZones: true, 21 DisableUpgradeMigration: true, 22 EnableCustomUpgrades: true, 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 := &structs.AutopilotConfig{ 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, &structs.AutopilotConfig{ 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, &structs.AutopilotConfig{ 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 }