This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain_test.go
329 lines (265 loc) · 10.3 KB
/
main_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
package main_test
import (
"errors"
"fmt"
"os/exec"
"github.com/cloudfoundry/cli/plugin/models"
"github.com/cloudfoundry/cli/testhelpers/rpc_server"
fake_rpc_handlers "github.com/cloudfoundry/cli/testhelpers/rpc_server/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("DiegoEnabler", func() {
Describe("Commands", func() {
var (
rpcHandlers *fake_rpc_handlers.FakeHandlers
ts *test_rpc_server.TestServer
err error
)
BeforeEach(func() {
rpcHandlers = &fake_rpc_handlers.FakeHandlers{}
})
JustBeforeEach(func() {
//set rpc.CallCoreCommand to a successful call
rpcHandlers.CallCoreCommandStub = func(_ []string, retVal *bool) error {
*retVal = true
return nil
}
//set rpc.GetOutputAndReset to return empty string; this is used by CliCommand()/CliWithoutTerminalOutput()
rpcHandlers.GetOutputAndResetStub = func(_ bool, retVal *[]string) error {
*retVal = []string{"{}"}
return nil
}
ts, err = test_rpc_server.NewTestRpcServer(rpcHandlers)
Expect(err).NotTo(HaveOccurred())
err = ts.Start()
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
ts.Stop()
})
Context("enable-diego", func() {
var args []string
JustBeforeEach(func() {
args = []string{ts.Port(), "enable-diego", "test-app"}
})
It("needs APP_NAME as argument", func() {
args = []string{ts.Port(), "enable-diego"}
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("required argument `APP_NAME` was not provided"))
})
Context("when the args are properly provided", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{}
return nil
}
})
It("calls GetApp() twice, one to get app guid, another to verify flag is set", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(rpcHandlers.GetAppCallCount()).To(Equal(3))
})
})
Context("when the app is found", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{Guid: "test-app-guid"}
return nil
}
})
It("sets diego flag with /v2/apps endpoint", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(rpcHandlers.CallCoreCommandCallCount()).To(Equal(1))
output, _ := rpcHandlers.CallCoreCommandArgsForCall(0)
Expect(output[1]).To(ContainSubstring("v2/apps/test-app-guid"))
Expect(output[5]).To(ContainSubstring(`"diego":true`))
})
})
Context("when the app is not found", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{Guid: "test-app-guid"}
return errors.New("error in GetApp")
}
})
It("exits with error when GetApp() returns error", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("error in GetApp"))
Expect(session.ExitCode()).To(Equal(1))
})
})
Context("when the app was successfully changed to Diego", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{Guid: "test-app-guid", Diego: true}
return nil
}
})
It("exit 0 after veriftying the flag is correct set", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("Verifying test-app Diego support is set to true"))
Expect(session).To(gbytes.Say("OK"))
Expect(session.ExitCode()).To(Equal(0))
})
})
Context("when the change to Diego failed", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{Guid: "test-app-guid", Diego: false}
return nil
}
})
It("exit 1 after veriftying the flag is not correct set", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("Verifying test-app Diego support is set to true"))
Expect(session).To(gbytes.Say("FAILED"))
Expect(session.ExitCode()).To(Equal(1))
})
})
Context("when the app has no routes", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(appName string, retVal *plugin_models.GetAppModel) error {
Expect(appName).To(Equal("test-app"))
*retVal = plugin_models.GetAppModel{
Guid: "test-app-guid",
Name: "test-app",
}
return nil
}
rpcHandlers.GetCurrentSpaceStub = func(_ string, retVal *plugin_models.Space) error {
*retVal = plugin_models.Space{
SpaceFields: plugin_models.SpaceFields{
Name: "some-space",
},
}
return nil
}
rpcHandlers.GetSpaceStub = func(spaceName string, retVal *plugin_models.GetSpace_Model) error {
if spaceName != "some-space" {
return fmt.Errorf("expected spaceName argument to be %s, was %s", "some-space", spaceName)
}
*retVal = plugin_models.GetSpace_Model{
GetSpaces_Model: plugin_models.GetSpaces_Model{
Name: "some-space",
},
Organization: plugin_models.GetSpace_Orgs{Name: "some-org"},
}
return nil
}
rpcHandlers.UsernameStub = func(_ string, retVal *string) error {
*retVal = "some-user"
return nil
}
})
It("warns the user that the health check type will be set to none", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
// This output is colored but this assertion does not test for color. This is because the terminal package disables color when the binary is run in a non-tty session.
Expect(session.Out).To(gbytes.Say("WARNING: Assuming health check of type process \\('none'\\) for app with no mapped routes\\. Use 'cf set-health-check' to change this\\. App test-app to Diego in space some-space / org some-org as some-user"))
})
})
})
Context("disable-diego", func() {
var args []string
JustBeforeEach(func() {
args = []string{ts.Port(), "disable-diego", "test-app"}
})
It("needs APP_NAME as argument", func() {
args = []string{ts.Port(), "disable-diego"}
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("required argument `APP_NAME` was not provided"))
})
Context("when the app is found", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{Guid: "test-app-guid"}
return nil
}
})
It("sets diego flag with /v2/apps endpoint", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(rpcHandlers.CallCoreCommandCallCount()).To(Equal(1))
output, _ := rpcHandlers.CallCoreCommandArgsForCall(0)
Expect(output[1]).To(ContainSubstring("v2/apps/test-app-guid"))
Expect(output[5]).To(ContainSubstring(`"diego":false`))
})
})
Context("has-diego-enabled", func() {
var args []string
JustBeforeEach(func() {
args = []string{ts.Port(), "has-diego-enabled", "test-app"}
})
It("needs APP_NAME as argument", func() {
args = []string{ts.Port(), "has-diego-enabled"}
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("required argument `APP_NAME` was not provided"))
})
Context("when the params are properly provided", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{}
return nil
}
})
It("calls GetApp() to get app model", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(rpcHandlers.GetAppCallCount()).To(Equal(1))
})
})
Context("when the app does not exist", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{Guid: ""}
return nil
}
})
It("notifies user app is not found", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("App test-app not found"))
Expect(session.ExitCode()).To(Equal(1))
})
})
Context("when the app is on Diego", func() {
BeforeEach(func() {
rpcHandlers.GetAppStub = func(_ string, retVal *plugin_models.GetAppModel) error {
*retVal = plugin_models.GetAppModel{Guid: "test-app-guid", Diego: true}
return nil
}
})
It("outputs the app's Diego flag value", func() {
session, err := gexec.Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait()
Expect(session).To(gbytes.Say("true"))
Expect(session.ExitCode()).To(Equal(0))
})
})
})
})
})
})