Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/eks/test_kubeconfig.py
1567 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
import os
14
import shutil
15
import tempfile
16
17
from botocore.compat import OrderedDict
18
19
from awscli.testutils import mock, unittest
20
from tests.functional.eks.test_util import get_testdata
21
from awscli.customizations.eks.kubeconfig import (_get_new_kubeconfig_content,
22
KubeconfigWriter,
23
KubeconfigLoader,
24
KubeconfigValidator,
25
Kubeconfig,
26
KubeconfigInaccessableError)
27
class TestKubeconfigWriter(unittest.TestCase):
28
def setUp(self):
29
self._writer = KubeconfigWriter()
30
31
def test_write_order(self):
32
content = OrderedDict([
33
("current-context", "context"),
34
("apiVersion", "v1")
35
])
36
file_to_write = tempfile.NamedTemporaryFile(mode='w').name
37
self.addCleanup(os.remove, file_to_write)
38
39
config = Kubeconfig(file_to_write, content)
40
self._writer.write_kubeconfig(config)
41
42
with open(file_to_write, 'r') as stream:
43
self.assertMultiLineEqual(stream.read(),
44
"current-context: context\n"
45
"apiVersion: v1\n")
46
def test_write_makedirs(self):
47
content = OrderedDict([
48
("current-context", "context"),
49
("apiVersion", "v1")
50
])
51
containing_dir = tempfile.mkdtemp()
52
self.addCleanup(shutil.rmtree, containing_dir)
53
config_path = os.path.join(containing_dir,
54
"dir1",
55
"dir2",
56
"dir3")
57
58
config = Kubeconfig(config_path, content)
59
self._writer.write_kubeconfig(config)
60
61
with open(config_path, 'r') as stream:
62
self.assertMultiLineEqual(stream.read(),
63
"current-context: context\n"
64
"apiVersion: v1\n")
65
66
def test_write_directory(self):
67
content = OrderedDict([
68
("current-context", "context"),
69
("apiVersion", "v1")
70
])
71
containing_dir = tempfile.mkdtemp()
72
self.addCleanup(shutil.rmtree, containing_dir)
73
74
config = Kubeconfig(containing_dir, content)
75
self.assertRaises(KubeconfigInaccessableError,
76
self._writer.write_kubeconfig,
77
config)
78
79
class TestKubeconfigLoader(unittest.TestCase):
80
def setUp(self):
81
# This mock validator allows all kubeconfigs
82
self._validator = mock.Mock(spec=KubeconfigValidator)
83
self._loader = KubeconfigLoader(self._validator)
84
85
self._temp_directory = tempfile.mkdtemp()
86
self.addCleanup(shutil.rmtree, self._temp_directory)
87
88
def _clone_config(self, config):
89
"""
90
Copies the testdata named config into the temp directory,
91
Returns the new path
92
93
:param config: The name of the testdata to copy
94
:type config: str
95
"""
96
old_path = os.path.abspath(get_testdata(config))
97
new_path = os.path.join(self._temp_directory, config)
98
shutil.copy2(old_path,
99
new_path)
100
return new_path
101
102
def test_load_simple(self):
103
simple_path = self._clone_config("valid_simple")
104
content = OrderedDict([
105
("apiVersion", "v1"),
106
("clusters", [
107
OrderedDict([
108
("cluster", OrderedDict([
109
("server", "simple")
110
])),
111
("name", "simple")
112
])
113
]),
114
("contexts", None),
115
("current-context", "simple"),
116
("kind", "Config"),
117
("preferences", OrderedDict()),
118
("users", None)
119
])
120
loaded_config = self._loader.load_kubeconfig(simple_path)
121
self.assertEqual(loaded_config.content, content)
122
self._validator.validate_config.assert_called_with(
123
Kubeconfig(simple_path, content)
124
)
125
126
def test_load_noexist(self):
127
no_exist_path = os.path.join(self._temp_directory,
128
"this_does_not_exist")
129
loaded_config = self._loader.load_kubeconfig(no_exist_path)
130
self.assertEqual(loaded_config.content,
131
_get_new_kubeconfig_content())
132
self._validator.validate_config.assert_called_with(
133
Kubeconfig(no_exist_path, _get_new_kubeconfig_content())
134
)
135
136
def test_load_empty(self):
137
empty_path = self._clone_config("valid_empty_existing")
138
loaded_config = self._loader.load_kubeconfig(empty_path)
139
self.assertEqual(loaded_config.content,
140
_get_new_kubeconfig_content())
141
self._validator.validate_config.assert_called_with(
142
Kubeconfig(empty_path, _get_new_kubeconfig_content())
143
)
144
145
def test_load_directory(self):
146
current_directory = self._temp_directory
147
self.assertRaises(KubeconfigInaccessableError,
148
self._loader.load_kubeconfig,
149
current_directory)
150
self._validator.validate_config.assert_not_called()
151
152