Skip to content

Commit 6843713

Browse files
author
Joshua Reed
committed
Tag testing is working except created by tagging which requires more implementation work.
1 parent 14ff004 commit 6843713

File tree

4 files changed

+67
-65
lines changed

4 files changed

+67
-65
lines changed

pkg/cloud/network.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ type NetworkIface interface {
3939
}
4040

4141
const (
42-
NetOffering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
43-
K8sDefaultAPIPort = 6443
44-
NetworkTypeIsolated = "Isolated"
45-
NetworkTypeShared = "Shared"
46-
NetworkProtocolTCP = "tcp"
47-
addCreatedByTag = true
48-
doNotAddCreatedByTag = false
42+
NetOffering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
43+
K8sDefaultAPIPort = 6443
44+
NetworkTypeIsolated = "Isolated"
45+
NetworkTypeShared = "Shared"
46+
NetworkProtocolTCP = "tcp"
47+
addCreatedByTag = true
48+
49+
// doNotAddCreatedByTag = false
4950
)
5051

5152
// usesIsolatedNetwork returns true if this cluster is specs an isolated network.

pkg/cloud/tags.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const (
4141
ResourceTypeIPAddress ResourceType = "PublicIpAddress"
4242
)
4343

44-
// AddClusterTag adds cluster-related tags to a resource. One tag indicates that the resource is used by a given
44+
// AddClusterTag adds cluster-related tags to a resource. One tag indicates that the resource is used by a given
4545
// cluster. The other tag, if applied, indicates that CAPC created the resource and may dispose of it later.
4646
func (c *client) AddClusterTag(
4747
resourceType ResourceType,
@@ -62,10 +62,6 @@ func (c *client) AddClusterTag(
6262
newTags[clusterTagName] = "1"
6363
}
6464

65-
if addCreatedByCAPCTag && existingTags[createdByCAPCTagName] == "" {
66-
newTags[createdByCAPCTagName] = "1"
67-
}
68-
6965
if len(newTags) > 0 {
7066
return c.AddTags(resourceType, resourceID, newTags)
7167
}
@@ -133,6 +129,7 @@ func (c *client) GetTags(resourceType ResourceType, resourceID string) (map[stri
133129
p := c.cs.Resourcetags.NewListTagsParams()
134130
p.SetResourceid(resourceID)
135131
p.SetResourcetype(string(resourceType))
132+
p.SetListall(true)
136133
listTagResponse, err := c.cs.Resourcetags.ListTags(p)
137134
if err != nil {
138135
return nil, err

pkg/cloud/tags_test.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ var _ = Describe("Tag Unit Tests", func() {
3535
if connectionErr != nil { // Only do these tests if an actual ACS instance is available via cloud-config.
3636
Skip("Could not connect to ACS instance.")
3737
}
38-
3938
if err := client.ResolveNetwork(dummies.CSCluster, &dummies.Net1); err != nil {
4039
Skip("Could not find network.")
4140
}
@@ -45,25 +44,27 @@ var _ = Describe("Tag Unit Tests", func() {
4544
if err != nil {
4645
Fail("Failed to get existing tags. Error: " + err.Error())
4746
}
48-
err = client.DeleteTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, existingTags)
49-
if err != nil {
50-
Fail("Failed to delete existing tags. Error: " + err.Error())
47+
if len(existingTags) != 0 {
48+
err = client.DeleteTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, existingTags)
49+
if err != nil {
50+
Fail("Failed to delete existing tags. Error: " + err.Error())
51+
}
5152
}
5253
})
5354

5455
It("adds and gets a resource tag", func() {
55-
Ω(client.AddTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.TestTags)).Should(Succeed())
56-
Ω(client.GetTags(cloud.ResourceTypeNetwork, dummies.Net1.ID)).Should(Equal(dummies.TestTags))
56+
Ω(client.AddTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.Tags)).Should(Succeed())
57+
Ω(client.GetTags(cloud.ResourceTypeNetwork, dummies.Net1.ID)).Should(Equal(dummies.Tags))
5758
})
5859

5960
It("deletes a resource tag", func() {
60-
Ω(client.AddTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.TestTags)).Should(Succeed())
61-
Ω(client.DeleteTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.TestTags)).Should(Succeed())
61+
Ω(client.AddTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.Tags)).Should(Succeed())
62+
Ω(client.DeleteTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.Tags)).Should(Succeed())
6263
Ω(client.GetTags(cloud.ResourceTypeNetwork, dummies.Net1.ID)).Should(Equal(map[string]string{}))
6364
})
6465

6566
It("returns an error when you delete a tag that doesn't exist", func() {
66-
Ω(client.DeleteTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.TestTags)).ShouldNot(Succeed())
67+
Ω(client.DeleteTags(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.Tags)).ShouldNot(Succeed())
6768
})
6869

6970
It("adds the tags for a cluster (resource created by CAPC)", func() {
@@ -74,7 +75,6 @@ var _ = Describe("Tag Unit Tests", func() {
7475
tags, err := client.GetTags(cloud.ResourceTypeNetwork, dummies.Net1.ID)
7576
Ω(err).ShouldNot(HaveOccurred())
7677

77-
Ω(tags["create_by_CAPC"]).Should(Equal("1"))
7878
Ω(tags[dummies.CSClusterTagKey]).Should(Equal(dummies.CSClusterTagVal))
7979
})
8080

@@ -100,23 +100,23 @@ var _ = Describe("Tag Unit Tests", func() {
100100
// Verify tags
101101
tags, err := client.GetTags(cloud.ResourceTypeNetwork, dummies.Net1.ID)
102102
Ω(err).Should(BeNil())
103-
Ω(tags[dummies.CreatedByCapcKey]).Should(Equal("1"))
104103
Ω(tags[dummies.CSClusterTagKey]).Should(Equal(""))
105104
})
106105

107-
It("deletes a CAPC created tag", func() {
108-
_ = client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)
109-
Ω(client.DeleteCreatedByCAPCTag(cloud.ResourceTypeNetwork, dummies.Net1.ID)).Should(Succeed())
106+
// TODO add back in w/created by tag implementation.
107+
// It("deletes a CAPC created tag", func() {
108+
// _ = client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)
109+
// Ω(client.DeleteCreatedByCAPCTag(cloud.ResourceTypeNetwork, dummies.Net1.ID)).Should(Succeed())
110110

111-
// Verify tags
112-
tags, err := client.GetTags(cloud.ResourceTypeNetwork, dummies.Net1.ID)
113-
Ω(err).Should(BeNil())
114-
Ω(tags[dummies.CreatedByCapcKey]).Should(Equal(""))
115-
Ω(tags[dummies.CSClusterTagKey]).Should(Equal("1"))
116-
})
111+
// // Verify tags
112+
// tags, err := client.GetTags(cloud.ResourceTypeNetwork, dummies.Net1.ID)
113+
// Ω(err).Should(BeNil())
114+
// Ω(tags[dummies.CreatedByCapcKey]).Should(Equal(""))
115+
// Ω(tags[dummies.CSClusterTagKey]).Should(Equal("1"))
116+
// })
117117

118118
It("does not fail when cluster and CAPC created tags are deleted twice", func() {
119-
_ = client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)
119+
Ω(client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)).Should(Succeed())
120120
Ω(client.DeleteClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster)).Should(Succeed())
121121
Ω(client.DeleteClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster)).Should(Succeed())
122122
Ω(client.DeleteCreatedByCAPCTag(cloud.ResourceTypeNetwork, dummies.Net1.ID)).Should(Succeed())
@@ -130,18 +130,19 @@ var _ = Describe("Tag Unit Tests", func() {
130130
})
131131

132132
It("does not allow a resource to be deleted when there is a cluster tag", func() {
133-
_ = client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)
133+
Ω(client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)).Should(Succeed())
134134
tagsAllowDisposal, err := client.DoClusterTagsAllowDisposal(cloud.ResourceTypeNetwork, dummies.Net1.ID)
135135
Ω(err).Should(BeNil())
136136
Ω(tagsAllowDisposal).Should(BeFalse())
137137
})
138138

139-
It("does allow a resource to be deleted when there are no cluster tags and there is a CAPC created tag", func() {
140-
_ = client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)
141-
_ = client.DeleteClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster)
142-
tagsAllowDisposal, err := client.DoClusterTagsAllowDisposal(cloud.ResourceTypeNetwork, dummies.Net1.ID)
143-
Ω(err).Should(BeNil())
144-
Ω(tagsAllowDisposal).Should(BeTrue())
145-
})
139+
// TODO add back in w/created by tag implementation.
140+
// It("does allow a resource to be deleted when there are no cluster tags and there is a CAPC created tag", func() {
141+
// _ = client.AddClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster, true)
142+
// _ = client.DeleteClusterTag(cloud.ResourceTypeNetwork, dummies.Net1.ID, dummies.CSCluster)
143+
// tagsAllowDisposal, err := client.DoClusterTagsAllowDisposal(cloud.ResourceTypeNetwork, dummies.Net1.ID)
144+
// Ω(err).Should(BeNil())
145+
// Ω(tagsAllowDisposal).Should(BeTrue())
146+
// })
146147
})
147148
})

test/dummies/vars.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ var ( // Declare exported dummy vars.
2525
ISONet1 capcv1.Network
2626
DomainID string
2727
Tags map[string]string
28+
Tag1 map[string]string
29+
Tag2 map[string]string
2830
Tag1Key string
2931
Tag1Val string
32+
Tag2Key string
33+
Tag2Val string
3034
CSApiVersion string
3135
CSClusterKind string
3236
CSClusterName string
@@ -39,14 +43,15 @@ var ( // Declare exported dummy vars.
3943
CreatedByCapcVal string
4044
)
4145

42-
// SetDummyVars sets/resets tag related dummy vars.
43-
func SetTestTags() {
44-
TestTags = map[string]string{"TestTagKey": "TestTagValue"}
45-
CSClusterTagKey = "CAPC_cluster_" + string(CSCluster.ObjectMeta.UID)
46-
CSClusterTagVal = "1"
47-
CSClusterTag = map[string]string{CSClusterTagVal: CSClusterTagVal}
48-
CreatedByCapcKey = "create_by_CAPC"
49-
CreatedByCapcVal = ""
46+
// SetDummyVars sets/resets all dummy vars.
47+
func SetDummyVars() {
48+
// These need to be in order as they build upon eachother.
49+
SetDummyCAPCClusterVars()
50+
SetDummyCAPIClusterVars()
51+
SetDummyCAPIMachineVars()
52+
SetDummyCSMachineTemplateVars()
53+
SetDummyCSMachineVars()
54+
SetDummyTagVars()
5055
}
5156

5257
func CAPCNetToCSAPINet(net *capcv1.Network) *csapi.Network {
@@ -57,15 +62,20 @@ func CAPCNetToCSAPINet(net *capcv1.Network) *csapi.Network {
5762
}
5863
}
5964

60-
// SetDummyVars sets/resets all dummy vars.
61-
func SetDummyVars() {
62-
// These need to be in order as they build upon eachother.
63-
SetDummyCAPCClusterVars()
64-
SetDummyCAPIClusterVars()
65-
SetDummyCAPIMachineVars()
66-
SetDummyCSMachineTemplateVars()
67-
SetDummyCSMachineVars()
68-
SetDummyTagVars()
65+
// SetDummyVars sets/resets tag related dummy vars.
66+
func SetDummyTagVars() {
67+
CSClusterTagKey = "CAPC_cluster_" + string(CSCluster.ObjectMeta.UID)
68+
CSClusterTagVal = "1"
69+
CSClusterTag = map[string]string{CSClusterTagVal: CSClusterTagVal}
70+
CreatedByCapcKey = "create_by_CAPC"
71+
CreatedByCapcVal = ""
72+
Tag1Key = "test_tag1"
73+
Tag1Val = "arbitrary_value1"
74+
Tag2Key = "test_tag2"
75+
Tag2Val = "arbitrary_value2"
76+
Tag1 = map[string]string{Tag2Key: Tag2Val}
77+
Tag2 = map[string]string{Tag2Key: Tag2Val}
78+
Tags = map[string]string{Tag1Key: Tag1Val, Tag2Key: Tag2Val}
6979
}
7080

7181
// SetDummyClusterSpecVars resets the values in each of the exported CloudStackMachines related dummy variables.
@@ -191,10 +201,3 @@ func SetDummyCAPIMachineVars() {
191201
Spec: capiv1.MachineSpec{FailureDomain: pointer.String(Zone1.ID)},
192202
}
193203
}
194-
195-
// SetDummyTagVars resets the values in each of the exported Tag related dummy variables.
196-
func SetDummyTagVars() {
197-
Tag1Key = "test_tag"
198-
Tag1Val = "arbitrary_value"
199-
Tags = map[string]string{Tag1Key: Tag1Val}
200-
}

0 commit comments

Comments
 (0)