github.com/manicqin/nomad@v0.9.5/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 ServerStabilizationTime: 100 * time.Second, 19 EnableRedundancyZones: true, 20 DisableUpgradeMigration: true, 21 EnableCustomUpgrades: true, 22 } 23 24 if err := s.AutopilotSetConfig(0, expected); err != nil { 25 t.Fatal(err) 26 } 27 28 idx, config, err := s.AutopilotConfig() 29 if err != nil { 30 t.Fatal(err) 31 } 32 if idx != 0 { 33 t.Fatalf("bad: %d", idx) 34 } 35 if !reflect.DeepEqual(expected, config) { 36 t.Fatalf("bad: %#v, %#v", expected, config) 37 } 38 } 39 40 func TestStateStore_AutopilotCAS(t *testing.T) { 41 s := testStateStore(t) 42 43 expected := &structs.AutopilotConfig{ 44 CleanupDeadServers: true, 45 } 46 47 if err := s.AutopilotSetConfig(0, expected); err != nil { 48 t.Fatal(err) 49 } 50 if err := s.AutopilotSetConfig(1, expected); err != nil { 51 t.Fatal(err) 52 } 53 54 // Do a CAS with an index lower than the entry 55 ok, err := s.AutopilotCASConfig(2, 0, &structs.AutopilotConfig{ 56 CleanupDeadServers: false, 57 }) 58 if ok || err != nil { 59 t.Fatalf("expected (false, nil), got: (%v, %#v)", ok, err) 60 } 61 62 // Check that the index is untouched and the entry 63 // has not been updated. 64 idx, config, err := s.AutopilotConfig() 65 if err != nil { 66 t.Fatal(err) 67 } 68 if idx != 1 { 69 t.Fatalf("bad: %d", idx) 70 } 71 if !config.CleanupDeadServers { 72 t.Fatalf("bad: %#v", config) 73 } 74 75 // Do another CAS, this time with the correct index 76 ok, err = s.AutopilotCASConfig(2, 1, &structs.AutopilotConfig{ 77 CleanupDeadServers: false, 78 }) 79 if !ok || err != nil { 80 t.Fatalf("expected (true, nil), got: (%v, %#v)", ok, err) 81 } 82 83 // Make sure the config was updated 84 idx, config, err = s.AutopilotConfig() 85 if err != nil { 86 t.Fatal(err) 87 } 88 if idx != 2 { 89 t.Fatalf("bad: %d", idx) 90 } 91 if config.CleanupDeadServers { 92 t.Fatalf("bad: %#v", config) 93 } 94 }