github.com/lmb/consul@v1.4.1/command/maint/maint_test.go (about) 1 package maint 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/consul/agent" 8 "github.com/hashicorp/consul/agent/structs" 9 "github.com/mitchellh/cli" 10 ) 11 12 func TestMaintCommand_noTabs(t *testing.T) { 13 t.Parallel() 14 if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { 15 t.Fatal("help has tabs") 16 } 17 } 18 19 func TestMaintCommand_ConflictingArgs(t *testing.T) { 20 t.Parallel() 21 ui := cli.NewMockUi() 22 c := New(ui) 23 c.flags.SetOutput(ui.ErrorWriter) 24 25 if code := c.Run([]string{"-enable", "-disable"}); code != 1 { 26 t.Fatalf("expected return code 1, got %d", code) 27 } 28 29 if code := c.Run([]string{"-disable", "-reason=broken"}); code != 1 { 30 t.Fatalf("expected return code 1, got %d", code) 31 } 32 33 if code := c.Run([]string{"-reason=broken"}); code != 1 { 34 t.Fatalf("expected return code 1, got %d", code) 35 } 36 37 if code := c.Run([]string{"-service=redis"}); code != 1 { 38 t.Fatalf("expected return code 1, got %d", code) 39 } 40 } 41 42 func TestMaintCommand_NoArgs(t *testing.T) { 43 t.Parallel() 44 a := agent.NewTestAgent(t.Name(), ``) 45 defer a.Shutdown() 46 47 // Register the service and put it into maintenance mode 48 service := &structs.NodeService{ 49 ID: "test", 50 Service: "test", 51 } 52 if err := a.AddService(service, nil, false, "", agent.ConfigSourceLocal); err != nil { 53 t.Fatalf("err: %v", err) 54 } 55 if err := a.EnableServiceMaintenance("test", "broken 1", ""); err != nil { 56 t.Fatalf("err: %s", err) 57 } 58 59 // Enable node maintenance 60 a.EnableNodeMaintenance("broken 2", "") 61 62 // Run consul maint with no args (list mode) 63 ui := cli.NewMockUi() 64 c := New(ui) 65 c.flags.SetOutput(ui.ErrorWriter) 66 67 args := []string{"-http-addr=" + a.HTTPAddr()} 68 code := c.Run(args) 69 if code != 0 { 70 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 71 } 72 73 // Ensure the service shows up in the list 74 out := ui.OutputWriter.String() 75 if !strings.Contains(out, "test") { 76 t.Fatalf("bad:\n%s", out) 77 } 78 if !strings.Contains(out, "broken 1") { 79 t.Fatalf("bad:\n%s", out) 80 } 81 82 // Ensure the node shows up in the list 83 if !strings.Contains(out, a.Config.NodeName) { 84 t.Fatalf("bad:\n%s", out) 85 } 86 if !strings.Contains(out, "broken 2") { 87 t.Fatalf("bad:\n%s", out) 88 } 89 } 90 91 func TestMaintCommand_EnableNodeMaintenance(t *testing.T) { 92 t.Parallel() 93 a := agent.NewTestAgent(t.Name(), ``) 94 defer a.Shutdown() 95 96 ui := cli.NewMockUi() 97 c := New(ui) 98 c.flags.SetOutput(ui.ErrorWriter) 99 100 args := []string{ 101 "-http-addr=" + a.HTTPAddr(), 102 "-enable", 103 "-reason=broken", 104 } 105 code := c.Run(args) 106 if code != 0 { 107 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 108 } 109 110 if !strings.Contains(ui.OutputWriter.String(), "now enabled") { 111 t.Fatalf("bad: %#v", ui.OutputWriter.String()) 112 } 113 } 114 115 func TestMaintCommand_DisableNodeMaintenance(t *testing.T) { 116 t.Parallel() 117 a := agent.NewTestAgent(t.Name(), ``) 118 defer a.Shutdown() 119 120 ui := cli.NewMockUi() 121 c := New(ui) 122 c.flags.SetOutput(ui.ErrorWriter) 123 124 args := []string{ 125 "-http-addr=" + a.HTTPAddr(), 126 "-disable", 127 } 128 code := c.Run(args) 129 if code != 0 { 130 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 131 } 132 133 if !strings.Contains(ui.OutputWriter.String(), "now disabled") { 134 t.Fatalf("bad: %#v", ui.OutputWriter.String()) 135 } 136 } 137 138 func TestMaintCommand_EnableServiceMaintenance(t *testing.T) { 139 t.Parallel() 140 a := agent.NewTestAgent(t.Name(), ``) 141 defer a.Shutdown() 142 143 // Register the service 144 service := &structs.NodeService{ 145 ID: "test", 146 Service: "test", 147 } 148 if err := a.AddService(service, nil, false, "", agent.ConfigSourceLocal); err != nil { 149 t.Fatalf("err: %v", err) 150 } 151 152 ui := cli.NewMockUi() 153 c := New(ui) 154 c.flags.SetOutput(ui.ErrorWriter) 155 156 args := []string{ 157 "-http-addr=" + a.HTTPAddr(), 158 "-enable", 159 "-service=test", 160 "-reason=broken", 161 } 162 code := c.Run(args) 163 if code != 0 { 164 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 165 } 166 167 if !strings.Contains(ui.OutputWriter.String(), "now enabled") { 168 t.Fatalf("bad: %#v", ui.OutputWriter.String()) 169 } 170 } 171 172 func TestMaintCommand_DisableServiceMaintenance(t *testing.T) { 173 t.Parallel() 174 a := agent.NewTestAgent(t.Name(), ``) 175 defer a.Shutdown() 176 177 // Register the service 178 service := &structs.NodeService{ 179 ID: "test", 180 Service: "test", 181 } 182 if err := a.AddService(service, nil, false, "", agent.ConfigSourceLocal); err != nil { 183 t.Fatalf("err: %v", err) 184 } 185 186 ui := cli.NewMockUi() 187 c := New(ui) 188 c.flags.SetOutput(ui.ErrorWriter) 189 190 args := []string{ 191 "-http-addr=" + a.HTTPAddr(), 192 "-disable", 193 "-service=test", 194 } 195 code := c.Run(args) 196 if code != 0 { 197 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 198 } 199 200 if !strings.Contains(ui.OutputWriter.String(), "now disabled") { 201 t.Fatalf("bad: %#v", ui.OutputWriter.String()) 202 } 203 } 204 205 func TestMaintCommand_ServiceMaintenance_NoService(t *testing.T) { 206 t.Parallel() 207 a := agent.NewTestAgent(t.Name(), ``) 208 defer a.Shutdown() 209 210 ui := cli.NewMockUi() 211 c := New(ui) 212 c.flags.SetOutput(ui.ErrorWriter) 213 214 args := []string{ 215 "-http-addr=" + a.HTTPAddr(), 216 "-enable", 217 "-service=redis", 218 "-reason=broken", 219 } 220 code := c.Run(args) 221 if code != 1 { 222 t.Fatalf("expected response code 1, got %d", code) 223 } 224 225 if !strings.Contains(ui.ErrorWriter.String(), "No service registered") { 226 t.Fatalf("bad: %#v", ui.ErrorWriter.String()) 227 } 228 }