|
| 1 | +/* |
| 2 | +Copyright 2020 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 agentpools |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net/http" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2020-02-01/containerservice" |
| 25 | + "github.com/Azure/go-autorest/autorest" |
| 26 | + "github.com/golang/mock/gomock" |
| 27 | + . "github.com/onsi/gomega" |
| 28 | + "sigs.k8s.io/cluster-api-provider-azure/cloud/services/agentpools/mock_agentpools" |
| 29 | +) |
| 30 | + |
| 31 | +func TestReconcile(t *testing.T) { |
| 32 | + g := NewWithT(t) |
| 33 | + |
| 34 | + provisioningstatetestcases := []struct { |
| 35 | + name string |
| 36 | + agentpoolspec Spec |
| 37 | + provisioningStatesToTest []string |
| 38 | + expectedError string |
| 39 | + expect func(m *mock_agentpools.MockClientMockRecorder, provisioningstate string) |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "agentpool in terminal provisioning state", |
| 43 | + agentpoolspec: Spec{ |
| 44 | + ResourceGroup: "my-rg", |
| 45 | + Cluster: "my-cluster", |
| 46 | + Name: "my-agentpool", |
| 47 | + }, |
| 48 | + provisioningStatesToTest: []string{"Canceled", "Succeeded", "Failed"}, |
| 49 | + expectedError: "", |
| 50 | + expect: func(m *mock_agentpools.MockClientMockRecorder, provisioningstate string) { |
| 51 | + m.CreateOrUpdate(context.TODO(), "my-rg", "my-cluster", "my-agentpool", gomock.Any()).Return(nil) |
| 52 | + m.Get(context.TODO(), "my-rg", "my-cluster", "my-agentpool").Return(containerservice.AgentPool{ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{ |
| 53 | + ProvisioningState: &provisioningstate, |
| 54 | + }}, nil) |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "agentpool in nonterminal provisioning state", |
| 59 | + agentpoolspec: Spec{ |
| 60 | + ResourceGroup: "my-rg", |
| 61 | + Cluster: "my-cluster", |
| 62 | + Name: "my-agentpool", |
| 63 | + }, |
| 64 | + provisioningStatesToTest: []string{"Deleting", "InProgress", "randomStringHere"}, |
| 65 | + expectedError: "", |
| 66 | + expect: func(m *mock_agentpools.MockClientMockRecorder, provisioningstate string) { |
| 67 | + m.Get(context.TODO(), "my-rg", "my-cluster", "my-agentpool").Return(containerservice.AgentPool{ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{ |
| 68 | + ProvisioningState: &provisioningstate, |
| 69 | + }}, nil) |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tc := range provisioningstatetestcases { |
| 75 | + for _, provisioningstate := range tc.provisioningStatesToTest { |
| 76 | + t.Logf("Testing agentpool provision state: " + provisioningstate) |
| 77 | + t.Run(tc.name, func(t *testing.T) { |
| 78 | + mockCtrl := gomock.NewController(t) |
| 79 | + agentpoolsMock := mock_agentpools.NewMockClient(mockCtrl) |
| 80 | + |
| 81 | + tc.expect(agentpoolsMock.EXPECT(), provisioningstate) |
| 82 | + |
| 83 | + s := &Service{ |
| 84 | + Client: agentpoolsMock, |
| 85 | + } |
| 86 | + |
| 87 | + err := s.Reconcile(context.TODO(), &tc.agentpoolspec) |
| 88 | + if tc.expectedError != "" { |
| 89 | + g.Expect(err).To(HaveOccurred()) |
| 90 | + g.Expect(err).To(MatchError(tc.expectedError)) |
| 91 | + } else { |
| 92 | + g.Expect(err).NotTo(HaveOccurred()) |
| 93 | + mockCtrl.Finish() |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + testcases := []struct { |
| 100 | + name string |
| 101 | + agentpoolspec Spec |
| 102 | + expectedError string |
| 103 | + expect func(m *mock_agentpools.MockClientMockRecorder) |
| 104 | + }{ |
| 105 | + { |
| 106 | + name: "no agentpool exists", |
| 107 | + agentpoolspec: Spec{ |
| 108 | + ResourceGroup: "my-rg", |
| 109 | + Cluster: "my-cluster", |
| 110 | + Name: "my-agentpool", |
| 111 | + }, |
| 112 | + expectedError: "", |
| 113 | + expect: func(m *mock_agentpools.MockClientMockRecorder) { |
| 114 | + m.CreateOrUpdate(context.TODO(), "my-rg", "my-cluster", "my-agentpool", gomock.Any()).Return(nil) |
| 115 | + m.Get(context.TODO(), "my-rg", "my-cluster", "my-agentpool").Return(containerservice.AgentPool{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not Found")) |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tc := range testcases { |
| 121 | + t.Logf("Testing " + tc.name) |
| 122 | + t.Run(tc.name, func(t *testing.T) { |
| 123 | + mockCtrl := gomock.NewController(t) |
| 124 | + agentpoolsMock := mock_agentpools.NewMockClient(mockCtrl) |
| 125 | + |
| 126 | + tc.expect(agentpoolsMock.EXPECT()) |
| 127 | + |
| 128 | + s := &Service{ |
| 129 | + Client: agentpoolsMock, |
| 130 | + } |
| 131 | + |
| 132 | + err := s.Reconcile(context.TODO(), &tc.agentpoolspec) |
| 133 | + if tc.expectedError != "" { |
| 134 | + g.Expect(err).To(HaveOccurred()) |
| 135 | + g.Expect(err).To(MatchError(tc.expectedError)) |
| 136 | + } else { |
| 137 | + g.Expect(err).NotTo(HaveOccurred()) |
| 138 | + mockCtrl.Finish() |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments