|
| 1 | +""" |
| 2 | +Fails when a Helm defined template, or a call to one, is not prefixed with the |
| 3 | +name of the chart it lives in. |
| 4 | +
|
| 5 | +To run tests for this script: |
| 6 | + python3 -m unittest check_namespaced_defines.py |
| 7 | +""" |
| 8 | + |
| 9 | +import re |
| 10 | +import unittest |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +NAME_PATTERN = r'\b(?:define|include|template)\s+"([^"]+)"' |
| 14 | +CHART_PATTERN = r"(?:^|/)deploy/helm/(?P<chart>[^/]+)/templates/" |
| 15 | + |
| 16 | + |
| 17 | +def chart_of(path): |
| 18 | + """The chart whose templates directory holds path, or None if it is outside one.""" |
| 19 | + found = re.search(CHART_PATTERN, Path(path).as_posix()) |
| 20 | + return found.group("chart") if found else None |
| 21 | + |
| 22 | + |
| 23 | +def unprefixed(file_contents, chart_name): |
| 24 | + """The defined template names in file_contents, and the calls to them, lacking the chart prefix.""" |
| 25 | + names = re.findall(NAME_PATTERN, file_contents) |
| 26 | + return sorted({name for name in names if not name.startswith(f"{chart_name}.")}) |
| 27 | + |
| 28 | + |
| 29 | +# Run these with: python3 -m unittest check_namespaced_defines.py |
| 30 | +class TestCoreMethods(unittest.TestCase): |
| 31 | + def test_chart_of(self): |
| 32 | + self.assertEqual( |
| 33 | + chart_of("deploy/helm/trino-operator/templates/x.yaml"), "trino-operator" |
| 34 | + ) |
| 35 | + # An absolute path has to give the same answer, or a manual run invents a chart name. |
| 36 | + self.assertEqual( |
| 37 | + chart_of("/tmp/wt/deploy/helm/secret-operator/templates/a/b.yaml"), |
| 38 | + "secret-operator", |
| 39 | + ) |
| 40 | + self.assertIsNone(chart_of("deploy/helm/trino-operator/values.yaml")) |
| 41 | + self.assertIsNone(chart_of("README.md")) |
| 42 | + |
| 43 | + def test_prefixed_is_accepted(self): |
| 44 | + text = '{{- define "trino-operator.labels" -}}\n{{ include "trino-operator.chart" . }}\n' |
| 45 | + self.assertEqual(unprefixed(text, "trino-operator"), []) |
| 46 | + |
| 47 | + def test_unprefixed_define_is_reported(self): |
| 48 | + self.assertEqual( |
| 49 | + unprefixed('{{- define "helper.thing" -}}', "trino-operator"), |
| 50 | + ["helper.thing"], |
| 51 | + ) |
| 52 | + |
| 53 | + def test_unprefixed_call_is_reported(self): |
| 54 | + # A renamed definition whose call sites did not move with it stops the chart rendering. |
| 55 | + text = ( |
| 56 | + '{{ include "operator.fullname" . }}\n{{ template "operator.labels" . }}\n' |
| 57 | + ) |
| 58 | + self.assertEqual( |
| 59 | + unprefixed(text, "trino-operator"), ["operator.fullname", "operator.labels"] |
| 60 | + ) |
| 61 | + |
| 62 | + def test_keyword_must_stand_alone(self): |
| 63 | + # Without a word boundary any identifier ending in the keyword matches. |
| 64 | + self.assertEqual( |
| 65 | + unprefixed('{{ .Values.xinclude "bar.baz" }}', "foo-operator"), [] |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + import sys |
| 71 | + |
| 72 | + if not sys.argv[1:]: |
| 73 | + print(f"usage: {sys.argv[0]} deploy/helm/<chart>/templates/*", file=sys.stderr) |
| 74 | + sys.exit(2) |
| 75 | + |
| 76 | + failed = False |
| 77 | + for path in sys.argv[1:]: |
| 78 | + chart_name = chart_of(path) |
| 79 | + if chart_name is None: |
| 80 | + print( |
| 81 | + f"{sys.argv[0]}: {path} is not inside deploy/helm/<chart>/templates/", |
| 82 | + file=sys.stderr, |
| 83 | + ) |
| 84 | + sys.exit(2) |
| 85 | + file_contents = Path(path).read_text(encoding="utf-8") |
| 86 | + unprefixed_names = unprefixed(file_contents, chart_name) |
| 87 | + if unprefixed_names: |
| 88 | + failed = True |
| 89 | + print( |
| 90 | + f"{path}: defined templates and the calls to them must be prefixed with '{chart_name}.'" |
| 91 | + ) |
| 92 | + for name in unprefixed_names: |
| 93 | + print(f" {name}") |
| 94 | + |
| 95 | + sys.exit(1 if failed else 0) |
0 commit comments