@@ -44,13 +44,17 @@ var _ = Describe("Tag Unit Tests", func() {
44
44
client , connectionErr := cloud .NewClient ("../../cloud-config" )
45
45
46
46
const (
47
- tagKey = "test_tag"
48
- tagValue = "arbitrary_value"
47
+ tagKey = "test_tag"
48
+ tagValue = "arbitrary_value"
49
+ clusterID = "123456"
50
+ createdByCAPCTag = "created_by_CAPC"
51
+ clusterTag = "CAPC_cluster_" + clusterID
49
52
)
50
53
51
54
var (
52
55
networkID string
53
56
testTags map [string ]string
57
+ csCluster * infrav1.CloudStackCluster
54
58
)
55
59
56
60
BeforeEach (func () {
@@ -64,25 +68,109 @@ var _ = Describe("Tag Unit Tests", func() {
64
68
65
69
networkID = cluster .Status .NetworkID
66
70
testTags = map [string ]string {tagKey : tagValue }
67
- })
71
+ csCluster = & infrav1.CloudStackCluster {}
72
+ csCluster .SetUID (clusterID )
68
73
69
- It ("Tags a network with an arbitrary tag." , func () {
70
- // Delete the tag if it already exists from a prior test run, otherwise the test will fail.
71
- _ = client .DeleteTags (cloud .ResourceTypeNetwork , networkID , testTags )
72
- Ω (client .AddTags (cloud .ResourceTypeNetwork , networkID , testTags )).Should (Succeed ())
74
+ // Delete any existing tags
75
+ existingTags , err := client .GetTags (cloud .ResourceTypeNetwork , networkID )
76
+ if err != nil {
77
+ Fail ("Failed to get existing tags. Error: " + err .Error ())
78
+ }
79
+ err = client .DeleteTags (cloud .ResourceTypeNetwork , networkID , existingTags )
80
+ if err != nil {
81
+ Fail ("Failed to delete existing tags. Error: " + err .Error ())
82
+ }
73
83
})
74
84
75
- It ("Fetches said tag." , func () {
85
+ It ("Adds and gets a resource tag" , func () {
86
+ Ω (client .AddTags (cloud .ResourceTypeNetwork , networkID , testTags )).Should (Succeed ())
76
87
tags , err := client .GetTags (cloud .ResourceTypeNetwork , networkID )
77
88
Ω (err ).Should (BeNil ())
78
89
Ω (tags [tagKey ]).Should (Equal (tagValue ))
79
90
})
80
91
81
- It ("Deletes said tag." , func () {
92
+ It ("deletes a resource tag" , func () {
93
+ _ = client .AddTags (cloud .ResourceTypeNetwork , networkID , testTags )
82
94
Ω (client .DeleteTags (cloud .ResourceTypeNetwork , networkID , testTags )).Should (Succeed ())
83
95
remainingTags , err := client .GetTags (cloud .ResourceTypeNetwork , networkID )
84
96
Ω (err ).Should (BeNil ())
85
97
Ω (remainingTags [tagKey ]).Should (Equal ("" ))
86
98
})
99
+
100
+ It ("adds the tags for a cluster (resource created by CAPC)" , func () {
101
+ Ω (client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )).Should (Succeed ())
102
+
103
+ // Verify tags
104
+ tags , err := client .GetTags (cloud .ResourceTypeNetwork , networkID )
105
+ Ω (err ).Should (BeNil ())
106
+ Ω (tags [createdByCAPCTag ]).Should (Equal ("1" ))
107
+ Ω (tags [clusterTag ]).Should (Equal ("1" ))
108
+ })
109
+
110
+ It ("does not fail when the cluster tags are added twice" , func () {
111
+ Ω (client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )).Should (Succeed ())
112
+ Ω (client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )).Should (Succeed ())
113
+ })
114
+
115
+ It ("adds the tags for a cluster (resource NOT created by CAPC)" , func () {
116
+ Ω (client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , false )).Should (Succeed ())
117
+
118
+ // Verify tags
119
+ tags , err := client .GetTags (cloud .ResourceTypeNetwork , networkID )
120
+ Ω (err ).Should (BeNil ())
121
+ Ω (tags [createdByCAPCTag ]).Should (Equal ("" ))
122
+ Ω (tags [clusterTag ]).Should (Equal ("1" ))
123
+ })
124
+
125
+ It ("deletes a cluster tag" , func () {
126
+ _ = client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )
127
+ Ω (client .DeleteClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster )).Should (Succeed ())
128
+
129
+ // Verify tags
130
+ tags , err := client .GetTags (cloud .ResourceTypeNetwork , networkID )
131
+ Ω (err ).Should (BeNil ())
132
+ Ω (tags [createdByCAPCTag ]).Should (Equal ("1" ))
133
+ Ω (tags [clusterTag ]).Should (Equal ("" ))
134
+ })
135
+
136
+ It ("deletes a CAPC created tag" , func () {
137
+ _ = client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )
138
+ Ω (client .DeleteCreatedByCAPCTag (cloud .ResourceTypeNetwork , networkID )).Should (Succeed ())
139
+
140
+ // Verify tags
141
+ tags , err := client .GetTags (cloud .ResourceTypeNetwork , networkID )
142
+ Ω (err ).Should (BeNil ())
143
+ Ω (tags [createdByCAPCTag ]).Should (Equal ("" ))
144
+ Ω (tags [clusterTag ]).Should (Equal ("1" ))
145
+ })
146
+
147
+ It ("does not fail when cluster and CAPC created tags are deleted twice" , func () {
148
+ _ = client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )
149
+ Ω (client .DeleteClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster )).Should (Succeed ())
150
+ Ω (client .DeleteClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster )).Should (Succeed ())
151
+ Ω (client .DeleteCreatedByCAPCTag (cloud .ResourceTypeNetwork , networkID )).Should (Succeed ())
152
+ Ω (client .DeleteCreatedByCAPCTag (cloud .ResourceTypeNetwork , networkID )).Should (Succeed ())
153
+ })
154
+
155
+ It ("does not allow a resource to be deleted when there are no tags" , func () {
156
+ tagsAllowDisposal , err := client .DoClusterTagsAllowDisposal (cloud .ResourceTypeNetwork , networkID )
157
+ Ω (err ).Should (BeNil ())
158
+ Ω (tagsAllowDisposal ).Should (BeFalse ())
159
+ })
160
+
161
+ It ("does not allow a resource to be deleted when there is a cluster tag" , func () {
162
+ _ = client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )
163
+ tagsAllowDisposal , err := client .DoClusterTagsAllowDisposal (cloud .ResourceTypeNetwork , networkID )
164
+ Ω (err ).Should (BeNil ())
165
+ Ω (tagsAllowDisposal ).Should (BeFalse ())
166
+ })
167
+
168
+ It ("does allow a resource to be deleted when there are no cluster tags and there is a CAPC created tag" , func () {
169
+ _ = client .AddClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster , true )
170
+ _ = client .DeleteClusterTag (cloud .ResourceTypeNetwork , networkID , csCluster )
171
+ tagsAllowDisposal , err := client .DoClusterTagsAllowDisposal (cloud .ResourceTypeNetwork , networkID )
172
+ Ω (err ).Should (BeNil ())
173
+ Ω (tagsAllowDisposal ).Should (BeTrue ())
174
+ })
87
175
})
88
176
})
0 commit comments