Debian Codegraph @

magnum_capi_helm/tests/test_helm.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 from magnum.common import utils
16 from oslo_concurrency import processutils
17
18 from magnum_capi_helm import helm
19 from magnum_capi_helm.tests import base
20
21
22 class TestHelmClient(base.TestCase):
23 def test_mergeconcat_dicts(self):
24 defaults = dict(foo="bar", asdf=dict(a="b", c="d"))
25 overrides = dict(asdf=dict(a="c"))
26
27 result = helm.mergeconcat(defaults, overrides)
28
29 expected = dict(foo="bar", asdf=dict(a="c", c="d"))
30 self.assertEqual(expected, result)
31
32 def test_mergeconcat_list(self):
33 defaults = ["foo", "bar"]
34 overrides = ["bar", "baz"]
35
36 result = helm.mergeconcat(defaults, overrides)
37
38 expected = ["foo", "bar", "bar", "baz"]
39 self.assertEqual(expected, result)
40
41 @mock.patch.object(utils, "execute")
42 def test_install_or_upgrade(self, mock_execute):
43 mock_execute.return_value = '[{"foo": "bar"}]', ""
44
45 client = helm.Client()
46 result = client.install_or_upgrade(
47 "myfirstcluster",
48 "mychart",
49 dict(foo="bar", b=42),
50 repo="http://myrepo",
51 version="v1.42",
52 namespace="mynamespace",
53 )
54
55 self.assertEqual([{"foo": "bar"}], result)
56 mock_execute.assert_called_once_with(
57 "helm",
58 "upgrade",
59 "myfirstcluster",
60 "mychart",
61 "--history-max",
62 10,
63 "--install",
64 "--output",
65 "json",
66 "--timeout",
67 "5m",
68 "--values",
69 "-",
70 "--namespace",
71 "mynamespace",
72 "--repo",
73 "http://myrepo",
74 "--version",
75 "v1.42",
76 process_input='{"foo": "bar", "b": 42}',
77 )
78
79 @mock.patch.object(utils, "execute")
80 def test_install_or_upgrade_oci(self, mock_execute):
81 mock_execute.return_value = '[{"foo": "bar"}]', ""
82
83 client = helm.Client()
84 result = client.install_or_upgrade(
85 "myfirstcluster",
86 "oci://localhost:5000/helm-charts/mychart",
87 dict(foo="bar", b=42),
88 version="v1.42",
89 namespace="mynamespace",
90 )
91
92 self.assertEqual([{"foo": "bar"}], result)
93 mock_execute.assert_called_once_with(
94 "helm",
95 "upgrade",
96 "myfirstcluster",
97 "oci://localhost:5000/helm-charts/mychart",
98 "--history-max",
99 10,
100 "--install",
101 "--output",
102 "json",
103 "--timeout",
104 "5m",
105 "--values",
106 "-",
107 "--namespace",
108 "mynamespace",
109 "--version",
110 "v1.42",
111 process_input='{"foo": "bar", "b": 42}',
112 )
113
114 @mock.patch.object(helm.CONF, "capi_helm")
115 @mock.patch.object(utils, "execute")
116 def test_uninstall_release_works(self, mock_execute, mock_conf):
117 mock_execute.return_value = "", ""
118 mock_conf.kubeconfig_file = "/etc/magnum/kubeconfig"
119
120 client = helm.Client()
121 result = client.uninstall_release(
122 "myfirstcluster", namespace="mynamespace"
123 )
124
125 self.assertIsNone(result)
126 mock_execute.assert_called_once_with(
127 "helm",
128 "uninstall",
129 "myfirstcluster",
130 "--timeout",
131 "5m",
132 "--namespace",
133 "mynamespace",
134 "--kubeconfig",
135 "/etc/magnum/kubeconfig",
136 )
137
138 @mock.patch.object(utils, "execute")
139 def test_uninstall_release_ignore_not_found(self, mock_execute):
140 mock_execute.side_effect = processutils.ProcessExecutionError(
141 stderr="release: not found"
142 )
143
144 client = helm.Client()
145 result = client.uninstall_release(
146 "myfirstcluster", namespace="mynamespace"
147 )
148
149 self.assertIsNone(result)
150
151 @mock.patch.object(utils, "execute")
152 def test_uninstall_release_raises(self, mock_execute):
153 mock_execute.side_effect = processutils.ProcessExecutionError(
154 stderr="oh dear!"
155 )
156 client = helm.Client()
157
158 self.assertRaises(
159 processutils.ProcessExecutionError,
160 client.uninstall_release,
161 "myfirstcluster",
162 namespace="mynamespace",
163 )