|
| 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 availabilityzones |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net/http" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "sigs.k8s.io/cluster-api-provider-azure/cloud/services/availabilityzones/mock_availabilityzones" |
| 25 | + |
| 26 | + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute" |
| 27 | + "github.com/Azure/go-autorest/autorest" |
| 28 | + "github.com/golang/mock/gomock" |
| 29 | + |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha2" |
| 32 | + "sigs.k8s.io/cluster-api-provider-azure/cloud/scope" |
| 33 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 35 | +) |
| 36 | + |
| 37 | +func TestGetAvailabilityZones(t *testing.T) { |
| 38 | + testcases := []struct { |
| 39 | + name string |
| 40 | + availabilityZoneSpec Spec |
| 41 | + expectedError string |
| 42 | + expect func(m *mock_availabilityzones.MockClientMockRecorder) |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "empty availability zones", |
| 46 | + availabilityZoneSpec: Spec{VMSize: "Standard_B2ms"}, |
| 47 | + expectedError: "", |
| 48 | + expect: func(m *mock_availabilityzones.MockClientMockRecorder) { |
| 49 | + m.ListComplete(context.TODO()).Return(compute.ResourceSkusResultIterator{}, nil) |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "empty availability zones with error", |
| 54 | + availabilityZoneSpec: Spec{VMSize: "Standard_B2ms"}, |
| 55 | + expectedError: "#: Internal Server Error: StatusCode=500", |
| 56 | + expect: func(m *mock_availabilityzones.MockClientMockRecorder) { |
| 57 | + m.ListComplete(context.TODO()).Return(compute.ResourceSkusResultIterator{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")) |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "availability zones exist", |
| 62 | + availabilityZoneSpec: Spec{VMSize: "Standard_B2ms"}, |
| 63 | + expectedError: "", |
| 64 | + expect: func(m *mock_availabilityzones.MockClientMockRecorder) { |
| 65 | + m.ListComplete(context.TODO()).Return(compute.NewResourceSkusResultIterator(compute.ResourceSkusResultPage{}), nil) |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tc := range testcases { |
| 71 | + t.Run(tc.name, func(t *testing.T) { |
| 72 | + mockCtrl := gomock.NewController(t) |
| 73 | + azMock := mock_availabilityzones.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(azMock.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: infrav1.VnetSpec{Name: "my-vnet", ResourceGroup: "my-rg"}, |
| 96 | + Subnets: []*infrav1.SubnetSpec{{ |
| 97 | + Name: "my-subnet", |
| 98 | + Role: infrav1.SubnetNode, |
| 99 | + }}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("Failed to create test context: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + s := &Service{ |
| 109 | + Scope: clusterScope, |
| 110 | + Client: azMock, |
| 111 | + } |
| 112 | + |
| 113 | + if _, err := s.Get(context.TODO(), &tc.availabilityZoneSpec); err != nil { |
| 114 | + if tc.expectedError == "" || err.Error() != tc.expectedError { |
| 115 | + t.Fatalf("got an unexpected error: %v", err) |
| 116 | + } |
| 117 | + } else { |
| 118 | + if tc.expectedError != "" { |
| 119 | + t.Fatalf("expected an error: %v", tc.expectedError) |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments