Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/eks/test_update_kubeconfig.py
2621 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
import glob
15
import os
16
import shutil
17
import sys
18
import tempfile
19
from argparse import Namespace
20
21
import botocore
22
from botocore.compat import OrderedDict
23
24
import awscli.customizations.eks.kubeconfig as kubeconfig
25
from awscli.customizations.eks.exceptions import EKSClusterError, EKSError
26
from awscli.customizations.eks.ordered_yaml import ordered_yaml_load
27
from awscli.customizations.eks.update_kubeconfig import (API_VERSION,
28
EKSClient,
29
KubeconfigSelector)
30
from awscli.customizations.utils import uni_print
31
from awscli.testutils import mock, unittest
32
from tests.functional.eks.test_util import (
33
describe_cluster_creating_response, describe_cluster_deleting_response,
34
describe_cluster_no_status_response, describe_cluster_response,
35
describe_cluster_response_outpost_cluster, get_testdata)
36
37
38
def generate_env_variable(files):
39
"""
40
Generate a string which is an environment variable
41
containing the absolute paths for each file in files
42
43
:param files: The names of the files to put in the environment variable
44
:type files: list
45
"""
46
output = ""
47
for file in files:
48
if len(output) == 0:
49
output = file
50
else:
51
output += os.path.pathsep + file
52
return output
53
54
55
EXAMPLE_ARN = "arn:aws:eks:region:111222333444:cluster/ExampleCluster"
56
57
class TestKubeconfigSelector(unittest.TestCase):
58
def setUp(self):
59
self._validator = kubeconfig.KubeconfigValidator()
60
self._loader = kubeconfig.KubeconfigLoader(self._validator)
61
62
def assert_chosen_path(self,
63
env_variable,
64
path_in,
65
cluster_name,
66
chosen_path):
67
selector = KubeconfigSelector(env_variable, path_in,
68
self._validator,
69
self._loader)
70
self.assertEqual(selector.choose_kubeconfig(cluster_name).path,
71
chosen_path)
72
73
def test_parse_env_variable(self):
74
paths = [
75
"",
76
"",
77
get_testdata("valid_bad_cluster"),
78
get_testdata("valid_bad_cluster2"),
79
"",
80
get_testdata("valid_existing"),
81
""
82
]
83
84
env_variable = generate_env_variable(paths)
85
86
selector = KubeconfigSelector(env_variable, None, self._validator,
87
self._loader)
88
self.assertEqual(selector._paths, [path for path in paths
89
if len(path) > 0])
90
91
def test_choose_env_only(self):
92
paths = [
93
get_testdata("valid_simple"),
94
get_testdata("valid_existing")
95
] + glob.glob(get_testdata("invalid_*")) + [
96
get_testdata("valid_bad_context"),
97
get_testdata("valid_no_user")
98
]
99
env_variable = generate_env_variable(paths)
100
self.assert_chosen_path(env_variable,
101
None,
102
EXAMPLE_ARN,
103
get_testdata("valid_simple"))
104
105
def test_choose_existing(self):
106
paths = [
107
get_testdata("valid_simple"),
108
get_testdata("valid_existing")
109
] + glob.glob(get_testdata("invalid_*")) + [
110
get_testdata("valid_bad_context"),
111
get_testdata("valid_no_user"),
112
get_testdata("output_single"),
113
get_testdata("output_single_with_role")
114
]
115
env_variable = generate_env_variable(paths)
116
self.assert_chosen_path(env_variable,
117
None,
118
EXAMPLE_ARN,
119
get_testdata("output_single"))
120
121
def test_arg_override(self):
122
paths = [
123
get_testdata("valid_simple"),
124
get_testdata("valid_existing")
125
] + glob.glob(get_testdata("invalid_*")) + [
126
get_testdata("valid_bad_context"),
127
get_testdata("valid_no_user"),
128
get_testdata("output_single"),
129
get_testdata("output_single_with_role")
130
]
131
env_variable = generate_env_variable(paths)
132
self.assert_chosen_path(env_variable,
133
get_testdata("output_combined"),
134
EXAMPLE_ARN,
135
get_testdata("output_combined"))
136
137
def test_first_corrupted(self):
138
paths = glob.glob(get_testdata("invalid_*")) + [
139
get_testdata("valid_bad_context"),
140
get_testdata("valid_no_user")
141
]
142
env_variable = generate_env_variable(paths)
143
selector = KubeconfigSelector(env_variable, None, self._validator,
144
self._loader)
145
self.assertRaises(kubeconfig.KubeconfigCorruptedError,
146
selector.choose_kubeconfig,
147
EXAMPLE_ARN)
148
149
def test_arg_override_first_corrupted(self):
150
paths = glob.glob(get_testdata("invalid_*")) + [
151
get_testdata("valid_bad_context"),
152
get_testdata("valid_no_user")
153
]
154
env_variable = generate_env_variable(paths)
155
self.assert_chosen_path(env_variable,
156
get_testdata("output_combined"),
157
EXAMPLE_ARN,
158
get_testdata("output_combined"))
159
160
class TestEKSClient(unittest.TestCase):
161
def setUp(self):
162
self._correct_cluster_entry = OrderedDict([
163
("cluster", OrderedDict([
164
("certificate-authority-data", describe_cluster_response()\
165
["cluster"]["certificateAuthority"]["data"]),
166
("server", describe_cluster_response()["cluster"]["endpoint"])
167
])),
168
("name", describe_cluster_response()["cluster"]["arn"])
169
])
170
171
self._correct_user_entry = OrderedDict([
172
("name", describe_cluster_response()["cluster"]["arn"]),
173
("user", OrderedDict([
174
("exec", OrderedDict([
175
("apiVersion", API_VERSION),
176
("args",
177
[
178
"--region",
179
"region",
180
"eks",
181
"get-token",
182
"--cluster-name",
183
"ExampleCluster",
184
"--output",
185
"json",
186
]),
187
("command", "aws")
188
]))
189
]))
190
])
191
192
self._correct_user_entry_outpost_cluster = OrderedDict([
193
("name", describe_cluster_response()["cluster"]["arn"]),
194
("user", OrderedDict([
195
("exec", OrderedDict([
196
("apiVersion", API_VERSION),
197
("args",
198
[
199
"--region",
200
"region",
201
"eks",
202
"get-token",
203
"--cluster-id",
204
"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
205
"--output",
206
"json",
207
]),
208
("command", "aws")
209
]))
210
]))
211
])
212
213
self._mock_client = mock.Mock()
214
self._mock_client.describe_cluster.return_value =\
215
describe_cluster_response()
216
217
self._session = mock.Mock(spec=botocore.session.Session)
218
self._session.create_client.return_value = self._mock_client
219
self._session.profile = None
220
221
self._client = EKSClient(self._session, parsed_args=Namespace(cluster_name="ExampleCluster", role_arn=None, proxy_url=None))
222
223
def test_get_cluster_description(self):
224
self.assertEqual(self._client.cluster_description,
225
describe_cluster_response()["cluster"])
226
self._mock_client.describe_cluster.assert_called_once_with(
227
name="ExampleCluster"
228
)
229
self._session.create_client.assert_called_once_with("eks")
230
231
def test_get_cluster_description_no_status(self):
232
self._mock_client.describe_cluster.return_value = \
233
describe_cluster_no_status_response()
234
with self.assertRaises(EKSClusterError):
235
self._client.cluster_description
236
self._mock_client.describe_cluster.assert_called_once_with(
237
name="ExampleCluster"
238
)
239
self._session.create_client.assert_called_once_with("eks")
240
241
def test_get_cluster_entry(self):
242
self.assertEqual(self._client.get_cluster_entry(),
243
self._correct_cluster_entry)
244
self._mock_client.describe_cluster.assert_called_once_with(
245
name="ExampleCluster"
246
)
247
self._session.create_client.assert_called_once_with("eks")
248
249
def test_get_cluster_entry_with_proxy_url_passed(self):
250
proxy_url = "https://myproxy.com"
251
correct_cluster_entry_with_proxy_url = OrderedDict([
252
("cluster", OrderedDict([
253
("certificate-authority-data", describe_cluster_response()\
254
["cluster"]["certificateAuthority"]["data"]),
255
("server", describe_cluster_response()["cluster"]["endpoint"]),
256
("proxy-url", proxy_url)
257
])),
258
("name", describe_cluster_response()["cluster"]["arn"])
259
])
260
client = EKSClient(self._session, parsed_args=Namespace(cluster_name="ProxiedCluster",
261
role_arn=None,
262
proxy_url=proxy_url))
263
self.assertEqual(client.get_cluster_entry(),
264
correct_cluster_entry_with_proxy_url)
265
self._mock_client.describe_cluster.assert_called_once_with(
266
name="ProxiedCluster"
267
)
268
self._session.create_client.assert_called_once_with("eks")
269
270
def test_get_user_entry(self):
271
self.assertEqual(self._client.get_user_entry(),
272
self._correct_user_entry)
273
self._mock_client.describe_cluster.assert_called_once_with(
274
name="ExampleCluster"
275
)
276
self._session.create_client.assert_called_once_with("eks")
277
278
def test_get_user_entry_outpost_cluster(self):
279
self._mock_client.describe_cluster.return_value =\
280
describe_cluster_response_outpost_cluster()
281
self.assertEqual(self._client.get_user_entry(),
282
self._correct_user_entry_outpost_cluster)
283
self._mock_client.describe_cluster.assert_called_once_with(
284
name="ExampleCluster"
285
)
286
self._session.create_client.assert_called_once_with("eks")
287
288
def test_get_both(self):
289
self.assertEqual(self._client.get_cluster_entry(),
290
self._correct_cluster_entry)
291
self.assertEqual(self._client.get_user_entry(),
292
self._correct_user_entry)
293
self._mock_client.describe_cluster.assert_called_once_with(
294
name="ExampleCluster"
295
)
296
self._session.create_client.assert_called_once_with("eks")
297
298
def test_cluster_creating(self):
299
self._mock_client.describe_cluster.return_value =\
300
describe_cluster_creating_response()
301
with self.assertRaises(EKSClusterError):
302
self._client.cluster_description
303
self._mock_client.describe_cluster.assert_called_once_with(
304
name="ExampleCluster"
305
)
306
self._session.create_client.assert_called_once_with("eks")
307
308
def test_cluster_deleting(self):
309
self._mock_client.describe_cluster.return_value =\
310
describe_cluster_deleting_response()
311
with self.assertRaises(EKSClusterError):
312
self._client.cluster_description
313
self._mock_client.describe_cluster.assert_called_once_with(
314
name="ExampleCluster"
315
)
316
self._session.create_client.assert_called_once_with("eks")
317
318
def test_profile(self):
319
self._session.profile = "profile"
320
self._correct_user_entry["user"]["exec"]["env"] = [
321
OrderedDict([
322
("name", "AWS_PROFILE"),
323
("value", "profile")
324
])
325
]
326
self.assertEqual(self._client.get_user_entry(),
327
self._correct_user_entry)
328
self._mock_client.describe_cluster.assert_called_once_with(
329
name="ExampleCluster"
330
)
331
self._session.create_client.assert_called_once_with("eks")
332
333
def test_create_user_with_alias(self):
334
self._correct_user_entry["name"] = "alias"
335
self.assertEqual(self._client.get_user_entry(user_alias="alias"),
336
self._correct_user_entry)
337
self._mock_client.describe_cluster.assert_called_once_with(
338
name="ExampleCluster"
339
)
340
self._session.create_client.assert_called_once_with("eks")
341
342
def test_create_user_without_alias(self):
343
self.assertEqual(self._client.get_user_entry(),
344
self._correct_user_entry)
345
self._mock_client.describe_cluster.assert_called_once_with(
346
name="ExampleCluster"
347
)
348
self._session.create_client.assert_called_once_with("eks")
349
350