Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Azure Fake Cloud for reusing #88319

Merged
merged 1 commit into from Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion staging/src/k8s.io/legacy-cloud-providers/azure/BUILD
Expand Up @@ -120,7 +120,6 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
"//staging/src/k8s.io/cloud-provider:go_default_library",
"//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library",
"//staging/src/k8s.io/legacy-cloud-providers/azure/auth:go_default_library",
Expand Down
Expand Up @@ -63,7 +63,7 @@ func TestCommonAttachDisk(t *testing.T) {
}

for i, test := range testCases {
testCloud := getTestCloud()
testCloud := GetTestCloud()
common := &controllerCommon{
location: testCloud.Location,
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestCommonDetachDisk(t *testing.T) {
}

for i, test := range testCases {
testCloud := getTestCloud()
testCloud := GetTestCloud()
common := &controllerCommon{
location: testCloud.Location,
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestGetDiskLun(t *testing.T) {
}

for i, test := range testCases {
testCloud := getTestCloud()
testCloud := GetTestCloud()
common := &controllerCommon{
location: testCloud.Location,
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestGetNextDiskLun(t *testing.T) {
}

for i, test := range testCases {
testCloud := getTestCloud()
testCloud := GetTestCloud()
common := &controllerCommon{
location: testCloud.Location,
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestDisksAreAttached(t *testing.T) {
}

for i, test := range testCases {
testCloud := getTestCloud()
testCloud := GetTestCloud()
common := &controllerCommon{
location: testCloud.Location,
storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix,
Expand Down
Expand Up @@ -53,7 +53,7 @@ func TestStandardAttachDisk(t *testing.T) {
}

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

Expand Down Expand Up @@ -90,7 +90,7 @@ func TestStandardDetachDisk(t *testing.T) {
}

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

Expand Down Expand Up @@ -140,7 +140,7 @@ func TestGetDataDisks(t *testing.T) {
},
}
for i, test := range testCases {
testCloud := getTestCloud()
testCloud := GetTestCloud()
vmSet := testCloud.vmSet
setTestVirtualMachines(testCloud, map[string]string{"vm1": "PowerState/Running"}, false)

Expand Down
59 changes: 59 additions & 0 deletions staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go
Expand Up @@ -34,7 +34,10 @@ import (

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/legacy-cloud-providers/azure/auth"
azcache "k8s.io/legacy-cloud-providers/azure/cache"
"k8s.io/legacy-cloud-providers/azure/retry"
)
Expand Down Expand Up @@ -875,6 +878,10 @@ func (fDC *fakeDisksClient) CreateOrUpdate(ctx context.Context, resourceGroupNam
fDC.mutex.Lock()
defer fDC.mutex.Unlock()

provisioningStateSucceeded := string(compute.ProvisioningStateSucceeded)
diskParameter.DiskProperties = &compute.DiskProperties{ProvisioningState: &provisioningStateSucceeded}
diskParameter.ID = &diskName

if _, ok := fDC.FakeStore[resourceGroupName]; !ok {
fDC.FakeStore[resourceGroupName] = make(map[string]compute.Disk)
}
Expand Down Expand Up @@ -991,3 +998,55 @@ func (f *fakeVMSet) GetDataDisks(nodeName types.NodeName, crt azcache.AzureCache
func (f *fakeVMSet) GetPowerStatusByNodeName(name string) (string, error) {
return "", fmt.Errorf("unimplemented")
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe could add a comment here: why make this func public

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// GetTestCloud returns a fake azure cloud for unit tests in Azure related CSI drivers
func GetTestCloud() (az *Cloud) {
az = &Cloud{
Config: Config{
AzureAuthConfig: auth.AzureAuthConfig{
TenantID: "tenant",
SubscriptionID: "subscription",
},
ResourceGroup: "rg",
VnetResourceGroup: "rg",
RouteTableResourceGroup: "rg",
SecurityGroupResourceGroup: "rg",
Location: "westus",
VnetName: "vnet",
SubnetName: "subnet",
SecurityGroupName: "nsg",
RouteTableName: "rt",
PrimaryAvailabilitySetName: "as",
MaximumLoadBalancerRuleCount: 250,
VMType: vmTypeStandard,
},
nodeZones: map[string]sets.String{},
nodeInformerSynced: func() bool { return true },
nodeResourceGroups: map[string]string{},
unmanagedNodes: sets.NewString(),
routeCIDRs: map[string]string{},
eventRecorder: &record.FakeRecorder{},
}
az.DisksClient = newFakeDisksClient()
az.InterfacesClient = newFakeAzureInterfacesClient()
az.LoadBalancerClient = newFakeAzureLBClient()
az.PublicIPAddressesClient = newFakeAzurePIPClient(az.Config.SubscriptionID)
az.RoutesClient = newFakeRoutesClient()
az.RouteTablesClient = newFakeRouteTablesClient()
az.SecurityGroupsClient = newFakeAzureNSGClient()
az.SubnetsClient = newFakeAzureSubnetsClient()
az.VirtualMachineScaleSetsClient = newFakeVirtualMachineScaleSetsClient()
az.VirtualMachineScaleSetVMsClient = newFakeVirtualMachineScaleSetVMsClient()
az.VirtualMachinesClient = newFakeAzureVirtualMachinesClient()
az.vmSet = newAvailabilitySet(az)
az.vmCache, _ = az.newVMCache()
az.lbCache, _ = az.newLBCache()
az.nsgCache, _ = az.newNSGCache()
az.rtCache, _ = az.newRouteTableCache()

common := &controllerCommon{cloud: az}
az.controllerCommon = common
az.ManagedDiskController = &ManagedDiskController{common: common}

return az
}
Expand Up @@ -82,7 +82,7 @@ func setTestVirtualMachines(c *Cloud, vmList map[string]string, isDataDisksFull
}

func TestInstanceID(t *testing.T) {
cloud := getTestCloud()
cloud := GetTestCloud()
cloud.Config.UseInstanceMetadata = true

testcases := []struct {
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
}

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

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

Expand Down
Expand Up @@ -378,7 +378,7 @@ func TestEnsureLoadBalancerDeleted(t *testing.T) {
},
}

az := getTestCloud()
az := GetTestCloud()
for i, c := range tests {
clusterResources := getClusterResources(az, vmCount, availabilitySetCount)
getTestSecurityGroup(az)
Expand Down Expand Up @@ -488,7 +488,7 @@ func TestServiceOwnsPublicIP(t *testing.T) {
}

func TestGetPublicIPAddressResourceGroup(t *testing.T) {
az := getTestCloud()
az := GetTestCloud()

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

for i, test := range testCases {
az := getTestCloud()
az := GetTestCloud()
clusterResources := getClusterResources(az, 3, 3)

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

for i, test := range testCases {
az := getTestCloud()
az := GetTestCloud()
err := az.SubnetsClient.CreateOrUpdate(context.TODO(), "rg", "vnet", "testSubnet", test.exsistingSubnet)
if err != nil {
t.Fatalf("TestCase[%d] meets unexpected error: %v", i, err)
Expand Down Expand Up @@ -960,7 +960,7 @@ func TestDeterminePublicIPName(t *testing.T) {
},
}
for i, test := range testCases {
az := getTestCloud()
az := GetTestCloud()
service := getTestService("test1", v1.ProtocolTCP, nil, 80)
service.Spec.LoadBalancerIP = test.loadBalancerIP
for _, existingPIP := range test.exsistingPIPs {
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func TestReconcileLoadBalancerRule(t *testing.T) {
},
}
for i, test := range testCases {
az := getTestCloud()
az := GetTestCloud()
az.Config.LoadBalancerSku = test.loadBalancerSku
probe, lbrule, err := az.reconcileLoadBalancerRule(&test.service, test.wantLb,
"frontendIPConfigID", "backendPoolID", "lbname", to.Int32Ptr(0))
Expand Down Expand Up @@ -1547,7 +1547,7 @@ func TestReconcileLoadBalancer(t *testing.T) {
}

for i, test := range testCases {
az := getTestCloud()
az := GetTestCloud()
az.Config.LoadBalancerSku = test.loadBalancerSku
az.DisableOutboundSNAT = test.disableOutboundSnat
if test.preConfigLBType != "" {
Expand Down Expand Up @@ -1583,7 +1583,7 @@ func TestReconcileLoadBalancer(t *testing.T) {
}

func TestGetServiceLoadBalancerStatus(t *testing.T) {
az := getTestCloud()
az := GetTestCloud()
service := getTestService("test1", v1.ProtocolTCP, nil, 80)
internalService := getInternalTestService("test1", 80)

Expand Down Expand Up @@ -1804,7 +1804,7 @@ func TestReconcileSecurityGroup(t *testing.T) {
}

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

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

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

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

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

for i, test := range testCases {
az := getTestCloud()
az := GetTestCloud()
az.Config.PreConfiguredBackendPoolLoadBalancerTypes = test.preConfiguredBackendPoolLoadBalancerTypes
var service v1.Service
if test.isInternalService {
Expand Down
Expand Up @@ -127,7 +127,7 @@ func TestGenerateStorageAccountName(t *testing.T) {
}

func TestMapLoadBalancerNameToVMSet(t *testing.T) {
az := getTestCloud()
az := GetTestCloud()
az.PrimaryAvailabilitySetName = "primary"

cases := []struct {
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestMapLoadBalancerNameToVMSet(t *testing.T) {
}

func TestGetAzureLoadBalancerName(t *testing.T) {
az := getTestCloud()
az := GetTestCloud()
az.PrimaryAvailabilitySetName = "primary"

cases := []struct {
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestGetAzureLoadBalancerName(t *testing.T) {
}

func TestGetLoadBalancingRuleName(t *testing.T) {
az := getTestCloud()
az := GetTestCloud()
az.PrimaryAvailabilitySetName = "primary"

svc := &v1.Service{
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestGetLoadBalancingRuleName(t *testing.T) {
}

func TestGetFrontendIPConfigName(t *testing.T) {
az := getTestCloud()
az := GetTestCloud()
az.PrimaryAvailabilitySetName = "primary"

svc := &v1.Service{
Expand Down