-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathundeploy_command_test.go
More file actions
234 lines (213 loc) · 10.8 KB
/
undeploy_command_test.go
File metadata and controls
234 lines (213 loc) · 10.8 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
package commands_test
import (
"fmt"
cliFakes "github.com/cloudfoundry-incubator/multiapps-cli-plugin/cli/fakes"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/baseclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient"
mtaFake "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient/fakes"
mtaV2Fake "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient_v2/fakes"
mtaV2fake "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient_v2/fakes"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
pluginFakes "code.cloudfoundry.org/cli/v8/plugin/pluginfakes"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/commands"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/testutil"
utilFakes "github.com/cloudfoundry-incubator/multiapps-cli-plugin/util/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("UndeployCommand", func() {
Describe("Execute", func() {
const org = "test-org"
const space = "test-space"
const spaceId = "test-space-guid"
const user = "test-user"
const mtaID = "test"
const ongoingOperationId = "999"
var name string
var cliConnection *pluginFakes.FakeCliConnection
var mtaClient *mtaFake.FakeMtaClientOperations
var testClientFactory *commands.TestClientFactory
var command *commands.UndeployCommand
var oc = testutil.NewUIOutputCapturer()
var ex = testutil.NewUIExpector()
var undeployOperation = models.Operation{
State: "FINISHED",
ProcessID: testutil.ProcessID,
ProcessType: "UNDEPLOY",
Messages: []*models.Message{&testutil.SimpleMessage},
}
var ongoingOperation = models.Operation{
AcquiredLock: true,
Messages: []*models.Message{&testutil.SimpleMessage},
MtaID: mtaID,
ProcessID: ongoingOperationId,
ProcessType: "DEPLOY",
SpaceID: spaceId,
State: "RUNNING",
User: user,
}
var ongoingOperations = []*models.Operation{&ongoingOperation}
var getOutputLines = func(processID string, abortedProcessId string) []string {
lines := []string{}
lines = append(lines,
"Undeploying multi-target app "+mtaID+" in org "+org+" / space "+space+" as "+user+"...")
if abortedProcessId != "" {
lines = append(lines,
"Executing action \"abort\" on operation "+abortedProcessId+"...",
"OK")
}
lines = append(lines,
"Test message",
"Process finished.",
"Use \"cf dmol -i "+processID+"\" to download the logs of the process.")
return lines
}
BeforeEach(func() {
ui.DisableTerminalOutput(true)
name = command.GetPluginCommand().Name
cliConnection = cliFakes.NewFakeCliConnectionBuilder().
CurrentOrg("test-org-guid", org, nil).
CurrentSpace(spaceId, space, nil).
Username(user, nil).
AccessToken("bearer test-token", nil).Build()
mtaClient = mtaFake.NewFakeMtaClientBuilder().
GetMta(mtaID, nil, nil).
GetMtaOperations(&[]string{mtaID}[0], nil, nil, nil, nil).
StartMtaOperation(testutil.OperationResult, mtaclient.ResponseHeader{Location: "operations/1000?embed=messages"}, nil).
GetMtaOperation(testutil.ProcessID, "messages", &undeployOperation, nil).Build()
mtaV2Client := mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace(mtaID, nil, nil, nil).Build()
testClientFactory = commands.NewTestClientFactory(mtaClient, mtaV2Client, nil)
command = commands.NewUndeployCommand()
testTokenFactory := commands.NewTestTokenFactory(cliConnection)
deployServiceURLCalculator := utilFakes.NewDeployServiceURLFakeCalculator("deploy-service.test.ondemand.com")
command.InitializeAll(name, cliConnection, testutil.NewCustomTransport(200), testClientFactory, testTokenFactory, deployServiceURLCalculator)
})
// 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{"test-mta-id", "-unknown-flag"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -unknown-flag")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// wrong arguments - error
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{"test-mta-id", "y", "z"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Wrong arguments")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// no arguments - error
Context("with no 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{}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Missing positional argument \"MTA_ID\"")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// no MTA argument - error
Context("with no mta id argument", 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{"-f"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Missing positional argument \"MTA_ID\"")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// non-existing MTA_ID - failure
Context("with an incorrect mta id provided", func() {
It("should display error and exit with non-zero status", func() {
var clientError = baseclient.NewClientError(testutil.ClientError)
testClientFactory.MtaV2Client = mtaV2Fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test-non-existing-id", nil, nil, clientError).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test-non-existing-id", "-f"}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Could not get multi-target app test-non-existing-id:", 2)
})
})
// non-existing MTA_ID and namespace commbination - failure
Context("with an incorrect mta id and namespace provided", func() {
It("should display error and exit with non-zero status", func() {
mta_id := "non-existing-mta"
namespace := "with-a-namespace"
custom_error := testutil.NewCustomError(404, "mtas", "MTA with name \""+mta_id+"\" and namespace \""+namespace+"\" does not exist")
var clientError = baseclient.NewClientError(custom_error)
testClientFactory.MtaV2Client = mtaV2Fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace(mta_id, &namespace, nil, clientError).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{mta_id, "-f", "--namespace", namespace}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Multi-target app "+mta_id+" with namespace "+namespace+" not found", 2)
})
})
// existing MTA_ID and ongoing operations and force option
Context("with a correct mta id provided and ongoing operation found and force option provided", func() {
It("should try to abort the conflicting process and fail it", func() {
testClientFactory.MtaClient = mtaFake.NewFakeMtaClientBuilder().
GetMtaOperations(&[]string{mtaID}[0], nil, nil, ongoingOperations, nil).
GetOperationActions(ongoingOperationId, []string{"abort"}, nil).
ExecuteAction(ongoingOperationId, "abort", mtaclient.ResponseHeader{Location: "operations/999?embed=messages"}, fmt.Errorf("test-error")).Build()
testClientFactory.MtaV2Client = mtaV2Fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace(mtaID, nil, nil, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{mtaID, "-f"}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Could not execute action \"abort\" on operation 999: test-error", 3)
})
It("should try to abort the conflicting process and success", func() {
testClientFactory.MtaClient = mtaFake.NewFakeMtaClientBuilder().
GetMtaOperations(&[]string{mtaID}[0], nil, nil, ongoingOperations, nil).
GetOperationActions(ongoingOperationId, []string{"abort"}, nil).
ExecuteAction(ongoingOperationId, "abort", mtaclient.ResponseHeader{Location: "operations/999?embed=messages"}, nil).
StartMtaOperation(testutil.OperationResult, mtaclient.ResponseHeader{Location: "operations/1000?embed=messages"}, nil).
GetMtaOperation(testutil.ProcessID, "messages", &undeployOperation, nil).Build()
testClientFactory.MtaV2Client = mtaV2Fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace(mtaID, nil, nil, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{mtaID, "-f"}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output, getOutputLines(testutil.ProcessID, ongoingOperationId))
})
})
// existing MTA_ID and no ongoing operations - success
Context("with a correct mta id provided and no ongoing operations", func() {
It("should proceed without trying to abort conflicting process", func() {
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test", "-f"}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output, getOutputLines(testutil.ProcessID, ""))
})
It("should proceed without trying to abort conflicting process with more options", func() {
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test", "-delete-services", "-no-restart-subscribed-apps", "-delete-service-brokers", "-do-not-fail-on-missing-permissions", "-f"}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output, getOutputLines(testutil.ProcessID, ""))
})
})
// unable to start operation - failure
Context("with a correct mta id provided and failing start of operation", func() {
It("should display error and exit with non-zero status", func() {
testClientFactory.MtaClient = mtaFake.NewFakeMtaClientBuilder().
GetMtaOperations(&[]string{mtaID}[0], nil, nil, nil, nil).
StartMtaOperation(testutil.OperationResult, mtaclient.ResponseHeader{Location: "operations/1000?embed=messages"}, fmt.Errorf("test-error")).Build()
testClientFactory.MtaV2Client = mtaV2Fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace(mtaID, nil, nil, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{testutil.ProcessID, "-f"}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Could not create undeploy process: test-error", 2)
})
})
})
})