@@ -33,43 +33,55 @@ import (
3333)
3434
3535var (
36- fakeNSG = NSGSpec {
36+ annotation = azure .SecurityRuleLastAppliedAnnotation
37+ fakeNSG = NSGSpec {
3738 Name : "test-nsg" ,
3839 Location : "test-location" ,
3940 ClusterName : "my-cluster" ,
4041 SecurityRules : infrav1.SecurityRules {
41- {
42- Name : "allow_ssh" ,
43- Description : "Allow SSH" ,
44- Priority : 2200 ,
45- Protocol : infrav1 .SecurityGroupProtocolTCP ,
46- Direction : infrav1 .SecurityRuleDirectionInbound ,
47- Source : pointer .String ("*" ),
48- SourcePorts : pointer .String ("*" ),
49- Destination : pointer .String ("*" ),
50- DestinationPorts : pointer .String ("22" ),
51- },
52- {
53- Name : "other_rule" ,
54- Description : "Test Rule" ,
55- Priority : 500 ,
56- Protocol : infrav1 .SecurityGroupProtocolTCP ,
57- Direction : infrav1 .SecurityRuleDirectionInbound ,
58- Source : pointer .String ("*" ),
59- SourcePorts : pointer .String ("*" ),
60- Destination : pointer .String ("*" ),
61- DestinationPorts : pointer .String ("80" ),
62- },
42+ securityRule1 ,
6343 },
6444 ResourceGroup : "test-group" ,
6545 }
66- fakeNSG2 = NSGSpec {
46+ noRulesNSG = NSGSpec {
6747 Name : "test-nsg-2" ,
6848 Location : "test-location" ,
6949 ClusterName : "my-cluster" ,
7050 SecurityRules : infrav1.SecurityRules {},
7151 ResourceGroup : "test-group" ,
7252 }
53+ multipleRulesNSG = NSGSpec {
54+ Name : "multiple-rules-nsg" ,
55+ Location : "test-location" ,
56+ ClusterName : "my-cluster" ,
57+ SecurityRules : infrav1.SecurityRules {
58+ securityRule1 ,
59+ securityRule2 ,
60+ },
61+ ResourceGroup : "test-group" ,
62+ }
63+ securityRule1 = infrav1.SecurityRule {
64+ Name : "allow_ssh" ,
65+ Description : "Allow SSH" ,
66+ Priority : 2200 ,
67+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
68+ Direction : infrav1 .SecurityRuleDirectionInbound ,
69+ Source : pointer .String ("*" ),
70+ SourcePorts : pointer .String ("*" ),
71+ Destination : pointer .String ("*" ),
72+ DestinationPorts : pointer .String ("22" ),
73+ }
74+ securityRule2 = infrav1.SecurityRule {
75+ Name : "other_rule" ,
76+ Description : "Test Rule" ,
77+ Priority : 500 ,
78+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
79+ Direction : infrav1 .SecurityRuleDirectionInbound ,
80+ Source : pointer .String ("*" ),
81+ SourcePorts : pointer .String ("*" ),
82+ Destination : pointer .String ("*" ),
83+ DestinationPorts : pointer .String ("80" ),
84+ }
7385 errFake = errors .New ("this is an error" )
7486 notDoneError = azure .NewOperationNotDoneError (& infrav1.Future {})
7587)
@@ -81,13 +93,36 @@ func TestReconcileSecurityGroups(t *testing.T) {
8193 expect func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder )
8294 }{
8395 {
84- name : "create multiple security groups succeeds, should return no error" ,
96+ name : "create single security group with single rule succeeds, should return no error" ,
97+ expectedError : "" ,
98+ expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
99+ s .IsVnetManaged ().Return (true )
100+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG })
101+ s .UpdateAnnotationJSON (annotation , map [string ]interface {}{fakeNSG .Name : map [string ]string {securityRule1 .Name : securityRule1 .Description }}).Times (1 )
102+ r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (nil , nil )
103+ s .UpdatePutStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , nil )
104+ },
105+ },
106+ {
107+ name : "create single security group with multiple rules succeeds, should return no error" ,
108+ expectedError : "" ,
109+ expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
110+ s .IsVnetManaged ().Return (true )
111+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& multipleRulesNSG })
112+ s .UpdateAnnotationJSON (annotation , map [string ]interface {}{multipleRulesNSG .Name : map [string ]string {securityRule1 .Name : securityRule1 .Description , securityRule2 .Name : securityRule2 .Description }}).Times (1 )
113+ r .CreateOrUpdateResource (gomockinternal .AContext (), & multipleRulesNSG , serviceName ).Return (nil , nil )
114+ s .UpdatePutStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , nil )
115+ },
116+ },
117+ {
118+ name : "create multiple security groups, should return no error" ,
85119 expectedError : "" ,
86120 expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
87121 s .IsVnetManaged ().Return (true )
88- s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & fakeNSG2 })
122+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & noRulesNSG })
123+ s .UpdateAnnotationJSON (annotation , map [string ]interface {}{fakeNSG .Name : map [string ]string {securityRule1 .Name : securityRule1 .Description }}).Times (1 )
89124 r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (nil , nil )
90- r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG2 , serviceName ).Return (nil , nil )
125+ r .CreateOrUpdateResource (gomockinternal .AContext (), & noRulesNSG , serviceName ).Return (nil , nil )
91126 s .UpdatePutStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , nil )
92127 },
93128 },
@@ -96,9 +131,10 @@ func TestReconcileSecurityGroups(t *testing.T) {
96131 expectedError : errFake .Error (),
97132 expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
98133 s .IsVnetManaged ().Return (true )
99- s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & fakeNSG2 })
134+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & noRulesNSG })
135+ s .UpdateAnnotationJSON (annotation , map [string ]interface {}{fakeNSG .Name : map [string ]string {securityRule1 .Name : securityRule1 .Description }}).Times (1 )
100136 r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (nil , errFake )
101- r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG2 , serviceName ).Return (nil , nil )
137+ r .CreateOrUpdateResource (gomockinternal .AContext (), & noRulesNSG , serviceName ).Return (nil , nil )
102138 s .UpdatePutStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , errFake )
103139 },
104140 },
@@ -107,9 +143,10 @@ func TestReconcileSecurityGroups(t *testing.T) {
107143 expectedError : errFake .Error (),
108144 expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
109145 s .IsVnetManaged ().Return (true )
110- s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & fakeNSG2 })
146+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & noRulesNSG })
147+ s .UpdateAnnotationJSON (annotation , map [string ]interface {}{fakeNSG .Name : map [string ]string {securityRule1 .Name : securityRule1 .Description }}).Times (1 )
111148 r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (nil , errFake )
112- r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG2 , serviceName ).Return (nil , notDoneError )
149+ r .CreateOrUpdateResource (gomockinternal .AContext (), & noRulesNSG , serviceName ).Return (nil , notDoneError )
113150 s .UpdatePutStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , errFake )
114151 },
115152 },
@@ -119,6 +156,7 @@ func TestReconcileSecurityGroups(t *testing.T) {
119156 expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
120157 s .IsVnetManaged ().Return (true )
121158 s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG })
159+ s .UpdateAnnotationJSON (annotation , map [string ]interface {}{fakeNSG .Name : map [string ]string {securityRule1 .Name : securityRule1 .Description }})
122160 r .CreateOrUpdateResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (nil , notDoneError )
123161 s .UpdatePutStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , notDoneError )
124162 },
@@ -171,9 +209,9 @@ func TestDeleteSecurityGroups(t *testing.T) {
171209 expectedError : "" ,
172210 expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
173211 s .IsVnetManaged ().Return (true )
174- s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & fakeNSG2 })
212+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & noRulesNSG })
175213 r .DeleteResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (nil )
176- r .DeleteResource (gomockinternal .AContext (), & fakeNSG2 , serviceName ).Return (nil )
214+ r .DeleteResource (gomockinternal .AContext (), & noRulesNSG , serviceName ).Return (nil )
177215 s .UpdateDeleteStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , nil )
178216 },
179217 },
@@ -182,9 +220,9 @@ func TestDeleteSecurityGroups(t *testing.T) {
182220 expectedError : errFake .Error (),
183221 expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
184222 s .IsVnetManaged ().Return (true )
185- s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & fakeNSG2 })
223+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & noRulesNSG })
186224 r .DeleteResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (errFake )
187- r .DeleteResource (gomockinternal .AContext (), & fakeNSG2 , serviceName ).Return (nil )
225+ r .DeleteResource (gomockinternal .AContext (), & noRulesNSG , serviceName ).Return (nil )
188226 s .UpdateDeleteStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , errFake )
189227 },
190228 },
@@ -193,9 +231,9 @@ func TestDeleteSecurityGroups(t *testing.T) {
193231 expectedError : errFake .Error (),
194232 expect : func (s * mock_securitygroups.MockNSGScopeMockRecorder , r * mock_async.MockReconcilerMockRecorder ) {
195233 s .IsVnetManaged ().Return (true )
196- s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & fakeNSG2 })
234+ s .NSGSpecs ().Return ([]azure.ResourceSpecGetter {& fakeNSG , & noRulesNSG })
197235 r .DeleteResource (gomockinternal .AContext (), & fakeNSG , serviceName ).Return (errFake )
198- r .DeleteResource (gomockinternal .AContext (), & fakeNSG2 , serviceName ).Return (notDoneError )
236+ r .DeleteResource (gomockinternal .AContext (), & noRulesNSG , serviceName ).Return (notDoneError )
199237 s .UpdateDeleteStatus (infrav1 .SecurityGroupsReadyCondition , serviceName , errFake )
200238 },
201239 },
0 commit comments