Debian Codegraph @

tests/unit/test_rec_subs.py

raw symbols SCIP
1 # SPDX-License-Identifier: GPL-2.0
2 # pylint: disable=protected-access
3
4 import unittest
5 from typing import Any
6
7 from parameterized import parameterized
8
9 from podman_compose import rec_subs
10
11
12 class TestRecSubs(unittest.TestCase):
13 substitutions = [
14 # dict with environment variables
15 (
16 "service's environment is low priority",
17 {"environment": {"v1": "low priority", "actual-v1": "$v1"}},
18 {"environment": {"v1": "low priority", "actual-v1": "high priority"}},
19 ),
20 (
21 "service's environment can be used in other values",
22 {"environment": {"v100": "v1.0.0", "image": "abc:$v100"}},
23 {"environment": {"v100": "v1.0.0", "image": "abc:v1.0.0"}},
24 ),
25 (
26 "Non-variable should not be substituted",
27 {"environment": {"non_var": "$$v1", "vx": "$non_var"}, "image": "abc:$non_var"},
28 {"environment": {"non_var": "$v1", "vx": "$v1"}, "image": "abc:$v1"},
29 ),
30 (
31 "service environment with unpopulated ${VARIABLE:-default} format",
32 {"environment": {"v100": "${v100:-low priority}", "actual-v100": "$v100"}},
33 {"environment": {"v100": "low priority", "actual-v100": "low priority"}},
34 ),
35 (
36 "service environment with populated ${VARIABLE:-default} format",
37 {"environment": {"v1": "${v1:-low priority}", "actual-v1": "$v1"}},
38 {"environment": {"v1": "high priority", "actual-v1": "high priority"}},
39 ),
40 # list
41 (
42 "Values in list are substituted",
43 ["$v1", "low priority"],
44 ["high priority", "low priority"],
45 ),
46 # str
47 (
48 "Value with ${VARIABLE} format",
49 "${v1}",
50 "high priority",
51 ),
52 (
53 "Value with ${VARIABLE:-default} format",
54 ["${v1:-default}", "${empty:-default}", "${not_exits:-default}"],
55 ["high priority", "default", "default"],
56 ),
57 (
58 "Value with ${VARIABLE-default} format",
59 ["${v1-default}", "${empty-default}", "${not_exits-default}"],
60 ["high priority", "", "default"],
61 ),
62 (
63 "Value $$ means $",
64 "$$v1",
65 "$v1",
66 ),
67 ]
68
69 @parameterized.expand(substitutions)
70 def test_rec_subs(self, desc: str, input: Any, expected: Any) -> None:
71 sub_dict = {"v1": "high priority", "empty": ""}
72 result = rec_subs(input, sub_dict)
73 self.assertEqual(result, expected, msg=desc)
74
75 def test_env_var_substitution_in_dictionary_keys(self) -> None:
76 sub_dict = {"NAME": "TEST1", "NAME2": "TEST2"}
77 input = {
78 'services': {
79 'test': {
80 'image': 'busybox',
81 'labels': {
82 '$NAME and ${NAME2}': '${NAME2} and $NAME',
83 'test1.${NAME}': 'test1',
84 '$NAME': '${NAME2}',
85 '${NAME}.test2': 'Host(`${NAME2}`)',
86 },
87 }
88 }
89 }
90 result = rec_subs(input, sub_dict)
91 expected = {
92 'services': {
93 'test': {
94 'image': 'busybox',
95 'labels': {
96 'TEST1 and TEST2': 'TEST2 and TEST1',
97 'test1.TEST1': 'test1',
98 'TEST1': 'TEST2',
99 'TEST1.test2': 'Host(`TEST2`)',
100 },
101 }
102 }
103 }
104 self.assertEqual(result, expected)