1
1
import unittest
2
-
3
2
from kubernetes import client , config
4
3
from kubernetes .client .rest import ApiException
5
4
17
16
18
17
19
18
class ClusterSecretCases (unittest .TestCase ):
19
+
20
+ def setUp (self ) -> None :
21
+ self .cluster_secret_manager = ClusterSecretManager (
22
+ custom_objects_api = custom_objects_api ,
23
+ api_instance = api_instance
24
+ )
25
+ super ().setUp ()
26
+
27
+ def tearDown (self ) -> None :
28
+ self .cluster_secret_manager .cleanup ()
29
+ super ().tearDown ()
30
+
20
31
@classmethod
21
32
def setUpClass (cls ) -> None :
22
33
# Wait for the cluster secret pod to be ready before running tests
@@ -33,6 +44,7 @@ def setUpClass(cls) -> None:
33
44
print (f"Namespace '{ namespace_name } ' already exists." )
34
45
else :
35
46
print (f"Error creating namespace '{ namespace_name } ': { e } " )
47
+
36
48
super ().setUpClass ()
37
49
38
50
def test_running (self ):
@@ -42,20 +54,16 @@ def test_running(self):
42
54
def test_simple_cluster_secret (self ):
43
55
name = "simple-cluster-secret"
44
56
username_data = "MTIzNDU2Cg=="
45
- cluster_secret_manager = ClusterSecretManager (
46
- custom_objects_api = custom_objects_api ,
47
- api_instance = api_instance
48
- )
49
57
50
- cluster_secret_manager .create_cluster_secret (
58
+ self . cluster_secret_manager .create_cluster_secret (
51
59
name = name ,
52
60
namespace = USER_NAMESPACES [0 ],
53
61
data = {"username" : username_data }
54
62
)
55
63
56
64
# We expect the secret to be in ALL namespaces
57
65
self .assertTrue (
58
- cluster_secret_manager .validate_namespace_secrets (
66
+ self . cluster_secret_manager .validate_namespace_secrets (
59
67
name = name ,
60
68
data = {"username" : username_data },
61
69
)
@@ -64,13 +72,9 @@ def test_simple_cluster_secret(self):
64
72
def test_complex_cluster_secret (self ):
65
73
name = "complex-cluster-secret"
66
74
username_data = "MTIzNDU2Cg=="
67
- cluster_secret_manager = ClusterSecretManager (
68
- custom_objects_api = custom_objects_api ,
69
- api_instance = api_instance
70
- )
71
75
72
76
# Create a secret in all user namespace expect the first one
73
- cluster_secret_manager .create_cluster_secret (
77
+ self . cluster_secret_manager .create_cluster_secret (
74
78
name = name ,
75
79
namespace = USER_NAMESPACES [0 ],
76
80
data = {"username" : username_data },
@@ -80,7 +84,7 @@ def test_complex_cluster_secret(self):
80
84
81
85
# Ensure the secrets is only present where is to suppose to be
82
86
self .assertTrue (
83
- cluster_secret_manager .validate_namespace_secrets (
87
+ self . cluster_secret_manager .validate_namespace_secrets (
84
88
name = name ,
85
89
data = {"username" : username_data },
86
90
namespaces = USER_NAMESPACES [1 :],
@@ -91,36 +95,32 @@ def test_patch_cluster_secret_data(self):
91
95
name = "dynamic-cluster-secret"
92
96
username_data = "MTIzNDU2Cg=="
93
97
updated_data = "Nzg5MTAxMTIxMgo="
94
- cluster_secret_manager = ClusterSecretManager (
95
- custom_objects_api = custom_objects_api ,
96
- api_instance = api_instance
97
- )
98
98
99
99
# Create a secret with username_data
100
- cluster_secret_manager .create_cluster_secret (
100
+ self . cluster_secret_manager .create_cluster_secret (
101
101
name = name ,
102
102
namespace = USER_NAMESPACES [0 ],
103
103
data = {"username" : username_data },
104
104
)
105
105
106
106
# Ensure the secret is created with the right data
107
107
self .assertTrue (
108
- cluster_secret_manager .validate_namespace_secrets (
108
+ self . cluster_secret_manager .validate_namespace_secrets (
109
109
name = name ,
110
110
data = {"username" : username_data },
111
111
)
112
112
)
113
113
114
114
# Update the cluster secret's data
115
- cluster_secret_manager .update_data_cluster_secret (
115
+ self . cluster_secret_manager .update_data_cluster_secret (
116
116
name = name ,
117
117
data = {"username" : updated_data },
118
118
namespace = USER_NAMESPACES [0 ],
119
119
)
120
120
121
121
# Ensure the secrets are updated with the right data (at some point)
122
122
self .assertTrue (
123
- cluster_secret_manager .validate_namespace_secrets (
123
+ self . cluster_secret_manager .validate_namespace_secrets (
124
124
name = name ,
125
125
data = {"username" : updated_data },
126
126
),
@@ -130,12 +130,8 @@ def test_patch_cluster_secret_data(self):
130
130
def test_patch_cluster_secret_match_namespaces (self ):
131
131
name = "dynamic-cluster-secret-match-namespaces"
132
132
username_data = "MTIzNDU2Cg=="
133
- cluster_secret_manager = ClusterSecretManager (
134
- custom_objects_api = custom_objects_api ,
135
- api_instance = api_instance
136
- )
137
133
138
- cluster_secret_manager .create_cluster_secret (
134
+ self . cluster_secret_manager .create_cluster_secret (
139
135
name = name ,
140
136
namespace = USER_NAMESPACES [0 ],
141
137
data = {"username" : username_data },
@@ -145,7 +141,7 @@ def test_patch_cluster_secret_match_namespaces(self):
145
141
)
146
142
147
143
self .assertTrue (
148
- cluster_secret_manager .validate_namespace_secrets (
144
+ self . cluster_secret_manager .validate_namespace_secrets (
149
145
name = name ,
150
146
data = {"username" : username_data },
151
147
namespaces = [
@@ -156,15 +152,15 @@ def test_patch_cluster_secret_match_namespaces(self):
156
152
)
157
153
158
154
# Update the cluster match_namespace to ALL user namespace
159
- cluster_secret_manager .update_data_cluster_secret (
155
+ self . cluster_secret_manager .update_data_cluster_secret (
160
156
name = name ,
161
157
namespace = USER_NAMESPACES [0 ],
162
158
match_namespace = USER_NAMESPACES ,
163
159
data = {"username" : username_data },
164
160
)
165
161
166
162
self .assertTrue (
167
- cluster_secret_manager .validate_namespace_secrets (
163
+ self . cluster_secret_manager .validate_namespace_secrets (
168
164
name = name ,
169
165
data = {"username" : username_data },
170
166
namespaces = USER_NAMESPACES ,
@@ -175,40 +171,103 @@ def test_patch_cluster_secret_match_namespaces(self):
175
171
def test_simple_cluster_secret_deleted (self ):
176
172
name = "simple-cluster-secret-deleted"
177
173
username_data = "MTIzNDU2Cg=="
178
- cluster_secret_manager = ClusterSecretManager (
179
- custom_objects_api = custom_objects_api ,
180
- api_instance = api_instance
181
- )
182
174
183
- cluster_secret_manager .create_cluster_secret (
175
+ self . cluster_secret_manager .create_cluster_secret (
184
176
name = name ,
185
177
namespace = USER_NAMESPACES [0 ],
186
178
data = {"username" : username_data }
187
179
)
188
180
189
181
# We expect the secret to be in ALL namespaces
190
182
self .assertTrue (
191
- cluster_secret_manager .validate_namespace_secrets (
183
+ self . cluster_secret_manager .validate_namespace_secrets (
192
184
name = name ,
193
185
data = {"username" : username_data }
194
186
)
195
187
)
196
188
197
- cluster_secret_manager .delete_cluster_secret (
189
+ self . cluster_secret_manager .delete_cluster_secret (
198
190
name = name ,
199
191
namespace = USER_NAMESPACES [0 ],
200
192
)
201
193
202
194
# We expect the secret to be in NO namespaces
203
195
self .assertTrue (
204
- cluster_secret_manager .validate_namespace_secrets (
196
+ self . cluster_secret_manager .validate_namespace_secrets (
205
197
name = name ,
206
198
data = {"username" : username_data },
207
199
namespaces = [],
208
200
),
209
201
f'secret { name } should be deleted from all namespaces.'
210
202
)
211
203
204
+ def test_value_from_cluster_secret (self ):
205
+ cluster_secret_name = "value-from-cluster-secret"
206
+ secret_name = "basic-secret-example"
207
+
208
+ username_data = "MTIzNDU2Cg=="
209
+
210
+ # Create a kubernetes secrets
211
+ self .cluster_secret_manager .create_secret (
212
+ name = secret_name ,
213
+ namespace = USER_NAMESPACES [0 ],
214
+ data = {'username' : username_data }
215
+ )
216
+
217
+ # Create the cluster secret
218
+ self .cluster_secret_manager .create_cluster_secret (
219
+ name = cluster_secret_name ,
220
+ namespace = USER_NAMESPACES [0 ],
221
+ secret_key_ref = {
222
+ 'name' : secret_name ,
223
+ 'namespace' : USER_NAMESPACES [0 ],
224
+ },
225
+ )
226
+
227
+ # We expect the secret to be in ALL namespaces
228
+ self .assertTrue (
229
+ self .cluster_secret_manager .validate_namespace_secrets (
230
+ name = cluster_secret_name ,
231
+ data = {"username" : username_data },
232
+ ),
233
+ msg = f'Cluster secret should take the data from the { secret_name } secret.'
234
+ )
235
+
236
+ def test_value_from_with_keys_cluster_secret (self ):
237
+ cluster_secret_name = "value-from-with-keys-cluster-secret"
238
+ secret_name = "k8s-basic-secret-example"
239
+
240
+ username_data = "MTIzNDU2Cg=="
241
+ password_data = "aGloaXBhc3M="
242
+ more_data = "aWlpaWlhYWE="
243
+
244
+ # Create a kubernetes secrets
245
+ self .cluster_secret_manager .create_secret (
246
+ name = secret_name ,
247
+ namespace = USER_NAMESPACES [0 ],
248
+ data = {'username' : username_data , 'password' : password_data , 'more-data' : more_data }
249
+ )
250
+
251
+ # Create the cluster secret
252
+ self .cluster_secret_manager .create_cluster_secret (
253
+ name = cluster_secret_name ,
254
+ namespace = USER_NAMESPACES [0 ],
255
+ secret_key_ref = {
256
+ 'name' : secret_name ,
257
+ 'namespace' : USER_NAMESPACES [0 ],
258
+ 'keys' : ['username' , 'password' ]
259
+ },
260
+ )
261
+
262
+ # We expect the secret to be in ALL namespaces
263
+ self .assertTrue (
264
+ self .cluster_secret_manager .validate_namespace_secrets (
265
+ name = cluster_secret_name ,
266
+ data = {'username' : username_data , 'password' : password_data },
267
+ ),
268
+ msg = f'Cluster secret should take the data from the { secret_name } secret but only the keys specified.'
269
+ )
270
+
212
271
213
272
if __name__ == '__main__' :
214
273
unittest .main ()
0 commit comments