|
| 1 | +/* |
| 2 | +Copyright 2022 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 cloud_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + |
| 22 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 23 | + "github.com/golang/mock/gomock" |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud" |
| 27 | + "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies" |
| 28 | +) |
| 29 | + |
| 30 | +var _ = Describe("AffinityGroup Unit Tests", func() { |
| 31 | + var ( // Declare shared vars. |
| 32 | + mockCtrl *gomock.Controller |
| 33 | + mockClient *cloudstack.CloudStackClient |
| 34 | + ags *cloudstack.MockAffinityGroupServiceIface |
| 35 | + client cloud.Client |
| 36 | + ) |
| 37 | + |
| 38 | + BeforeEach(func() { |
| 39 | + // Setup new mock services. |
| 40 | + mockCtrl = gomock.NewController(GinkgoT()) |
| 41 | + mockClient = cloudstack.NewMockClient(mockCtrl) |
| 42 | + ags = mockClient.AffinityGroup.(*cloudstack.MockAffinityGroupServiceIface) |
| 43 | + client = cloud.NewClientFromCSAPIClient(mockClient) |
| 44 | + dummies.SetDummyVars() |
| 45 | + }) |
| 46 | + |
| 47 | + AfterEach(func() { |
| 48 | + mockCtrl.Finish() |
| 49 | + }) |
| 50 | + |
| 51 | + It("fetches an affinity group", func() { |
| 52 | + dummies.AffinityGroup.ID = "" // Force name fetching. |
| 53 | + ags.EXPECT().GetAffinityGroupByName(dummies.AffinityGroup.Name).Return(&cloudstack.AffinityGroup{}, 1, nil) |
| 54 | + |
| 55 | + Ω(client.GetOrCreateAffinityGroup(dummies.AffinityGroup)).Should(Succeed()) |
| 56 | + }) |
| 57 | + It("creates an affinity group", func() { |
| 58 | + dummies.SetDummyDomainAndAccount() |
| 59 | + dummies.SetDummyDomainID() |
| 60 | + ags.EXPECT().GetAffinityGroupByID(dummies.AffinityGroup.ID).Return(nil, -1, errors.New("FakeError")) |
| 61 | + ags.EXPECT().NewCreateAffinityGroupParams(dummies.AffinityGroup.Name, dummies.AffinityGroup.Type). |
| 62 | + Return(&cloudstack.CreateAffinityGroupParams{}) |
| 63 | + ags.EXPECT().CreateAffinityGroup(ParamMatch(And(NameEquals(dummies.AffinityGroup.Name)))). |
| 64 | + Return(&cloudstack.CreateAffinityGroupResponse{}, nil) |
| 65 | + |
| 66 | + Ω(client.GetOrCreateAffinityGroup(dummies.AffinityGroup)).Should(Succeed()) |
| 67 | + }) |
| 68 | + |
| 69 | + Context("AffinityGroup Integ Tests", func() { |
| 70 | + client, connectionErr := cloud.NewClient("../../cloud-config") |
| 71 | + |
| 72 | + BeforeEach(func() { |
| 73 | + if connectionErr != nil { // Only do these tests if an actual ACS instance is available via cloud-config. |
| 74 | + Skip("Could not connect to ACS instance.") |
| 75 | + } |
| 76 | + dummies.AffinityGroup.ID = "" // Force name fetching. |
| 77 | + }) |
| 78 | + AfterEach(func() { |
| 79 | + mockCtrl.Finish() |
| 80 | + }) |
| 81 | + |
| 82 | + It("Creates an affinity group.", func() { |
| 83 | + Ω(client.GetOrCreateAffinityGroup(dummies.AffinityGroup)).Should(Succeed()) |
| 84 | + }) |
| 85 | + It("Associates an affinity group.", func() { |
| 86 | + if err := client.GetOrCreateVMInstance( |
| 87 | + dummies.CSMachine1, dummies.CAPIMachine, dummies.CSCluster, dummies.CSZone1, dummies.CSAffinityGroup, "", |
| 88 | + ); err != nil { |
| 89 | + Skip("Could not create VM." + err.Error()) |
| 90 | + } |
| 91 | + Ω(client.GetOrCreateAffinityGroup(dummies.AffinityGroup)).Should(Succeed()) |
| 92 | + Ω(client.AssociateAffinityGroup(dummies.CSMachine1, *dummies.AffinityGroup)).Should(Succeed()) |
| 93 | + }) |
| 94 | + It("Deletes an affinity group.", func() { |
| 95 | + Ω(client.DeleteAffinityGroup(dummies.AffinityGroup)).Should(Succeed()) |
| 96 | + Ω(client.FetchAffinityGroup(dummies.AffinityGroup)).ShouldNot(Succeed()) |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments