github.com/Iqoqo/consul@v1.4.5/api/operator_autopilot_test.go (about) 1 package api 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/consul/testutil" 7 "github.com/hashicorp/consul/testutil/retry" 8 ) 9 10 func TestAPI_OperatorAutopilotGetSetConfiguration(t *testing.T) { 11 t.Parallel() 12 c, s := makeClient(t) 13 defer s.Stop() 14 s.WaitForSerfCheck(t) 15 16 operator := c.Operator() 17 config, err := operator.AutopilotGetConfiguration(nil) 18 if err != nil { 19 t.Fatalf("err: %v", err) 20 } 21 if !config.CleanupDeadServers { 22 t.Fatalf("bad: %v", config) 23 } 24 25 // Change a config setting 26 newConf := &AutopilotConfiguration{CleanupDeadServers: false} 27 if err := operator.AutopilotSetConfiguration(newConf, nil); err != nil { 28 t.Fatalf("err: %v", err) 29 } 30 31 config, err = operator.AutopilotGetConfiguration(nil) 32 if err != nil { 33 t.Fatalf("err: %v", err) 34 } 35 if config.CleanupDeadServers { 36 t.Fatalf("bad: %v", config) 37 } 38 } 39 40 func TestAPI_OperatorAutopilotCASConfiguration(t *testing.T) { 41 t.Parallel() 42 c, s := makeClient(t) 43 defer s.Stop() 44 45 retry.Run(t, func(r *retry.R) { 46 operator := c.Operator() 47 config, err := operator.AutopilotGetConfiguration(nil) 48 if err != nil { 49 t.Fatalf("err: %v", err) 50 } 51 if !config.CleanupDeadServers { 52 t.Fatalf("bad: %v", config) 53 } 54 55 // Pass an invalid ModifyIndex 56 { 57 newConf := &AutopilotConfiguration{ 58 CleanupDeadServers: false, 59 ModifyIndex: config.ModifyIndex - 1, 60 } 61 resp, err := operator.AutopilotCASConfiguration(newConf, nil) 62 if err != nil { 63 t.Fatalf("err: %v", err) 64 } 65 if resp { 66 t.Fatalf("bad: %v", resp) 67 } 68 } 69 70 // Pass a valid ModifyIndex 71 { 72 newConf := &AutopilotConfiguration{ 73 CleanupDeadServers: false, 74 ModifyIndex: config.ModifyIndex, 75 } 76 resp, err := operator.AutopilotCASConfiguration(newConf, nil) 77 if err != nil { 78 t.Fatalf("err: %v", err) 79 } 80 if !resp { 81 t.Fatalf("bad: %v", resp) 82 } 83 } 84 }) 85 } 86 87 func TestAPI_OperatorAutopilotServerHealth(t *testing.T) { 88 t.Parallel() 89 c, s := makeClientWithConfig(t, nil, func(c *testutil.TestServerConfig) { 90 c.RaftProtocol = 3 91 }) 92 defer s.Stop() 93 94 operator := c.Operator() 95 retry.Run(t, func(r *retry.R) { 96 out, err := operator.AutopilotServerHealth(nil) 97 if err != nil { 98 r.Fatalf("err: %v", err) 99 } 100 101 if len(out.Servers) != 1 || 102 !out.Servers[0].Healthy || 103 out.Servers[0].Name != s.Config.NodeName { 104 r.Fatalf("bad: %v", out) 105 } 106 }) 107 }