-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathtest_compile.py
339 lines (268 loc) · 11.4 KB
/
test_compile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
# Copyright 2019 The Kapitan Authors
# SPDX-FileCopyrightText: 2020 The Kapitan Authors <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
"compile tests"
import contextlib
import glob
import io
import logging
import os
import shutil
import sys
import tempfile
import unittest
import toml
import yaml
from kapitan.cached import reset_cache
from kapitan.cli import main
from kapitan.inventory import InventoryBackends
from kapitan.utils import directory_hash
logger = logging.getLogger(__name__)
TEST_PWD = os.getcwd()
TEST_RESOURCES_PATH = os.path.join(os.getcwd(), "tests/test_resources")
TEST_DOCKER_PATH = os.path.join(os.getcwd(), "examples/docker/")
TEST_TERRAFORM_PATH = os.path.join(os.getcwd(), "examples/terraform/")
TEST_KUBERNETES_PATH = os.path.join(os.getcwd(), "examples/kubernetes/")
class CompileTestResourcesTestObjs(unittest.TestCase):
def setUp(self):
reset_cache()
os.chdir(TEST_RESOURCES_PATH)
def test_compile_no_reveal(self):
# check if the --no-reveal flag takes precedence over --reveal when passed together
sys.argv = ["kapitan", "compile", "-t", "reveal-output", "--reveal", "--no-reveal"]
main()
with open("compiled/reveal-output/main.json") as f:
self.assertTrue("?{gpg:" in f.read())
def test_single_target_compile(self):
sys.argv = ["kapitan", "compile", "-t", "test-objects"]
main()
def test_plain_ref_revealed(self):
"check plain refs are revealed in test-objects"
for g in glob.glob("compiled/test-objects/*.json"):
with open(g) as f:
self.assertTrue("?{plain:" not in f.read())
def tearDown(self):
os.chdir(TEST_PWD)
reset_cache()
class CompileTestResourcesTestKadet(unittest.TestCase):
def setUp(self):
os.chdir(TEST_RESOURCES_PATH)
reset_cache()
def test_compile(self):
sys.argv = ["kapitan", "compile", "-t", "kadet-test"]
main()
def test_compile_with_input_params(self):
# input_params propagate through and written out to file
for g in glob.glob("compiled/kadet-test/test-1/*.yaml"):
with open(g, "r") as fp:
manifest = yaml.safe_load(fp.read())
namespace = manifest["metadata"]["namespace"]
team_name = manifest["metadata"]["labels"]["team_name"]
self.assertEqual(namespace, "ops")
self.assertEqual(team_name, "client-operations")
# same kadet function was called with new params should have
# different results
for g in glob.glob("compiled/kadet-test/test-2/*.yaml"):
with open(g, "r") as fp:
manifest = yaml.safe_load(fp.read())
namespace = manifest["metadata"]["namespace"]
team_name = manifest["metadata"]["labels"]["team_name"]
self.assertEqual(namespace, "team-2")
self.assertEqual(team_name, "SRE")
def tearDown(self):
os.chdir(TEST_PWD)
reset_cache()
class FailCompileTestResourcesTestKadet(unittest.TestCase):
def setUp(self):
os.chdir(TEST_RESOURCES_PATH)
reset_cache()
def test_compile(self):
sys.argv = ["kapitan", "compile", "-t", "fail-compile"]
main()
def tearDown(self):
os.chdir(TEST_PWD)
reset_cache()
class CompileTestResourcesTestJinja2InputParams(unittest.TestCase):
def setUp(self):
os.chdir(TEST_RESOURCES_PATH)
reset_cache()
def test_compile(self):
sys.argv = ["kapitan", "compile", "-t", "jinja2-input-params"]
main()
def test_compile_with_input_params(self):
# input_params propagate through and written out to file
for g in glob.glob("compiled/jinja2-input-params/test-1/*.yml"):
with open(g, "r") as fp:
manifest = yaml.safe_load(fp.read())
namespace = manifest["metadata"]["namespace"]
name = manifest["metadata"]["name"]
self.assertEqual(namespace, "ns1")
self.assertEqual(name, "test1")
# same jinja2 function was called with new params should have
# different results
for g in glob.glob("compiled/jinja2-input-params/test-2/*.yaml"):
with open(g, "r") as fp:
manifest = yaml.safe_load(fp.read())
namespace = manifest["metadata"]["namespace"]
name = manifest["metadata"]["name"]
self.assertEqual(namespace, "ns2")
self.assertEqual(name, "test2")
def tearDown(self):
os.chdir(TEST_PWD)
reset_cache()
class CompileTestResourcesTestJinja2PostfixStrip(unittest.TestCase):
def setUp(self):
os.chdir(TEST_RESOURCES_PATH)
reset_cache()
def test_compile(self):
sys.argv = ["kapitan", "compile", "-t", "jinja2-postfix-strip"]
main()
def test_compile_postfix_strip_disabled(self):
self.assertListEqual(os.listdir("compiled/jinja2-postfix-strip/unstripped"), ["stub.txt.j2"])
def test_compile_postfix_strip_overridden(self):
self.assertListEqual(os.listdir("compiled/jinja2-postfix-strip/stripped-overridden"), ["stub"])
def test_compile_postfix_strip_enabled(self):
self.assertListEqual(os.listdir("compiled/jinja2-postfix-strip/stripped"), ["stub.txt"])
def tearDown(self):
os.chdir(TEST_PWD)
reset_cache()
class CompileKubernetesTest(unittest.TestCase):
extraArgv = []
inventory_path = TEST_KUBERNETES_PATH
def setUp(self):
reset_cache()
os.chdir(self.inventory_path)
shutil.rmtree("compiled", ignore_errors=True)
def test_compile(self):
sys.argv = ["kapitan", "compile", "-c"] + self.extraArgv
main()
compile_dir = os.path.join(os.getcwd(), "compiled")
reference_dir = os.path.join(TEST_PWD, "tests/test_kubernetes_compiled")
compiled_dir_hash = directory_hash(compile_dir)
test_compiled_dir_hash = directory_hash(reference_dir)
self.assertEqual(compiled_dir_hash, test_compiled_dir_hash)
def test_compile_not_enough_args(self):
with self.assertRaises(SystemExit) as cm:
# Ignoring stdout for "kapitan --help"
with contextlib.redirect_stdout(io.StringIO()):
sys.argv = ["kapitan"]
main()
self.assertEqual(cm.exception.code, 1)
def test_compile_specific_target(self):
reset_cache()
sys.argv = ["kapitan", "compile", "-t", "minikube-mysql"] + self.extraArgv
main()
self.assertTrue(
os.path.exists("compiled/minikube-mysql") and not os.path.exists("compiled/minikube-es")
)
# Reset compiled dir
sys.argv = ["kapitan", "compile"] + self.extraArgv
main()
def test_compile_target_with_label(self):
reset_cache()
sys.argv = ["kapitan", "compile", "-l", "type=kadet"] + self.extraArgv
main()
self.assertTrue(
os.path.exists("compiled/minikube-nginx-kadet")
and not os.path.exists("compiled/minikube-nginx-jsonnet")
)
# Reset compiled dir
sys.argv = ["kapitan", "compile"] + self.extraArgv
main()
def test_compile_jsonnet_env(self):
sys.argv = ["kapitan", "compile", "-t", "jsonnet-env"] + self.extraArgv
main()
self.assertTrue(os.path.exists("compiled/jsonnet-env/jsonnet-env/env.yml"))
with open("compiled/jsonnet-env/jsonnet-env/env.yml", "r", encoding="utf-8") as f:
env = dict(yaml.safe_load(f))
logger.error(env)
self.assertEqual(set(env.keys()), {"applications", "parameters", "classes", "exports"})
self.assertEqual(env["applications"], ["a", "b", "c"])
self.assertEqual(env["classes"], ["common", "jsonnet-env"])
self.assertTrue("a" in env["parameters"])
self.assertEqual(env["parameters"]["a"], "aaaaa")
self.assertTrue("b" in env["parameters"])
self.assertEqual(env["parameters"]["b"], "bbbbb")
self.assertTrue("c" in env["parameters"])
self.assertEqual(env["parameters"]["c"], "ccccc")
self.assertEqual(env["exports"], {})
def tearDown(self):
shutil.rmtree("compiled", ignore_errors=True)
os.chdir(TEST_PWD)
reset_cache()
class CompileKubernetesTestReclassRs(CompileKubernetesTest):
def setUp(self):
super().setUp()
self.extraArgv = [f"--inventory-backend={(InventoryBackends.RECLASS_RS)}"]
@unittest.skip("Already tested")
def test_compile_not_enough_args(self):
pass
class CompileKubernetesTestOmegaconf(CompileKubernetesTest):
temp_dir = tempfile.mkdtemp()
def setUp(self):
reset_cache()
shutil.copytree(self.inventory_path, self.temp_dir, dirs_exist_ok=True)
self.inventory_path = self.temp_dir
super().setUp()
self.extraArgv = ["--inventory-backend=omegaconf"]
from kapitan.inventory.backends.omegaconf import migrate
migrate(self.temp_dir)
@unittest.skip("Already tested")
def test_compile_not_enough_args(self):
pass
def tearDown(self):
shutil.rmtree(self.temp_dir)
super().tearDown()
class CompileTerraformTest(unittest.TestCase):
def setUp(self):
os.chdir(TEST_TERRAFORM_PATH)
def test_compile(self):
sys.argv = ["kapitan", "compile"]
main()
compiled_dir_hash = directory_hash(os.getcwd() + "/compiled")
test_compiled_dir_hash = directory_hash(os.getcwd() + "/../../tests/test_terraform_compiled")
self.assertEqual(compiled_dir_hash, test_compiled_dir_hash)
def tearDown(self):
os.chdir(TEST_PWD)
reset_cache()
class PlainOutputTest(unittest.TestCase):
def setUp(self):
os.chdir(TEST_DOCKER_PATH)
def test_compile(self):
sys.argv = ["kapitan", "compile"]
main()
compiled_dir_hash = directory_hash(os.getcwd() + "/compiled")
test_compiled_dir_hash = directory_hash(os.getcwd() + "/../../tests/test_docker_compiled")
self.assertEqual(compiled_dir_hash, test_compiled_dir_hash)
def tearDown(self):
os.chdir(TEST_PWD)
reset_cache()
class TomlOutputTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
os.chdir(TEST_RESOURCES_PATH)
sys.argv = ["kapitan", "compile", "-t", "toml-output"]
main()
def setUp(self):
target_file_path = os.path.join(os.getcwd(), "inventory/targets/toml-output.yml")
with open(target_file_path) as target_file:
target = yaml.safe_load(target_file)
self.input_parameter = target["parameters"]["input"]
def test_jsonnet_output(self):
output_file_path = os.path.join(os.getcwd(), "compiled/toml-output/jsonnet-output/nested.toml")
expected = self.input_parameter["nested"]
with open(output_file_path) as output_file:
output = toml.load(output_file)
self.assertDictEqual(output, expected)
def test_kadet_output(self):
output_file_path = os.path.join(os.getcwd(), "compiled/toml-output/kadet-output/nested.toml")
expected = self.input_parameter["nested"]
with open(output_file_path) as output_file:
output = toml.load(output_file)
self.assertDictEqual(output, expected)
@classmethod
def tearDownClass(cls):
os.chdir(TEST_PWD)
reset_cache()