Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / osmclient / scripts / tests / tests_vim.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, mock_open
19 from click.testing import CliRunner
20 from osmclient.cli_commands import vim
21
22
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):
27 def setUp(self):
28 self.runner = CliRunner()
29 self.ctx_obj = Mock()
30
31 def test_vim_create_ca_cert(
32 self,
33 mock_create_config,
34 mock_client,
35 mock_check_client_version,
36 ):
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")):
41 self.runner.invoke(
42 vim.vim_create,
43 obj=self.ctx_obj,
44 args=[
45 "--name",
46 "vim1",
47 "--user",
48 "user1",
49 "--password",
50 "pass",
51 "--auth_url",
52 "http://test",
53 "--tenant",
54 "tenant1",
55 "--config",
56 json.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
57 "--account_type",
58 "openstack",
59 ],
60 )
61
62 mock_create_config.assert_called()
63 assert vim_config["ca_cert_content"] == "test"
64
65 self.ctx_obj.vim.create.assert_called_with(
66 "vim1",
67 {
68 "vim-username": "user1",
69 "vim-password": "pass",
70 "vim-url": "http://test",
71 "vim-tenant-name": "tenant1",
72 "vim-type": "openstack",
73 "description": None,
74 },
75 {"ca_cert_content": "test"},
76 None,
77 None,
78 wait=False,
79 )
80
81 def test_vim_create_no_config(
82 self,
83 mock_create_config,
84 mock_client,
85 mock_check_client_version,
86 ):
87 mock_client.return_value = self.ctx_obj
88 mock_create_config.return_value = {}
89
90 with patch("builtins.open", mock_open(read_data="test")):
91 self.runner.invoke(
92 vim.vim_create,
93 obj=self.ctx_obj,
94 args=[
95 "--name",
96 "vim1",
97 "--user",
98 "user1",
99 "--password",
100 "pass",
101 "--auth_url",
102 "http://test",
103 "--tenant",
104 "tenant1",
105 "--account_type",
106 "openstack",
107 ],
108 )
109 mock_check_client_version.assert_not_called()
110 mock_create_config.assert_called()
111
112 self.ctx_obj.vim.create.assert_called_with(
113 "vim1",
114 {
115 "vim-username": "user1",
116 "vim-password": "pass",
117 "vim-url": "http://test",
118 "vim-tenant-name": "tenant1",
119 "vim-type": "openstack",
120 "description": None,
121 },
122 {},
123 None,
124 None,
125 wait=False,
126 )
127
128 def test_vim_create_sdn(
129 self,
130 mock_create_config,
131 mock_client,
132 mock_check_client_version,
133 ):
134 mock_client.return_value = self.ctx_obj
135 mock_create_config.return_value = {}
136
137 with patch("builtins.open", mock_open(read_data="test")):
138 self.runner.invoke(
139 vim.vim_create,
140 obj=self.ctx_obj,
141 args=[
142 "--name",
143 "vim1",
144 "--user",
145 "user1",
146 "--password",
147 "pass",
148 "--auth_url",
149 "http://test",
150 "--tenant",
151 "tenant1",
152 "--account_type",
153 "openstack",
154 "--sdn_controller",
155 "controller",
156 "--sdn_port_mapping",
157 "port-map",
158 ],
159 )
160 mock_check_client_version.call_count == 2
161 mock_create_config.assert_called()
162
163 self.ctx_obj.vim.create.assert_called_with(
164 "vim1",
165 {
166 "vim-username": "user1",
167 "vim-password": "pass",
168 "vim-url": "http://test",
169 "vim-tenant-name": "tenant1",
170 "vim-type": "openstack",
171 "description": None,
172 },
173 {},
174 "controller",
175 "port-map",
176 wait=False,
177 )
178
179 def test_vim_update_ca_cert(
180 self,
181 mock_create_config,
182 mock_client,
183 mock_check_client_version,
184 ):
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")):
189 self.runner.invoke(
190 vim.vim_update,
191 obj=self.ctx_obj,
192 args=[
193 "vim1",
194 "--config",
195 json.dumps({"ca_cert": "/home/ubuntu/.ssh/id_rsa.pub"}),
196 ],
197 )
198
199 mock_check_client_version.assert_called()
200 mock_create_config.assert_called()
201 assert vim_config["ca_cert_content"] == "test"
202
203 self.ctx_obj.vim.update.assert_called_with(
204 "vim1",
205 {},
206 {"ca_cert_content": "test"},
207 None,
208 None,
209 wait=False,
210 )
211
212 def test_vim_update_no_config(
213 self,
214 mock_create_config,
215 mock_client,
216 mock_check_client_version,
217 ):
218 mock_client.return_value = self.ctx_obj
219
220 with patch("builtins.open", mock_open(read_data="test")):
221 self.runner.invoke(
222 vim.vim_update,
223 obj=self.ctx_obj,
224 args=[
225 "vim1",
226 "--password",
227 "passwd",
228 ],
229 )
230 mock_check_client_version.assert_called()
231 mock_create_config.assert_not_called()
232
233 self.ctx_obj.vim.update.assert_called_with(
234 "vim1",
235 {
236 "vim_password": "passwd",
237 },
238 None,
239 None,
240 None,
241 wait=False,
242 )
243
244 def test_vim_update_sdn(
245 self,
246 mock_create_config,
247 mock_client,
248 mock_check_client_version,
249 ):
250 mock_client.return_value = self.ctx_obj
251 mock_create_config.return_value = {}
252
253 with patch("builtins.open", mock_open(read_data="test")):
254 self.runner.invoke(
255 vim.vim_update,
256 obj=self.ctx_obj,
257 args=[
258 "vim1",
259 "--sdn_controller",
260 "controller",
261 "--sdn_port_mapping",
262 "port-map",
263 ],
264 )
265 mock_check_client_version.assert_called()
266 mock_create_config.assert_not_called()
267
268 self.ctx_obj.vim.update.assert_called_with(
269 "vim1",
270 {},
271 None,
272 "controller",
273 "port-map",
274 wait=False,
275 )