Skip to content

Commit 179f299

Browse files
committed
add unit tests for azure security groups
1 parent 178e4ba commit 179f299

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package securitygroups
18+
19+
import (
20+
"context"
21+
"net/http"
22+
"testing"
23+
24+
"github.com/Azure/go-autorest/autorest"
25+
"github.com/golang/mock/gomock"
26+
27+
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha2"
30+
"sigs.k8s.io/cluster-api-provider-azure/cloud/scope"
31+
"sigs.k8s.io/cluster-api-provider-azure/cloud/services/securitygroups/mock_securitygroups"
32+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
33+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
34+
)
35+
36+
func TestReconcileSecurityGroups(t *testing.T) {
37+
testcases := []struct {
38+
name string
39+
sgName string
40+
isControlPlane bool
41+
vnetSpec *infrav1.VnetSpec
42+
expect func(m *mock_securitygroups.MockClientMockRecorder)
43+
}{
44+
{
45+
name: "security group does not exists",
46+
sgName: "my-sg",
47+
isControlPlane: true,
48+
vnetSpec: &infrav1.VnetSpec{},
49+
expect: func(m *mock_securitygroups.MockClientMockRecorder) {
50+
m.CreateOrUpdate(context.TODO(), "my-rg", "my-sg", gomock.AssignableToTypeOf(network.SecurityGroup{}))
51+
},
52+
}, {
53+
name: "security group does not exist and it's not for a control plane",
54+
sgName: "my-sg",
55+
isControlPlane: false,
56+
vnetSpec: &infrav1.VnetSpec{},
57+
expect: func(m *mock_securitygroups.MockClientMockRecorder) {
58+
m.CreateOrUpdate(context.TODO(), "my-rg", "my-sg", gomock.AssignableToTypeOf(network.SecurityGroup{}))
59+
},
60+
}, {
61+
name: "skipping network security group reconcile in custom vnet mode",
62+
sgName: "my-sg",
63+
isControlPlane: false,
64+
vnetSpec: &infrav1.VnetSpec{ResourceGroup: "custom-vnet-rg", Name: "custom-vnet", ID: "id1"},
65+
expect: func(m *mock_securitygroups.MockClientMockRecorder) {
66+
67+
},
68+
},
69+
}
70+
for _, tc := range testcases {
71+
t.Run(tc.name, func(t *testing.T) {
72+
mockCtrl := gomock.NewController(t)
73+
sgMock := mock_securitygroups.NewMockClient(mockCtrl)
74+
75+
cluster := &clusterv1.Cluster{
76+
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster"},
77+
}
78+
79+
client := fake.NewFakeClient(cluster)
80+
81+
tc.expect(sgMock.EXPECT())
82+
83+
clusterScope, err := scope.NewClusterScope(scope.ClusterScopeParams{
84+
AzureClients: scope.AzureClients{
85+
SubscriptionID: "123",
86+
Authorizer: autorest.NullAuthorizer{},
87+
},
88+
Client: client,
89+
Cluster: cluster,
90+
AzureCluster: &infrav1.AzureCluster{
91+
Spec: infrav1.AzureClusterSpec{
92+
Location: "test-location",
93+
ResourceGroup: "my-rg",
94+
NetworkSpec: infrav1.NetworkSpec{
95+
Vnet: *tc.vnetSpec,
96+
},
97+
},
98+
},
99+
})
100+
if err != nil {
101+
t.Fatalf("Failed to create test context: %v", err)
102+
}
103+
104+
s := &Service{
105+
Scope: clusterScope,
106+
Client: sgMock,
107+
}
108+
109+
sgSpec := &Spec{
110+
Name: tc.sgName,
111+
IsControlPlane: tc.isControlPlane,
112+
}
113+
if err := s.Reconcile(context.TODO(), sgSpec); err != nil {
114+
t.Fatalf("got an unexpected error: %v", err)
115+
}
116+
})
117+
}
118+
}
119+
120+
func TestDeleteSecurityGroups(t *testing.T) {
121+
testcases := []struct {
122+
name string
123+
sgName string
124+
expect func(m *mock_securitygroups.MockClientMockRecorder)
125+
}{
126+
{
127+
name: "security group exists",
128+
sgName: "my-sg",
129+
expect: func(m *mock_securitygroups.MockClientMockRecorder) {
130+
m.Delete(context.TODO(), "my-rg", "my-sg")
131+
},
132+
},
133+
{
134+
name: "security group already deleted",
135+
sgName: "my-sg",
136+
expect: func(m *mock_securitygroups.MockClientMockRecorder) {
137+
m.Delete(context.TODO(), "my-rg", "my-sg").
138+
Return(autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not found"))
139+
},
140+
},
141+
}
142+
for _, tc := range testcases {
143+
t.Run(tc.name, func(t *testing.T) {
144+
mockCtrl := gomock.NewController(t)
145+
sgMock := mock_securitygroups.NewMockClient(mockCtrl)
146+
147+
cluster := &clusterv1.Cluster{
148+
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster"},
149+
}
150+
151+
client := fake.NewFakeClient(cluster)
152+
153+
tc.expect(sgMock.EXPECT())
154+
155+
clusterScope, err := scope.NewClusterScope(scope.ClusterScopeParams{
156+
AzureClients: scope.AzureClients{
157+
SubscriptionID: "123",
158+
Authorizer: autorest.NullAuthorizer{},
159+
},
160+
Client: client,
161+
Cluster: cluster,
162+
AzureCluster: &infrav1.AzureCluster{
163+
Spec: infrav1.AzureClusterSpec{
164+
Location: "test-location",
165+
ResourceGroup: "my-rg",
166+
},
167+
},
168+
})
169+
if err != nil {
170+
t.Fatalf("Failed to create test context: %v", err)
171+
}
172+
173+
s := &Service{
174+
Scope: clusterScope,
175+
Client: sgMock,
176+
}
177+
178+
sgSpec := &Spec{
179+
Name: tc.sgName,
180+
IsControlPlane: false,
181+
}
182+
183+
if err := s.Delete(context.TODO(), sgSpec); err != nil {
184+
t.Fatalf("got an unexpected error: %v", err)
185+
}
186+
})
187+
}
188+
}

0 commit comments

Comments
 (0)