@@ -19,19 +19,17 @@ package availabilitysets
1919import (
2020 "context"
2121 "errors"
22+ "testing"
2223
2324 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
2425 "github.com/Azure/go-autorest/autorest"
2526 "github.com/Azure/go-autorest/autorest/to"
2627 "github.com/golang/mock/gomock"
2728 . "github.com/onsi/gomega"
2829 "k8s.io/utils/pointer"
29- azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
3030 "sigs.k8s.io/cluster-api-provider-azure/cloud/services/resourceskus"
3131 gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock"
3232
33- "testing"
34-
3533 "k8s.io/klog/klogr"
3634 "sigs.k8s.io/cluster-api-provider-azure/cloud/services/availabilitysets/mock_availabilitysets"
3735)
@@ -48,8 +46,8 @@ func TestReconcileAvailabilitySets(t *testing.T) {
4846 expectedError : "" ,
4947 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
5048 s .V (gomock .AssignableToTypeOf (2 )).MinTimes (2 ).Return (klogr .New ())
49+ s .AvailabilitySet ().Return ("as-name" , true )
5150 s .ResourceGroup ().Return ("my-rg" )
52- s .AvailabilitySetSpecs ().Return ([]azure.AvailabilitySetSpec {{Name : "as-name" }}).Times (2 )
5351 s .ClusterName ().Return ("cl-name" )
5452 s .AdditionalTags ().Return (map [string ]string {})
5553 s .Location ().Return ("test-location" )
@@ -83,10 +81,10 @@ func TestReconcileAvailabilitySets(t *testing.T) {
8381 },
8482 },
8583 {
86- name : "noop if there are no availability set specs " ,
84+ name : "noop if the machine does not need to be assigned an availability set (machines without a deployment) " ,
8785 expectedError : "" ,
8886 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
89- s .AvailabilitySetSpecs ().Return (nil )
87+ s .AvailabilitySet ().Return ("as-name" , false )
9088 },
9189 setupSKUs : func (svc * Service ) {
9290 },
@@ -96,8 +94,8 @@ func TestReconcileAvailabilitySets(t *testing.T) {
9694 expectedError : "failed to create availability set as-name: something went wrong" ,
9795 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
9896 s .V (gomock .AssignableToTypeOf (2 )).AnyTimes ().Return (klogr .New ())
97+ s .AvailabilitySet ().Return ("as-name" , true )
9998 s .ResourceGroup ().Return ("my-rg" )
100- s .AvailabilitySetSpecs ().Return ([]azure.AvailabilitySetSpec {{Name : "as-name" }}).Times (2 )
10199 s .ClusterName ().Return ("cl-name" )
102100 s .AdditionalTags ().Return (map [string ]string {})
103101 s .Location ().Return ("test-location" )
@@ -164,47 +162,72 @@ func TestDeleteAvailabilitySets(t *testing.T) {
164162 expectedError : "" ,
165163 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
166164 s .V (gomock .AssignableToTypeOf (2 )).AnyTimes ().Return (klogr .New ())
167- s .ResourceGroup ().Return ("my-rg" )
168- s .AvailabilitySetSpecs ().Return ([]azure.AvailabilitySetSpec {{Name : "as-name" }})
165+ s .AvailabilitySet ().Return ("as-name" , true )
166+ s .ResourceGroup ().Return ("my-rg" ).Times (2 )
167+ m .Get (gomockinternal .AContext (), "my-rg" , "as-name" ).
168+ Return (compute.AvailabilitySet {AvailabilitySetProperties : & compute.AvailabilitySetProperties {}}, nil )
169169 m .Delete (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (nil )
170170 },
171171 },
172172 {
173- name : "noop if there are no availability sets " ,
173+ name : "noop if AvailabilitySet returns false " ,
174174 expectedError : "" ,
175175 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
176- s .AvailabilitySetSpecs ().Return (nil )
176+ s .AvailabilitySet ().Return ("as-name" , false )
177177 },
178178 },
179179 {
180- name : "returns error " ,
181- expectedError : "failed to delete availability set as-name in resource group my-rg: something went wrong " ,
180+ name : "noop if availability set has vms " ,
181+ expectedError : "" ,
182182 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
183- s .V (gomock .AssignableToTypeOf (2 )).AnyTimes ().Return (klogr .New ())
184- s .ResourceGroup ().Return ("my-rg" ).MinTimes (2 )
185- s .AvailabilitySetSpecs ().Return ([]azure.AvailabilitySetSpec {{Name : "as-name" }})
186- m .Delete (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (errors .New ("something went wrong" ))
183+ s .AvailabilitySet ().Return ("as-name" , true )
184+ s .ResourceGroup ().Return ("my-rg" )
185+ m .Get (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (compute.AvailabilitySet {
186+ AvailabilitySetProperties : & compute.AvailabilitySetProperties {VirtualMachines : & []compute.SubResource {
187+ {ID : to .StringPtr ("vm-id" )}}}}, nil )
187188 },
188189 },
189190 {
190- name : "noop if availability set is not found " ,
191+ name : "noop if availability set is already deleted - get returns 404 " ,
191192 expectedError : "" ,
192193 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
193194 s .V (gomock .AssignableToTypeOf (2 )).AnyTimes ().Return (klogr .New ())
195+ s .AvailabilitySet ().Return ("as-name" , true )
194196 s .ResourceGroup ().Return ("my-rg" )
195- s . AvailabilitySetSpecs (). Return ([]azure. AvailabilitySetSpec {{ Name : " as-name"}})
196- m . Delete ( gomockinternal . AContext (), "my-rg" , "as-name" ). Return ( autorest.DetailedError {StatusCode : 404 })
197+ m . Get ( gomockinternal . AContext (), "my-rg" , " as-name"). Return (compute. AvailabilitySet {},
198+ autorest.DetailedError {StatusCode : 404 })
197199 },
198200 },
199201 {
200- name : "noop if availability set is not found, and continue to the next one " ,
202+ name : "noop if availability set is already deleted - delete returns 404 " ,
201203 expectedError : "" ,
202204 expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
203205 s .V (gomock .AssignableToTypeOf (2 )).AnyTimes ().Return (klogr .New ())
204- s .ResourceGroup ().Return ("my-rg" ).MinTimes (2 )
205- s .AvailabilitySetSpecs ().Return ([]azure.AvailabilitySetSpec {{Name : "as-name-not-found" }, {Name : "as-name" }})
206- m .Delete (gomockinternal .AContext (), "my-rg" , "as-name-not-found" ).Return (autorest.DetailedError {StatusCode : 404 })
207- m .Delete (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (nil )
206+ s .AvailabilitySet ().Return ("as-name" , true )
207+ s .ResourceGroup ().Return ("my-rg" ).Times (2 )
208+ m .Get (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (compute.AvailabilitySet {}, nil )
209+ m .Delete (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (autorest.DetailedError {StatusCode : 404 })
210+ },
211+ },
212+ {
213+ name : "returns error when availability set get fails" ,
214+ expectedError : "failed to get availability set as-name in resource group my-rg: something went wrong" ,
215+ expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
216+ s .AvailabilitySet ().Return ("as-name" , true )
217+ s .ResourceGroup ().Return ("my-rg" ).Times (2 )
218+ m .Get (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (compute.AvailabilitySet {},
219+ errors .New ("something went wrong" ))
220+ },
221+ },
222+ {
223+ name : "returns error when delete fails" ,
224+ expectedError : "failed to delete availability set as-name in resource group my-rg: something went wrong" ,
225+ expect : func (s * mock_availabilitysets.MockAvailabilitySetScopeMockRecorder , m * mock_availabilitysets.MockClientMockRecorder ) {
226+ s .V (gomock .AssignableToTypeOf (2 )).AnyTimes ().Return (klogr .New ())
227+ s .AvailabilitySet ().Return ("as-name" , true )
228+ s .ResourceGroup ().Return ("my-rg" ).Times (3 )
229+ m .Get (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (compute.AvailabilitySet {}, nil )
230+ m .Delete (gomockinternal .AContext (), "my-rg" , "as-name" ).Return (errors .New ("something went wrong" ))
208231 },
209232 },
210233 }
0 commit comments