Skip to content

Commit 38eab63

Browse files
author
Joshua Reed
committed
All tests excepting isolated use cases passing.
1 parent 6843713 commit 38eab63

File tree

4 files changed

+64
-50
lines changed

4 files changed

+64
-50
lines changed

pkg/cloud/affinity_groups_test.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,19 @@ var _ = Describe("AffinityGroup Unit Tests", func() {
4949
})
5050

5151
It("fetches an affinity group", func() {
52+
dummies.AffinityGroup.ID = "" // Force name fetching.
5253
ags.EXPECT().GetAffinityGroupByName(dummies.AffinityGroup.Name).Return(&cloudstack.AffinityGroup{}, 1, nil)
5354

5455
Ω(client.GetOrCreateAffinityGroup(dummies.CSCluster, dummies.AffinityGroup)).Should(Succeed())
5556
})
5657
It("creates an affinity group", func() {
57-
dummies.AffinityGroup.ID = "FakeID"
58-
dummies.CSCluster.Spec.Account = "FakeAccount"
59-
dummies.CSCluster.Status.DomainID = "FakeDomainID"
58+
dummies.SetDummyDomainAndAccount()
59+
dummies.SetDummyDomainID()
6060
ags.EXPECT().GetAffinityGroupByID(dummies.AffinityGroup.ID).Return(nil, -1, errors.New("FakeError"))
6161
ags.EXPECT().NewCreateAffinityGroupParams(dummies.AffinityGroup.Name, dummies.AffinityGroup.Type).
6262
Return(&cloudstack.CreateAffinityGroupParams{})
63-
ags.EXPECT().CreateAffinityGroup(ParamMatch(And(AccountEquals("FakeAccount"), DomainIDEquals("FakeDomainID")))).
63+
ags.EXPECT().CreateAffinityGroup(ParamMatch(
64+
And(AccountEquals(dummies.Account), DomainIDEquals(dummies.DomainID)))).
6465
Return(&cloudstack.CreateAffinityGroupResponse{}, nil)
6566

6667
Ω(client.GetOrCreateAffinityGroup(dummies.CSCluster, dummies.AffinityGroup)).Should(Succeed())
@@ -69,24 +70,18 @@ var _ = Describe("AffinityGroup Unit Tests", func() {
6970
Context("AffinityGroup Integ Tests", func() {
7071
client, connectionErr := cloud.NewClient("../../cloud-config")
7172

72-
var ( // Declare shared vars.
73-
arbitraryAG *cloud.AffinityGroup
74-
)
7573
BeforeEach(func() {
7674
if connectionErr != nil { // Only do these tests if an actual ACS instance is available via cloud-config.
7775
Skip("Could not connect to ACS instance.")
7876
}
79-
arbitraryAG = &cloud.AffinityGroup{Name: "ArbitraryAffinityGroup", Type: cloud.AffinityGroupType}
77+
dummies.AffinityGroup.ID = "" // Force name fetching.
8078
})
8179
AfterEach(func() {
8280
mockCtrl.Finish()
8381
})
8482

8583
It("Creates an affinity group.", func() {
86-
Ω(client.GetOrCreateAffinityGroup(dummies.CSCluster, arbitraryAG)).Should(Succeed())
87-
arbitraryAG2 := &cloud.AffinityGroup{Name: arbitraryAG.Name}
88-
Ω(client.GetOrCreateAffinityGroup(dummies.CSCluster, arbitraryAG2)).Should(Succeed())
89-
Ω(arbitraryAG2).Should(Equal(arbitraryAG))
84+
Ω(client.GetOrCreateAffinityGroup(dummies.CSCluster, dummies.AffinityGroup)).Should(Succeed())
9085
})
9186
It("Associates an affinity group.", func() {
9287
if err := client.GetOrCreateCluster(dummies.CSCluster); err != nil {
@@ -95,12 +90,12 @@ var _ = Describe("AffinityGroup Unit Tests", func() {
9590
if err := client.GetOrCreateVMInstance(dummies.CSMachine1, dummies.CAPIMachine, dummies.CSCluster, ""); err != nil {
9691
Skip("Could not create VM." + err.Error())
9792
}
98-
Ω(client.GetOrCreateAffinityGroup(dummies.CSCluster, arbitraryAG)).Should(Succeed())
99-
Ω(client.AssociateAffinityGroup(dummies.CSMachine1, *arbitraryAG)).Should(Succeed())
93+
Ω(client.GetOrCreateAffinityGroup(dummies.CSCluster, dummies.AffinityGroup)).Should(Succeed())
94+
Ω(client.AssociateAffinityGroup(dummies.CSMachine1, *dummies.AffinityGroup)).Should(Succeed())
10095
})
10196
It("Deletes an affinity group.", func() {
102-
Ω(client.DeleteAffinityGroup(arbitraryAG)).Should(Succeed())
103-
Ω(client.FetchAffinityGroup(arbitraryAG)).ShouldNot(Succeed())
97+
Ω(client.DeleteAffinityGroup(dummies.AffinityGroup)).Should(Succeed())
98+
Ω(client.FetchAffinityGroup(dummies.AffinityGroup)).ShouldNot(Succeed())
10499
})
105100
})
106101
})

pkg/cloud/cluster.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,17 @@ func (c *client) resolveZones(csCluster *infrav1.CloudStackCluster) (retErr erro
3636
retErr = multierror.Append(retErr, errors.Errorf(
3737
"expected 1 Zone with name %s, but got %d", specZone.Name, count))
3838
} else {
39-
csCluster.Status.Zones[specZone.Name] = infrav1.Zone{
40-
Name: specZone.Name, ID: zoneID, Network: specZone.Network}
39+
specZone.ID = zoneID
4140
}
4241

43-
if retErr != nil {
44-
if resp, count, err := c.cs.Zone.GetZoneByID(specZone.ID); err != nil {
45-
return multierror.Append(retErr, errors.Wrapf(err, "could not get Zone by ID %s", specZone.ID))
46-
} else if count != 1 {
47-
return multierror.Append(retErr, errors.Errorf(
48-
"expected 1 Zone with UUID %s, but got %d", specZone.ID, count))
49-
} else {
50-
csCluster.Status.Zones[resp.Name] = infrav1.Zone{
51-
Name: resp.Name, ID: specZone.ID, Network: specZone.Network}
52-
}
42+
if resp, count, err := c.cs.Zone.GetZoneByID(specZone.ID); err != nil {
43+
return multierror.Append(retErr, errors.Wrapf(err, "could not get Zone by ID %s", specZone.ID))
44+
} else if count != 1 {
45+
return multierror.Append(retErr, errors.Errorf(
46+
"expected 1 Zone with UUID %s, but got %d", specZone.ID, count))
47+
} else {
48+
csCluster.Status.Zones[resp.Id] = infrav1.Zone{
49+
Name: resp.Name, ID: specZone.ID, Network: specZone.Network}
5350
}
5451
}
5552

pkg/cloud/cluster_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ package cloud_test
1818
import (
1919
"fmt"
2020

21-
"github.com/apache/cloudstack-go/v2/cloudstack"
21+
csapi "github.com/apache/cloudstack-go/v2/cloudstack"
22+
capcv1 "github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
2223
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
2324
"github.com/aws/cluster-api-provider-cloudstack/test/dummies"
2425
"github.com/golang/mock/gomock"
@@ -31,20 +32,21 @@ var _ = Describe("Cluster", func() {
3132
var (
3233
client cloud.Client
3334
mockCtrl *gomock.Controller
34-
mockClient *cloudstack.CloudStackClient
35-
zs *cloudstack.MockZoneServiceIface
36-
ds *cloudstack.MockDomainServiceIface
37-
ns *cloudstack.MockNetworkServiceIface
35+
mockClient *csapi.CloudStackClient
36+
zs *csapi.MockZoneServiceIface
37+
ds *csapi.MockDomainServiceIface
38+
ns *csapi.MockNetworkServiceIface
3839
)
3940

4041
BeforeEach(func() {
4142
mockCtrl = gomock.NewController(GinkgoT())
42-
mockClient = cloudstack.NewMockClient(mockCtrl)
43-
zs = mockClient.Zone.(*cloudstack.MockZoneServiceIface)
44-
ds = mockClient.Domain.(*cloudstack.MockDomainServiceIface)
45-
ns = mockClient.Network.(*cloudstack.MockNetworkServiceIface)
43+
mockClient = csapi.NewMockClient(mockCtrl)
44+
zs = mockClient.Zone.(*csapi.MockZoneServiceIface)
45+
ds = mockClient.Domain.(*csapi.MockDomainServiceIface)
46+
ns = mockClient.Network.(*csapi.MockNetworkServiceIface)
4647
client = cloud.NewClientFromCSAPIClient(mockClient)
4748
dummies.SetDummyVars()
49+
dummies.SetDummyDomainAndAccount()
4850
})
4951

5052
AfterEach(func() {
@@ -65,23 +67,22 @@ var _ = Describe("Cluster", func() {
6567
zs.EXPECT().GetZoneID(dummies.Zone1.Name).Return(dummies.Zone1.ID, 2, nil)
6668
zs.EXPECT().GetZoneByID(dummies.Zone1.ID).Return(nil, -1, fmt.Errorf("Not found"))
6769

68-
err := client.GetOrCreateCluster(dummies.CSCluster)
69-
Expect(err.Error()).To(ContainSubstring("Expected 1 Zone with name zoneName, but got 2."))
70-
Expect(err.Error()).To(ContainSubstring("Could not get Zone by ID zoneName.: Not found"))
70+
Ω(client.GetOrCreateCluster(dummies.CSCluster)).Should(MatchError(And(
71+
ContainSubstring("expected 1 Zone with name "+dummies.Zone1.Name+", but got 2"),
72+
ContainSubstring("could not get Zone by ID "+dummies.Zone1.ID+": Not found"))))
7173
})
7274

7375
It("translates Domain to DomainID when Domain is set", func() {
7476
zs.EXPECT().GetZoneID(dummies.Zone1.Name).Return(dummies.Zone1.ID, 1, nil)
77+
zs.EXPECT().GetZoneByID(dummies.Zone1.ID).Return(dummies.CAPCZoneToCSAPIZone(&dummies.Zone1), 1, nil)
7578
ds.EXPECT().GetDomainID(dummies.CSCluster.Spec.Domain).Return(dummies.DomainID, 1, nil)
79+
ns.EXPECT().GetNetworkByName(dummies.Net1.Name).Return(dummies.CAPCNetToCSAPINet(&dummies.Net1), 1, nil)
7680

77-
// End the fetching with a fake network error here.
78-
// Only trying to test domain functions.
79-
// TODO: turn the pkg/cloud/client.go client into a composition of interfaces such that the
80-
// individual services can be mocked.
81-
ns.EXPECT().GetNetworkID(dummies.Net1.Name).Return("", -1, fmt.Errorf("FakeError"))
82-
ns.EXPECT().GetNetworkByID(dummies.Net1.ID).Return(&cloudstack.Network{}, -1, fmt.Errorf("FakeError"))
81+
// Limit test to single zone.
82+
dummies.CSCluster.Spec.Zones = []capcv1.Zone{dummies.Zone1}
83+
dummies.CSCluster.Status.Zones = capcv1.ZoneStatusMap{}
8384

84-
Ω(client.GetOrCreateCluster(dummies.CSCluster)).ShouldNot(Succeed())
85+
Ω(client.GetOrCreateCluster(dummies.CSCluster)).Should(Succeed())
8586
Ω(dummies.CSCluster.Status.DomainID).Should(Equal(dummies.DomainID))
8687
})
8788
})

test/dummies/vars.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ var ( // Declare exported dummy vars.
2323
Net1 capcv1.Network
2424
Net2 capcv1.Network
2525
ISONet1 capcv1.Network
26+
Domain string
2627
DomainID string
28+
Account string
2729
Tags map[string]string
2830
Tag1 map[string]string
2931
Tag2 map[string]string
@@ -62,6 +64,13 @@ func CAPCNetToCSAPINet(net *capcv1.Network) *csapi.Network {
6264
}
6365
}
6466

67+
func CAPCZoneToCSAPIZone(net *capcv1.Zone) *csapi.Zone {
68+
return &csapi.Zone{
69+
Name: net.Name,
70+
Id: net.ID,
71+
}
72+
}
73+
6574
// SetDummyVars sets/resets tag related dummy vars.
6675
func SetDummyTagVars() {
6776
CSClusterTagKey = "CAPC_cluster_" + string(CSCluster.ObjectMeta.UID)
@@ -80,7 +89,6 @@ func SetDummyTagVars() {
8089

8190
// SetDummyClusterSpecVars resets the values in each of the exported CloudStackMachines related dummy variables.
8291
func SetDummyCSMachineTemplateVars() {
83-
DomainID = "FakeDomainId"
8492
CSMachineTemplate1 = &capcv1.CloudStackMachineTemplate{
8593
TypeMeta: metav1.TypeMeta{
8694
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
@@ -114,7 +122,6 @@ func SetDummyCSMachineTemplateVars() {
114122

115123
// SetDummyClusterSpecVars resets the values in each of the exported CloudStackMachines related dummy variables.
116124
func SetDummyCSMachineVars() {
117-
DomainID = "FakeDomainId"
118125
CSMachine1 = &capcv1.CloudStackMachine{
119126
TypeMeta: metav1.TypeMeta{
120127
APIVersion: CSApiVersion,
@@ -144,13 +151,18 @@ func SetDummyCSMachineVars() {
144151
// SetDummyClusterSpecVars resets the values in each of the exported CloudStackCluster related dummy variables.
145152
// It is intended to be called in BeforeEach( functions.
146153
func SetDummyCAPCClusterVars() {
154+
DomainID = "FakeDomainID"
155+
Domain = "FakeDomainName"
156+
Account = "FakeAccountName"
147157
CSApiVersion = "infrastructure.cluster.x-k8s.io/v1beta1"
148158
CSClusterKind = "CloudStackCluster"
149159
CSClusterName = "test-cluster"
160+
150161
CSlusterNamespace = "default"
151162
AffinityGroup = &cloud.AffinityGroup{
152163
Name: "FakeAffinityGroup",
153-
Type: cloud.AffinityGroupType}
164+
Type: cloud.AffinityGroupType,
165+
ID: "FakeAffinityGroupID"}
154166
Net1 = capcv1.Network{Name: "SharedGuestNet1", Type: cloud.NetworkTypeShared, ID: "FakeSharedNetID1"}
155167
Net2 = capcv1.Network{Name: "SharedGuestNet2", Type: cloud.NetworkTypeShared, ID: "FakeSharedNetID2"}
156168
ISONet1 = capcv1.Network{Name: "IsolatedGuestNet1", Type: cloud.NetworkTypeIsolated, ID: "FakeIsolatedNetID1"}
@@ -179,6 +191,15 @@ func SetDummyCAPCClusterVars() {
179191
}
180192
}
181193

194+
func SetDummyDomainAndAccount() {
195+
CSCluster.Spec.Account = Account
196+
CSCluster.Spec.Domain = Domain
197+
}
198+
199+
func SetDummyDomainID() {
200+
CSCluster.Status.DomainID = "FakeDomainID"
201+
}
202+
182203
// SetDummyCapiCluster resets the values in each of the exported CAPICluster related dummy variables.
183204
func SetDummyCAPIClusterVars() {
184205
CAPICluster = &clusterv1.Cluster{

0 commit comments

Comments
 (0)