Skip to content

Commit 8fb66ae

Browse files
authored
Merge pull request kubernetes#88319 from ZeroMagic/azuredisk_fake_test
Export Azure Fake Cloud for reusing
2 parents 58596b2 + e2c733a commit 8fb66ae

File tree

10 files changed

+135
-127
lines changed

10 files changed

+135
-127
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ go_test(
120120
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
121121
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
122122
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
123-
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
124123
"//staging/src/k8s.io/cloud-provider:go_default_library",
125124
"//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library",
126125
"//staging/src/k8s.io/legacy-cloud-providers/azure/auth:go_default_library",

staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_common_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestCommonAttachDisk(t *testing.T) {
6363
}
6464

6565
for i, test := range testCases {
66-
testCloud := getTestCloud()
66+
testCloud := GetTestCloud()
6767
common := &controllerCommon{
6868
location: testCloud.Location,
6969
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
@@ -112,7 +112,7 @@ func TestCommonDetachDisk(t *testing.T) {
112112
}
113113

114114
for i, test := range testCases {
115-
testCloud := getTestCloud()
115+
testCloud := GetTestCloud()
116116
common := &controllerCommon{
117117
location: testCloud.Location,
118118
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
@@ -153,7 +153,7 @@ func TestGetDiskLun(t *testing.T) {
153153
}
154154

155155
for i, test := range testCases {
156-
testCloud := getTestCloud()
156+
testCloud := GetTestCloud()
157157
common := &controllerCommon{
158158
location: testCloud.Location,
159159
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
@@ -192,7 +192,7 @@ func TestGetNextDiskLun(t *testing.T) {
192192
}
193193

194194
for i, test := range testCases {
195-
testCloud := getTestCloud()
195+
testCloud := GetTestCloud()
196196
common := &controllerCommon{
197197
location: testCloud.Location,
198198
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
@@ -234,7 +234,7 @@ func TestDisksAreAttached(t *testing.T) {
234234
}
235235

236236
for i, test := range testCases {
237-
testCloud := getTestCloud()
237+
testCloud := GetTestCloud()
238238
common := &controllerCommon{
239239
location: testCloud.Location,
240240
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,

staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_standard_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestStandardAttachDisk(t *testing.T) {
5353
}
5454

5555
for i, test := range testCases {
56-
testCloud := getTestCloud()
56+
testCloud := GetTestCloud()
5757
vmSet := testCloud.vmSet
5858
setTestVirtualMachines(testCloud, map[string]string{"vm1": "PowerState/Running"}, false)
5959

@@ -90,7 +90,7 @@ func TestStandardDetachDisk(t *testing.T) {
9090
}
9191

9292
for i, test := range testCases {
93-
testCloud := getTestCloud()
93+
testCloud := GetTestCloud()
9494
vmSet := testCloud.vmSet
9595
setTestVirtualMachines(testCloud, map[string]string{"vm1": "PowerState/Running"}, false)
9696

@@ -140,7 +140,7 @@ func TestGetDataDisks(t *testing.T) {
140140
},
141141
}
142142
for i, test := range testCases {
143-
testCloud := getTestCloud()
143+
testCloud := GetTestCloud()
144144
vmSet := testCloud.vmSet
145145
setTestVirtualMachines(testCloud, map[string]string{"vm1": "PowerState/Running"}, false)
146146

staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import (
3434

3535
v1 "k8s.io/api/core/v1"
3636
"k8s.io/apimachinery/pkg/types"
37+
"k8s.io/apimachinery/pkg/util/sets"
38+
"k8s.io/client-go/tools/record"
3739
cloudprovider "k8s.io/cloud-provider"
40+
"k8s.io/legacy-cloud-providers/azure/auth"
3841
azcache "k8s.io/legacy-cloud-providers/azure/cache"
3942
"k8s.io/legacy-cloud-providers/azure/retry"
4043
)
@@ -875,6 +878,10 @@ func (fDC *fakeDisksClient) CreateOrUpdate(ctx context.Context, resourceGroupNam
875878
fDC.mutex.Lock()
876879
defer fDC.mutex.Unlock()
877880

881+
provisioningStateSucceeded := string(compute.ProvisioningStateSucceeded)
882+
diskParameter.DiskProperties = &compute.DiskProperties{ProvisioningState: &provisioningStateSucceeded}
883+
diskParameter.ID = &diskName
884+
878885
if _, ok := fDC.FakeStore[resourceGroupName]; !ok {
879886
fDC.FakeStore[resourceGroupName] = make(map[string]compute.Disk)
880887
}
@@ -991,3 +998,55 @@ func (f *fakeVMSet) GetDataDisks(nodeName types.NodeName, crt azcache.AzureCache
991998
func (f *fakeVMSet) GetPowerStatusByNodeName(name string) (string, error) {
992999
return "", fmt.Errorf("unimplemented")
9931000
}
1001+
1002+
// GetTestCloud returns a fake azure cloud for unit tests in Azure related CSI drivers
1003+
func GetTestCloud() (az *Cloud) {
1004+
az = &Cloud{
1005+
Config: Config{
1006+
AzureAuthConfig: auth.AzureAuthConfig{
1007+
TenantID: "tenant",
1008+
SubscriptionID: "subscription",
1009+
},
1010+
ResourceGroup: "rg",
1011+
VnetResourceGroup: "rg",
1012+
RouteTableResourceGroup: "rg",
1013+
SecurityGroupResourceGroup: "rg",
1014+
Location: "westus",
1015+
VnetName: "vnet",
1016+
SubnetName: "subnet",
1017+
SecurityGroupName: "nsg",
1018+
RouteTableName: "rt",
1019+
PrimaryAvailabilitySetName: "as",
1020+
MaximumLoadBalancerRuleCount: 250,
1021+
VMType: vmTypeStandard,
1022+
},
1023+
nodeZones: map[string]sets.String{},
1024+
nodeInformerSynced: func() bool { return true },
1025+
nodeResourceGroups: map[string]string{},
1026+
unmanagedNodes: sets.NewString(),
1027+
routeCIDRs: map[string]string{},
1028+
eventRecorder: &record.FakeRecorder{},
1029+
}
1030+
az.DisksClient = newFakeDisksClient()
1031+
az.InterfacesClient = newFakeAzureInterfacesClient()
1032+
az.LoadBalancerClient = newFakeAzureLBClient()
1033+
az.PublicIPAddressesClient = newFakeAzurePIPClient(az.Config.SubscriptionID)
1034+
az.RoutesClient = newFakeRoutesClient()
1035+
az.RouteTablesClient = newFakeRouteTablesClient()
1036+
az.SecurityGroupsClient = newFakeAzureNSGClient()
1037+
az.SubnetsClient = newFakeAzureSubnetsClient()
1038+
az.VirtualMachineScaleSetsClient = newFakeVirtualMachineScaleSetsClient()
1039+
az.VirtualMachineScaleSetVMsClient = newFakeVirtualMachineScaleSetVMsClient()
1040+
az.VirtualMachinesClient = newFakeAzureVirtualMachinesClient()
1041+
az.vmSet = newAvailabilitySet(az)
1042+
az.vmCache, _ = az.newVMCache()
1043+
az.lbCache, _ = az.newLBCache()
1044+
az.nsgCache, _ = az.newNSGCache()
1045+
az.rtCache, _ = az.newRouteTableCache()
1046+
1047+
common := &controllerCommon{cloud: az}
1048+
az.controllerCommon = common
1049+
az.ManagedDiskController = &ManagedDiskController{common: common}
1050+
1051+
return az
1052+
}

staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func setTestVirtualMachines(c *Cloud, vmList map[string]string, isDataDisksFull
8282
}
8383

8484
func TestInstanceID(t *testing.T) {
85-
cloud := getTestCloud()
85+
cloud := GetTestCloud()
8686
cloud.Config.UseInstanceMetadata = true
8787

8888
testcases := []struct {
@@ -215,7 +215,7 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
215215
}
216216

217217
for _, test := range testcases {
218-
cloud := getTestCloud()
218+
cloud := GetTestCloud()
219219
setTestVirtualMachines(cloud, test.vmList, false)
220220
providerID := "azure://" + cloud.getStandardMachineID("subscription", "rg", test.nodeName)
221221
hasShutdown, err := cloud.InstanceShutdownByProviderID(context.Background(), providerID)
@@ -236,7 +236,7 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
236236
}
237237

238238
func TestNodeAddresses(t *testing.T) {
239-
cloud := getTestCloud()
239+
cloud := GetTestCloud()
240240
cloud.Config.UseInstanceMetadata = true
241241
metadataTemplate := `{"compute":{"name":"%s"},"network":{"interface":[{"ipv4":{"ipAddress":[{"privateIpAddress":"%s","publicIpAddress":"%s"}]},"ipv6":{"ipAddress":[{"privateIpAddress":"%s","publicIpAddress":"%s"}]}}]}}`
242242

staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func TestEnsureLoadBalancerDeleted(t *testing.T) {
378378
},
379379
}
380380

381-
az := getTestCloud()
381+
az := GetTestCloud()
382382
for i, c := range tests {
383383
clusterResources := getClusterResources(az, vmCount, availabilitySetCount)
384384
getTestSecurityGroup(az)
@@ -488,7 +488,7 @@ func TestServiceOwnsPublicIP(t *testing.T) {
488488
}
489489

490490
func TestGetPublicIPAddressResourceGroup(t *testing.T) {
491-
az := getTestCloud()
491+
az := GetTestCloud()
492492

493493
for i, c := range []struct {
494494
desc string
@@ -693,7 +693,7 @@ func TestGetServiceLoadBalancer(t *testing.T) {
693693
}
694694

695695
for i, test := range testCases {
696-
az := getTestCloud()
696+
az := GetTestCloud()
697697
clusterResources := getClusterResources(az, 3, 3)
698698

699699
for _, existingLB := range test.existingLBs {
@@ -903,7 +903,7 @@ func TestIsFrontendIPChanged(t *testing.T) {
903903
}
904904

905905
for i, test := range testCases {
906-
az := getTestCloud()
906+
az := GetTestCloud()
907907
err := az.SubnetsClient.CreateOrUpdate(context.TODO(), "rg", "vnet", "testSubnet", test.exsistingSubnet)
908908
if err != nil {
909909
t.Fatalf("TestCase[%d] meets unexpected error: %v", i, err)
@@ -960,7 +960,7 @@ func TestDeterminePublicIPName(t *testing.T) {
960960
},
961961
}
962962
for i, test := range testCases {
963-
az := getTestCloud()
963+
az := GetTestCloud()
964964
service := getTestService("test1", v1.ProtocolTCP, nil, 80)
965965
service.Spec.LoadBalancerIP = test.loadBalancerIP
966966
for _, existingPIP := range test.exsistingPIPs {
@@ -1117,7 +1117,7 @@ func TestReconcileLoadBalancerRule(t *testing.T) {
11171117
},
11181118
}
11191119
for i, test := range testCases {
1120-
az := getTestCloud()
1120+
az := GetTestCloud()
11211121
az.Config.LoadBalancerSku = test.loadBalancerSku
11221122
probe, lbrule, err := az.reconcileLoadBalancerRule(&test.service, test.wantLb,
11231123
"frontendIPConfigID", "backendPoolID", "lbname", to.Int32Ptr(0))
@@ -1547,7 +1547,7 @@ func TestReconcileLoadBalancer(t *testing.T) {
15471547
}
15481548

15491549
for i, test := range testCases {
1550-
az := getTestCloud()
1550+
az := GetTestCloud()
15511551
az.Config.LoadBalancerSku = test.loadBalancerSku
15521552
az.DisableOutboundSNAT = test.disableOutboundSnat
15531553
if test.preConfigLBType != "" {
@@ -1583,7 +1583,7 @@ func TestReconcileLoadBalancer(t *testing.T) {
15831583
}
15841584

15851585
func TestGetServiceLoadBalancerStatus(t *testing.T) {
1586-
az := getTestCloud()
1586+
az := GetTestCloud()
15871587
service := getTestService("test1", v1.ProtocolTCP, nil, 80)
15881588
internalService := getInternalTestService("test1", 80)
15891589

@@ -1804,7 +1804,7 @@ func TestReconcileSecurityGroup(t *testing.T) {
18041804
}
18051805

18061806
for i, test := range testCases {
1807-
az := getTestCloud()
1807+
az := GetTestCloud()
18081808
for name, sg := range test.existingSgs {
18091809
err := az.SecurityGroupsClient.CreateOrUpdate(context.TODO(), "rg", name, sg, "")
18101810
if err != nil {
@@ -1852,7 +1852,7 @@ func TestSafeDeletePublicIP(t *testing.T) {
18521852
}
18531853

18541854
for i, test := range testCases {
1855-
az := getTestCloud()
1855+
az := GetTestCloud()
18561856
err := az.PublicIPAddressesClient.CreateOrUpdate(context.TODO(), "rg", "pip1", network.PublicIPAddress{
18571857
Name: to.StringPtr("pip1"),
18581858
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
@@ -1971,7 +1971,7 @@ func TestReconcilePublicIP(t *testing.T) {
19711971
}
19721972

19731973
for i, test := range testCases {
1974-
az := getTestCloud()
1974+
az := GetTestCloud()
19751975
service := getTestService("test1", v1.ProtocolTCP, nil, 80)
19761976
service.Annotations = test.annotations
19771977
for _, pip := range test.existingPIPs {
@@ -2082,7 +2082,7 @@ func TestEnsurePublicIPExists(t *testing.T) {
20822082
}
20832083

20842084
for i, test := range testCases {
2085-
az := getTestCloud()
2085+
az := GetTestCloud()
20862086
service := getTestService("test1", v1.ProtocolTCP, nil, 80)
20872087
for _, pip := range test.existingPIPs {
20882088
err := az.PublicIPAddressesClient.CreateOrUpdate(context.TODO(), "rg", to.String(pip.Name), pip)
@@ -2134,7 +2134,7 @@ func TestShouldUpdateLoadBalancer(t *testing.T) {
21342134
}
21352135

21362136
for i, test := range testCases {
2137-
az := getTestCloud()
2137+
az := GetTestCloud()
21382138
service := getTestService("test1", v1.ProtocolTCP, nil, 80)
21392139
if test.lbHasDeletionTimestamp {
21402140
service.ObjectMeta.DeletionTimestamp = &metav1.Time{Time: time.Now()}
@@ -2221,7 +2221,7 @@ func TestIsBackendPoolPreConfigured(t *testing.T) {
22212221
}
22222222

22232223
for i, test := range testCases {
2224-
az := getTestCloud()
2224+
az := GetTestCloud()
22252225
az.Config.PreConfiguredBackendPoolLoadBalancerTypes = test.preConfiguredBackendPoolLoadBalancerTypes
22262226
var service v1.Service
22272227
if test.isInternalService {

staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestGenerateStorageAccountName(t *testing.T) {
127127
}
128128

129129
func TestMapLoadBalancerNameToVMSet(t *testing.T) {
130-
az := getTestCloud()
130+
az := GetTestCloud()
131131
az.PrimaryAvailabilitySetName = "primary"
132132

133133
cases := []struct {
@@ -175,7 +175,7 @@ func TestMapLoadBalancerNameToVMSet(t *testing.T) {
175175
}
176176

177177
func TestGetAzureLoadBalancerName(t *testing.T) {
178-
az := getTestCloud()
178+
az := GetTestCloud()
179179
az.PrimaryAvailabilitySetName = "primary"
180180

181181
cases := []struct {
@@ -256,7 +256,7 @@ func TestGetAzureLoadBalancerName(t *testing.T) {
256256
}
257257

258258
func TestGetLoadBalancingRuleName(t *testing.T) {
259-
az := getTestCloud()
259+
az := GetTestCloud()
260260
az.PrimaryAvailabilitySetName = "primary"
261261

262262
svc := &v1.Service{
@@ -337,7 +337,7 @@ func TestGetLoadBalancingRuleName(t *testing.T) {
337337
}
338338

339339
func TestGetFrontendIPConfigName(t *testing.T) {
340-
az := getTestCloud()
340+
az := GetTestCloud()
341341
az.PrimaryAvailabilitySetName = "primary"
342342

343343
svc := &v1.Service{

0 commit comments

Comments
 (0)