Updated python version and flake8 in tox
[pti/o2.git] / helm_sdk / tests / test_sdk.py
1 ########
2 # Copyright (c) 2019 Cloudify Platform Ltd. All rights reserved
3 #
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
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 import mock
17
18 from . import HelmTestBase, HELM_BINARY
19 from helm_sdk.exceptions import CloudifyHelmSDKError
20
21 mock_flags = [{'name': 'dry-run'},
22               {'name': 'timeout', 'value': '100'}]
23 mock_set_args = [{'name': 'x', 'value': 'y'},
24                  {'name': 'a', 'value': 'b'}]
25
26
27 class HelmSDKTest(HelmTestBase):
28
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',
33                               'my_chart',
34                               mock_flags,
35                               mock_set_args,
36                               token='demotoken',
37                               apiserver='https://1.0.0.0')
38
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',
43                                 'my_chart',
44                                 mock_flags,
45                                 mock_set_args,
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"})
53
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',
58                               'my_chart',
59                               mock_flags,
60                               mock_set_args,
61                               apiserver='https://1.0.0.0')
62
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',
67                               'my_chart',
68                               mock_flags,
69                               mock_set_args,
70                               token='demotoken')
71
72     def test_uninstall_with_kubekonfig(self):
73         mock_execute = mock.Mock()
74         self.helm.execute = mock_execute
75         self.helm.uninstall('release1',
76                             mock_flags,
77                             kubeconfig='/path/to/config')
78         cmd_expected = [HELM_BINARY, 'uninstall', 'release1',
79                         '--kubeconfig=/path/to/config', '--dry-run',
80                         '--timeout=100']
81         mock_execute.assert_called_once_with(cmd_expected)
82
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',
87                                 mock_flags,
88                                 apiserver='https://1.0.0.0')
89
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',
94                                 mock_flags,
95                                 token='demotoken')
96
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)
104
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)
111
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',
116                               'example/mariadb',
117                               mock_flags,
118                               mock_set_args,
119                               token='demotoken',
120                               apiserver='https://1.0.0.0')
121
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',
126                                 'my_chart',
127                                 mock_flags,
128                                 mock_set_args,
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',
133                         'a=b']
134         mock_execute.assert_called_once_with(cmd_expected, True)
135         self.assertEqual(out, {"name": "release1"})
136
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',
141                               'my_chart',
142                               mock_flags,
143                               mock_set_args,
144                               apiserver='https://1.0.0.0')
145
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',
150                               'my_chart',
151                               mock_flags,
152                               mock_set_args,
153                               token='demotoken')
154
155     def test_upgrade_no_chart(self):
156         with self.assertRaisesRegexp(CloudifyHelmSDKError,
157                                      'Must provide chart for upgrade '
158                                      'release.'):
159             self.helm.upgrade(release_name='release1',
160                               flags=mock_flags,
161                               set_values=mock_set_args,
162                               kubeconfig='/path/to/config')