1 # Copyright 2021 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from unittest
.mock
import Mock
, patch
21 from click
.testing
import CliRunner
22 from osmclient
.common
.exceptions
import NotFound
23 from osmclient
.scripts
import osm
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
):
33 self
.runner
= CliRunner()
39 "_admin": {"detailed-status": "status", "operationalState": "state"},
45 mock_check_client_version
,
50 mock_client
.return_value
= self
.ctx_obj
67 json
.dumps({"juju-https-proxy": "http://squid:3128"}),
70 mock_check_client_version
.assert_called()
71 self
.ctx_obj
.paas
.create
.assert_called_with(
75 "endpoints": ["1.2.3.4:17070"],
78 "description": "description",
79 "config": {"juju-https-proxy": "http://squid:3128"},
82 mock_pretty_table
.assert_not_called()
83 self
.table
.add_row
.assert_not_called()
84 mock_print
.assert_not_called()
89 mock_check_client_version
,
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()
102 def test_paas_update_with_args(
105 mock_check_client_version
,
110 mock_client
.return_value
= self
.ctx_obj
129 json
.dumps({"juju-https-proxy": "http://squid:3128"}),
132 mock_check_client_version
.assert_called()
133 self
.ctx_obj
.paas
.update
.assert_called_with(
136 "name": "paas_new_name",
138 "endpoints": ["1.2.3.4:17070"],
141 "description": "description",
142 "config": {"juju-https-proxy": "http://squid:3128"},
145 mock_pretty_table
.assert_not_called()
146 self
.table
.add_row
.assert_not_called()
147 mock_print
.assert_not_called()
149 def test_paas_delete(
152 mock_check_client_version
,
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()
165 def test_paas_delete_force(
168 mock_check_client_version
,
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()
184 mock_check_client_version
,
189 mock_client
.return_value
= self
.ctx_obj
190 mock_pretty_table
.return_value
= self
.table
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
)
201 def test_paas_list_long(
204 mock_check_client_version
,
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
]
214 osm
.cli_osm
, ["paas-list", "--filter", "somefilter", "--long"]
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"]
221 self
.table
.add_row
.assert_called_with(
222 ["name", "1234", "project_name", "state", "status"]
224 mock_print
.assert_called_with(self
.table
)
226 def test_paas_list_literal(
229 mock_check_client_version
,
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)
245 def test_paas_list_empty(
248 mock_check_client_version
,
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
)
266 mock_check_client_version
,
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
)
280 def test_paas_show_literal(
283 mock_check_client_version
,
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)
298 def test_paas_show_literal_throws_exception(
301 mock_check_client_version
,
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()