-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmta_command_test.go
More file actions
291 lines (270 loc) · 12.2 KB
/
mta_command_test.go
File metadata and controls
291 lines (270 loc) · 12.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
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"
cf_client_fakes "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/cfrestclient/fakes"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
mtaV2fake "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient_v2/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("MtaCommand", func() {
Describe("Execute", func() {
const org = "test-org"
const space = "test-space"
const user = "test-user"
var namespace = "namespace"
var name string
var cliConnection *plugin_fakes.FakeCliConnection
var clientFactory *commands.TestClientFactory
var cfClient cf_client_fakes.FakeCloudFoundryClient
var command *commands.MtaCommand
var oc = testutil.NewUIOutputCapturer()
var ex = testutil.NewUIExpector()
var getOutputLines = func(mtaID, version, namespace string, apps, services [][]string) []string {
var lines []string
lines = append(lines,
fmt.Sprintf("Showing health and status for multi-target app %s in org %s / space %s as %s...", mtaID, org, space, user))
lines = append(lines, "OK")
lines = append(lines, fmt.Sprintf("Version: %s", version))
lines = append(lines, fmt.Sprintf("Namespace: %s", namespace))
lines = append(lines, "")
lines = append(lines, "Apps:")
lines = append(lines, testutil.GetTableOutputLines(
[]string{"name", "requested state", "instances", "memory", "disk", "urls"}, apps)...)
lines = append(lines, "")
lines = append(lines, "Services:")
if len(services) > 0 {
lines = append(lines, testutil.GetTableOutputLines(
[]string{"name", "service", "plan", "bound apps", "last operation"}, services)...)
}
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).
APIEndpoint("https://example.com", nil).
Build()
cfClient = cf_client_fakes.FakeCloudFoundryClient{
Apps: getApps("test-mta-module-1", "started"),
AppProcessStats: getProcessStats(1, 1, 512*1024*1024, 1024*1024*1024),
AppRoutes: getAppRoutes("test-1", "bosh-lite.com"),
Services: getServices("test-service-1", "test", "free", "create", "succeeded"),
ServiceBindings: getServiceBindings([]string{"test-mta-module-1"}),
}
mtaV2Client := mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtas("any_mtaId", &namespace, "any_spaceGuid", nil, nil).Build()
clientFactory = commands.NewTestClientFactory(nil, mtaV2Client, nil)
command = commands.NewMtaCommand()
testTokenFactory := commands.NewTestTokenFactory(cliConnection)
deployServiceURLCalculator := util_fakes.NewDeployServiceURLFakeCalculator("deploy-service.test.ondemand.com")
command.InitializeAll(name, cliConnection, testutil.NewCustomTransport(200), clientFactory, testTokenFactory, deployServiceURLCalculator)
command.CfClient = cfClient
})
// 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{"x", "y", "z"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Wrong arguments")
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
})
})
// with unknown flags and one valid flag - error
Context("with unknown flags 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{"fakeMta", "--nonValidFlag", "--namespace", "-u"}).ToInt()
})
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: --nonValidFlag")
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.MtaV2Client = mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test", nil, nil, 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{"test", "-u", host}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Could not get multi-target app test:", 2)
})
})
// backend returns a "not found" 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.MtaV2Client = mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test", nil, nil, newClientError(404, "404 Not Found", `MTA with id "test" does not exist`)).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test"}).ToInt()
})
ex.ExpectFailureOnLine(status, output, "Multi-target app test not found", 2)
})
})
// backend returns a non-empty response - success
Context("with a non-empty response returned by the backend", func() {
It("should print a information about the deployed MTA and exit with zero status", func() {
clientFactory.MtaV2Client = mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test-mta-id", &namespace, []*models.Mta{testutil.GetMta("test-mta-id", "test-version", "namespace", []*models.Module{
testutil.GetMtaModule("test-mta-module-1", []string{}, []string{})},
[]string{"test-service-1"})}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test-mta-id", "--namespace", namespace}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output,
getOutputLines("test-mta-id", "test-version", "namespace",
[][]string{{"test-mta-module-1", "started", "1/1", "512M", "1G", "test-1.bosh-lite.com"}},
[][]string{{"test-service-1", "test", "free", "test-mta-module-1", "create succeeded"}}))
})
})
// backend returns a non-empty response - success
Context("with a non-empty response returned by the backend but no namespace", func() {
It("should print a information about the deployed MTA and exit with zero status", func() {
clientFactory.MtaV2Client = mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test-mta-id", nil, []*models.Mta{testutil.GetMta("test-mta-id", "test-version", "", []*models.Module{
testutil.GetMtaModule("test-mta-module-1", []string{}, []string{})},
[]string{"test-service-1"})}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test-mta-id"}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output,
getOutputLines("test-mta-id", "test-version", "",
[][]string{{"test-mta-module-1", "started", "1/1", "512M", "1G", "test-1.bosh-lite.com"}},
[][]string{{"test-service-1", "test", "free", "test-mta-module-1", "create succeeded"}}))
})
})
// backend returns a non-empty response - success
Context("with a non-empty response without services returned by the backend", func() {
It("should print information about the deployed MTA and exit with zero status", func() {
clientFactory.MtaV2Client = mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test-mta-id", &namespace, []*models.Mta{testutil.GetMta("test-mta-id", "test-version", "namespace", []*models.Module{
testutil.GetMtaModule("test-mta-module-1", []string{}, []string{})},
[]string{})}, nil).Build()
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test-mta-id", "--namespace", namespace}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output,
getOutputLines("test-mta-id", "test-version", "namespace",
[][]string{[]string{"test-mta-module-1", "started", "1/1", "512M", "1G", "test-1.bosh-lite.com"}},
[][]string{}))
})
})
// backend returns a non-empty response - success
Context("with a non-empty response with unknown MTA version", func() {
It("should print information about the deployed MTA and exit with zero status", func() {
clientFactory.MtaV2Client = mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test-mta-id", nil, []*models.Mta{testutil.GetMta("test-mta-id", "0.0.0-unknown", "", []*models.Module{}, []string{})}, nil).Build()
command.CfClient = cf_client_fakes.FakeCloudFoundryClient{}
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test-mta-id"}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output,
getOutputLines("test-mta-id", "?", "", [][]string{}, [][]string{}))
})
})
// backend returns a non-empty response - success
Context("with a non-empty response without services and apps returned by the backend", func() {
It("should print information about the deployed MTA and exit with zero status", func() {
clientFactory.MtaV2Client = mtaV2fake.NewFakeMtaV2ClientBuilder().
GetMtasForThisSpace("test-mta-id", &namespace, []*models.Mta{testutil.GetMta("test-mta-id", "test-version", "namespace", []*models.Module{}, []string{})}, nil).Build()
command.CfClient = cf_client_fakes.FakeCloudFoundryClient{}
output, status := oc.CaptureOutputAndStatus(func() int {
return command.Execute([]string{"test-mta-id", "--namespace", namespace}).ToInt()
})
ex.ExpectSuccessWithOutput(status, output,
getOutputLines("test-mta-id", "test-version", "namespace", [][]string{}, [][]string{}))
})
})
})
})
func getApps(name, state string) []models.CloudFoundryApplication {
return []models.CloudFoundryApplication{
{
Name: name,
Guid: "app-guid",
State: state,
},
}
}
func getProcessStats(runningInstances, totalInstances int64, memory, diskQuota int64) []models.ApplicationProcessStatistics {
var processes []models.ApplicationProcessStatistics
for i := int64(0); i < runningInstances; i++ {
processMemory := memory / runningInstances
if i == 0 {
processMemory += memory % runningInstances
}
disk := diskQuota / runningInstances
if i == 0 {
disk += diskQuota % runningInstances
}
processes = append(processes, models.ApplicationProcessStatistics{
State: "RUNNING",
Memory: processMemory,
Disk: disk,
})
}
for i := runningInstances; i < totalInstances; i++ {
processes = append(processes, models.ApplicationProcessStatistics{
State: "STOPPED",
Memory: 0,
Disk: 0,
})
}
return processes
}
func getAppRoutes(host, domain string) []models.ApplicationRoute {
return []models.ApplicationRoute{
{
Host: host,
Url: host + "." + domain,
},
}
}
func getServices(name, offering, plan, opType, opState string) []models.CloudFoundryServiceInstance {
return []models.CloudFoundryServiceInstance{
{
Guid: "service-guid",
Name: name,
Type: "managed",
LastOperation: models.LastOperation{
Type: opType,
State: opState,
},
PlanGuid: "plan-guid",
SpaceGuid: "space-guid",
Plan: models.ServicePlan{
Guid: "plan-guid",
Name: plan,
OfferingGuid: "offering-guid",
},
Offering: models.ServiceOffering{
Guid: "offering-guid",
Name: offering,
},
},
}
}
func getServiceBindings(boundApplications []string) []models.ServiceBinding {
var bindings []models.ServiceBinding
for _, appName := range boundApplications {
bindings = append(bindings, models.ServiceBinding{
Guid: "binding-guid",
AppGuid: "app-guid",
AppName: appName,
})
}
return bindings
}