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
.cli_commands
import vim
23 @patch("osmclient.cli_commands.utils.check_client_version")
24 @patch("osmclient.scripts.osm.client.Client")
25 @patch("osmclient.cli_commands.utils.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")):
56 json
.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
62 mock_create_config
.assert_called()
63 assert vim_config
["ca_cert_content"] == "test"
65 self
.ctx_obj
.vim
.create
.assert_called_with(
68 "vim-username": "user1",
69 "vim-password": "pass",
70 "vim-url": "http://test",
71 "vim-tenant-name": "tenant1",
72 "vim-type": "openstack",
75 {"ca_cert_content": "test"},
81 def test_vim_create_no_config(
85 mock_check_client_version
,
87 mock_client
.return_value
= self
.ctx_obj
88 mock_create_config
.return_value
= {}
90 with
patch("builtins.open", mock_open(read_data
="test")):
109 mock_check_client_version
.assert_not_called()
110 mock_create_config
.assert_called()
112 self
.ctx_obj
.vim
.create
.assert_called_with(
115 "vim-username": "user1",
116 "vim-password": "pass",
117 "vim-url": "http://test",
118 "vim-tenant-name": "tenant1",
119 "vim-type": "openstack",
128 def test_vim_create_sdn(
132 mock_check_client_version
,
134 mock_client
.return_value
= self
.ctx_obj
135 mock_create_config
.return_value
= {}
137 with
patch("builtins.open", mock_open(read_data
="test")):
156 "--sdn_port_mapping",
160 mock_check_client_version
.call_count
== 2
161 mock_create_config
.assert_called()
163 self
.ctx_obj
.vim
.create
.assert_called_with(
166 "vim-username": "user1",
167 "vim-password": "pass",
168 "vim-url": "http://test",
169 "vim-tenant-name": "tenant1",
170 "vim-type": "openstack",
179 def test_vim_update_ca_cert(
183 mock_check_client_version
,
185 mock_client
.return_value
= self
.ctx_obj
186 mock_create_config
.return_value
= {"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}
187 vim_config
= mock_create_config
.return_value
188 with
patch("builtins.open", mock_open(read_data
="test")):
195 json
.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
199 mock_check_client_version
.assert_called()
200 mock_create_config
.assert_called()
201 assert vim_config
["ca_cert_content"] == "test"
203 self
.ctx_obj
.vim
.update
.assert_called_with(
206 {"ca_cert_content": "test"},
212 def test_vim_update_no_config(
216 mock_check_client_version
,
218 mock_client
.return_value
= self
.ctx_obj
220 with
patch("builtins.open", mock_open(read_data
="test")):
230 mock_check_client_version
.assert_called()
231 mock_create_config
.assert_not_called()
233 self
.ctx_obj
.vim
.update
.assert_called_with(
236 "vim_password": "passwd",
244 def test_vim_update_sdn(
248 mock_check_client_version
,
250 mock_client
.return_value
= self
.ctx_obj
251 mock_create_config
.return_value
= {}
253 with
patch("builtins.open", mock_open(read_data
="test")):
261 "--sdn_port_mapping",
265 mock_check_client_version
.assert_called()
266 mock_create_config
.assert_not_called()
268 self
.ctx_obj
.vim
.update
.assert_called_with(