-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_common_steps.py
More file actions
69 lines (52 loc) · 3.06 KB
/
Copy pathtest_common_steps.py
File metadata and controls
69 lines (52 loc) · 3.06 KB
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
"""Tests for the common-steps feature: files in a common directory are rendered into every test case."""
import tempfile
import unittest
from os import makedirs, path
from beku.kuttl import TestCase
def _write(file_path: str, content: str) -> None:
makedirs(path.dirname(file_path), exist_ok=True)
with open(file_path, encoding="utf8", mode="w") as stream:
stream.write(content)
class TestCommonSteps(unittest.TestCase):
def test_common_dir_rendered_into_test_case(self):
with tempfile.TemporaryDirectory() as tmp:
template_dir = path.join(tmp, "templates")
common_dir = path.join(tmp, "commons")
target_dir = path.join(tmp, "_work")
# A test with its own step, and a plain + templated common file.
_write(path.join(template_dir, "mytest", "00-install.yaml"), "step\n")
_write(path.join(common_dir, "99-teardown.yaml"), "teardown\n")
_write(path.join(common_dir, "50-note.txt.j2"), "ns={{ NAMESPACE }}\n")
TestCase(name="mytest", values={}).expand(template_dir, target_dir, "kuttl-fixed", common_dir)
tc_dir = path.join(target_dir, "mytest", "mytest")
# the test's own step is present
self.assertTrue(path.isfile(path.join(tc_dir, "00-install.yaml")))
# the common plain file is present
self.assertTrue(path.isfile(path.join(tc_dir, "99-teardown.yaml")))
# the common template is rendered (suffix stripped, NAMESPACE substituted)
rendered = path.join(tc_dir, "50-note.txt")
self.assertTrue(path.isfile(rendered))
with open(rendered, encoding="utf8") as stream:
self.assertIn("ns=kuttl-fixed", stream.read())
def test_missing_common_dir_is_skipped(self):
with tempfile.TemporaryDirectory() as tmp:
template_dir = path.join(tmp, "templates")
target_dir = path.join(tmp, "_work")
_write(path.join(template_dir, "mytest", "00-install.yaml"), "step\n")
# Non-existent common dir must not raise and must not add anything.
TestCase(name="mytest", values={}).expand(
template_dir, target_dir, "kuttl-fixed", path.join(tmp, "does-not-exist")
)
tc_dir = path.join(target_dir, "mytest", "mytest")
self.assertTrue(path.isfile(path.join(tc_dir, "00-install.yaml")))
self.assertFalse(path.isfile(path.join(tc_dir, "99-teardown.yaml")))
def test_no_common_dir_argument_is_backwards_compatible(self):
with tempfile.TemporaryDirectory() as tmp:
template_dir = path.join(tmp, "templates")
target_dir = path.join(tmp, "_work")
_write(path.join(template_dir, "mytest", "00-install.yaml"), "step\n")
# Old call signature (no common_dir) keeps working.
TestCase(name="mytest", values={}).expand(template_dir, target_dir, "kuttl-fixed")
self.assertTrue(path.isfile(path.join(target_dir, "mytest", "mytest", "00-install.yaml")))
if __name__ == "__main__":
unittest.main()