forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_command_test.go
More file actions
260 lines (222 loc) · 8.21 KB
/
stack_command_test.go
File metadata and controls
260 lines (222 loc) · 8.21 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
package v7_test
import (
"errors"
"code.cloudfoundry.org/cli/v9/actor/v7action"
"code.cloudfoundry.org/cli/v9/resources"
"code.cloudfoundry.org/cli/v9/actor/actionerror"
"code.cloudfoundry.org/cli/v9/command/commandfakes"
. "code.cloudfoundry.org/cli/v9/command/v7"
"code.cloudfoundry.org/cli/v9/command/v7/v7fakes"
"code.cloudfoundry.org/cli/v9/util/configv3"
"code.cloudfoundry.org/cli/v9/util/ui"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
)
var _ = Describe("Stack Command", func() {
var (
cmd StackCommand
testUI *ui.UI
fakeConfig *commandfakes.FakeConfig
fakeSharedActor *commandfakes.FakeSharedActor
fakeActor *v7fakes.FakeActor
binaryName string
executeErr error
stackName string
)
BeforeEach(func() {
testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
fakeConfig = new(commandfakes.FakeConfig)
fakeSharedActor = new(commandfakes.FakeSharedActor)
fakeActor = new(v7fakes.FakeActor)
cmd = StackCommand{
BaseCommand: BaseCommand{
UI: testUI,
Config: fakeConfig,
SharedActor: fakeSharedActor,
Actor: fakeActor,
},
}
binaryName = "faceman"
fakeConfig.BinaryNameReturns(binaryName)
stackName = "some-stack-name"
cmd.RequiredArgs.StackName = stackName
})
JustBeforeEach(func() {
executeErr = cmd.Execute(nil)
})
Context("When the environment is not setup correctly", func() {
When("checking target fails", func() {
BeforeEach(func() {
fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
})
It("returns an error", func() {
Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(0))
Expect(checkTargetedOrg).To(BeFalse())
Expect(checkTargetedSpace).To(BeFalse())
})
})
When("retrieving user information errors", func() {
var expectedErr error
BeforeEach(func() {
expectedErr = errors.New("some current user error")
fakeActor.GetCurrentUserReturns(configv3.User{}, expectedErr)
})
It("return an error", func() {
Expect(executeErr).To(Equal(expectedErr))
})
})
})
Context("When the environment is setup correctly", func() {
BeforeEach(func() {
fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
fakeActor.GetCurrentUserReturns(configv3.User{Name: "banana"}, nil)
})
Context("When the stack exists", func() {
BeforeEach(func() {
stack := resources.Stack{
Name: "some-stack-name",
GUID: "some-stack-guid",
Description: "some-stack-desc",
}
fakeActor.GetStackByNameReturns(stack, v7action.Warnings{"some-warning-1"}, nil)
})
When("The --guid flag is not provided", func() {
It("Displays the stack information", func() {
Expect(executeErr).ToNot(HaveOccurred())
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
// NOTE: DISPLAY EXPECTS
Expect(testUI.Err).To(Say("some-warning-1"))
})
})
When("The --guid flag is provided", func() {
BeforeEach(func() {
cmd.GUID = true
})
It("displays just the guid", func() {
Expect(executeErr).ToNot(HaveOccurred())
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
Expect(testUI.Err).To(Say("some-warning-1"))
})
})
})
Context("When the stack has no state", func() {
BeforeEach(func() {
stack := resources.Stack{
Name: "some-stack-name",
GUID: "some-stack-guid",
Description: "some-stack-desc",
}
fakeActor.GetStackByNameReturns(stack, v7action.Warnings{}, nil)
})
It("Displays the stack information without state", func() {
Expect(executeErr).ToNot(HaveOccurred())
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
Expect(testUI.Out).To(Say("name:\\s+some-stack-name"))
Expect(testUI.Out).To(Say("description:\\s+some-stack-desc"))
Expect(testUI.Out).NotTo(Say("state:"))
})
})
Context("When the stack has a state", func() {
Context("When the state is ACTIVE", func() {
BeforeEach(func() {
stack := resources.Stack{
Name: "some-stack-name",
GUID: "some-stack-guid",
Description: "some-stack-desc",
State: "ACTIVE",
}
fakeActor.GetStackByNameReturns(stack, v7action.Warnings{}, nil)
})
It("Displays the stack information with state but no reason", func() {
Expect(executeErr).ToNot(HaveOccurred())
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
Expect(testUI.Out).To(Say("name:\\s+some-stack-name"))
Expect(testUI.Out).To(Say("description:\\s+some-stack-desc"))
Expect(testUI.Out).To(Say("state:\\s+ACTIVE"))
Expect(testUI.Out).NotTo(Say("reason:"))
})
})
Context("When the state is not ACTIVE and has a reason", func() {
BeforeEach(func() {
stack := resources.Stack{
Name: "some-stack-name",
GUID: "some-stack-guid",
Description: "some-stack-desc",
State: "DEPRECATED",
StateReason: "This stack is being phased out",
}
fakeActor.GetStackByNameReturns(stack, v7action.Warnings{}, nil)
})
It("Displays the stack information with state and reason", func() {
Expect(executeErr).ToNot(HaveOccurred())
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
Expect(testUI.Out).To(Say("name:\\s+some-stack-name"))
Expect(testUI.Out).To(Say("description:\\s+some-stack-desc"))
Expect(testUI.Out).To(Say("state:\\s+DEPRECATED"))
Expect(testUI.Out).To(Say("reason:\\s+This stack is being phased out"))
})
})
Context("When the state is not ACTIVE but has no reason", func() {
BeforeEach(func() {
stack := resources.Stack{
Name: "some-stack-name",
GUID: "some-stack-guid",
Description: "some-stack-desc",
State: "RESTRICTED",
}
fakeActor.GetStackByNameReturns(stack, v7action.Warnings{}, nil)
})
It("Displays the stack information with state and empty reason", func() {
Expect(executeErr).ToNot(HaveOccurred())
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
Expect(testUI.Out).To(Say("name:\\s+some-stack-name"))
Expect(testUI.Out).To(Say("description:\\s+some-stack-desc"))
Expect(testUI.Out).To(Say("state:\\s+RESTRICTED"))
Expect(testUI.Out).To(Say("reason:"))
})
})
})
When("The Stack does not Exist", func() {
expectedError := actionerror.StackNotFoundError{Name: "some-stack-name"}
BeforeEach(func() {
fakeActor.GetStackByNameReturns(
resources.Stack{},
v7action.Warnings{"some-warning-1"},
expectedError,
)
})
It("Fails and returns a StackNotFoundError", func() {
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
Expect(executeErr).To(Equal(expectedError))
Expect(testUI.Err).To(Say("some-warning-1"))
})
})
When("There was an error in the actor", func() {
BeforeEach(func() {
fakeActor.GetStackByNameReturns(
resources.Stack{},
v7action.Warnings{"some-warning-1"},
errors.New("some-random-error"),
)
})
It("Fails and returns a StackNotFoundError", func() {
Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
Expect(executeErr).To(MatchError(errors.New("some-random-error")))
Expect(testUI.Err).To(Say("some-warning-1"))
})
})
})
})