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