-
Notifications
You must be signed in to change notification settings - Fork 5
/
rolling-restart_test.go
388 lines (313 loc) · 13.7 KB
/
rolling-restart_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package main
import (
"bytes"
"fmt"
"os"
"reflect"
"testing"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/plugin/pluginfakes"
"github.com/stretchr/testify/require"
)
var (
rr *RollingRestart
cliConn *pluginfakes.FakeCliConnection
output []string
spinnerBuffer bytes.Buffer
exitCode int
twoInstanceResponse = []string{"{", "\"0\": {", "\"state\": \"RUNNING\",", "\"uptime\": 5,", "\"since\": 1511990275", "},", "\"1\": {", "\"state\": \"RUNNING\",", "\"uptime\": 5,", "\"since\": 1511990327", "}", "}"}
alwaysRestartingResponse = []string{"{", "\"0\": {", "\"state\": \"STARTING\",", "\"uptime\": 5,", "\"since\": 1511990275", "},", "\"1\": {", "\"state\": \"RUNNING\",", "\"uptime\": 5,", "\"since\": 1511990327", "}", "}"}
singleInstanceResponse = []string{"{", "\"0\": {", "\"state\": \"RUNNING\",", "\"uptime\": 5,", "\"since\": 1511990275", "}", "}"}
badInstanceResponse = []string{"bad", "response"}
)
type testError struct {
arg int
prob string
}
func (e *testError) Error() string {
return e.prob
}
func TestMain(m *testing.M) {
rr = &RollingRestart{}
cliConn = &pluginfakes.FakeCliConnection{}
output = []string{}
oldPrintln := printLine
defer func() { printLine = oldPrintln }()
oldPrintf := printFormatted
defer func() { printFormatted = oldPrintf }()
oldPrintRedBold := printRedBold
defer func() { printRedBold = oldPrintRedBold }()
oldExit := exit
defer func() { exit = oldExit }()
printLine = printlnStub
printFormatted = printfStub
printRedBold = printRedBoldStub
exit = exitStub
fakeSpinner := NewSpinner(&spinnerBuffer)
oldMaxRestartWaitCycles := maxRestartWaitCycles
defer func() { maxRestartWaitCycles = oldMaxRestartWaitCycles }()
oldSpinner := spinner
defer func() { spinner = oldSpinner }()
maxRestartWaitCycles = 1
spinner = fakeSpinner
code := m.Run()
os.Exit(code)
}
func TestRollingRestart_Run_Success(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(true, true, twoInstanceResponse)
setupCliCommandStub(true, true)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, 2, cliConn.CliCommandCallCount())
require.Equal(t, []string{"restart-app-instance", "testApp", "0"}, cliConn.CliCommandArgsForCall(0))
require.Equal(t, []string{"restart-app-instance", "testApp", "1"}, cliConn.CliCommandArgsForCall(1))
require.Equal(t, 4, cliConn.CliCommandWithoutTerminalOutputCallCount())
require.Equal(t, []string{"app", "testApp", "--guid"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(0))
require.Equal(t, []string{"curl", "-X", "GET", "/v2/apps/valid-app-guid/instances"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(1))
require.Equal(t, []string{"curl", "-X", "GET", "/v2/apps/valid-app-guid/instances"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(2))
require.Equal(t, []string{"curl", "-X", "GET", "/v2/apps/valid-app-guid/instances"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(3))
require.Equal(t, 4, len(output))
require.Equal(t, "Beginning restart of app instances for testApp.\n", output[0])
require.Equal(t, "Checking status of instance 0.\n", output[1])
require.Equal(t, "Checking status of instance 1.\n", output[2])
require.Equal(t, "Finished restart of app instances for testApp.\n", output[3])
require.Contains(t, spinnerBuffer.String(), "OK")
require.Equal(t, exitCode, 0)
}
func TestRollingRestart_Run_Success_SingleAppInstance(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(true, true, singleInstanceResponse)
setupCliCommandStub(true, true)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, 3, cliConn.CliCommandCallCount())
require.Equal(t, []string{"scale", "testApp", "-i", "2"}, cliConn.CliCommandArgsForCall(0))
require.Equal(t, []string{"restart-app-instance", "testApp", "0"}, cliConn.CliCommandArgsForCall(1))
require.Equal(t, []string{"scale", "testApp", "-i", "1"}, cliConn.CliCommandArgsForCall(2))
require.Equal(t, 4, cliConn.CliCommandWithoutTerminalOutputCallCount())
require.Equal(t, []string{"app", "testApp", "--guid"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(0))
require.Equal(t, []string{"curl", "-X", "GET", "/v2/apps/valid-app-guid/instances"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(1))
require.Equal(t, []string{"curl", "-X", "GET", "/v2/apps/valid-app-guid/instances"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(2))
require.Equal(t, []string{"curl", "-X", "GET", "/v2/apps/valid-app-guid/instances"}, cliConn.CliCommandWithoutTerminalOutputArgsForCall(2))
require.Equal(t, 7, len(output))
require.Equal(t, "Only found a single instance of testApp, scaling up to two instances.\n", output[0])
require.Equal(t, "Checking status of instance 1.\n", output[1])
require.Equal(t, "Finished scaling testApp to two instances.\n", output[2])
require.Equal(t, "Beginning restart of app instances for testApp.\n", output[3])
require.Equal(t, "Checking status of instance 0.\n", output[4])
require.Equal(t, "Scaling testApp back down to one instance.\n", output[5])
require.Equal(t, "Finished restart of app instances for testApp.\n", output[6])
require.Contains(t, spinnerBuffer.String(), "OK")
require.Equal(t, exitCode, 0)
}
func TestRollingRestart_Run_NoAppNameProvided(t *testing.T) {
resetOutput()
rr.Run(cliConn, []string{"rolling-restart"})
require.Equal(t, exitCode, 1)
require.Equal(t, "An application name was not provided. Usage: cf rolling-restart APP_NAME\n", output[0])
}
func TestRollingRestart_Run_MultipleAppNameProvided(t *testing.T) {
resetOutput()
rr.Run(cliConn, []string{"rolling-restart", "firstApp", "secondApp"})
require.Equal(t, exitCode, 1)
require.Equal(t, "Only a single app name is currently supported, please try again.\n", output[0])
}
func TestRollingRestart_Run_ArguementParsingError(t *testing.T) {
resetOutput()
rr.Run(cliConn, []string{"rolling-restart", "firstApp", "secondApp"})
require.Equal(t, exitCode, 1)
require.Equal(t, "Only a single app name is currently supported, please try again.\n", output[0])
}
func TestRollingRestart_Run_NotLoggedIn(t *testing.T) {
resetOutput()
setupIsLoggedInStub(false, false)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Equal(t, "You are not logged in, please log in and try again.\n", output[0])
}
func TestRollingRestart_Run_isLoggedInThrowsErr(t *testing.T) {
resetOutput()
setupIsLoggedInStub(false, true)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "CLI FAILURE")
}
func TestRollingRestart_Run_OrgNotSet(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(false, false)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "The logged in user does not have an Org set, please select an Org and Space and try again.")
}
func TestRollingRestart_Run_hasOrgThrowsErr(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(false, true)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "CLI FAILURE")
}
func TestRollingRestart_Run_SpaceNotSet(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(false, false)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "The logged in user does not have a Space set, please select a Space and try again.")
}
func TestRollingRestart_Run_hasSpaceThrowsError(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(false, true)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "CLI FAILURE")
}
func TestRollingRestart_Run_GetGuidThrowsError(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(false, true, twoInstanceResponse)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "CliCommandWithoutTerminalStubError")
}
func TestRollingRestart_Run_GetInstancesThrowsError(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(true, false, twoInstanceResponse)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "Failed to get the instance information for testApp.\n")
}
func TestRollingRestart_Run_ScaleAppThrowsError(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(true, true, singleInstanceResponse)
setupCliCommandStub(true, false)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[1], "Failed to scale testApp to two instances.")
}
func TestRollingRestart_Run_RestartInstanceThrowsError(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(true, true, twoInstanceResponse)
setupCliCommandStub(false, true)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[1], "Failed to restart instance 0.\n")
}
func TestRollingRestart_Run_InstanceJsonUnmarshallError(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, true)
setupCliCommandWihtoutTerminalOutputStub(true, true, badInstanceResponse)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "CLI FAILURE")
}
func TestRollingRestart_Run_InstanceDoesNotRestartInCycleLimit(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(true, true, alwaysRestartingResponse)
setupCliCommandStub(true, true)
rr.Run(cliConn, []string{"rolling-restart", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "Beginning restart of app instances for testApp.")
require.Contains(t, output[1], "Checking status of instance 0.")
require.Contains(t, output[2], "Application did not restart within 1 Second(s), failing out. Check your current application state.")
}
func TestRollingRestart_Run_InstanceDoesNotRestartCustomCycleLimit(t *testing.T) {
resetOutput()
setupIsLoggedInStub(true, false)
setupHasOrganizationStub(true, false)
setupHasSpaceStub(true, false)
setupCliCommandWihtoutTerminalOutputStub(true, true, alwaysRestartingResponse)
setupCliCommandStub(true, true)
rr.Run(cliConn, []string{"rolling-restart", "--max-cycles", "2", "testApp"})
require.Equal(t, exitCode, 1)
require.Contains(t, output[0], "Beginning restart of app instances for testApp.")
require.Contains(t, output[1], "Checking status of instance 0.")
require.Contains(t, output[2], "Application did not restart within 2 Second(s), failing out. Check your current application state.")
}
func setupHasSpaceStub(hasSpace bool, throwError bool) {
cliConn.HasSpaceStub = func() (bool, error) {
if throwError {
return false, errors.New("CLI FAILURE\n")
}
return hasSpace, nil
}
}
func setupHasOrganizationStub(hasOrganization bool, throwError bool) {
cliConn.HasOrganizationStub = func() (bool, error) {
if throwError {
return false, errors.New("CLI FAILURE\n")
}
return hasOrganization, nil
}
}
func setupIsLoggedInStub(isLoggedIn bool, throwError bool) {
cliConn.IsLoggedInStub = func() (bool, error) {
if throwError {
return false, errors.New("CLI FAILURE\n")
}
return isLoggedIn, nil
}
}
func setupCliCommandWihtoutTerminalOutputStub(getGUIDSuccess bool, getInstanceStatusSuccess bool, instanceResponse []string) {
cliConn.CliCommandWithoutTerminalOutputStub = func(args ...string) ([]string, error) {
if reflect.DeepEqual(args, []string{"app", "testApp", "--guid"}) && getGUIDSuccess {
return []string{"valid-app-guid"}, nil
} else if reflect.DeepEqual(args, []string{"curl", "-X", "GET", "/v2/apps/valid-app-guid/instances"}) && getInstanceStatusSuccess {
return instanceResponse, nil
}
return nil, &testError{1, "CliCommandWithoutTerminalStubError"}
}
}
func setupCliCommandStub(restartSuccess bool, scaleSuccess bool) {
cliConn.CliCommandStub = func(args ...string) ([]string, error) {
if args[0] == "restart-app-instance" && args[1] == "testApp" && (args[2] == "0" || args[2] == "1") && restartSuccess {
return nil, nil
} else if args[0] == "scale" && args[1] == "testApp" && args[2] == "-i" && args[3] == "2" && scaleSuccess {
return nil, nil
}
return nil, &testError{1, "CliCommandStubError"}
}
}
func printlnStub(a ...interface{}) (n int, err error) {
output = append(output, fmt.Sprintln(a...))
return 0, nil
}
func printfStub(format string, a ...interface{}) (n int, err error) {
output = append(output, fmt.Sprintf(format, a...))
return 0, nil
}
func printRedBoldStub(a ...interface{}) (n int, err error) {
return 0, nil
}
func exitStub(code int) {
exitCode = code
}
func resetOutput() {
output = []string{}
cliConn = &pluginfakes.FakeCliConnection{}
}