-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmta_operations_command_test.go
More file actions
358 lines (345 loc) · 18.2 KB
/
mta_operations_command_test.go
File metadata and controls
358 lines (345 loc) · 18.2 KB
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
package commands_test
import (
"fmt"
plugin_fakes "code.cloudfoundry.org/cli/v8/plugin/pluginfakes"
cli_fakes "github.com/cloudfoundry-incubator/multiapps-cli-plugin/cli/fakes"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
mtafake "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient/fakes"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/commands"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/testutil"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
util_fakes "github.com/cloudfoundry-incubator/multiapps-cli-plugin/util/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("MtaOperationsCommand", func() {
Describe("Execute", func() {
const org = "test-org"
const space = "test-space"
const user = "test-user"
var name string
var cliConnection *plugin_fakes.FakeCliConnection
var clientFactory *commands.TestClientFactory
var command *commands.MtaOperationsCommand
var oc = testutil.NewUIOutputCapturer()
var ex = testutil.NewUIExpector()
var getOutputLines = func(operationsDetails [][]string) []string {
lines := []string{}
if len(operationsDetails) > 0 {
lines = append(lines, testutil.GetTableOutputLines([]string{"id", "type", "mta id", "namespace", "status", "started at", "started by"}, operationsDetails)...)
} else {
lines = append(lines, "No multi-target app operations found")
}
return lines
}
BeforeEach(func() {
ui.DisableTerminalOutput(true)
name = command.GetPluginCommand().Name
cliConnection = cli_fakes.NewFakeCliConnectionBuilder().
CurrentOrg("test-org-guid", org, nil).
CurrentSpace("test-space-guid", space, nil).
Username(user, nil).
AccessToken("bearer test-token", nil).Build()
mtaClient := mtafake.NewFakeMtaClientBuilder().
GetMta("test", nil, nil).Build()
clientFactory = commands.NewTestClientFactory(mtaClient, nil, nil)
command = commands.NewMtaOperationsCommand()
testTokenFactory := commands.NewTestTokenFactory(cliConnection)
deployServiceURLCalculator := util_fakes.NewDeployServiceURLFakeCalculator("deploy-service.test.ondemand.com")
command.InitializeAll(name, cliConnection, testutil.NewCustomTransport(200), clientFactory, testTokenFactory, deployServiceURLCalculator)
})
// with an unknown flag - error
Context("with an unknown flag", func() {
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-a"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// with an unknown flag and one valid flag
Context("with an unknown flag and one valid flag", func() {
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-a", "--last"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// wrong arguments
Context("with wrong arguments", func() {
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"x", "y", "z"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Wrong arguments")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// can't connect to backend - error
Context("when can't connect to backend", func() {
const host = "x"
It("should print an error and exit with a non-zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, nil, []*models.Operation{}, fmt.Errorf("Get https://%s/rest/test/test/mta: dial tcp: lookup %s: no such host", host, host)).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-u", host}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Could not get multi-target app operations:", 2)
})
})
// backend returns an an error response - error
Context("with an error response returned by the backend", func() {
It("should print an error and exit with a non-zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, nil, []*models.Operation{}, fmt.Errorf("unknown error (status 404)")).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Could not get multi-target app operations:", 2)
})
})
Context("with empty response returned by the backend", func() {
It("should print info and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, nil, []*models.Operation{}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{}).ToInt()
})
expectedOutput := getOutputLines([][]string{})
expectedOutput = append([]string{
"Getting active multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with non-empty response returned by the backend", func() {
It("should print info and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, nil, []*models.Operation{
testutil.GetOperation("111", "test-space", "test", "namespace", "deploy", "ERROR", false)}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"111", "deploy", "test", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting active multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with non-empty response returned by the backend containing a nil value for MTA ID", func() {
It("should print info and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, nil, []*models.Operation{
testutil.GetOperation("111", "test-space", "", "", "deploy", "ERROR", false)}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"111", "deploy", "N/A", "", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting active multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with more than 1 operations returned by the backend", func() {
It("should print info and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, nil, []*models.Operation{
testutil.GetOperation("test-1", "test-space", "test-mta-1", "namespace", "deploy", "ERROR", true),
testutil.GetOperation("test-2", "test-space", "test-mta-2", "namespace", "deploy", "ERROR", false),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"test-1", "deploy", "test-mta-1", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-2", "deploy", "test-mta-2", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting active multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with more than 1 operations returned by the backend and last option provided", func() {
It("should print the info of the last 2 operations and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, &[]int64{2}[0], nil, []*models.Operation{
testutil.GetOperation("test-2", "test-space", "test-mta-2", "namespace", "deploy", "ERROR", false),
testutil.GetOperation("test-3", "test-space", "test-mta-3", "namespace", "deploy", "ERROR", false),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-last", "2"}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"test-2", "deploy", "test-mta-2", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-3", "deploy", "test-mta-3", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting last 2 multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with more than 1 operations returned by the backend and last option provided", func() {
It("should print the info for all of the operations and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, &[]int64{10}[0], nil, []*models.Operation{
testutil.GetOperation("test-1", "test-space", "test-mta-1", "namespace", "deploy", "ERROR", true),
testutil.GetOperation("test-2", "test-space", "test-mta-2", "namespace", "deploy", "ERROR", false),
testutil.GetOperation("test-3", "test-space", "test-mta-3", "namespace", "deploy", "ERROR", false),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-last", "10"}).ToInt()
})
expectedOutput := getOutputLines([][]string{
[]string{"test-1", "deploy", "test-mta-1", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
[]string{"test-2", "deploy", "test-mta-2", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
[]string{"test-3", "deploy", "test-mta-3", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting last 10 multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with more than 1 operations returned by the backend and last option provided", func() {
It("should print the info for the last operation and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, &[]int64{10}[0], nil, []*models.Operation{
testutil.GetOperation("test-1", "test-space", "test-mta-1", "namespace", "deploy", "ERROR", true),
testutil.GetOperation("test-2", "test-space", "test-mta-2", "namespace", "deploy", "ERROR", false),
testutil.GetOperation("test-3", "test-space", "test-mta-3", "namespace", "deploy", "ERROR", false),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-last", "1"}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"test-1", "deploy", "test-mta-1", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-2", "deploy", "test-mta-2", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-3", "deploy", "test-mta-3", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting last multi-target app operation in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with empty response returned by the backend and last option provided", func() {
It("should print the info for all of the operations and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, &[]int64{10}[0], nil, []*models.Operation{}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-last", "10"}).ToInt()
})
expectedOutput := getOutputLines([][]string{})
expectedOutput = append([]string{
"Getting last 10 multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with more than 1 operations returned by the backend and no options provided", func() {
It("should print the info for operations in active state and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, &[]int64{10}[0], nil, []*models.Operation{
testutil.GetOperation("test-1", "test-space", "test-mta-1", "namespace", "deploy", "ERROR", true),
testutil.GetOperation("test-2", "test-space", "test-mta-2", "namespace", "deploy", "RUNNING", false),
testutil.GetOperation("test-3", "test-space", "test-mta-3", "namespace", "deploy", "ERROR", false),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"test-1", "deploy", "test-mta-1", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-2", "deploy", "test-mta-2", "namespace", "RUNNING", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-3", "deploy", "test-mta-3", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting active multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with more than 1 operations returned by the backend and no options provided", func() {
It("should print the info for operations in active state, not include operations in finished state and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, []string{"SLP_TASK_STATE_ERROR", "SLP_TASK_STATE_RUNNING"}, []*models.Operation{
testutil.GetOperation("test-1", "test-space", "test-mta-1", "namespace", "deploy", "ERROR", true),
testutil.GetOperation("test-2", "test-space", "test-mta-2", "namespace", "deploy", "RUNNING", false),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"test-1", "deploy", "test-mta-1", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-2", "deploy", "test-mta-2", "namespace", "RUNNING", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting active multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with more than 1 operations returned by the backend and all option provided", func() {
It("should print the info for operations in active and finished state and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(nil, nil, nil, []*models.Operation{
testutil.GetOperation("test-1", "test-space", "test-mta-1", "namespace", "deploy", "ERROR", true),
testutil.GetOperation("test-2", "test-space", "test-mta-2", "namespace", "deploy", "RUNNING", false),
testutil.GetOperation("test-3", "test-space", "test-mta-3", "namespace", "deploy", "FINISHED", false),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"-all"}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"test-1", "deploy", "test-mta-1", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-2", "deploy", "test-mta-2", "namespace", "RUNNING", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
{"test-3", "deploy", "test-mta-3", "namespace", "FINISHED", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting all multi-target app operations in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
Context("with mta id provided", func() {
It("should print the info for the operation and return with zero status", func() {
clientFactory.MtaClient = mtafake.NewFakeMtaClientBuilder().
GetMtaOperations(&[]string{"test-mta-id"}[0], &[]int64{1}[0], nil, []*models.Operation{
testutil.GetOperation("test-1", "test-space", "test-mta-id", "namespace", "deploy", "ERROR", true),
}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"--mta", "test-mta-id"}).ToInt()
})
expectedOutput := getOutputLines([][]string{
{"test-1", "deploy", "test-mta-id", "namespace", "ERROR", "2016-03-04T14:23:24.521Z[Etc/UTC]", "admin"},
})
expectedOutput = append([]string{
"Getting multi-target app operations for test-mta-id in org test-org / space test-space as test-user...",
"OK",
}, expectedOutput...)
ex.ExpectSuccessWithOutput(status, output, expectedOutput)
})
})
})
})