Addition of PaaS
[osm/osmclient.git] / osmclient / scripts / tests / test_paas_operations.py
1 # Copyright 2021 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 import json
17 import unittest
18 from unittest.mock import Mock, patch
19 import yaml
20
21 from click.testing import CliRunner
22 from osmclient.common.exceptions import NotFound
23 from osmclient.scripts import osm
24
25
26 @patch("builtins.print")
27 @patch("osmclient.scripts.osm.PrettyTable")
28 @patch("osmclient.scripts.osm.client.Client")
29 @patch("osmclient.scripts.osm.check_client_version")
30 @patch("osmclient.scripts.osm.get_project")
31 class TestPaaS(unittest.TestCase):
32 def setUp(self):
33 self.runner = CliRunner()
34 self.ctx_obj = Mock()
35 self.table = Mock()
36 self.paas_data = {
37 "name": "name",
38 "_id": "1234",
39 "_admin": {"detailed-status": "status", "operationalState": "state"},
40 }
41
42 def test_paas_add(
43 self,
44 mock_get_project,
45 mock_check_client_version,
46 mock_client,
47 mock_pretty_table,
48 mock_print,
49 ):
50 mock_client.return_value = self.ctx_obj
51 self.runner.invoke(
52 osm.cli_osm,
53 [
54 "paas-add",
55 "name",
56 "--paas_type",
57 "juju",
58 "--endpoints",
59 "1.2.3.4:17070",
60 "--user",
61 "user",
62 "--secret",
63 "secret",
64 "--description",
65 "description",
66 "--config",
67 json.dumps({"juju-https-proxy": "http://squid:3128"}),
68 ],
69 )
70 mock_check_client_version.assert_called()
71 self.ctx_obj.paas.create.assert_called_with(
72 {
73 "name": "name",
74 "paas_type": "juju",
75 "endpoints": ["1.2.3.4:17070"],
76 "user": "user",
77 "secret": "secret",
78 "description": "description",
79 "config": {"juju-https-proxy": "http://squid:3128"},
80 }
81 )
82 mock_pretty_table.assert_not_called()
83 self.table.add_row.assert_not_called()
84 mock_print.assert_not_called()
85
86 def test_paas_update(
87 self,
88 mock_get_project,
89 mock_check_client_version,
90 mock_client,
91 mock_pretty_table,
92 mock_print,
93 ):
94 mock_client.return_value = self.ctx_obj
95 self.runner.invoke(osm.cli_osm, ["paas-update", "name"])
96 mock_check_client_version.assert_called()
97 self.ctx_obj.paas.update.assert_called_with("name", {})
98 mock_pretty_table.assert_not_called()
99 self.table.add_row.assert_not_called()
100 mock_print.assert_not_called()
101
102 def test_paas_update_with_args(
103 self,
104 mock_get_project,
105 mock_check_client_version,
106 mock_client,
107 mock_pretty_table,
108 mock_print,
109 ):
110 mock_client.return_value = self.ctx_obj
111 self.runner.invoke(
112 osm.cli_osm,
113 [
114 "paas-update",
115 "name",
116 "--newname",
117 "paas_new_name",
118 "--paas_type",
119 "juju",
120 "--endpoints",
121 "1.2.3.4:17070",
122 "--user",
123 "user",
124 "--secret",
125 "secret",
126 "--description",
127 "description",
128 "--config",
129 json.dumps({"juju-https-proxy": "http://squid:3128"}),
130 ],
131 )
132 mock_check_client_version.assert_called()
133 self.ctx_obj.paas.update.assert_called_with(
134 "name",
135 {
136 "name": "paas_new_name",
137 "paas_type": "juju",
138 "endpoints": ["1.2.3.4:17070"],
139 "user": "user",
140 "secret": "secret",
141 "description": "description",
142 "config": {"juju-https-proxy": "http://squid:3128"},
143 },
144 )
145 mock_pretty_table.assert_not_called()
146 self.table.add_row.assert_not_called()
147 mock_print.assert_not_called()
148
149 def test_paas_delete(
150 self,
151 mock_get_project,
152 mock_check_client_version,
153 mock_client,
154 mock_pretty_table,
155 mock_print,
156 ):
157 mock_client.return_value = self.ctx_obj
158 self.runner.invoke(osm.cli_osm, ["paas-delete", "name"])
159 mock_check_client_version.assert_called()
160 self.ctx_obj.paas.delete.assert_called_with("name", force=False)
161 mock_pretty_table.assert_not_called()
162 self.table.add_row.assert_not_called()
163 mock_print.assert_not_called()
164
165 def test_paas_delete_force(
166 self,
167 mock_get_project,
168 mock_check_client_version,
169 mock_client,
170 mock_pretty_table,
171 mock_print,
172 ):
173 mock_client.return_value = self.ctx_obj
174 self.runner.invoke(osm.cli_osm, ["paas-delete", "name", "--force"])
175 mock_check_client_version.assert_called()
176 self.ctx_obj.paas.delete.assert_called_with("name", force=True)
177 mock_pretty_table.assert_not_called()
178 self.table.add_row.assert_not_called()
179 mock_print.assert_not_called()
180
181 def test_paas_list(
182 self,
183 mock_get_project,
184 mock_check_client_version,
185 mock_client,
186 mock_pretty_table,
187 mock_print,
188 ):
189 mock_client.return_value = self.ctx_obj
190 mock_pretty_table.return_value = self.table
191
192 self.ctx_obj.paas.list.return_value = [self.paas_data]
193 self.runner.invoke(osm.cli_osm, ["paas-list", "--filter", "somefilter"])
194 mock_check_client_version.assert_called()
195 self.ctx_obj.paas.list.assert_called_with("somefilter")
196 mock_pretty_table.assert_called_with(["Name", "Id", "Operational State"])
197 mock_get_project.assert_not_called()
198 self.table.add_row.assert_called_with(["name", "1234", "state"])
199 mock_print.assert_called_with(self.table)
200
201 def test_paas_list_long(
202 self,
203 mock_get_project,
204 mock_check_client_version,
205 mock_client,
206 mock_pretty_table,
207 mock_print,
208 ):
209 mock_client.return_value = self.ctx_obj
210 mock_pretty_table.return_value = self.table
211 mock_get_project.return_value = ("5678", "project_name")
212 self.ctx_obj.paas.list.return_value = [self.paas_data]
213 self.runner.invoke(
214 osm.cli_osm, ["paas-list", "--filter", "somefilter", "--long"]
215 )
216 mock_check_client_version.assert_called()
217 self.ctx_obj.paas.list.assert_called_with("somefilter")
218 mock_pretty_table.assert_called_with(
219 ["Name", "Id", "Project", "Operational State", "Detailed Status"]
220 )
221 self.table.add_row.assert_called_with(
222 ["name", "1234", "project_name", "state", "status"]
223 )
224 mock_print.assert_called_with(self.table)
225
226 def test_paas_list_literal(
227 self,
228 mock_get_project,
229 mock_check_client_version,
230 mock_client,
231 mock_pretty_table,
232 mock_print,
233 ):
234 mock_client.return_value = self.ctx_obj
235 self.ctx_obj.paas.list.return_value = [self.paas_data]
236 self.runner.invoke(osm.cli_osm, ["paas-list", "--literal"])
237 mock_check_client_version.assert_called()
238 self.ctx_obj.paas.list.assert_called()
239 mock_pretty_table.assert_not_called()
240 self.table.add_row.assert_not_called()
241 mock_print.assert_called_with(
242 yaml.safe_dump([self.paas_data], indent=4, default_flow_style=False)
243 )
244
245 def test_paas_list_empty(
246 self,
247 mock_get_project,
248 mock_check_client_version,
249 mock_client,
250 mock_pretty_table,
251 mock_print,
252 ):
253 mock_client.return_value = self.ctx_obj
254 mock_pretty_table.return_value = self.table
255 self.ctx_obj.paas.list.return_value = []
256 self.runner.invoke(osm.cli_osm, ["paas-list", "--filter", "somefilter"])
257 mock_check_client_version.assert_called()
258 self.ctx_obj.paas.list.assert_called_with("somefilter")
259 mock_get_project.assert_not_called()
260 mock_pretty_table.assert_called_with(["Name", "Id", "Operational State"])
261 mock_print.assert_called_with(self.table)
262
263 def test_paas_show(
264 self,
265 mock_get_project,
266 mock_check_client_version,
267 mock_client,
268 mock_pretty_table,
269 mock_print,
270 ):
271 mock_client.return_value = self.ctx_obj
272 mock_pretty_table.return_value = self.table
273 self.ctx_obj.paas.get.return_value = self.paas_data
274 self.runner.invoke(osm.cli_osm, ["paas-show", "name"])
275 self.ctx_obj.paas.get.assert_called_with("name")
276 mock_pretty_table.assert_called_with(["key", "attribute"])
277 self.assertEqual(self.table.add_row.call_count, len(self.paas_data))
278 mock_print.assert_called_with(self.table)
279
280 def test_paas_show_literal(
281 self,
282 mock_get_project,
283 mock_check_client_version,
284 mock_client,
285 mock_pretty_table,
286 mock_print,
287 ):
288 mock_client.return_value = self.ctx_obj
289 self.ctx_obj.paas.get.return_value = self.paas_data
290 self.runner.invoke(osm.cli_osm, ["paas-show", "name", "--literal"])
291 self.ctx_obj.paas.get.assert_called_with("name")
292 mock_pretty_table.assert_not_called()
293 self.table.add_row.assert_not_called()
294 mock_print.assert_called_with(
295 yaml.safe_dump(self.paas_data, indent=4, default_flow_style=False)
296 )
297
298 def test_paas_show_literal_throws_exception(
299 self,
300 mock_get_project,
301 mock_check_client_version,
302 mock_client,
303 mock_pretty_table,
304 mock_print,
305 ):
306 mock_client.return_value = self.ctx_obj
307 self.ctx_obj.paas.get.side_effect = NotFound()
308 self.runner.invoke(osm.cli_osm, ["paas-show", "name", "--literal"])
309 self.ctx_obj.paas.get.assert_called_with("name")
310 mock_pretty_table.assert_not_called()
311 self.table.add_row.assert_not_called()
312 mock_print.assert_not_called()