11package asg_syncer_test
22
33import (
4+ "fmt"
45 "time"
56
67 "code.cloudfoundry.org/lager/lagertest"
78 "code.cloudfoundry.org/policy-server/asg_syncer"
9+ "code.cloudfoundry.org/policy-server/cc_client"
810 ccfakes "code.cloudfoundry.org/policy-server/cc_client/fakes"
11+ "code.cloudfoundry.org/policy-server/store"
912 dbfakes "code.cloudfoundry.org/policy-server/store/fakes"
1013 uaafakes "code.cloudfoundry.org/policy-server/uaa_client/fakes"
1114 . "github.com/onsi/ginkgo"
@@ -27,12 +30,206 @@ var _ = Describe("ASGSyncer", func() {
2730 fakeCCClient = & ccfakes.CCClient {}
2831 logger = lagertest .NewTestLogger ("test" )
2932 })
30- It ("pulls ASG data from CAPI and stores it in the SecurityGroupStore" , func () {
31- syncer := asg_syncer .NewASGSyncer (logger , fakeStore , fakeUAAClient , fakeCCClient , requestTimeout )
32- Expect (syncer ).ToNot (BeNil ())
33+ Describe ("NewASGSyncer()" , func () {
34+ asgSyncer := asg_syncer .NewASGSyncer (logger , fakeStore , fakeUAAClient , fakeCCClient , requestTimeout )
3335
34- By ("querying CAPI for /v3/security_groups, handling pagination" , func () {
35- Expect (syncer ).ToNot (BeNil ())
36+ Expect (asgSyncer ).To (Equal (& asg_syncer.ASGSyncer {
37+ Logger : logger ,
38+ Store : fakeStore ,
39+ UAAClient : fakeUAAClient ,
40+ CCClient : fakeCCClient ,
41+ RequestTimeout : requestTimeout ,
42+ }))
43+ })
44+ Describe ("asgSyncer.Sync()" , func () {
45+ var asgSyncer * asg_syncer.ASGSyncer
46+ BeforeEach (func () {
47+ asgSyncer = asg_syncer .NewASGSyncer (logger , fakeStore , fakeUAAClient , fakeCCClient , requestTimeout )
48+ fakeUAAClient .GetTokenReturns ("fake-token" , nil )
49+ fakeCCClient .GetSecurityGroupsReturns ([]cc_client.SecurityGroupResource {{
50+ GUID : "first-guid" ,
51+ Name : "asg-1" ,
52+ GloballyEnabled : cc_client.SecurityGroupGloballyEnabled {Running : true , Staging : true },
53+ Rules : []cc_client.SecurityGroupRule {{
54+ Protocol : "ICMP" ,
55+ Destination : "10.10.10.10/32" ,
56+ Code : 4 ,
57+ Type : 1 ,
58+ Description : "fake icmp rule" ,
59+ Log : false ,
60+ }, {
61+ Protocol : "TCP" ,
62+ Destination : "20.20.20.20/32" ,
63+ Ports : "80-1024" ,
64+ Description : "fake tcp rule" ,
65+ Log : true ,
66+ }},
67+ Relationships : cc_client.SecurityGroupRelationships {},
68+ }, {
69+ GUID : "second-guid" ,
70+ Name : "asg-2" ,
71+ Rules : []cc_client.SecurityGroupRule {{
72+ Protocol : "UDP" ,
73+ Destination : "0.0.0/0" ,
74+ Ports : "53" ,
75+ Description : "fake dns rule" ,
76+ Log : true ,
77+ }},
78+ Relationships : cc_client.SecurityGroupRelationships {
79+ RunningSpaces : cc_client.SecurityGroupSpaceRelationship {
80+ Data : []map [string ]string {{
81+ "guid" : "space-1-guid" ,
82+ }, {
83+ "guid" : "space-2-guid" ,
84+ }},
85+ },
86+ StagingSpaces : cc_client.SecurityGroupSpaceRelationship {
87+ Data : []map [string ]string {{
88+ "guid" : "space-3-guid" ,
89+ }},
90+ },
91+ },
92+ }}, nil )
93+ })
94+ It ("pulls properly" , func () {
95+ err := asgSyncer .Poll ()
96+ Expect (err ).To (BeNil ())
97+
98+ By ("Getting a UAA token" , func () {
99+ Expect (fakeUAAClient .GetTokenCallCount ()).To (Equal (1 ))
100+ })
101+ By ("Requesting data from CAPI" , func () {
102+ Expect (fakeCCClient .GetSecurityGroupsCallCount ()).To (Equal (1 ))
103+ Expect (fakeCCClient .GetSecurityGroupsArgsForCall (0 )).To (Equal ("fake-token" ))
104+ })
105+ By ("calling Replace() on the store" , func () {
106+ Expect (fakeStore .ReplaceCallCount ()).To (Equal (1 ))
107+ })
108+ By ("Translating cc_client security group resources into store security groups" , func () {
109+ Expect (fakeStore .ReplaceArgsForCall (0 )).To (Equal ([]store.SecurityGroup {{
110+ Guid : "first-guid" ,
111+ Name : "asg-1" ,
112+ StagingDefault : true ,
113+ RunningDefault : true ,
114+ Rules : `[{"protocol":"ICMP","destination":"10.10.10.10/32","ports":"","type":1,"code":4,"description":"fake icmp rule","log":false},{"protocol":"TCP","destination":"20.20.20.20/32","ports":"80-1024","type":0,"code":0,"description":"fake tcp rule","log":true}]` ,
115+ RunningSpaceGuids : []string {},
116+ StagingSpaceGuids : []string {},
117+ }, {
118+ Guid : "second-guid" ,
119+ Name : "asg-2" ,
120+ Rules : `[{"protocol":"UDP","destination":"0.0.0/0","ports":"53","type":0,"code":0,"description":"fake dns rule","log":true}]` ,
121+ RunningSpaceGuids : []string {"space-1-guid" , "space-2-guid" },
122+ StagingSpaceGuids : []string {"space-3-guid" },
123+ }}))
124+ })
125+ })
126+ Context ("when acquiring a lock" , func () {
127+ Context ("and the lock is already taken" , func () {
128+ It ("doesn't poll capi or update the database" , func () {
129+ err := asgSyncer .Poll ()
130+ Expect (err ).ToNot (HaveOccurred ())
131+ Expect (fakeCCClient .GetSecurityGroupsCallCount ()).To (Equal (0 ))
132+ Expect (fakeStore .ReplaceCallCount ()).To (Equal (0 ))
133+ })
134+ It ("debug logs that another policy-server has the lock" , func () {
135+ Expect (logger .Logs ()).To (Equal ("something" ))
136+ })
137+ })
138+ Context ("and the lock is obtained successfully" , func () {
139+ It ("polls CAPI and updates the database" , func () {
140+ err := asgSyncer .Poll ()
141+ Expect (err ).ToNot (HaveOccurred ())
142+ Expect (fakeCCClient .GetSecurityGroupsCallCount ()).To (Equal (1 ))
143+ Expect (fakeStore .ReplaceCallCount ()).To (Equal (1 ))
144+ })
145+ It ("info logs that it is the leader now" , func () {
146+ Expect (logger .Logs ()).To (Equal ("something" ))
147+ })
148+ })
149+ Context ("and an error occurs obtaining the lock" , func () {
150+ It ("returns a relevant error" , func () {
151+ err := asgSyncer .Poll ()
152+ Expect (err ).To (HaveOccurred ())
153+ Expect (err .Error ()).To (Equal ("uaa error" ))
154+
155+ })
156+ It ("doesn't poll capi or update the database" , func () {
157+ asgSyncer .Poll ()
158+ Expect (fakeCCClient .GetSecurityGroupsCallCount ()).To (Equal (0 ))
159+ Expect (fakeStore .ReplaceCallCount ()).To (Equal (0 ))
160+ })
161+ })
162+
163+ })
164+
165+ Context ("when errors occur" , func () {
166+
167+ Context ("getting a UAA token" , func () {
168+ BeforeEach (func () {
169+ fakeUAAClient .GetTokenReturns ("" , fmt .Errorf ("uaa error" ))
170+ })
171+ It ("returns a relevant error" , func () {
172+ err := asgSyncer .Poll ()
173+ Expect (err ).To (HaveOccurred ())
174+ Expect (err .Error ()).To (Equal ("uaa error" ))
175+
176+ })
177+ It ("doesn't poll capi or update the database" , func () {
178+ asgSyncer .Poll ()
179+ Expect (fakeCCClient .GetSecurityGroupsCallCount ()).To (Equal (0 ))
180+ Expect (fakeStore .ReplaceCallCount ()).To (Equal (0 ))
181+ })
182+ })
183+
184+ Context ("polling CAPI" , func () {
185+ BeforeEach (func () {
186+ fakeCCClient .GetSecurityGroupsReturns ([]cc_client.SecurityGroupResource {}, fmt .Errorf ("capi error" ))
187+ })
188+ It ("returns a relevant error" , func () {
189+ err := asgSyncer .Poll ()
190+ Expect (err ).To (HaveOccurred ())
191+ Expect (err .Error ()).To (Equal ("capi error" ))
192+ })
193+ It ("doesn't update the database" , func () {
194+ asgSyncer .Poll ()
195+ Expect (fakeStore .ReplaceCallCount ()).To (Equal (0 ))
196+ })
197+ })
198+
199+ Context ("when CAPI returns bad relationship data" , func () {
200+ BeforeEach (func () {
201+ fakeCCClient .GetSecurityGroupsReturns ([]cc_client.SecurityGroupResource {{
202+ Name : "bad-asg" ,
203+ GUID : "bad-asg-guid" ,
204+ Relationships : cc_client.SecurityGroupRelationships {
205+ RunningSpaces : cc_client.SecurityGroupSpaceRelationship {
206+ Data : []map [string ]string {{"blarg" : "blargh" }},
207+ },
208+ },
209+ }}, nil )
210+ })
211+ It ("returns a relevant error" , func () {
212+ err := asgSyncer .Poll ()
213+ Expect (err ).To (HaveOccurred ())
214+ Expect (err .Error ()).To (Equal ("no 'guid' found for running-space-relationship on asg 'bad-asg-guid'" ))
215+
216+ })
217+ It ("doesn't update the database" , func () {
218+ asgSyncer .Poll ()
219+ Expect (fakeStore .ReplaceCallCount ()).To (Equal (0 ))
220+ })
221+ })
222+
223+ Context ("replacing data in the store" , func () {
224+ BeforeEach (func () {
225+ fakeStore .ReplaceReturns (fmt .Errorf ("db error" ))
226+ })
227+ It ("returns a relevant error" , func () {
228+ err := asgSyncer .Poll ()
229+ Expect (err ).To (HaveOccurred ())
230+ Expect (err .Error ()).To (Equal ("db error" ))
231+ })
232+ })
36233 })
37234 })
38235})
0 commit comments