2 # Copyright (c) 2019 Cloudify Platform Ltd. All rights reserved
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # * See the License for the specific language governing permissions and
14 # * limitations under the License.
18 from . import HelmTestBase, HELM_BINARY
19 from helm_sdk.exceptions import CloudifyHelmSDKError
21 mock_flags = [{'name': 'dry-run'},
22 {'name': 'timeout', 'value': '100'}]
23 mock_set_args = [{'name': 'x', 'value': 'y'},
24 {'name': 'a', 'value': 'b'}]
27 class HelmSDKTest(HelmTestBase):
29 def test_install_with_token_and_api(self):
30 with self.assertRaisesRegexp(CloudifyHelmSDKError,
31 'Must provide kubeconfig file path.'):
32 self.helm.install('release1',
37 apiserver='https://1.0.0.0')
39 def test_install_with_kubeconfig(self):
40 mock_execute = mock.Mock(return_value='{"manifest":"resourceA"}')
41 self.helm.execute = mock_execute
42 out = self.helm.install('release1',
46 kubeconfig='/path/to/config')
47 cmd_expected = [HELM_BINARY, 'install', 'release1', 'my_chart',
48 '--wait', '--output=json',
49 '--kubeconfig=/path/to/config', '--dry-run',
50 '--timeout=100', '--set', 'x=y', '--set', 'a=b']
51 mock_execute.assert_called_once_with(cmd_expected, True)
52 self.assertEqual(out, {"manifest": "resourceA"})
54 def test_install_no_token_and_no_kubeconfig(self):
55 with self.assertRaisesRegexp(CloudifyHelmSDKError,
56 'Must provide kubeconfig file path.'):
57 self.helm.install('release1',
61 apiserver='https://1.0.0.0')
63 def test_install_no_apiserver_and_no_kubeconfig(self):
64 with self.assertRaisesRegexp(CloudifyHelmSDKError,
65 'Must provide kubeconfig file path.'):
66 self.helm.install('release1',
72 def test_uninstall_with_kubekonfig(self):
73 mock_execute = mock.Mock()
74 self.helm.execute = mock_execute
75 self.helm.uninstall('release1',
77 kubeconfig='/path/to/config')
78 cmd_expected = [HELM_BINARY, 'uninstall', 'release1',
79 '--kubeconfig=/path/to/config', '--dry-run',
81 mock_execute.assert_called_once_with(cmd_expected)
83 def test_uninstall_no_token_and_no_kubeconfig(self):
84 with self.assertRaisesRegexp(CloudifyHelmSDKError,
85 'Must provide kubeconfig file path.'):
86 self.helm.uninstall('release1',
88 apiserver='https://1.0.0.0')
90 def test_uninstall_no_apiserver_and_no_kubeconfig(self):
91 with self.assertRaisesRegexp(CloudifyHelmSDKError,
92 'Must provide kubeconfig file path.'):
93 self.helm.uninstall('release1',
97 def test_repo_add(self):
98 mock_execute = mock.Mock()
99 self.helm.execute = mock_execute
100 self.helm.repo_add('my_repo', 'https://github.com/repo')
101 cmd_expected = [HELM_BINARY, 'repo', 'add', 'my_repo',
102 'https://github.com/repo']
103 mock_execute.assert_called_once_with(cmd_expected)
105 def test_repo_remove(self):
106 mock_execute = mock.Mock()
107 self.helm.execute = mock_execute
108 self.helm.repo_remove('my_repo')
109 cmd_expected = [HELM_BINARY, 'repo', 'remove', 'my_repo']
110 mock_execute.assert_called_once_with(cmd_expected)
112 def test_upgrade_with_token_and_api(self):
113 with self.assertRaisesRegexp(CloudifyHelmSDKError,
114 'Must provide kubeconfig file path.'):
115 self.helm.upgrade('release1',
120 apiserver='https://1.0.0.0')
122 def test_upgrade_with_kubeconfig(self):
123 mock_execute = mock.Mock(return_value='{"name":"release1"}')
124 self.helm.execute = mock_execute
125 out = self.helm.upgrade('release1',
129 kubeconfig='/path/to/config')
130 cmd_expected = [HELM_BINARY, 'upgrade', 'release1', 'my_chart',
131 '--atomic', '-o=json', '--kubeconfig=/path/to/config',
132 '--dry-run', '--timeout=100', '--set', 'x=y', '--set',
134 mock_execute.assert_called_once_with(cmd_expected, True)
135 self.assertEqual(out, {"name": "release1"})
137 def test_upgrade_no_token_and_no_kubeconfig(self):
138 with self.assertRaisesRegexp(CloudifyHelmSDKError,
139 'Must provide kubeconfig file path.'):
140 self.helm.upgrade('release1',
144 apiserver='https://1.0.0.0')
146 def test_upgrade_no_apiserver_and_no_kubeconfig(self):
147 with self.assertRaisesRegexp(CloudifyHelmSDKError,
148 'Must provide kubeconfig file path.'):
149 self.helm.upgrade('release1',
155 def test_upgrade_no_chart(self):
156 with self.assertRaisesRegexp(CloudifyHelmSDKError,
157 'Must provide chart for upgrade '
159 self.helm.upgrade(release_name='release1',
161 set_values=mock_set_args,
162 kubeconfig='/path/to/config')