Repository navigation
Expand file tree
/
Copy pathtest_gitops_config_v0.py
More file actions
154 lines (118 loc) · 7.25 KB
/
Copy pathtest_gitops_config_v0.py
File metadata and controls
154 lines (118 loc) · 7.25 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
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
import unittest
import pytest
from gitopscli.gitops_config import GitOpsConfig
from gitopscli.gitops_exception import GitOpsException
class GitOpsConfigV0Test(unittest.TestCase):
def setUp(self):
self.yaml = {
"deploymentConfig": {"applicationName": "my-app", "org": "my-org", "repository": "my-repo"},
"previewConfig": {
"route": {"host": {"template": "my-{SHA256_8CHAR_BRANCH_HASH}-host-template"}},
"replace": [{"path": "a.b", "variable": "ROUTE_HOST"}, {"path": "c.d", "variable": "GIT_COMMIT"}],
},
}
def load(self) -> GitOpsConfig:
return GitOpsConfig.from_yaml(self.yaml)
def assert_load_error(self, error_msg: str) -> None:
with pytest.raises(GitOpsException) as ex:
self.load()
self.assertEqual(error_msg, str(ex.value))
def test_application_name(self):
config = self.load()
self.assertEqual(config.application_name, "my-app")
def test_application_name_missing(self):
del self.yaml["deploymentConfig"]["applicationName"]
self.assert_load_error("Key 'deploymentConfig.applicationName' not found in GitOps config!")
def test_application_name_not_a_string(self):
self.yaml["deploymentConfig"]["applicationName"] = 1
self.assert_load_error("Item 'deploymentConfig.applicationName' should be a string in GitOps config!")
def test_team_config_org(self):
config = self.load()
self.assertEqual(config.preview_template_organisation, "my-org")
self.assertEqual(config.preview_target_organisation, "my-org")
self.assertTrue(config.is_preview_template_equal_target())
def test_deployment_config_org_missing(self):
del self.yaml["deploymentConfig"]["org"]
self.assert_load_error("Key 'deploymentConfig.org' not found in GitOps config!")
def test_deployment_config_org_not_a_string(self):
self.yaml["deploymentConfig"]["org"] = True
self.assert_load_error("Item 'deploymentConfig.org' should be a string in GitOps config!")
def test_deployment_config_repo(self):
config = self.load()
self.assertEqual(config.preview_template_repository, "my-repo")
self.assertEqual(config.preview_target_repository, "my-repo")
self.assertTrue(config.is_preview_template_equal_target())
def test_deployment_config_repo_missing(self):
del self.yaml["deploymentConfig"]["repository"]
self.assert_load_error("Key 'deploymentConfig.repository' not found in GitOps config!")
def test_deployment_config_repo_not_a_string(self):
self.yaml["deploymentConfig"]["repository"] = []
self.assert_load_error("Item 'deploymentConfig.repository' should be a string in GitOps config!")
def test_preview_template_branch_is_none(self):
config = self.load()
self.assertIsNone(config.preview_template_branch)
def test_preview_target_branch_is_none(self):
config = self.load()
self.assertIsNone(config.preview_target_branch)
def test_route_host_template(self):
config = self.load()
self.assertEqual(config.preview_host_template, "my-${PREVIEW_ID_HASH}-host-template")
def test_route_host(self):
config = self.load()
self.assertEqual(config.get_preview_host("preview-1"), "my-3e355b4a-host-template")
def test_route_missing(self):
del self.yaml["previewConfig"]["route"]
self.assert_load_error("Key 'previewConfig.route.host.template' not found in GitOps config!")
def test_route_host_missing(self):
del self.yaml["previewConfig"]["route"]["host"]
self.assert_load_error("Key 'previewConfig.route.host.template' not found in GitOps config!")
def test_route_host_template_missing(self):
del self.yaml["previewConfig"]["route"]["host"]["template"]
self.assert_load_error("Key 'previewConfig.route.host.template' not found in GitOps config!")
def test_route_host_template_not_a_string(self):
self.yaml["previewConfig"]["route"]["host"]["template"] = []
self.assert_load_error("Item 'previewConfig.route.host.template' should be a string in GitOps config!")
def test_namespace_template(self):
config = self.load()
self.assertEqual(config.preview_target_namespace_template, "${APPLICATION_NAME}-${PREVIEW_ID_HASH}-preview")
def test_namespace(self):
config = self.load()
self.assertEqual(config.get_preview_namespace("preview-1"), "my-app-3e355b4a-preview")
def test_replacements(self):
config = self.load()
self.assertEqual(config.replacements.keys(), {"Chart.yaml", "values.yaml"})
self.assertEqual(len(config.replacements["Chart.yaml"]), 1)
self.assertEqual(config.replacements["Chart.yaml"][0].path, "name")
self.assertEqual(config.replacements["Chart.yaml"][0].value_template, "${PREVIEW_NAMESPACE}")
self.assertEqual(len(config.replacements["values.yaml"]), 2)
self.assertEqual(config.replacements["values.yaml"][0].path, "a.b")
self.assertEqual(config.replacements["values.yaml"][0].value_template, "${PREVIEW_HOST}")
self.assertEqual(config.replacements["values.yaml"][1].path, "c.d")
self.assertEqual(config.replacements["values.yaml"][1].value_template, "${GIT_HASH}")
def test_replacements_missing(self):
del self.yaml["previewConfig"]["replace"]
self.assert_load_error("Key 'previewConfig.replace' not found in GitOps config!")
def test_replacements_not_a_list(self):
self.yaml["previewConfig"]["replace"] = "foo"
self.assert_load_error("Item 'previewConfig.replace' should be a list in GitOps config!")
def test_replacements_invalid_list(self):
self.yaml["previewConfig"]["replace"] = ["foo"]
self.assert_load_error("Item 'previewConfig.replace.[0]' should be an object in GitOps config!")
def test_replacements_invalid_list_items_missing_path(self):
del self.yaml["previewConfig"]["replace"][1]["path"]
self.assert_load_error("Key 'previewConfig.replace.[1].path' not found in GitOps config!")
def test_replacements_invalid_list_items_missing_variable(self):
del self.yaml["previewConfig"]["replace"][0]["variable"]
self.assert_load_error("Key 'previewConfig.replace.[0].variable' not found in GitOps config!")
def test_replacements_invalid_list_items_path_not_a_string(self):
self.yaml["previewConfig"]["replace"][0]["path"] = 42
self.assert_load_error("Item 'previewConfig.replace.[0].path' should be a string in GitOps config!")
def test_replacements_invalid_list_items_variable_not_a_string(self):
self.yaml["previewConfig"]["replace"][0]["variable"] = []
self.assert_load_error("Item 'previewConfig.replace.[0].variable' should be a string in GitOps config!")
def test_replacements_invalid_list_items_unknown_variable(self):
self.yaml["previewConfig"]["replace"][0]["variable"] = "FOO"
self.assert_load_error("Replacement value '${FOO}' for path 'a.b' contains invalid variable: FOO")
def test_replacements_invalid_list_items_invalid_variable(self):
self.yaml["previewConfig"]["replace"][0]["variable"] = "{FOO"
self.assert_load_error("Item 'previewConfig.replace.[0].variable' must not contain '{' or '}'!")