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
, mock_open
19 from click
.testing
import CliRunner
20 from osmclient
.scripts
import osm
23 @patch("osmclient.scripts.osm.check_client_version")
24 @patch("osmclient.scripts.osm.client.Client")
25 @patch("osmclient.scripts.osm.create_config")
26 class TestVim(unittest
.TestCase
):
28 self
.runner
= CliRunner()
31 def test_vim_create_ca_cert(
35 mock_check_client_version
,
37 mock_client
.return_value
= self
.ctx_obj
38 mock_create_config
.return_value
= {"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}
39 vim_config
= mock_create_config
.return_value
40 with
patch("builtins.open", mock_open(read_data
="test")):
57 json
.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
63 mock_create_config
.assert_called()
64 assert vim_config
["ca_cert_content"] == "test"
66 self
.ctx_obj
.vim
.create
.assert_called_with(
69 "vim-username": "user1",
70 "vim-password": "pass",
71 "vim-url": "http://test",
72 "vim-tenant-name": "tenant1",
73 "vim-type": "openstack",
76 {"ca_cert_content": "test"},
82 def test_vim_create_no_config(
86 mock_check_client_version
,
88 mock_client
.return_value
= self
.ctx_obj
89 mock_create_config
.return_value
= {}
91 with
patch("builtins.open", mock_open(read_data
="test")):
111 mock_check_client_version
.assert_not_called()
112 mock_create_config
.assert_called()
114 self
.ctx_obj
.vim
.create
.assert_called_with(
117 "vim-username": "user1",
118 "vim-password": "pass",
119 "vim-url": "http://test",
120 "vim-tenant-name": "tenant1",
121 "vim-type": "openstack",
130 def test_vim_create_sdn(
134 mock_check_client_version
,
136 mock_client
.return_value
= self
.ctx_obj
137 mock_create_config
.return_value
= {}
139 with
patch("builtins.open", mock_open(read_data
="test")):
159 "--sdn_port_mapping",
163 mock_check_client_version
.call_count
== 2
164 mock_create_config
.assert_called()
166 self
.ctx_obj
.vim
.create
.assert_called_with(
169 "vim-username": "user1",
170 "vim-password": "pass",
171 "vim-url": "http://test",
172 "vim-tenant-name": "tenant1",
173 "vim-type": "openstack",
182 def test_vim_update_ca_cert(
186 mock_check_client_version
,
188 mock_client
.return_value
= self
.ctx_obj
189 mock_create_config
.return_value
= {"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}
190 vim_config
= mock_create_config
.return_value
191 with
patch("builtins.open", mock_open(read_data
="test")):
199 json
.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
203 mock_check_client_version
.assert_called()
204 mock_create_config
.assert_called()
205 assert vim_config
["ca_cert_content"] == "test"
207 self
.ctx_obj
.vim
.update
.assert_called_with(
210 {"ca_cert_content": "test"},
216 def test_vim_update_no_config(
220 mock_check_client_version
,
222 mock_client
.return_value
= self
.ctx_obj
224 with
patch("builtins.open", mock_open(read_data
="test")):
235 mock_check_client_version
.assert_called()
236 mock_create_config
.assert_not_called()
238 self
.ctx_obj
.vim
.update
.assert_called_with(
241 "vim_password": "passwd",
249 def test_vim_update_sdn(
253 mock_check_client_version
,
255 mock_client
.return_value
= self
.ctx_obj
256 mock_create_config
.return_value
= {}
258 with
patch("builtins.open", mock_open(read_data
="test")):
267 "--sdn_port_mapping",
271 mock_check_client_version
.assert_called()
272 mock_create_config
.assert_not_called()
274 self
.ctx_obj
.vim
.update
.assert_called_with(