Skip to content

Commit b711bd9

Browse files
committed
Add unit tests for route policy commands, actor, and ccv3 client
1 parent ab8e7ad commit b711bd9

5 files changed

Lines changed: 1015 additions & 0 deletions

File tree

Lines changed: 384 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,384 @@
1+
package v7action_test
2+
3+
import (
4+
"errors"
5+
6+
"code.cloudfoundry.org/cli/v9/actor/actionerror"
7+
. "code.cloudfoundry.org/cli/v9/actor/v7action"
8+
"code.cloudfoundry.org/cli/v9/actor/v7action/v7actionfakes"
9+
"code.cloudfoundry.org/cli/v9/api/cloudcontroller/ccv3"
10+
"code.cloudfoundry.org/cli/v9/resources"
11+
. "github.com/onsi/ginkgo/v2"
12+
. "github.com/onsi/gomega"
13+
)
14+
15+
var _ = Describe("Route Policy Actions", func() {
16+
var (
17+
actor *Actor
18+
fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
19+
)
20+
21+
BeforeEach(func() {
22+
actor, fakeCloudControllerClient, _, _, _, _, _ = NewTestActor()
23+
})
24+
25+
Describe("AddRoutePolicy", func() {
26+
var (
27+
warnings Warnings
28+
executeErr error
29+
)
30+
31+
JustBeforeEach(func() {
32+
warnings, executeErr = actor.AddRoutePolicy("apps.example.com", "cf:any", "myapp", "")
33+
})
34+
35+
When("the API calls are successful", func() {
36+
BeforeEach(func() {
37+
fakeCloudControllerClient.GetDomainsReturns(
38+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
39+
ccv3.Warnings{"domain-warning"},
40+
nil,
41+
)
42+
fakeCloudControllerClient.GetRoutesReturns(
43+
[]resources.Route{{GUID: "route-guid", Host: "myapp"}},
44+
ccv3.Warnings{"routes-warning"},
45+
nil,
46+
)
47+
fakeCloudControllerClient.CreateRoutePolicyReturns(
48+
resources.RoutePolicy{GUID: "policy-guid"},
49+
ccv3.Warnings{"create-warning"},
50+
nil,
51+
)
52+
})
53+
54+
It("creates the route policy and returns all warnings", func() {
55+
Expect(executeErr).NotTo(HaveOccurred())
56+
Expect(warnings).To(ConsistOf("domain-warning", "routes-warning", "create-warning"))
57+
58+
Expect(fakeCloudControllerClient.CreateRoutePolicyCallCount()).To(Equal(1))
59+
passedPolicy := fakeCloudControllerClient.CreateRoutePolicyArgsForCall(0)
60+
Expect(passedPolicy.Source).To(Equal("cf:any"))
61+
Expect(passedPolicy.RouteGUID).To(Equal("route-guid"))
62+
})
63+
})
64+
65+
When("getting the domain fails", func() {
66+
BeforeEach(func() {
67+
fakeCloudControllerClient.GetDomainsReturns(
68+
nil,
69+
ccv3.Warnings{"domain-warning"},
70+
errors.New("domain-error"),
71+
)
72+
})
73+
74+
It("returns the error and domain warning", func() {
75+
Expect(executeErr).To(MatchError("domain-error"))
76+
Expect(warnings).To(ConsistOf("domain-warning"))
77+
})
78+
})
79+
80+
When("getting routes fails", func() {
81+
BeforeEach(func() {
82+
fakeCloudControllerClient.GetDomainsReturns(
83+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
84+
ccv3.Warnings{"domain-warning"},
85+
nil,
86+
)
87+
fakeCloudControllerClient.GetRoutesReturns(nil, ccv3.Warnings{"routes-warning"}, errors.New("routes-error"))
88+
})
89+
90+
It("returns the error and all collected warnings", func() {
91+
Expect(executeErr).To(MatchError("routes-error"))
92+
Expect(warnings).To(ConsistOf("domain-warning", "routes-warning"))
93+
})
94+
})
95+
96+
When("the route is not found", func() {
97+
BeforeEach(func() {
98+
fakeCloudControllerClient.GetDomainsReturns(
99+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
100+
ccv3.Warnings{"domain-warning"},
101+
nil,
102+
)
103+
fakeCloudControllerClient.GetRoutesReturns(
104+
[]resources.Route{},
105+
ccv3.Warnings{"routes-warning"},
106+
nil,
107+
)
108+
})
109+
110+
It("returns a RouteNotFoundError", func() {
111+
Expect(executeErr).To(MatchError(actionerror.RouteNotFoundError{
112+
Host: "myapp",
113+
DomainName: "apps.example.com",
114+
Path: "",
115+
}))
116+
Expect(warnings).To(ConsistOf("domain-warning", "routes-warning"))
117+
})
118+
})
119+
120+
When("creating the route policy fails", func() {
121+
BeforeEach(func() {
122+
fakeCloudControllerClient.GetDomainsReturns(
123+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
124+
ccv3.Warnings{"domain-warning"},
125+
nil,
126+
)
127+
fakeCloudControllerClient.GetRoutesReturns(
128+
[]resources.Route{{GUID: "route-guid", Host: "myapp"}},
129+
ccv3.Warnings{"routes-warning"},
130+
nil,
131+
)
132+
fakeCloudControllerClient.CreateRoutePolicyReturns(
133+
resources.RoutePolicy{},
134+
ccv3.Warnings{"create-warning"},
135+
errors.New("create-error"),
136+
)
137+
})
138+
139+
It("returns the error and all collected warnings", func() {
140+
Expect(executeErr).To(MatchError("create-error"))
141+
Expect(warnings).To(ConsistOf("domain-warning", "routes-warning", "create-warning"))
142+
})
143+
})
144+
})
145+
146+
Describe("DeleteRoutePolicyBySource", func() {
147+
var (
148+
warnings Warnings
149+
executeErr error
150+
)
151+
152+
JustBeforeEach(func() {
153+
warnings, executeErr = actor.DeleteRoutePolicyBySource("apps.example.com", "cf:any", "myapp", "")
154+
})
155+
156+
When("the API calls are successful", func() {
157+
BeforeEach(func() {
158+
fakeCloudControllerClient.GetDomainsReturns(
159+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
160+
ccv3.Warnings{"domain-warning"},
161+
nil,
162+
)
163+
fakeCloudControllerClient.GetRoutesReturns(
164+
[]resources.Route{{GUID: "route-guid", Host: "myapp"}},
165+
ccv3.Warnings{"routes-warning"},
166+
nil,
167+
)
168+
fakeCloudControllerClient.GetRoutePoliciesReturns(
169+
[]resources.RoutePolicy{
170+
{GUID: "policy-guid", Source: "cf:any", RouteGUID: "route-guid"},
171+
},
172+
ccv3.IncludedResources{},
173+
ccv3.Warnings{"get-policies-warning"},
174+
nil,
175+
)
176+
fakeCloudControllerClient.DeleteRoutePolicyReturns(
177+
"",
178+
ccv3.Warnings{"delete-warning"},
179+
nil,
180+
)
181+
})
182+
183+
It("deletes the route policy and returns all warnings", func() {
184+
Expect(executeErr).NotTo(HaveOccurred())
185+
Expect(warnings).To(ConsistOf("domain-warning", "routes-warning", "get-policies-warning", "delete-warning"))
186+
187+
Expect(fakeCloudControllerClient.DeleteRoutePolicyCallCount()).To(Equal(1))
188+
deletedGUID := fakeCloudControllerClient.DeleteRoutePolicyArgsForCall(0)
189+
Expect(deletedGUID).To(Equal("policy-guid"))
190+
})
191+
})
192+
193+
When("getting the domain fails", func() {
194+
BeforeEach(func() {
195+
fakeCloudControllerClient.GetDomainsReturns(
196+
nil,
197+
ccv3.Warnings{"domain-warning"},
198+
errors.New("domain-error"),
199+
)
200+
})
201+
202+
It("returns the error and domain warning", func() {
203+
Expect(executeErr).To(MatchError("domain-error"))
204+
Expect(warnings).To(ConsistOf("domain-warning"))
205+
})
206+
})
207+
208+
When("getting routes fails", func() {
209+
BeforeEach(func() {
210+
fakeCloudControllerClient.GetDomainsReturns(
211+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
212+
ccv3.Warnings{"domain-warning"},
213+
nil,
214+
)
215+
fakeCloudControllerClient.GetRoutesReturns(nil, ccv3.Warnings{"routes-warning"}, errors.New("routes-error"))
216+
})
217+
218+
It("returns the error and all collected warnings", func() {
219+
Expect(executeErr).To(MatchError("routes-error"))
220+
Expect(warnings).To(ConsistOf("domain-warning", "routes-warning"))
221+
})
222+
})
223+
224+
When("the route is not found", func() {
225+
BeforeEach(func() {
226+
fakeCloudControllerClient.GetDomainsReturns(
227+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
228+
ccv3.Warnings{"domain-warning"},
229+
nil,
230+
)
231+
fakeCloudControllerClient.GetRoutesReturns(
232+
[]resources.Route{},
233+
ccv3.Warnings{"routes-warning"},
234+
nil,
235+
)
236+
})
237+
238+
It("returns a RouteNotFoundError", func() {
239+
Expect(executeErr).To(MatchError(actionerror.RouteNotFoundError{
240+
Host: "myapp",
241+
DomainName: "apps.example.com",
242+
Path: "",
243+
}))
244+
Expect(warnings).To(ConsistOf("domain-warning", "routes-warning"))
245+
})
246+
})
247+
248+
When("getting route policies fails", func() {
249+
BeforeEach(func() {
250+
fakeCloudControllerClient.GetDomainsReturns(
251+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
252+
ccv3.Warnings{},
253+
nil,
254+
)
255+
fakeCloudControllerClient.GetRoutesReturns(
256+
[]resources.Route{{GUID: "route-guid", Host: "myapp"}},
257+
ccv3.Warnings{},
258+
nil,
259+
)
260+
fakeCloudControllerClient.GetRoutePoliciesReturns(
261+
nil,
262+
ccv3.IncludedResources{},
263+
ccv3.Warnings{"get-policies-warning"},
264+
errors.New("get-policies-error"),
265+
)
266+
})
267+
268+
It("returns the error", func() {
269+
Expect(executeErr).To(MatchError("get-policies-error"))
270+
Expect(warnings).To(ConsistOf("get-policies-warning"))
271+
})
272+
})
273+
274+
When("no matching policy is found", func() {
275+
BeforeEach(func() {
276+
fakeCloudControllerClient.GetDomainsReturns(
277+
[]resources.Domain{{Name: "apps.example.com", GUID: "domain-guid"}},
278+
ccv3.Warnings{},
279+
nil,
280+
)
281+
fakeCloudControllerClient.GetRoutesReturns(
282+
[]resources.Route{{GUID: "route-guid", Host: "myapp"}},
283+
ccv3.Warnings{},
284+
nil,
285+
)
286+
fakeCloudControllerClient.GetRoutePoliciesReturns(
287+
[]resources.RoutePolicy{
288+
{GUID: "other-policy-guid", Source: "cf:app:some-app-guid", RouteGUID: "route-guid"},
289+
},
290+
ccv3.IncludedResources{},
291+
ccv3.Warnings{},
292+
nil,
293+
)
294+
})
295+
296+
It("returns a RoutePolicyNotFoundError", func() {
297+
Expect(executeErr).To(MatchError(actionerror.RoutePolicyNotFoundError{Source: "cf:any"}))
298+
})
299+
})
300+
})
301+
302+
Describe("GetRoutePoliciesForSpace", func() {
303+
var (
304+
policiesWithRoutes []RoutePolicyWithRoute
305+
warnings Warnings
306+
executeErr error
307+
)
308+
309+
JustBeforeEach(func() {
310+
policiesWithRoutes, warnings, executeErr = actor.GetRoutePoliciesForSpace(
311+
"space-guid", "", "", "", "",
312+
)
313+
})
314+
315+
When("the API calls are successful", func() {
316+
BeforeEach(func() {
317+
fakeCloudControllerClient.GetRoutePoliciesReturns(
318+
[]resources.RoutePolicy{
319+
{GUID: "p-guid", Source: "cf:any", RouteGUID: "r-guid"},
320+
},
321+
ccv3.IncludedResources{
322+
Routes: []resources.Route{
323+
{GUID: "r-guid", Host: "backend", DomainGUID: "domain-guid"},
324+
},
325+
},
326+
ccv3.Warnings{"get-policies-warning"},
327+
nil,
328+
)
329+
fakeCloudControllerClient.GetDomainReturns(
330+
resources.Domain{Name: "apps.example.com", GUID: "domain-guid"},
331+
ccv3.Warnings{},
332+
nil,
333+
)
334+
})
335+
336+
It("returns policies with route info and all warnings", func() {
337+
Expect(executeErr).NotTo(HaveOccurred())
338+
Expect(warnings).To(ConsistOf("get-policies-warning"))
339+
Expect(policiesWithRoutes).To(HaveLen(1))
340+
Expect(policiesWithRoutes[0].GUID).To(Equal("p-guid"))
341+
Expect(policiesWithRoutes[0].Source).To(Equal("cf:any"))
342+
Expect(policiesWithRoutes[0].Route.Host).To(Equal("backend"))
343+
Expect(policiesWithRoutes[0].DomainName).To(Equal("apps.example.com"))
344+
Expect(policiesWithRoutes[0].ScopeType).To(Equal("any"))
345+
346+
Expect(fakeCloudControllerClient.GetDomainCallCount()).To(Equal(1))
347+
domainGUIDArg := fakeCloudControllerClient.GetDomainArgsForCall(0)
348+
Expect(domainGUIDArg).To(Equal("domain-guid"))
349+
})
350+
})
351+
352+
When("there are no route policies", func() {
353+
BeforeEach(func() {
354+
fakeCloudControllerClient.GetRoutePoliciesReturns(
355+
[]resources.RoutePolicy{},
356+
ccv3.IncludedResources{},
357+
ccv3.Warnings{},
358+
nil,
359+
)
360+
})
361+
362+
It("returns an empty slice and no error", func() {
363+
Expect(executeErr).NotTo(HaveOccurred())
364+
Expect(policiesWithRoutes).To(BeEmpty())
365+
})
366+
})
367+
368+
When("getting route policies fails", func() {
369+
BeforeEach(func() {
370+
fakeCloudControllerClient.GetRoutePoliciesReturns(
371+
nil,
372+
ccv3.IncludedResources{},
373+
ccv3.Warnings{"get-policies-warning"},
374+
errors.New("get-policies-error"),
375+
)
376+
})
377+
378+
It("returns the error and warnings", func() {
379+
Expect(executeErr).To(MatchError("get-policies-error"))
380+
Expect(warnings).To(ConsistOf("get-policies-warning"))
381+
})
382+
})
383+
})
384+
})

0 commit comments

Comments
 (0)