Refactor openstack new_vminstance method
[osm/RO.git] / RO-VIM-openstack / osm_rovim_openstack / tests / test_vimconn_openstack.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2017 Intel Corporation.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
20 ##
21
22 """
23 This module contains unit tests for the OpenStack VIM connector
24 Run this directly with python2 or python3.
25 """
26 import copy
27 from copy import deepcopy
28 import logging
29 import unittest
30
31 import mock
32 from mock import MagicMock, patch
33 from neutronclient.v2_0.client import Client
34 from osm_ro_plugin import vimconn
35 from osm_ro_plugin.vimconn import VimConnConnectionException, VimConnException
36 from osm_rovim_openstack.vimconn_openstack import vimconnector
37
38 __author__ = "Igor D.C."
39 __date__ = "$23-aug-2017 23:59:59$"
40
41 # Variables Used in TestNewVmInstance Class
42 name = "basicvm"
43 description = "my firewall"
44 start = True
45 image_id = "408b73-e9cc-5a6a-t270-82cc4811bd4a"
46 flavor_id = "208b73-e9cc-5a6a-t270-82cc4811bd4a"
47 affinity_group_list = []
48 net_list = []
49 cloud_config = {}
50 disk_list = []
51 disk_list2 = [
52 {"size": 10, "image_id": image_id},
53 {"size": 20},
54 ]
55 availability_zone_index = 0
56 availability_zone_list = ["nova"]
57 floating_network_vim_id = "108b73-e9cc-5a6a-t270-82cc4811bd4a"
58 net_id = "83372685-f67f-49fd-8722-eabb7692fc22"
59 net2_id = "46472685-f67f-49fd-8722-eabb7692fc22"
60 mac_address = "00:00:5e:00:53:af"
61 port_id = "03372685-f67f-49fd-8722-eabb7692fc22"
62 time_return_value = 156570000
63 port2_id = "17472685-f67f-49fd-8722-eabb7692fc22"
64 root_vol_id = "tc408b73-r9cc-5a6a-a270-82cc4811bd4a"
65 ip_addr1 = "20.3.4.5"
66 volume_id = "ac408b73-b9cc-4a6a-a270-82cc4811bd4a"
67 volume_id2 = "o4e0e83-b9uu-4akk-a234-89cc4811bd4a"
68 volume_id3 = "44e0e83-t9uu-4akk-a234-p9cc4811bd4a"
69
70
71 class TestSfcOperations(unittest.TestCase):
72 @mock.patch("logging.getLogger", autospec=True)
73 def setUp(self, mock_logger):
74 # Instantiate dummy VIM connector so we can test it
75 # It throws exception because of dummy parameters,
76 # We are disabling the logging of exception not to print them to console.
77 mock_logger = logging.getLogger()
78 mock_logger.disabled = True
79 self.vimconn = vimconnector(
80 "123",
81 "openstackvim",
82 "456",
83 "789",
84 "http://dummy.url",
85 None,
86 "user",
87 "pass",
88 )
89
90 def _test_new_sfi(
91 self,
92 create_sfc_port_pair,
93 sfc_encap,
94 ingress_ports=["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
95 egress_ports=["230cdf1b-de37-4891-bc07-f9010cf1f967"],
96 ):
97 # input to VIM connector
98 name = "osm_sfi"
99 # + ingress_ports
100 # + egress_ports
101 # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
102 correlation = "nsh"
103 if sfc_encap is not None:
104 if not sfc_encap:
105 correlation = None
106
107 # what OpenStack is assumed to respond (patch OpenStack"s return value)
108 dict_from_neutron = {
109 "port_pair": {
110 "id": "3d7ddc13-923c-4332-971e-708ed82902ce",
111 "name": name,
112 "description": "",
113 "tenant_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
114 "project_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
115 "ingress": ingress_ports[0] if len(ingress_ports) else None,
116 "egress": egress_ports[0] if len(egress_ports) else None,
117 "service_function_parameters": {"correlation": correlation},
118 }
119 }
120 create_sfc_port_pair.return_value = dict_from_neutron
121
122 # what the VIM connector is expected to
123 # send to OpenStack based on the input
124 dict_to_neutron = {
125 "port_pair": {
126 "name": name,
127 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
128 "egress": "230cdf1b-de37-4891-bc07-f9010cf1f967",
129 "service_function_parameters": {"correlation": correlation},
130 }
131 }
132
133 # call the VIM connector
134 if sfc_encap is None:
135 result = self.vimconn.new_sfi(name, ingress_ports, egress_ports)
136 else:
137 result = self.vimconn.new_sfi(name, ingress_ports, egress_ports, sfc_encap)
138
139 # assert that the VIM connector made the expected call to OpenStack
140 create_sfc_port_pair.assert_called_with(dict_to_neutron)
141 # assert that the VIM connector had the expected result / return value
142 self.assertEqual(result, dict_from_neutron["port_pair"]["id"])
143
144 def _test_new_sf(self, create_sfc_port_pair_group):
145 # input to VIM connector
146 name = "osm_sf"
147 instances = [
148 "bbd01220-cf72-41f2-9e70-0669c2e5c4cd",
149 "12ba215e-3987-4892-bd3a-d0fd91eecf98",
150 "e25a7c79-14c8-469a-9ae1-f601c9371ffd",
151 ]
152
153 # what OpenStack is assumed to respond (patch OpenStack"s return value)
154 dict_from_neutron = {
155 "port_pair_group": {
156 "id": "3d7ddc13-923c-4332-971e-708ed82902ce",
157 "name": name,
158 "description": "",
159 "tenant_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
160 "project_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
161 "port_pairs": instances,
162 "group_id": 1,
163 "port_pair_group_parameters": {
164 "lb_fields": [],
165 "ppg_n_tuple_mapping": {
166 "ingress_n_tuple": {},
167 "egress_n_tuple": {},
168 },
169 },
170 }
171 }
172 create_sfc_port_pair_group.return_value = dict_from_neutron
173
174 # what the VIM connector is expected to
175 # send to OpenStack based on the input
176 dict_to_neutron = {
177 "port_pair_group": {
178 "name": name,
179 "port_pairs": [
180 "bbd01220-cf72-41f2-9e70-0669c2e5c4cd",
181 "12ba215e-3987-4892-bd3a-d0fd91eecf98",
182 "e25a7c79-14c8-469a-9ae1-f601c9371ffd",
183 ],
184 }
185 }
186
187 # call the VIM connector
188 result = self.vimconn.new_sf(name, instances)
189
190 # assert that the VIM connector made the expected call to OpenStack
191 create_sfc_port_pair_group.assert_called_with(dict_to_neutron)
192 # assert that the VIM connector had the expected result / return value
193 self.assertEqual(result, dict_from_neutron["port_pair_group"]["id"])
194
195 def _test_new_sfp(self, create_sfc_port_chain, sfc_encap, spi):
196 # input to VIM connector
197 name = "osm_sfp"
198 classifications = [
199 "2bd2a2e5-c5fd-4eac-a297-d5e255c35c19",
200 "00f23389-bdfa-43c2-8b16-5815f2582fa8",
201 ]
202 sfs = [
203 "2314daec-c262-414a-86e3-69bb6fa5bc16",
204 "d8bfdb5d-195e-4f34-81aa-6135705317df",
205 ]
206
207 # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
208 correlation = "nsh"
209 chain_id = 33
210 if spi:
211 chain_id = spi
212
213 # what OpenStack is assumed to respond (patch OpenStack"s return value)
214 dict_from_neutron = {
215 "port_chain": {
216 "id": "5bc05721-079b-4b6e-a235-47cac331cbb6",
217 "name": name,
218 "description": "",
219 "tenant_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
220 "project_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
221 "chain_id": chain_id,
222 "flow_classifiers": classifications,
223 "port_pair_groups": sfs,
224 "chain_parameters": {"correlation": correlation},
225 }
226 }
227 create_sfc_port_chain.return_value = dict_from_neutron
228
229 # what the VIM connector is expected to
230 # send to OpenStack based on the input
231 dict_to_neutron = {
232 "port_chain": {
233 "name": name,
234 "flow_classifiers": [
235 "2bd2a2e5-c5fd-4eac-a297-d5e255c35c19",
236 "00f23389-bdfa-43c2-8b16-5815f2582fa8",
237 ],
238 "port_pair_groups": [
239 "2314daec-c262-414a-86e3-69bb6fa5bc16",
240 "d8bfdb5d-195e-4f34-81aa-6135705317df",
241 ],
242 "chain_parameters": {"correlation": correlation},
243 }
244 }
245 if spi:
246 dict_to_neutron["port_chain"]["chain_id"] = spi
247
248 # call the VIM connector
249 if sfc_encap is None:
250 dict_to_neutron["port_chain"]["chain_parameters"] = {"correlation": "mpls"}
251 if spi is None:
252 result = self.vimconn.new_sfp(
253 name, classifications, sfs, sfc_encap=False
254 )
255 else:
256 result = self.vimconn.new_sfp(
257 name, classifications, sfs, sfc_encap=False, spi=spi
258 )
259 else:
260 if spi is None:
261 result = self.vimconn.new_sfp(name, classifications, sfs, sfc_encap)
262 else:
263 result = self.vimconn.new_sfp(
264 name, classifications, sfs, sfc_encap, spi
265 )
266
267 # assert that the VIM connector made the expected call to OpenStack
268 create_sfc_port_chain.assert_called_with(dict_to_neutron)
269 # assert that the VIM connector had the expected result / return value
270 self.assertEqual(result, dict_from_neutron["port_chain"]["id"])
271
272 def _test_new_classification(self, create_sfc_flow_classifier, ctype):
273 # input to VIM connector
274 name = "osm_classification"
275 definition = {
276 "ethertype": "IPv4",
277 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
278 "protocol": "tcp",
279 "source_ip_prefix": "192.168.2.0/24",
280 "source_port_range_max": 99,
281 "source_port_range_min": 50,
282 }
283
284 # what OpenStack is assumed to respond (patch OpenStack"s return value)
285 dict_from_neutron = {"flow_classifier": copy.copy(definition)}
286 dict_from_neutron["flow_classifier"][
287 "id"
288 ] = "7735ec2c-fddf-4130-9712-32ed2ab6a372"
289 dict_from_neutron["flow_classifier"]["name"] = name
290 dict_from_neutron["flow_classifier"]["description"] = ""
291 dict_from_neutron["flow_classifier"][
292 "tenant_id"
293 ] = "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c"
294 dict_from_neutron["flow_classifier"][
295 "project_id"
296 ] = "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c"
297 create_sfc_flow_classifier.return_value = dict_from_neutron
298
299 # what the VIM connector is expected to
300 # send to OpenStack based on the input
301 dict_to_neutron = {"flow_classifier": copy.copy(definition)}
302 dict_to_neutron["flow_classifier"]["name"] = "osm_classification"
303
304 # call the VIM connector
305 result = self.vimconn.new_classification(name, ctype, definition)
306
307 # assert that the VIM connector made the expected call to OpenStack
308 create_sfc_flow_classifier.assert_called_with(dict_to_neutron)
309 # assert that the VIM connector had the expected result / return value
310 self.assertEqual(result, dict_from_neutron["flow_classifier"]["id"])
311
312 @mock.patch.object(Client, "create_sfc_flow_classifier")
313 def test_new_classification(self, create_sfc_flow_classifier):
314 self._test_new_classification(
315 create_sfc_flow_classifier, "legacy_flow_classifier"
316 )
317
318 @mock.patch.object(Client, "create_sfc_flow_classifier")
319 def test_new_classification_unsupported_type(self, create_sfc_flow_classifier):
320 self.assertRaises(
321 vimconn.VimConnNotSupportedException,
322 self._test_new_classification,
323 create_sfc_flow_classifier,
324 "h265",
325 )
326
327 @mock.patch.object(Client, "create_sfc_port_pair")
328 def test_new_sfi_with_sfc_encap(self, create_sfc_port_pair):
329 self._test_new_sfi(create_sfc_port_pair, True)
330
331 @mock.patch.object(Client, "create_sfc_port_pair")
332 def test_new_sfi_without_sfc_encap(self, create_sfc_port_pair):
333 self._test_new_sfi(create_sfc_port_pair, False)
334
335 @mock.patch.object(Client, "create_sfc_port_pair")
336 def test_new_sfi_default_sfc_encap(self, create_sfc_port_pair):
337 self._test_new_sfi(create_sfc_port_pair, None)
338
339 @mock.patch.object(Client, "create_sfc_port_pair")
340 def test_new_sfi_bad_ingress_ports(self, create_sfc_port_pair):
341 ingress_ports = [
342 "5311c75d-d718-4369-bbda-cdcc6da60fcc",
343 "a0273f64-82c9-11e7-b08f-6328e53f0fa7",
344 ]
345 self.assertRaises(
346 vimconn.VimConnNotSupportedException,
347 self._test_new_sfi,
348 create_sfc_port_pair,
349 True,
350 ingress_ports=ingress_ports,
351 )
352 ingress_ports = []
353 self.assertRaises(
354 vimconn.VimConnNotSupportedException,
355 self._test_new_sfi,
356 create_sfc_port_pair,
357 True,
358 ingress_ports=ingress_ports,
359 )
360
361 @mock.patch.object(Client, "create_sfc_port_pair")
362 def test_new_sfi_bad_egress_ports(self, create_sfc_port_pair):
363 egress_ports = [
364 "230cdf1b-de37-4891-bc07-f9010cf1f967",
365 "b41228fe-82c9-11e7-9b44-17504174320b",
366 ]
367 self.assertRaises(
368 vimconn.VimConnNotSupportedException,
369 self._test_new_sfi,
370 create_sfc_port_pair,
371 True,
372 egress_ports=egress_ports,
373 )
374 egress_ports = []
375 self.assertRaises(
376 vimconn.VimConnNotSupportedException,
377 self._test_new_sfi,
378 create_sfc_port_pair,
379 True,
380 egress_ports=egress_ports,
381 )
382
383 @mock.patch.object(vimconnector, "get_sfi")
384 @mock.patch.object(Client, "create_sfc_port_pair_group")
385 def test_new_sf(self, create_sfc_port_pair_group, get_sfi):
386 get_sfi.return_value = {"sfc_encap": True}
387 self._test_new_sf(create_sfc_port_pair_group)
388
389 @mock.patch.object(vimconnector, "get_sfi")
390 @mock.patch.object(Client, "create_sfc_port_pair_group")
391 def test_new_sf_inconsistent_sfc_encap(self, create_sfc_port_pair_group, get_sfi):
392 get_sfi.return_value = {"sfc_encap": "nsh"}
393 self.assertRaises(
394 vimconn.VimConnNotSupportedException,
395 self._test_new_sf,
396 create_sfc_port_pair_group,
397 )
398
399 @mock.patch.object(Client, "create_sfc_port_chain")
400 def test_new_sfp_with_sfc_encap(self, create_sfc_port_chain):
401 self._test_new_sfp(create_sfc_port_chain, True, None)
402
403 @mock.patch.object(Client, "create_sfc_port_chain")
404 def test_new_sfp_without_sfc_encap(self, create_sfc_port_chain):
405 self._test_new_sfp(create_sfc_port_chain, None, None)
406 self._test_new_sfp(create_sfc_port_chain, None, 25)
407
408 @mock.patch.object(Client, "create_sfc_port_chain")
409 def test_new_sfp_default_sfc_encap(self, create_sfc_port_chain):
410 self._test_new_sfp(create_sfc_port_chain, None, None)
411
412 @mock.patch.object(Client, "create_sfc_port_chain")
413 def test_new_sfp_with_sfc_encap_spi(self, create_sfc_port_chain):
414 self._test_new_sfp(create_sfc_port_chain, True, 25)
415
416 @mock.patch.object(Client, "create_sfc_port_chain")
417 def test_new_sfp_default_sfc_encap_spi(self, create_sfc_port_chain):
418 self._test_new_sfp(create_sfc_port_chain, None, 25)
419
420 @mock.patch.object(Client, "list_sfc_flow_classifiers")
421 def test_get_classification_list(self, list_sfc_flow_classifiers):
422 # what OpenStack is assumed to return to the VIM connector
423 list_sfc_flow_classifiers.return_value = {
424 "flow_classifiers": [
425 {
426 "source_port_range_min": 2000,
427 "destination_ip_prefix": "192.168.3.0/24",
428 "protocol": "udp",
429 "description": "",
430 "ethertype": "IPv4",
431 "l7_parameters": {},
432 "source_port_range_max": 2000,
433 "destination_port_range_min": 3000,
434 "source_ip_prefix": "192.168.2.0/24",
435 "logical_destination_port": None,
436 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
437 "destination_port_range_max": None,
438 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
439 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
440 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
441 "name": "fc1",
442 }
443 ]
444 }
445
446 # call the VIM connector
447 filter_dict = {"protocol": "tcp", "ethertype": "IPv4"}
448 result = self.vimconn.get_classification_list(filter_dict.copy())
449
450 # assert that VIM connector called OpenStack with the expected filter
451 list_sfc_flow_classifiers.assert_called_with(**filter_dict)
452 # assert that the VIM connector successfully
453 # translated and returned the OpenStack result
454 self.assertEqual(
455 result,
456 [
457 {
458 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
459 "name": "fc1",
460 "description": "",
461 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
462 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
463 "ctype": "legacy_flow_classifier",
464 "definition": {
465 "source_port_range_min": 2000,
466 "destination_ip_prefix": "192.168.3.0/24",
467 "protocol": "udp",
468 "ethertype": "IPv4",
469 "l7_parameters": {},
470 "source_port_range_max": 2000,
471 "destination_port_range_min": 3000,
472 "source_ip_prefix": "192.168.2.0/24",
473 "logical_destination_port": None,
474 "destination_port_range_max": None,
475 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
476 },
477 }
478 ],
479 )
480
481 def _test_get_sfi_list(self, list_port_pair, correlation, sfc_encap):
482 # what OpenStack is assumed to return to the VIM connector
483 list_port_pair.return_value = {
484 "port_pairs": [
485 {
486 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
487 "description": "",
488 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
489 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
490 "service_function_parameters": {"correlation": correlation},
491 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
492 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
493 "name": "osm_sfi",
494 }
495 ]
496 }
497
498 # call the VIM connector
499 filter_dict = {"name": "osm_sfi", "description": ""}
500 result = self.vimconn.get_sfi_list(filter_dict.copy())
501
502 # assert that VIM connector called OpenStack with the expected filter
503 list_port_pair.assert_called_with(**filter_dict)
504 # assert that the VIM connector successfully
505 # translated and returned the OpenStack result
506 self.assertEqual(
507 result,
508 [
509 {
510 "ingress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
511 "description": "",
512 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
513 "egress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
514 "sfc_encap": sfc_encap,
515 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
516 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
517 "name": "osm_sfi",
518 }
519 ],
520 )
521
522 @mock.patch.object(Client, "list_sfc_port_pairs")
523 def test_get_sfi_list_with_sfc_encap(self, list_sfc_port_pairs):
524 self._test_get_sfi_list(list_sfc_port_pairs, "nsh", True)
525
526 @mock.patch.object(Client, "list_sfc_port_pairs")
527 def test_get_sfi_list_without_sfc_encap(self, list_sfc_port_pairs):
528 self._test_get_sfi_list(list_sfc_port_pairs, None, False)
529
530 @mock.patch.object(Client, "list_sfc_port_pair_groups")
531 def test_get_sf_list(self, list_sfc_port_pair_groups):
532 # what OpenStack is assumed to return to the VIM connector
533 list_sfc_port_pair_groups.return_value = {
534 "port_pair_groups": [
535 {
536 "port_pairs": [
537 "08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2",
538 "0d63799c-82d6-11e7-8deb-a746bb3ae9f5",
539 ],
540 "description": "",
541 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
542 "port_pair_group_parameters": {},
543 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
544 "id": "f4a0bde8-82d5-11e7-90e1-a72b762fa27f",
545 "name": "osm_sf",
546 }
547 ]
548 }
549
550 # call the VIM connector
551 filter_dict = {"name": "osm_sf", "description": ""}
552 result = self.vimconn.get_sf_list(filter_dict.copy())
553
554 # assert that VIM connector called OpenStack with the expected filter
555 list_sfc_port_pair_groups.assert_called_with(**filter_dict)
556 # assert that the VIM connector successfully
557 # translated and returned the OpenStack result
558 self.assertEqual(
559 result,
560 [
561 {
562 "sfis": [
563 "08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2",
564 "0d63799c-82d6-11e7-8deb-a746bb3ae9f5",
565 ],
566 "description": "",
567 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
568 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
569 "id": "f4a0bde8-82d5-11e7-90e1-a72b762fa27f",
570 "name": "osm_sf",
571 }
572 ],
573 )
574
575 def _test_get_sfp_list(self, list_sfc_port_chains, correlation, sfc_encap):
576 # what OpenStack is assumed to return to the VIM connector
577 list_sfc_port_chains.return_value = {
578 "port_chains": [
579 {
580 "port_pair_groups": [
581 "7d8e3bf8-82d6-11e7-a032-8ff028839d25",
582 "7dc9013e-82d6-11e7-a5a6-a3a8d78a5518",
583 ],
584 "flow_classifiers": [
585 "1333c2f4-82d7-11e7-a5df-9327f33d104e",
586 "1387ab44-82d7-11e7-9bb0-476337183905",
587 ],
588 "description": "",
589 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
590 "chain_parameters": {"correlation": correlation},
591 "chain_id": 40,
592 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
593 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
594 "name": "osm_sfp",
595 }
596 ]
597 }
598
599 # call the VIM connector
600 filter_dict = {"name": "osm_sfp", "description": ""}
601 result = self.vimconn.get_sfp_list(filter_dict.copy())
602
603 # assert that VIM connector called OpenStack with the expected filter
604 list_sfc_port_chains.assert_called_with(**filter_dict)
605 # assert that the VIM connector successfully
606 # translated and returned the OpenStack result
607 self.assertEqual(
608 result,
609 [
610 {
611 "service_functions": [
612 "7d8e3bf8-82d6-11e7-a032-8ff028839d25",
613 "7dc9013e-82d6-11e7-a5a6-a3a8d78a5518",
614 ],
615 "classifications": [
616 "1333c2f4-82d7-11e7-a5df-9327f33d104e",
617 "1387ab44-82d7-11e7-9bb0-476337183905",
618 ],
619 "description": "",
620 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
621 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
622 "sfc_encap": sfc_encap,
623 "spi": 40,
624 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
625 "name": "osm_sfp",
626 }
627 ],
628 )
629
630 @mock.patch.object(Client, "list_sfc_port_chains")
631 def test_get_sfp_list_with_sfc_encap(self, list_sfc_port_chains):
632 self._test_get_sfp_list(list_sfc_port_chains, "nsh", True)
633
634 @mock.patch.object(Client, "list_sfc_port_chains")
635 def test_get_sfp_list_without_sfc_encap(self, list_sfc_port_chains):
636 self._test_get_sfp_list(list_sfc_port_chains, None, False)
637
638 @mock.patch.object(Client, "list_sfc_flow_classifiers")
639 def test_get_classification(self, list_sfc_flow_classifiers):
640 # what OpenStack is assumed to return to the VIM connector
641 list_sfc_flow_classifiers.return_value = {
642 "flow_classifiers": [
643 {
644 "source_port_range_min": 2000,
645 "destination_ip_prefix": "192.168.3.0/24",
646 "protocol": "udp",
647 "description": "",
648 "ethertype": "IPv4",
649 "l7_parameters": {},
650 "source_port_range_max": 2000,
651 "destination_port_range_min": 3000,
652 "source_ip_prefix": "192.168.2.0/24",
653 "logical_destination_port": None,
654 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
655 "destination_port_range_max": None,
656 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
657 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
658 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
659 "name": "fc1",
660 }
661 ]
662 }
663
664 # call the VIM connector
665 result = self.vimconn.get_classification("22198366-d4e8-4d6b-b4d2-637d5d6cbb7d")
666
667 # assert that VIM connector called OpenStack with the expected filter
668 list_sfc_flow_classifiers.assert_called_with(
669 id="22198366-d4e8-4d6b-b4d2-637d5d6cbb7d"
670 )
671 # assert that VIM connector successfully returned the OpenStack result
672 self.assertEqual(
673 result,
674 {
675 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
676 "name": "fc1",
677 "description": "",
678 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
679 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
680 "ctype": "legacy_flow_classifier",
681 "definition": {
682 "source_port_range_min": 2000,
683 "destination_ip_prefix": "192.168.3.0/24",
684 "protocol": "udp",
685 "ethertype": "IPv4",
686 "l7_parameters": {},
687 "source_port_range_max": 2000,
688 "destination_port_range_min": 3000,
689 "source_ip_prefix": "192.168.2.0/24",
690 "logical_destination_port": None,
691 "destination_port_range_max": None,
692 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
693 },
694 },
695 )
696
697 @mock.patch.object(Client, "list_sfc_flow_classifiers")
698 def test_get_classification_many_results(self, list_sfc_flow_classifiers):
699 # what OpenStack is assumed to return to the VIM connector
700 list_sfc_flow_classifiers.return_value = {
701 "flow_classifiers": [
702 {
703 "source_port_range_min": 2000,
704 "destination_ip_prefix": "192.168.3.0/24",
705 "protocol": "udp",
706 "description": "",
707 "ethertype": "IPv4",
708 "l7_parameters": {},
709 "source_port_range_max": 2000,
710 "destination_port_range_min": 3000,
711 "source_ip_prefix": "192.168.2.0/24",
712 "logical_destination_port": None,
713 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
714 "destination_port_range_max": None,
715 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
716 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
717 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
718 "name": "fc1",
719 },
720 {
721 "source_port_range_min": 1000,
722 "destination_ip_prefix": "192.168.3.0/24",
723 "protocol": "udp",
724 "description": "",
725 "ethertype": "IPv4",
726 "l7_parameters": {},
727 "source_port_range_max": 1000,
728 "destination_port_range_min": 3000,
729 "source_ip_prefix": "192.168.2.0/24",
730 "logical_destination_port": None,
731 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
732 "destination_port_range_max": None,
733 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
734 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
735 "id": "3196bafc-82dd-11e7-a205-9bf6c14b0721",
736 "name": "fc2",
737 },
738 ]
739 }
740
741 # call the VIM connector
742 self.assertRaises(
743 vimconn.VimConnConflictException,
744 self.vimconn.get_classification,
745 "3196bafc-82dd-11e7-a205-9bf6c14b0721",
746 )
747
748 # assert the VIM connector called OpenStack with the expected filter
749 list_sfc_flow_classifiers.assert_called_with(
750 id="3196bafc-82dd-11e7-a205-9bf6c14b0721"
751 )
752
753 @mock.patch.object(Client, "list_sfc_flow_classifiers")
754 def test_get_classification_no_results(self, list_sfc_flow_classifiers):
755 # what OpenStack is assumed to return to the VIM connector
756 list_sfc_flow_classifiers.return_value = {"flow_classifiers": []}
757
758 # call the VIM connector
759 self.assertRaises(
760 vimconn.VimConnNotFoundException,
761 self.vimconn.get_classification,
762 "3196bafc-82dd-11e7-a205-9bf6c14b0721",
763 )
764
765 # assert the VIM connector called OpenStack with the expected filter
766 list_sfc_flow_classifiers.assert_called_with(
767 id="3196bafc-82dd-11e7-a205-9bf6c14b0721"
768 )
769
770 @mock.patch.object(Client, "list_sfc_port_pairs")
771 def test_get_sfi(self, list_sfc_port_pairs):
772 # what OpenStack is assumed to return to the VIM connector
773 list_sfc_port_pairs.return_value = {
774 "port_pairs": [
775 {
776 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
777 "description": "",
778 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
779 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
780 "service_function_parameters": {"correlation": "nsh"},
781 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
782 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
783 "name": "osm_sfi1",
784 },
785 ]
786 }
787
788 # call the VIM connector
789 result = self.vimconn.get_sfi("c121ebdd-7f2d-4213-b933-3325298a6966")
790
791 # assert the VIM connector called OpenStack with the expected filter
792 list_sfc_port_pairs.assert_called_with(
793 id="c121ebdd-7f2d-4213-b933-3325298a6966"
794 )
795 # assert the VIM connector successfully returned the OpenStack result
796 self.assertEqual(
797 result,
798 {
799 "ingress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
800 "egress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
801 "sfc_encap": True,
802 "description": "",
803 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
804 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
805 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
806 "name": "osm_sfi1",
807 },
808 )
809
810 @mock.patch.object(Client, "list_sfc_port_pairs")
811 def test_get_sfi_many_results(self, list_sfc_port_pairs):
812 # what OpenStack is assumed to return to the VIM connector
813 list_sfc_port_pairs.return_value = {
814 "port_pairs": [
815 {
816 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
817 "description": "",
818 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
819 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
820 "service_function_parameters": {"correlation": "nsh"},
821 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
822 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
823 "name": "osm_sfi1",
824 },
825 {
826 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
827 "description": "",
828 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
829 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
830 "service_function_parameters": {"correlation": "nsh"},
831 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
832 "id": "c0436d92-82db-11e7-8f9c-5fa535f1261f",
833 "name": "osm_sfi2",
834 },
835 ]
836 }
837
838 # call the VIM connector
839 self.assertRaises(
840 vimconn.VimConnConflictException,
841 self.vimconn.get_sfi,
842 "c0436d92-82db-11e7-8f9c-5fa535f1261f",
843 )
844
845 # assert that VIM connector called OpenStack with the expected filter
846 list_sfc_port_pairs.assert_called_with(
847 id="c0436d92-82db-11e7-8f9c-5fa535f1261f"
848 )
849
850 @mock.patch.object(Client, "list_sfc_port_pairs")
851 def test_get_sfi_no_results(self, list_sfc_port_pairs):
852 # what OpenStack is assumed to return to the VIM connector
853 list_sfc_port_pairs.return_value = {"port_pairs": []}
854
855 # call the VIM connector
856 self.assertRaises(
857 vimconn.VimConnNotFoundException,
858 self.vimconn.get_sfi,
859 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
860 )
861
862 # assert that VIM connector called OpenStack with the expected filter
863 list_sfc_port_pairs.assert_called_with(
864 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
865 )
866
867 @mock.patch.object(Client, "list_sfc_port_pair_groups")
868 def test_get_sf(self, list_sfc_port_pair_groups):
869 # what OpenStack is assumed to return to the VIM connector
870 list_sfc_port_pair_groups.return_value = {
871 "port_pair_groups": [
872 {
873 "port_pairs": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
874 "description": "",
875 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
876 "port_pair_group_parameters": {},
877 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
878 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
879 "name": "osm_sf1",
880 }
881 ]
882 }
883
884 # call the VIM connector
885 result = self.vimconn.get_sf("b22892fc-82d9-11e7-ae85-0fea6a3b3757")
886
887 # assert that VIM connector called OpenStack with the expected filter
888 list_sfc_port_pair_groups.assert_called_with(
889 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
890 )
891 # assert that VIM connector successfully returned the OpenStack result
892 self.assertEqual(
893 result,
894 {
895 "description": "",
896 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
897 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
898 "sfis": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
899 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
900 "name": "osm_sf1",
901 },
902 )
903
904 @mock.patch.object(Client, "list_sfc_port_pair_groups")
905 def test_get_sf_many_results(self, list_sfc_port_pair_groups):
906 # what OpenStack is assumed to return to the VIM connector
907 list_sfc_port_pair_groups.return_value = {
908 "port_pair_groups": [
909 {
910 "port_pairs": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
911 "description": "",
912 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
913 "port_pair_group_parameters": {},
914 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
915 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
916 "name": "osm_sf1",
917 },
918 {
919 "port_pairs": ["0d63799c-82d6-11e7-8deb-a746bb3ae9f5"],
920 "description": "",
921 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
922 "port_pair_group_parameters": {},
923 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
924 "id": "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
925 "name": "osm_sf2",
926 },
927 ]
928 }
929
930 # call the VIM connector
931 self.assertRaises(
932 vimconn.VimConnConflictException,
933 self.vimconn.get_sf,
934 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
935 )
936
937 # assert that VIM connector called OpenStack with the expected filter
938 list_sfc_port_pair_groups.assert_called_with(
939 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
940 )
941
942 @mock.patch.object(Client, "list_sfc_port_pair_groups")
943 def test_get_sf_no_results(self, list_sfc_port_pair_groups):
944 # what OpenStack is assumed to return to the VIM connector
945 list_sfc_port_pair_groups.return_value = {"port_pair_groups": []}
946
947 # call the VIM connector
948 self.assertRaises(
949 vimconn.VimConnNotFoundException,
950 self.vimconn.get_sf,
951 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
952 )
953
954 # assert that VIM connector called OpenStack with the expected filter
955 list_sfc_port_pair_groups.assert_called_with(
956 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
957 )
958
959 @mock.patch.object(Client, "list_sfc_port_chains")
960 def test_get_sfp(self, list_sfc_port_chains):
961 # what OpenStack is assumed to return to the VIM connector
962 list_sfc_port_chains.return_value = {
963 "port_chains": [
964 {
965 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
966 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
967 "description": "",
968 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
969 "chain_parameters": {"correlation": "nsh"},
970 "chain_id": 40,
971 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
972 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
973 "name": "osm_sfp1",
974 }
975 ]
976 }
977
978 # call the VIM connector
979 result = self.vimconn.get_sfp("821bc9be-82d7-11e7-8ce3-23a08a27ab47")
980
981 # assert that VIM connector called OpenStack with the expected filter
982 list_sfc_port_chains.assert_called_with(
983 id="821bc9be-82d7-11e7-8ce3-23a08a27ab47"
984 )
985 # assert that VIM connector successfully returned the OpenStack result
986 self.assertEqual(
987 result,
988 {
989 "service_functions": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
990 "classifications": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
991 "description": "",
992 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
993 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
994 "sfc_encap": True,
995 "spi": 40,
996 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
997 "name": "osm_sfp1",
998 },
999 )
1000
1001 @mock.patch.object(Client, "list_sfc_port_chains")
1002 def test_get_sfp_many_results(self, list_sfc_port_chains):
1003 # what OpenStack is assumed to return to the VIM connector
1004 list_sfc_port_chains.return_value = {
1005 "port_chains": [
1006 {
1007 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
1008 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
1009 "description": "",
1010 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1011 "chain_parameters": {"correlation": "nsh"},
1012 "chain_id": 40,
1013 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1014 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
1015 "name": "osm_sfp1",
1016 },
1017 {
1018 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
1019 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
1020 "description": "",
1021 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1022 "chain_parameters": {"correlation": "nsh"},
1023 "chain_id": 50,
1024 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1025 "id": "5d002f38-82de-11e7-a770-f303f11ce66a",
1026 "name": "osm_sfp2",
1027 },
1028 ]
1029 }
1030
1031 # call the VIM connector
1032 self.assertRaises(
1033 vimconn.VimConnConflictException,
1034 self.vimconn.get_sfp,
1035 "5d002f38-82de-11e7-a770-f303f11ce66a",
1036 )
1037
1038 # assert that VIM connector called OpenStack with the expected filter
1039 list_sfc_port_chains.assert_called_with(
1040 id="5d002f38-82de-11e7-a770-f303f11ce66a"
1041 )
1042
1043 @mock.patch.object(Client, "list_sfc_port_chains")
1044 def test_get_sfp_no_results(self, list_sfc_port_chains):
1045 # what OpenStack is assumed to return to the VIM connector
1046 list_sfc_port_chains.return_value = {"port_chains": []}
1047
1048 # call the VIM connector
1049 self.assertRaises(
1050 vimconn.VimConnNotFoundException,
1051 self.vimconn.get_sfp,
1052 "5d002f38-82de-11e7-a770-f303f11ce66a",
1053 )
1054
1055 # assert that VIM connector called OpenStack with the expected filter
1056 list_sfc_port_chains.assert_called_with(
1057 id="5d002f38-82de-11e7-a770-f303f11ce66a"
1058 )
1059
1060 @mock.patch.object(Client, "delete_sfc_flow_classifier")
1061 def test_delete_classification(self, delete_sfc_flow_classifier):
1062 result = self.vimconn.delete_classification(
1063 "638f957c-82df-11e7-b7c8-132706021464"
1064 )
1065 delete_sfc_flow_classifier.assert_called_with(
1066 "638f957c-82df-11e7-b7c8-132706021464"
1067 )
1068 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1069
1070 @mock.patch.object(Client, "delete_sfc_port_pair")
1071 def test_delete_sfi(self, delete_sfc_port_pair):
1072 result = self.vimconn.delete_sfi("638f957c-82df-11e7-b7c8-132706021464")
1073 delete_sfc_port_pair.assert_called_with("638f957c-82df-11e7-b7c8-132706021464")
1074 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1075
1076 @mock.patch.object(Client, "delete_sfc_port_pair_group")
1077 def test_delete_sf(self, delete_sfc_port_pair_group):
1078 result = self.vimconn.delete_sf("638f957c-82df-11e7-b7c8-132706021464")
1079 delete_sfc_port_pair_group.assert_called_with(
1080 "638f957c-82df-11e7-b7c8-132706021464"
1081 )
1082 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1083
1084 @mock.patch.object(Client, "delete_sfc_port_chain")
1085 def test_delete_sfp(self, delete_sfc_port_chain):
1086 result = self.vimconn.delete_sfp("638f957c-82df-11e7-b7c8-132706021464")
1087 delete_sfc_port_chain.assert_called_with("638f957c-82df-11e7-b7c8-132706021464")
1088 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1089
1090
1091 class Status:
1092 def __init__(self, s):
1093 self.status = s
1094
1095 def __str__(self):
1096 return self.status
1097
1098
1099 class CopyingMock(MagicMock):
1100 def __call__(self, *args, **kwargs):
1101 args = deepcopy(args)
1102 kwargs = deepcopy(kwargs)
1103 return super(CopyingMock, self).__call__(*args, **kwargs)
1104
1105
1106 class TestNewVmInstance(unittest.TestCase):
1107 @patch("logging.getLogger", autospec=True)
1108 def setUp(self, mock_logger):
1109 # Instantiate dummy VIM connector so we can test it
1110 # It throws exception because of dummy parameters,
1111 # We are disabling the logging of exception not to print them to console.
1112 mock_logger = logging.getLogger()
1113 mock_logger.disabled = True
1114 self.vimconn = vimconnector(
1115 "123",
1116 "openstackvim",
1117 "456",
1118 "789",
1119 "http://dummy.url",
1120 None,
1121 "user",
1122 "pass",
1123 )
1124 self.vimconn.neutron = CopyingMock()
1125 self.vimconn.nova = CopyingMock()
1126 self.vimconn.cinder = CopyingMock()
1127 self.server = MagicMock(object, autospec=True)
1128 self.server.tenant_id = "408b73-r9cc-5a6a-a270-82cc4811bd4a"
1129 self.server.id = "908b73-e9cc-5a6a-t270-82cc4811bd4a"
1130 self.vimconn.config["security_groups"] = "default"
1131 self.vimconn.config["keypair"] = "my_keypair"
1132 self.vimconn.security_groups_id = "12345"
1133 self.vimconn.nova.api_version.get_string.return_value = "2.32"
1134
1135 @patch.object(vimconnector, "_get_ids_from_name")
1136 def test_prepare_port_dict_security_security_groups_exists_in_config(
1137 self, mock_get_ids
1138 ):
1139 """In VIM config security_groups exists, net port_security is True
1140 no_port_security_extension does not exist.
1141 """
1142 self.vimconn.config = {"security_groups": "example_security_group"}
1143 net = {"port_security": True}
1144 port_dict = {}
1145 result_dict = {"security_groups": "12345"}
1146
1147 self.vimconn._prepare_port_dict_security_groups(net, port_dict)
1148 self.assertDictEqual(result_dict, port_dict)
1149 mock_get_ids.assert_not_called()
1150
1151 @patch.object(vimconnector, "_get_ids_from_name")
1152 def test_prepare_port_dict_security_security_groups_exists_in_config_no_security_groups_id(
1153 self, mock_get_ids
1154 ):
1155 """In VIM config Security_groups exists, net port_security is True, vim security_groups_id does not exist,
1156 no_port_security_extension does not exist.
1157 """
1158 self.vimconn.config = {"security_groups": "example_security_group"}
1159 self.vimconn.security_groups_id = None
1160 net = {"port_security": True}
1161 port_dict = {}
1162 result_dict = {"security_groups": None}
1163
1164 self.vimconn._prepare_port_dict_security_groups(net, port_dict)
1165 self.assertDictEqual(result_dict, port_dict)
1166 mock_get_ids.assert_called()
1167
1168 @patch.object(vimconnector, "_get_ids_from_name")
1169 def test_prepare_port_dict_security_security_groups_exists_security_extension_true_in_config(
1170 self, mock_get_ids
1171 ):
1172 """In VIM config security_groups exists, net port_security is True, in VIM security_groups_id exists,
1173 no_port_security_extension set to True.
1174 """
1175 self.vimconn.config = {
1176 "security_groups": "example_security_group",
1177 "no_port_security_extension": True,
1178 }
1179 net = {"port_security": True}
1180 port_dict = {}
1181 result_dict = {}
1182
1183 self.vimconn._prepare_port_dict_security_groups(net, port_dict)
1184 self.assertDictEqual(result_dict, port_dict)
1185 mock_get_ids.assert_not_called()
1186
1187 @patch.object(vimconnector, "_get_ids_from_name")
1188 def test_prepare_port_dict_security_no_security_groups_in_config(
1189 self, mock_get_ids
1190 ):
1191 """In VIM config security_group does not exist, net port_security True, in VIM security_groups_id exists,
1192 no_port_security_extension does not exist."""
1193 self.vimconn.config = {}
1194 net = {"port_security": True}
1195 port_dict = {}
1196 result_dict = {}
1197
1198 self.vimconn._prepare_port_dict_security_groups(net, port_dict)
1199 self.assertDictEqual(result_dict, port_dict)
1200 mock_get_ids.assert_not_called()
1201
1202 @patch.object(vimconnector, "_get_ids_from_name")
1203 def test_prepare_port_dict_security_no_security_groups_security_extension_true_in_config(
1204 self, mock_get_ids
1205 ):
1206 """Security_group does not exist, net port_security is True, in VIM security_groups_id exists,
1207 no_port_security_extension set to True."""
1208 self.vimconn.config = {"no_port_security_extension": True}
1209 net = {"port_security": True}
1210 port_dict = {}
1211 result_dict = {}
1212
1213 self.vimconn._prepare_port_dict_security_groups(net, port_dict)
1214 self.assertDictEqual(result_dict, port_dict)
1215 mock_get_ids.assert_not_called()
1216
1217 @patch.object(vimconnector, "_get_ids_from_name")
1218 def test_prepare_port_dict_security_security_groups_exists_net_port_security_false(
1219 self, mock_get_ids
1220 ):
1221 """In VIM config security_group exists, net port_security False, security_groups_id exists,
1222 no_port_security_extension does not exist."""
1223 self.vimconn.config = {"security_groups": "example_security_group"}
1224 net = {"port_security": False}
1225 port_dict = {}
1226 result_dict = {}
1227
1228 self.vimconn._prepare_port_dict_security_groups(net, port_dict)
1229 self.assertDictEqual(result_dict, port_dict)
1230 mock_get_ids.assert_not_called()
1231
1232 @patch.object(vimconnector, "_get_ids_from_name")
1233 def test_prepare_port_dict_security_net_port_security_false_port_security_extension_true(
1234 self, mock_get_ids
1235 ):
1236 """In VIM config security_group exists, net port_security False, security_groups_id exists,
1237 no_port_security_extension set to True."""
1238 self.vimconn.config = {
1239 "security_groups": "example_security_group",
1240 "no_port_security_extension": True,
1241 }
1242 net = {"port_security": False}
1243 port_dict = {}
1244 result_dict = {}
1245
1246 self.vimconn._prepare_port_dict_security_groups(net, port_dict)
1247 self.assertDictEqual(result_dict, port_dict)
1248 mock_get_ids.assert_not_called()
1249
1250 def test_prepare_port_dict_binding_net_type_virtual(self):
1251 """net type is virtual."""
1252 net = {"type": "virtual"}
1253 port_dict = {}
1254 result_dict = {}
1255 self.vimconn._prepare_port_dict_binding(net, port_dict)
1256 self.assertDictEqual(result_dict, port_dict)
1257
1258 def test_prepare_port_dict_binding_net_type_vf(self):
1259 """net type is VF, vim_type is not VIO."""
1260 net = {"type": "VF"}
1261 self.vimconn.vim_type = None
1262 port_dict = {}
1263 result_dict = {"binding:vnic_type": "direct"}
1264 self.vimconn._prepare_port_dict_binding(net, port_dict)
1265 self.assertDictEqual(port_dict, result_dict)
1266
1267 def test_prepare_port_dict_binding_net_type_sriov_vim_type_vio(self):
1268 """net type is SR-IOV, vim_type is VIO."""
1269 net = {"type": "SR-IOV"}
1270 self.vimconn.vim_type = "VIO"
1271 port_dict = {}
1272 result_dict = {
1273 "binding:vnic_type": "direct",
1274 "port_security_enabled": False,
1275 "provider_security_groups": [],
1276 "security_groups": [],
1277 }
1278 self.vimconn._prepare_port_dict_binding(net, port_dict)
1279 self.assertDictEqual(port_dict, result_dict)
1280
1281 def test_prepare_port_dict_binding_net_type_passthrough(self):
1282 """net type is pci-passthrough."""
1283 net = {"type": "PCI-PASSTHROUGH"}
1284 port_dict = {}
1285 result_dict = {
1286 "binding:vnic_type": "direct-physical",
1287 }
1288 self.vimconn._prepare_port_dict_binding(net, port_dict)
1289 self.assertDictEqual(port_dict, result_dict)
1290
1291 def test_prepare_port_dict_binding_no_net_type(self):
1292 """net type is missing."""
1293 net = {}
1294 port_dict = {}
1295 with self.assertRaises(VimConnException) as err:
1296 self.vimconn._prepare_port_dict_binding(net, port_dict)
1297 self.assertEqual(str(err.exception), "Type is missing in the network details.")
1298
1299 def test_set_fixed_ip(self):
1300 """new_port has fixed ip."""
1301 net = {}
1302 new_port = {
1303 "port": {
1304 "fixed_ips": [{"ip_address": "10.1.2.3"}, {"ip_address": "20.1.2.3"}]
1305 }
1306 }
1307 result = {"ip": "10.1.2.3"}
1308 self.vimconn._set_fixed_ip(new_port, net)
1309 self.assertDictEqual(net, result)
1310
1311 def test_set_fixed_ip_no_fixed_ip(self):
1312 """new_port does not have fixed ip."""
1313 net = {}
1314 new_port = {"port": {}}
1315 result = {"ip": None}
1316 self.vimconn._set_fixed_ip(new_port, net)
1317 self.assertDictEqual(net, result)
1318
1319 def test_set_fixed_ip_raise_exception(self):
1320 """new_port does not have port details."""
1321 net = {}
1322 new_port = {}
1323 with self.assertRaises(Exception) as err:
1324 self.vimconn._set_fixed_ip(new_port, net)
1325 self.assertEqual(type(err.exception), KeyError)
1326
1327 def test_prepare_port_dict_mac_ip_addr(self):
1328 """mac address and ip address exist."""
1329 net = {
1330 "mac_address": mac_address,
1331 "ip_address": "10.0.1.5",
1332 }
1333 port_dict = {}
1334 result_dict = {
1335 "mac_address": mac_address,
1336 "fixed_ips": [{"ip_address": "10.0.1.5"}],
1337 }
1338 self.vimconn._prepare_port_dict_mac_ip_addr(net, port_dict)
1339 self.assertDictEqual(port_dict, result_dict)
1340
1341 def test_prepare_port_dict_mac_ip_addr_no_mac_and_ip(self):
1342 """mac address and ip address does not exist."""
1343 net = {}
1344 port_dict = {}
1345 result_dict = {}
1346 self.vimconn._prepare_port_dict_mac_ip_addr(net, port_dict)
1347 self.assertDictEqual(port_dict, result_dict)
1348
1349 def test_create_new_port(self):
1350 """new port has id and mac address."""
1351 new_port = {
1352 "port": {
1353 "id": port_id,
1354 "mac_address": mac_address,
1355 },
1356 }
1357 self.vimconn.neutron.create_port.return_value = new_port
1358 net, port_dict, created_items = {}, {}, {}
1359 expected_result = new_port
1360 expected_net = {
1361 "mac_adress": mac_address,
1362 "vim_id": port_id,
1363 }
1364 expected_created_items = {f"port:{port_id}": True}
1365 result = self.vimconn._create_new_port(port_dict, created_items, net)
1366 self.assertDictEqual(result, expected_result)
1367 self.assertEqual(net, expected_net)
1368 self.assertEqual(created_items, expected_created_items)
1369 self.vimconn.neutron.create_port.assert_called_once_with({"port": port_dict})
1370
1371 def test_create_new_port_without_mac_or_id(self):
1372 """new port does not have mac address or ID."""
1373 new_port = {}
1374 self.vimconn.neutron.create_port.return_value = new_port
1375 net, port_dict, created_items = {}, {}, {}
1376 with self.assertRaises(KeyError):
1377 self.vimconn._create_new_port(port_dict, created_items, net)
1378 self.vimconn.neutron.create_port.assert_called_once_with({"port": port_dict})
1379
1380 def test_create_new_port_neutron_create_port_raises_exception(self):
1381 """Neutron create port raises exception."""
1382 self.vimconn.neutron.create_port.side_effect = VimConnException(
1383 "New port is not created."
1384 )
1385 net, port_dict, created_items = {}, {}, {}
1386 with self.assertRaises(VimConnException):
1387 self.vimconn._create_new_port(port_dict, created_items, net)
1388 self.vimconn.neutron.create_port.assert_called_once_with({"port": port_dict})
1389
1390 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1391 @patch.object(vimconnector, "_prepare_port_dict_binding")
1392 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1393 @patch.object(vimconnector, "_create_new_port")
1394 @patch.object(vimconnector, "_set_fixed_ip")
1395 def test_create_port(
1396 self,
1397 mock_set_fixed_ip,
1398 mock_create_new_port,
1399 mock_prepare_port_dict_mac_ip_addr,
1400 mock_prepare_port_dict_binding,
1401 mock_prepare_port_dict_security_groups,
1402 ):
1403 """Net has name, type, net-id."""
1404
1405 net = {
1406 "net_id": net_id,
1407 "name": "management",
1408 "type": "virtual",
1409 }
1410 created_items = {}
1411 new_port = {
1412 "port": {
1413 "id": net_id,
1414 "mac_address": mac_address,
1415 "name": "management",
1416 "fixed_ips": [{"ip_address": ip_addr1}],
1417 },
1418 }
1419 mock_create_new_port.return_value = new_port
1420 expected_port = {
1421 "port-id": net_id,
1422 "tag": "management",
1423 }
1424 port_dict = {
1425 "network_id": net_id,
1426 "name": "management",
1427 "admin_state_up": True,
1428 }
1429
1430 new_port_result, port_result = self.vimconn._create_port(
1431 net, name, created_items
1432 )
1433
1434 self.assertDictEqual(new_port_result, new_port)
1435 self.assertDictEqual(port_result, expected_port)
1436
1437 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1438 mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict)
1439 mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict)
1440 mock_create_new_port.assert_called_once_with(port_dict, created_items, net)
1441 mock_set_fixed_ip.assert_called_once_with(new_port, net)
1442
1443 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1444 @patch.object(vimconnector, "_prepare_port_dict_binding")
1445 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1446 @patch.object(vimconnector, "_create_new_port")
1447 @patch.object(vimconnector, "_set_fixed_ip")
1448 def test_create_port_no_port_name(
1449 self,
1450 mock_set_fixed_ip,
1451 mock_create_new_port,
1452 mock_prepare_port_dict_mac_ip_addr,
1453 mock_prepare_port_dict_binding,
1454 mock_prepare_port_dict_security_groups,
1455 ):
1456 """Net has no name."""
1457 net = {
1458 "net_id": net_id,
1459 "type": "virtual",
1460 }
1461 created_items = {}
1462 new_port = {
1463 "port": {
1464 "id": net_id,
1465 "mac_address": mac_address,
1466 "name": name,
1467 "fixed_ips": [{"ip_address": ip_addr1}],
1468 },
1469 }
1470 mock_create_new_port.return_value = new_port
1471 expected_port = {
1472 "port-id": net_id,
1473 "tag": name,
1474 }
1475 port_dict = {
1476 "network_id": net_id,
1477 "admin_state_up": True,
1478 "name": name,
1479 }
1480
1481 new_port_result, port_result = self.vimconn._create_port(
1482 net, name, created_items
1483 )
1484
1485 self.assertDictEqual(new_port_result, new_port)
1486 self.assertDictEqual(port_result, expected_port)
1487
1488 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1489 mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict)
1490 mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict)
1491 mock_create_new_port.assert_called_once_with(port_dict, created_items, net)
1492 mock_set_fixed_ip.assert_called_once_with(new_port, net)
1493
1494 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1495 @patch.object(vimconnector, "_prepare_port_dict_binding")
1496 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1497 @patch.object(vimconnector, "_create_new_port")
1498 @patch.object(vimconnector, "_set_fixed_ip")
1499 def test_create_port_nova_api_version_smaller_than_232(
1500 self,
1501 mock_set_fixed_ip,
1502 mock_create_new_port,
1503 mock_prepare_port_dict_mac_ip_addr,
1504 mock_prepare_port_dict_binding,
1505 mock_prepare_port_dict_security_groups,
1506 ):
1507 """Nova api version is smaller than 2.32."""
1508 self.vimconn.nova.api_version.get_string.return_value = "2.30"
1509 net = {
1510 "net_id": net_id,
1511 "type": "virtual",
1512 }
1513 created_items = {}
1514 new_port = {
1515 "port": {
1516 "id": net_id,
1517 "mac_address": mac_address,
1518 "name": name,
1519 "fixed_ips": [{"ip_address": ip_addr1}],
1520 },
1521 }
1522 mock_create_new_port.return_value = new_port
1523 expected_port = {
1524 "port-id": net_id,
1525 }
1526 port_dict = {
1527 "network_id": net_id,
1528 "admin_state_up": True,
1529 "name": name,
1530 }
1531
1532 new_port_result, port_result = self.vimconn._create_port(
1533 net, name, created_items
1534 )
1535
1536 self.assertDictEqual(new_port_result, new_port)
1537 self.assertDictEqual(port_result, expected_port)
1538
1539 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1540 mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict)
1541 mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict)
1542 mock_create_new_port.assert_called_once_with(port_dict, created_items, net)
1543 mock_set_fixed_ip.assert_called_once_with(new_port, net)
1544
1545 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1546 @patch.object(vimconnector, "_prepare_port_dict_binding")
1547 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1548 @patch.object(vimconnector, "_create_new_port")
1549 @patch.object(vimconnector, "_set_fixed_ip")
1550 def test_create_port_create_new_port_raise_exception(
1551 self,
1552 mock_set_fixed_ip,
1553 mock_create_new_port,
1554 mock_prepare_port_dict_mac_ip_addr,
1555 mock_prepare_port_dict_binding,
1556 mock_prepare_port_dict_security_groups,
1557 ):
1558 """_create_new_port method raises exception."""
1559 net = {
1560 "net_id": net_id,
1561 "type": "virtual",
1562 }
1563 created_items = {}
1564 mock_create_new_port.side_effect = Exception
1565 port_dict = {
1566 "network_id": net_id,
1567 "admin_state_up": True,
1568 "name": name,
1569 }
1570
1571 with self.assertRaises(Exception):
1572 self.vimconn._create_port(net, name, created_items)
1573
1574 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1575 mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict)
1576 mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict)
1577 mock_create_new_port.assert_called_once_with(port_dict, created_items, net)
1578 mock_set_fixed_ip.assert_not_called()
1579
1580 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1581 @patch.object(vimconnector, "_prepare_port_dict_binding")
1582 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1583 @patch.object(vimconnector, "_create_new_port")
1584 @patch.object(vimconnector, "_set_fixed_ip")
1585 def test_create_port_create_sec_groups_raises_exception(
1586 self,
1587 mock_set_fixed_ip,
1588 mock_create_new_port,
1589 mock_prepare_port_dict_mac_ip_addr,
1590 mock_prepare_port_dict_binding,
1591 mock_prepare_port_dict_security_groups,
1592 ):
1593 """_prepare_port_dict_security_groups method raises exception."""
1594 net = {
1595 "net_id": net_id,
1596 "type": "virtual",
1597 }
1598 created_items = {}
1599 mock_prepare_port_dict_security_groups.side_effect = Exception
1600 port_dict = {
1601 "network_id": net_id,
1602 "admin_state_up": True,
1603 "name": name,
1604 }
1605
1606 with self.assertRaises(Exception):
1607 self.vimconn._create_port(net, name, created_items)
1608
1609 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1610
1611 mock_prepare_port_dict_binding.assert_not_called()
1612 mock_prepare_port_dict_mac_ip_addr.assert_not_called()
1613 mock_create_new_port.assert_not_called()
1614 mock_set_fixed_ip.assert_not_called()
1615
1616 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1617 @patch.object(vimconnector, "_prepare_port_dict_binding")
1618 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1619 @patch.object(vimconnector, "_create_new_port")
1620 @patch.object(vimconnector, "_set_fixed_ip")
1621 def test_create_port_create_port_dict_binding_raise_exception(
1622 self,
1623 mock_set_fixed_ip,
1624 mock_create_new_port,
1625 mock_prepare_port_dict_mac_ip_addr,
1626 mock_prepare_port_dict_binding,
1627 mock_prepare_port_dict_security_groups,
1628 ):
1629 """_prepare_port_dict_binding method raises exception."""
1630
1631 net = {
1632 "net_id": net_id,
1633 "type": "virtual",
1634 }
1635 created_items = {}
1636 mock_prepare_port_dict_binding.side_effect = Exception
1637 port_dict = {
1638 "network_id": net_id,
1639 "admin_state_up": True,
1640 "name": name,
1641 }
1642
1643 with self.assertRaises(Exception):
1644 self.vimconn._create_port(net, name, created_items)
1645
1646 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1647
1648 mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict)
1649
1650 mock_prepare_port_dict_mac_ip_addr.assert_not_called()
1651 mock_create_new_port.assert_not_called()
1652 mock_set_fixed_ip.assert_not_called()
1653
1654 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1655 @patch.object(vimconnector, "_prepare_port_dict_binding")
1656 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1657 @patch.object(vimconnector, "_create_new_port")
1658 @patch.object(vimconnector, "_set_fixed_ip")
1659 def test_create_port_create_port_mac_ip_addr_raise_exception(
1660 self,
1661 mock_set_fixed_ip,
1662 mock_create_new_port,
1663 mock_prepare_port_dict_mac_ip_addr,
1664 mock_prepare_port_dict_binding,
1665 mock_prepare_port_dict_security_groups,
1666 ):
1667 """prepare_port_dict_mac_ip_addr method raises exception."""
1668 net = {
1669 "net_id": net_id,
1670 "type": "virtual",
1671 }
1672 created_items = {}
1673 mock_prepare_port_dict_mac_ip_addr.side_effect = Exception
1674 port_dict = {
1675 "network_id": net_id,
1676 "admin_state_up": True,
1677 "name": name,
1678 }
1679
1680 with self.assertRaises(Exception):
1681 self.vimconn._create_port(net, name, created_items)
1682
1683 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1684 mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict)
1685 mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict)
1686
1687 mock_create_new_port.assert_not_called()
1688 mock_set_fixed_ip.assert_not_called()
1689
1690 @patch.object(vimconnector, "_prepare_port_dict_security_groups")
1691 @patch.object(vimconnector, "_prepare_port_dict_binding")
1692 @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr")
1693 @patch.object(vimconnector, "_create_new_port")
1694 @patch.object(vimconnector, "_set_fixed_ip")
1695 def test_create_port_create_port_set_fixed_ip_raise_exception(
1696 self,
1697 mock_set_fixed_ip,
1698 mock_create_new_port,
1699 mock_prepare_port_dict_mac_ip_addr,
1700 mock_prepare_port_dict_binding,
1701 mock_prepare_port_dict_security_groups,
1702 ):
1703 """_set_fixed_ip method raises exception."""
1704 net = {
1705 "net_id": net_id,
1706 "type": "virtual",
1707 }
1708 created_items = {}
1709 mock_set_fixed_ip.side_effect = VimConnException(
1710 "Port detail is missing in new_port."
1711 )
1712 port_dict = {
1713 "network_id": net_id,
1714 "admin_state_up": True,
1715 "name": name,
1716 }
1717 new_port = {
1718 "port": {
1719 "id": net_id,
1720 "mac_address": mac_address,
1721 "name": name,
1722 "fixed_ips": [{"ip_address": ip_addr1}],
1723 },
1724 }
1725 mock_create_new_port.return_value = new_port
1726
1727 with self.assertRaises(VimConnException):
1728 self.vimconn._create_port(net, name, created_items)
1729
1730 mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict)
1731 mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict)
1732 mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict)
1733 mock_create_new_port.assert_called_once_with(port_dict, created_items, net)
1734 mock_set_fixed_ip.assert_called_once_with(new_port, net)
1735
1736 @patch.object(vimconnector, "_reload_connection")
1737 @patch.object(vimconnector, "_create_port")
1738 def test_prepare_network_for_vm_instance_no_net_id(
1739 self, mock_create_port, mock_reload_connection
1740 ):
1741 """Nets do not have net_id"""
1742 mock_reload_connection.side_effect = None
1743 created_items = {}
1744 net_list = [
1745 {
1746 "use": "mgmt",
1747 "port_security": False,
1748 "exit_on_floating_ip_error": False,
1749 "port_security_disable_strategy": "full",
1750 },
1751 {
1752 "port_security": True,
1753 "exit_on_floating_ip_error": False,
1754 "floating_ip": True,
1755 },
1756 ]
1757 net_list_vim = []
1758 external_network, no_secured_ports = [], []
1759 expected_external_network, expected_no_secured_ports = [], []
1760 expected_net_list_vim = []
1761
1762 self.vimconn._prepare_network_for_vminstance(
1763 name,
1764 net_list,
1765 created_items,
1766 net_list_vim,
1767 external_network,
1768 no_secured_ports,
1769 )
1770 self.assertEqual(expected_net_list_vim, net_list_vim)
1771 self.assertEqual(external_network, expected_external_network)
1772 self.assertEqual(expected_no_secured_ports, no_secured_ports)
1773
1774 mock_create_port.assert_not_called()
1775
1776 @patch.object(vimconnector, "_reload_connection")
1777 @patch.object(vimconnector, "_create_port")
1778 def test_prepare_network_for_vm_instance_empty_net_list(
1779 self, mock_create_port, mock_reload_connection
1780 ):
1781 """Net list is empty."""
1782 mock_reload_connection.side_effect = None
1783 created_items = {}
1784 net_list_vim = []
1785 external_network, no_secured_ports = [], []
1786 expected_external_network, expected_no_secured_ports = [], []
1787 expected_net_list_vim = []
1788
1789 self.vimconn._prepare_network_for_vminstance(
1790 name,
1791 net_list,
1792 created_items,
1793 net_list_vim,
1794 external_network,
1795 no_secured_ports,
1796 )
1797 self.assertEqual(expected_net_list_vim, net_list_vim)
1798 self.assertEqual(external_network, expected_external_network)
1799 self.assertEqual(expected_no_secured_ports, no_secured_ports)
1800
1801 mock_create_port.assert_not_called()
1802
1803 @patch.object(vimconnector, "_reload_connection")
1804 @patch.object(vimconnector, "_create_port")
1805 def test_prepare_network_for_vm_instance_use_floating_ip_false_mgmt_net(
1806 self, mock_create_port, mock_reload_connection
1807 ):
1808 """Nets have net-id, floating_ip False, mgmt network."""
1809 mock_reload_connection.side_effect = None
1810 created_items = {}
1811 net_list = [
1812 {
1813 "net_id": net2_id,
1814 "floating_ip": False,
1815 "use": "mgmt",
1816 }
1817 ]
1818 net_list_vim = []
1819 mock_create_port.side_effect = [
1820 (
1821 {
1822 "port": {
1823 "id": port2_id,
1824 "mac_address": mac_address,
1825 "name": name,
1826 },
1827 },
1828 {"port-dict": port2_id},
1829 ),
1830 ]
1831 external_network, no_secured_ports = [], []
1832 expected_external_network, expected_no_secured_ports = [], []
1833 expected_net_list_vim = [{"port-dict": port2_id}]
1834 self.vimconn._prepare_network_for_vminstance(
1835 name,
1836 net_list,
1837 created_items,
1838 net_list_vim,
1839 external_network,
1840 no_secured_ports,
1841 )
1842 self.assertEqual(expected_net_list_vim, net_list_vim)
1843 self.assertEqual(external_network, expected_external_network)
1844 self.assertEqual(expected_no_secured_ports, no_secured_ports)
1845
1846 mock_create_port.assert_called_once_with(
1847 {
1848 "net_id": net2_id,
1849 "floating_ip": False,
1850 "use": "mgmt",
1851 },
1852 name,
1853 created_items,
1854 )
1855
1856 @patch.object(vimconnector, "_reload_connection")
1857 def test_prepare_network_for_vm_instance_mgmt_net_net_port_security_and_floating_ip_true(
1858 self, mock_reload_connection
1859 ):
1860 """Nets have net-id, use_floating_ip False in VIM config, mgmt network, net floating_ip is True."""
1861 self.vimconn.config["use_floating_ip"] = False
1862 mock_create_port = CopyingMock()
1863 mock_reload_connection.side_effect = None
1864 created_items = {}
1865 net_list = [
1866 {
1867 "net_id": net2_id,
1868 "floating_ip": True,
1869 "use": "mgmt",
1870 }
1871 ]
1872 net_list_vim = []
1873 mock_create_port.side_effect = [
1874 (
1875 {
1876 "port": {
1877 "id": port2_id,
1878 "mac_address": mac_address,
1879 "name": name,
1880 },
1881 },
1882 {"port-dict": port2_id},
1883 ),
1884 ]
1885 external_network, no_secured_ports = [], []
1886 expected_external_network = [
1887 {
1888 "net_id": net2_id,
1889 "floating_ip": True,
1890 "use": "mgmt",
1891 "exit_on_floating_ip_error": True,
1892 },
1893 ]
1894 expected_no_secured_ports = []
1895 expected_net_list_vim = [{"port-dict": port2_id}]
1896 with patch.object(vimconnector, "_create_port", mock_create_port):
1897 self.vimconn._prepare_network_for_vminstance(
1898 name,
1899 net_list,
1900 created_items,
1901 net_list_vim,
1902 external_network,
1903 no_secured_ports,
1904 )
1905 self.assertEqual(expected_net_list_vim, net_list_vim)
1906 self.assertEqual(external_network, expected_external_network)
1907 self.assertEqual(expected_no_secured_ports, no_secured_ports)
1908
1909 mock_create_port.assert_called_once_with(
1910 {
1911 "net_id": net2_id,
1912 "floating_ip": True,
1913 "use": "mgmt",
1914 },
1915 name,
1916 created_items,
1917 )
1918
1919 @patch.object(vimconnector, "_reload_connection")
1920 def test_prepare_network_for_vm_instance_use_floating_ip_true_mgmt_net_port_security_false(
1921 self, mock_reload_connection
1922 ):
1923 """Nets have net-id, use_floating_ip is True in VIM config, mgmt network, net port security is False."""
1924 mock_create_port = CopyingMock()
1925 self.vimconn.config["use_floating_ip"] = True
1926 self.vimconn.config["no_port_security_extension"] = False
1927 mock_reload_connection.side_effect = None
1928 created_items = {}
1929
1930 net_list = [
1931 {
1932 "net_id": net2_id,
1933 "use": "mgmt",
1934 "port_security": False,
1935 "exit_on_floating_ip_error": False,
1936 "port_security_disable_strategy": "full",
1937 }
1938 ]
1939 net_list_vim = []
1940 mock_create_port.side_effect = [
1941 (
1942 {
1943 "port": {
1944 "id": port2_id,
1945 "mac_address": mac_address,
1946 "name": name,
1947 },
1948 },
1949 {"port-dict": port2_id},
1950 ),
1951 ]
1952 external_network, no_secured_ports = [], []
1953 expected_external_network = [
1954 {
1955 "net_id": net2_id,
1956 "use": "mgmt",
1957 "port_security": False,
1958 "exit_on_floating_ip_error": False,
1959 "port_security_disable_strategy": "full",
1960 "floating_ip": True,
1961 },
1962 ]
1963 expected_no_secured_ports = [(port2_id, "full")]
1964 expected_net_list_vim = [{"port-dict": port2_id}]
1965 with patch.object(vimconnector, "_create_port", mock_create_port):
1966 self.vimconn._prepare_network_for_vminstance(
1967 name,
1968 net_list,
1969 created_items,
1970 net_list_vim,
1971 external_network,
1972 no_secured_ports,
1973 )
1974
1975 mock_create_port.assert_called_once_with(
1976 {
1977 "net_id": net2_id,
1978 "use": "mgmt",
1979 "port_security": False,
1980 "exit_on_floating_ip_error": False,
1981 "port_security_disable_strategy": "full",
1982 },
1983 name,
1984 created_items,
1985 )
1986 self.assertEqual(expected_net_list_vim, net_list_vim)
1987 self.assertEqual(external_network, expected_external_network)
1988 self.assertEqual(expected_no_secured_ports, no_secured_ports)
1989
1990 @patch.object(vimconnector, "_reload_connection")
1991 def test_prepare_network_for_vm_instance_use_fip_true_non_mgmt_net_port_security_false(
1992 self, mock_reload_connection
1993 ):
1994 """Nets have net-id, use_floating_ip True in VIM config, non-mgmt network, port security is False."""
1995 mock_create_port = CopyingMock()
1996 self.vimconn.config["use_floating_ip"] = True
1997 self.vimconn.config["no_port_security_extension"] = False
1998 mock_reload_connection.side_effect = None
1999 created_items = {}
2000
2001 net_list = [
2002 {
2003 "net_id": net2_id,
2004 "use": "other",
2005 "port_security": False,
2006 "port_security_disable_strategy": "full",
2007 }
2008 ]
2009 net_list_vim = []
2010 mock_create_port.side_effect = [
2011 (
2012 {
2013 "port": {
2014 "id": port2_id,
2015 "mac_address": mac_address,
2016 "name": name,
2017 },
2018 },
2019 {"port-dict": port2_id},
2020 ),
2021 ]
2022 external_network, no_secured_ports = [], []
2023 expected_external_network = []
2024 expected_no_secured_ports = [(port2_id, "full")]
2025 expected_net_list_vim = [{"port-dict": port2_id}]
2026 with patch.object(vimconnector, "_create_port", mock_create_port):
2027 self.vimconn._prepare_network_for_vminstance(
2028 name,
2029 net_list,
2030 created_items,
2031 net_list_vim,
2032 external_network,
2033 no_secured_ports,
2034 )
2035
2036 mock_create_port.assert_called_once_with(
2037 {
2038 "net_id": net2_id,
2039 "use": "other",
2040 "port_security": False,
2041 "port_security_disable_strategy": "full",
2042 },
2043 name,
2044 created_items,
2045 )
2046 self.assertEqual(expected_net_list_vim, net_list_vim)
2047 self.assertEqual(external_network, expected_external_network)
2048 self.assertEqual(expected_no_secured_ports, no_secured_ports)
2049
2050 @patch.object(vimconnector, "_reload_connection")
2051 def test_prepare_network_for_vm_instance_use_fip_true_non_mgmt_net_port_security_true(
2052 self, mock_reload_connection
2053 ):
2054 """Nets have net-id, use_floating_ip is True in VIM config, non-mgmt network, net port security is True."""
2055 mock_create_port = CopyingMock()
2056 self.vimconn.config["use_floating_ip"] = True
2057 self.vimconn.config["no_port_security_extension"] = True
2058 mock_reload_connection.side_effect = None
2059 created_items = {}
2060
2061 net_list = [
2062 {
2063 "net_id": net2_id,
2064 "use": "other",
2065 "port_security": True,
2066 "port_security_disable_strategy": "full",
2067 }
2068 ]
2069 net_list_vim = []
2070 mock_create_port.side_effect = [
2071 (
2072 {
2073 "port": {
2074 "id": port2_id,
2075 "mac_address": mac_address,
2076 "name": name,
2077 },
2078 },
2079 {"port-dict": port2_id},
2080 ),
2081 ]
2082 external_network, no_secured_ports = [], []
2083 expected_external_network = []
2084 expected_no_secured_ports = []
2085 expected_net_list_vim = [{"port-dict": port2_id}]
2086 with patch.object(vimconnector, "_create_port", mock_create_port):
2087 self.vimconn._prepare_network_for_vminstance(
2088 name,
2089 net_list,
2090 created_items,
2091 net_list_vim,
2092 external_network,
2093 no_secured_ports,
2094 )
2095
2096 mock_create_port.assert_called_once_with(
2097 {
2098 "net_id": net2_id,
2099 "use": "other",
2100 "port_security": True,
2101 "port_security_disable_strategy": "full",
2102 },
2103 name,
2104 created_items,
2105 )
2106 self.assertEqual(expected_net_list_vim, net_list_vim)
2107 self.assertEqual(external_network, expected_external_network)
2108 self.assertEqual(expected_no_secured_ports, no_secured_ports)
2109
2110 @patch.object(vimconnector, "_reload_connection")
2111 def test_prepare_network_for_vm_instance_create_port_raise_exception(
2112 self, mock_reload_connection
2113 ):
2114 """_create_port method raise exception."""
2115 mock_create_port = CopyingMock()
2116 self.vimconn.config["use_floating_ip"] = True
2117 self.vimconn.config["no_port_security_extension"] = True
2118 mock_reload_connection.side_effect = None
2119 created_items = {}
2120
2121 net_list = [
2122 {
2123 "net_id": net2_id,
2124 "use": "other",
2125 "port_security": True,
2126 "port_security_disable_strategy": "full",
2127 }
2128 ]
2129 net_list_vim = []
2130 mock_create_port.side_effect = KeyError
2131 external_network, no_secured_ports = [], []
2132 expected_external_network = []
2133 expected_no_secured_ports = []
2134 expected_net_list_vim = []
2135 with patch.object(vimconnector, "_create_port", mock_create_port):
2136 with self.assertRaises(Exception) as err:
2137 self.vimconn._prepare_network_for_vminstance(
2138 name,
2139 net_list,
2140 created_items,
2141 net_list_vim,
2142 external_network,
2143 no_secured_ports,
2144 )
2145
2146 self.assertEqual(type(err.exception), KeyError)
2147
2148 mock_create_port.assert_called_once_with(
2149 {
2150 "net_id": net2_id,
2151 "use": "other",
2152 "port_security": True,
2153 "port_security_disable_strategy": "full",
2154 },
2155 name,
2156 created_items,
2157 )
2158 self.assertEqual(expected_net_list_vim, net_list_vim)
2159 self.assertEqual(external_network, expected_external_network)
2160 self.assertEqual(expected_no_secured_ports, no_secured_ports)
2161
2162 @patch.object(vimconnector, "_reload_connection")
2163 def test_prepare_network_for_vm_instance_reload_connection_raise_exception(
2164 self, mock_reload_connection
2165 ):
2166 """_reload_connection method raises exception."""
2167 mock_create_port = CopyingMock()
2168 mock_reload_connection.side_effect = VimConnConnectionException(
2169 "Connection failed."
2170 )
2171 self.vimconn.config["use_floating_ip"] = True
2172 self.vimconn.config["no_port_security_extension"] = True
2173 created_items = {}
2174
2175 net_list = [
2176 {
2177 "net_id": net2_id,
2178 "use": "other",
2179 "port_security": True,
2180 "port_security_disable_strategy": "full",
2181 }
2182 ]
2183 net_list_vim = []
2184 mock_create_port.side_effect = None
2185 external_network, no_secured_ports = [], []
2186 expected_external_network = []
2187 expected_no_secured_ports = []
2188 expected_net_list_vim = []
2189 with patch.object(vimconnector, "_create_port", mock_create_port):
2190 with self.assertRaises(Exception) as err:
2191 self.vimconn._prepare_network_for_vminstance(
2192 name,
2193 net_list,
2194 created_items,
2195 net_list_vim,
2196 external_network,
2197 no_secured_ports,
2198 )
2199
2200 self.assertEqual(type(err.exception), VimConnConnectionException)
2201 self.assertEqual(str(err.exception), "Connection failed.")
2202 mock_reload_connection.assert_called_once()
2203 mock_create_port.assert_not_called()
2204 self.assertEqual(expected_net_list_vim, net_list_vim)
2205 self.assertEqual(external_network, expected_external_network)
2206 self.assertEqual(expected_no_secured_ports, no_secured_ports)
2207
2208 def test_prepare_persistent_root_volumes_vim_using_volume_id(self):
2209 """Existing persistent root volume with vim_volume_id."""
2210 vm_av_zone = ["nova"]
2211 base_disk_index = ord("a")
2212 disk = {"vim_volume_id": volume_id}
2213 block_device_mapping = {}
2214 existing_vim_volumes = []
2215 created_items = {}
2216 expected_boot_vol_id = None
2217 expected_block_device_mapping = {"vda": volume_id}
2218 expected_existing_vim_volumes = [{"id": volume_id}]
2219 boot_volume_id = self.vimconn._prepare_persistent_root_volumes(
2220 name,
2221 vm_av_zone,
2222 disk,
2223 base_disk_index,
2224 block_device_mapping,
2225 existing_vim_volumes,
2226 created_items,
2227 )
2228 self.assertEqual(boot_volume_id, expected_boot_vol_id)
2229 self.assertDictEqual(block_device_mapping, expected_block_device_mapping)
2230 self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes)
2231 self.vimconn.cinder.volumes.create.assert_not_called()
2232
2233 def test_prepare_persistent_non_root_volumes_vim_using_volume_id(self):
2234 """Existing persistent non root volume with vim_volume_id."""
2235 vm_av_zone = ["nova"]
2236 base_disk_index = ord("b")
2237 disk = {"vim_volume_id": volume_id}
2238 block_device_mapping = {}
2239 existing_vim_volumes = []
2240 created_items = {}
2241 expected_block_device_mapping = {"vdb": volume_id}
2242 expected_existing_vim_volumes = [{"id": volume_id}]
2243 self.vimconn._prepare_non_root_persistent_volumes(
2244 name,
2245 disk,
2246 vm_av_zone,
2247 block_device_mapping,
2248 base_disk_index,
2249 existing_vim_volumes,
2250 created_items,
2251 )
2252 self.assertDictEqual(block_device_mapping, expected_block_device_mapping)
2253 self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes)
2254 self.vimconn.cinder.volumes.create.assert_not_called()
2255
2256 def test_prepare_persistent_root_volumes_using_vim_id(self):
2257 """Existing persistent root volume with vim_id."""
2258 vm_av_zone = ["nova"]
2259 base_disk_index = ord("a")
2260 disk = {"vim_id": volume_id}
2261 block_device_mapping = {}
2262 existing_vim_volumes = []
2263 created_items = {}
2264 expected_boot_vol_id = None
2265 expected_block_device_mapping = {"vda": volume_id}
2266 expected_existing_vim_volumes = [{"id": volume_id}]
2267 boot_volume_id = self.vimconn._prepare_persistent_root_volumes(
2268 name,
2269 vm_av_zone,
2270 disk,
2271 base_disk_index,
2272 block_device_mapping,
2273 existing_vim_volumes,
2274 created_items,
2275 )
2276 self.assertEqual(boot_volume_id, expected_boot_vol_id)
2277 self.assertDictEqual(block_device_mapping, expected_block_device_mapping)
2278 self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes)
2279 self.vimconn.cinder.volumes.create.assert_not_called()
2280
2281 def test_prepare_persistent_non_root_volumes_using_vim_id(self):
2282 """Existing persistent root volume with vim_id."""
2283 vm_av_zone = ["nova"]
2284 base_disk_index = ord("b")
2285 disk = {"vim_id": volume_id}
2286 block_device_mapping = {}
2287 existing_vim_volumes = []
2288 created_items = {}
2289
2290 expected_block_device_mapping = {"vdb": volume_id}
2291 expected_existing_vim_volumes = [{"id": volume_id}]
2292 self.vimconn._prepare_non_root_persistent_volumes(
2293 name,
2294 disk,
2295 vm_av_zone,
2296 block_device_mapping,
2297 base_disk_index,
2298 existing_vim_volumes,
2299 created_items,
2300 )
2301
2302 self.assertDictEqual(block_device_mapping, expected_block_device_mapping)
2303 self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes)
2304 self.vimconn.cinder.volumes.create.assert_not_called()
2305
2306 def test_prepare_persistent_root_volumes_create(self):
2307 """Create persistent root volume."""
2308 self.vimconn.cinder.volumes.create.return_value.id = volume_id2
2309 vm_av_zone = ["nova"]
2310 base_disk_index = ord("a")
2311 disk = {"size": 10, "image_id": image_id}
2312 block_device_mapping = {}
2313 existing_vim_volumes = []
2314 created_items = {}
2315 expected_boot_vol_id = volume_id2
2316 expected_block_device_mapping = {"vda": volume_id2}
2317 expected_existing_vim_volumes = []
2318 boot_volume_id = self.vimconn._prepare_persistent_root_volumes(
2319 name,
2320 vm_av_zone,
2321 disk,
2322 base_disk_index,
2323 block_device_mapping,
2324 existing_vim_volumes,
2325 created_items,
2326 )
2327 self.assertEqual(boot_volume_id, expected_boot_vol_id)
2328 self.assertDictEqual(block_device_mapping, expected_block_device_mapping)
2329 self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes)
2330 self.vimconn.cinder.volumes.create.assert_called_once_with(
2331 size=10,
2332 name="basicvmvda",
2333 imageRef=image_id,
2334 availability_zone=["nova"],
2335 )
2336 self.assertEqual(created_items, {f"volume:{volume_id2}": True})
2337
2338 def test_prepare_persistent_non_root_volumes_create(self):
2339 """Create persistent non-root volume."""
2340 self.vimconn.cinder = CopyingMock()
2341 self.vimconn.cinder.volumes.create.return_value.id = volume_id2
2342 vm_av_zone = ["nova"]
2343 base_disk_index = ord("a")
2344 disk = {"size": 10}
2345 block_device_mapping = {}
2346 existing_vim_volumes = []
2347 created_items = {}
2348 expected_block_device_mapping = {"vda": volume_id2}
2349 expected_existing_vim_volumes = []
2350 self.vimconn._prepare_non_root_persistent_volumes(
2351 name,
2352 disk,
2353 vm_av_zone,
2354 block_device_mapping,
2355 base_disk_index,
2356 existing_vim_volumes,
2357 created_items,
2358 )
2359
2360 self.assertDictEqual(block_device_mapping, expected_block_device_mapping)
2361 self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes)
2362 self.vimconn.cinder.volumes.create.assert_called_once_with(
2363 size=10, name="basicvmvda", availability_zone=["nova"]
2364 )
2365 self.assertEqual(created_items, {f"volume:{volume_id2}": True})
2366
2367 def test_prepare_persistent_root_volumes_create_raise_exception(self):
2368 """Create persistent root volume raise exception."""
2369 self.vimconn.cinder.volumes.create.side_effect = Exception
2370 vm_av_zone = ["nova"]
2371 base_disk_index = ord("a")
2372 disk = {"size": 10, "image_id": image_id}
2373 block_device_mapping = {}
2374 existing_vim_volumes = []
2375 created_items = {}
2376
2377 with self.assertRaises(Exception):
2378 result = self.vimconn._prepare_persistent_root_volumes(
2379 name,
2380 vm_av_zone,
2381 disk,
2382 base_disk_index,
2383 block_device_mapping,
2384 existing_vim_volumes,
2385 created_items,
2386 )
2387
2388 self.assertEqual(result, None)
2389
2390 self.vimconn.cinder.volumes.create.assert_called_once_with(
2391 size=10,
2392 name="basicvmvda",
2393 imageRef=image_id,
2394 availability_zone=["nova"],
2395 )
2396 self.assertEqual(existing_vim_volumes, [])
2397 self.assertEqual(block_device_mapping, {})
2398 self.assertEqual(created_items, {})
2399
2400 def test_prepare_persistent_non_root_volumes_create_raise_exception(self):
2401 """Create persistent non-root volume raise exception."""
2402 self.vimconn.cinder.volumes.create.side_effect = Exception
2403 vm_av_zone = ["nova"]
2404 base_disk_index = ord("b")
2405 disk = {"size": 10}
2406 block_device_mapping = {}
2407 existing_vim_volumes = []
2408 created_items = {}
2409
2410 with self.assertRaises(Exception):
2411 self.vimconn._prepare_non_root_persistent_volumes(
2412 name,
2413 disk,
2414 vm_av_zone,
2415 block_device_mapping,
2416 base_disk_index,
2417 existing_vim_volumes,
2418 created_items,
2419 )
2420
2421 self.vimconn.cinder.volumes.create.assert_called_once_with(
2422 size=10, name="basicvmvdb", availability_zone=["nova"]
2423 )
2424 self.assertEqual(existing_vim_volumes, [])
2425 self.assertEqual(block_device_mapping, {})
2426 self.assertEqual(created_items, {})
2427
2428 @patch("time.sleep")
2429 def test_wait_for_created_volumes_availability_volume_status_available(
2430 self, mock_sleep
2431 ):
2432 """Created volume status is available."""
2433 elapsed_time = 5
2434 created_items = {f"volume:{volume_id2}": True}
2435 self.vimconn.cinder.volumes.get.return_value.status = "available"
2436
2437 result = self.vimconn._wait_for_created_volumes_availability(
2438 elapsed_time, created_items
2439 )
2440 self.assertEqual(result, elapsed_time)
2441 self.vimconn.cinder.volumes.get.assert_called_with(volume_id2)
2442 mock_sleep.assert_not_called()
2443
2444 @patch("time.sleep")
2445 def test_wait_for_existing_volumes_availability_volume_status_available(
2446 self, mock_sleep
2447 ):
2448 """Existing volume status is available."""
2449 elapsed_time = 5
2450 existing_vim_volumes = [{"id": volume_id2}]
2451 self.vimconn.cinder.volumes.get.return_value.status = "available"
2452
2453 result = self.vimconn._wait_for_existing_volumes_availability(
2454 elapsed_time, existing_vim_volumes
2455 )
2456 self.assertEqual(result, elapsed_time)
2457 self.vimconn.cinder.volumes.get.assert_called_with(volume_id2)
2458 mock_sleep.assert_not_called()
2459
2460 @patch("time.sleep")
2461 def test_wait_for_created_volumes_availability_status_processing_multiple_volumes(
2462 self, mock_sleep
2463 ):
2464 """Created volume status is processing."""
2465 elapsed_time = 5
2466 created_items = {
2467 f"volume:{volume_id2}": True,
2468 f"volume:{volume_id3}": True,
2469 }
2470 self.vimconn.cinder.volumes.get.side_effect = [
2471 Status("processing"),
2472 Status("available"),
2473 Status("available"),
2474 ]
2475
2476 result = self.vimconn._wait_for_created_volumes_availability(
2477 elapsed_time, created_items
2478 )
2479 self.assertEqual(result, 10)
2480 _call_mock_get_volumes = self.vimconn.cinder.volumes.get.call_args_list
2481 self.assertEqual(_call_mock_get_volumes[0][0], (volume_id2,))
2482 self.assertEqual(_call_mock_get_volumes[1][0], (volume_id2,))
2483 self.assertEqual(_call_mock_get_volumes[2][0], (volume_id3,))
2484 mock_sleep.assert_called_with(5)
2485 self.assertEqual(1, mock_sleep.call_count)
2486
2487 @patch("time.sleep")
2488 def test_wait_for_existing_volumes_availability_status_processing_multiple_volumes(
2489 self, mock_sleep
2490 ):
2491 """Existing volume status is processing."""
2492 elapsed_time = 5
2493 existing_vim_volumes = [
2494 {"id": volume_id2},
2495 {"id": "44e0e83-b9uu-4akk-t234-p9cc4811bd4a"},
2496 ]
2497 self.vimconn.cinder.volumes.get.side_effect = [
2498 Status("processing"),
2499 Status("available"),
2500 Status("available"),
2501 ]
2502
2503 result = self.vimconn._wait_for_existing_volumes_availability(
2504 elapsed_time, existing_vim_volumes
2505 )
2506 self.assertEqual(result, 10)
2507 _call_mock_get_volumes = self.vimconn.cinder.volumes.get.call_args_list
2508 self.assertEqual(_call_mock_get_volumes[0][0], (volume_id2,))
2509 self.assertEqual(_call_mock_get_volumes[1][0], (volume_id2,))
2510 self.assertEqual(
2511 _call_mock_get_volumes[2][0], ("44e0e83-b9uu-4akk-t234-p9cc4811bd4a",)
2512 )
2513 mock_sleep.assert_called_with(5)
2514 self.assertEqual(1, mock_sleep.call_count)
2515
2516 @patch("time.sleep")
2517 def test_wait_for_created_volumes_availability_volume_status_processing_timeout(
2518 self, mock_sleep
2519 ):
2520 """Created volume status is processing, elapsed time greater than timeout (1800)."""
2521 elapsed_time = 1805
2522 created_items = {f"volume:{volume_id2}": True}
2523 self.vimconn.cinder.volumes.get.side_effect = [
2524 Status("processing"),
2525 Status("processing"),
2526 ]
2527 with patch("time.sleep", mock_sleep):
2528 result = self.vimconn._wait_for_created_volumes_availability(
2529 elapsed_time, created_items
2530 )
2531 self.assertEqual(result, 1805)
2532 self.vimconn.cinder.volumes.get.assert_not_called()
2533 mock_sleep.assert_not_called()
2534
2535 @patch("time.sleep")
2536 def test_wait_for_existing_volumes_availability_volume_status_processing_timeout(
2537 self, mock_sleep
2538 ):
2539 """Exsiting volume status is processing, elapsed time greater than timeout (1800)."""
2540 elapsed_time = 1805
2541 existing_vim_volumes = [{"id": volume_id2}]
2542 self.vimconn.cinder.volumes.get.side_effect = [
2543 Status("processing"),
2544 Status("processing"),
2545 ]
2546
2547 result = self.vimconn._wait_for_existing_volumes_availability(
2548 elapsed_time, existing_vim_volumes
2549 )
2550 self.assertEqual(result, 1805)
2551 self.vimconn.cinder.volumes.get.assert_not_called()
2552 mock_sleep.assert_not_called()
2553
2554 @patch("time.sleep")
2555 def test_wait_for_created_volumes_availability_cinder_raise_exception(
2556 self, mock_sleep
2557 ):
2558 """Cinder get volumes raises exception for created volumes."""
2559 elapsed_time = 1000
2560 created_items = {f"volume:{volume_id2}": True}
2561 self.vimconn.cinder.volumes.get.side_effect = Exception
2562 with self.assertRaises(Exception):
2563 result = self.vimconn._wait_for_created_volumes_availability(
2564 elapsed_time, created_items
2565 )
2566 self.assertEqual(result, 1000)
2567 self.vimconn.cinder.volumes.get.assert_called_with(volume_id2)
2568 mock_sleep.assert_not_called()
2569
2570 @patch("time.sleep")
2571 def test_wait_for_existing_volumes_availability_cinder_raise_exception(
2572 self, mock_sleep
2573 ):
2574 """Cinder get volumes raises exception for existing volumes."""
2575 elapsed_time = 1000
2576 existing_vim_volumes = [{"id": volume_id2}]
2577 self.vimconn.cinder.volumes.get.side_effect = Exception
2578 with self.assertRaises(Exception):
2579 result = self.vimconn._wait_for_existing_volumes_availability(
2580 elapsed_time, existing_vim_volumes
2581 )
2582 self.assertEqual(result, 1000)
2583 self.vimconn.cinder.volumes.get.assert_called_with(volume_id2)
2584 mock_sleep.assert_not_called()
2585
2586 @patch("time.sleep")
2587 def test_wait_for_created_volumes_availability_no_volume_in_created_items(
2588 self, mock_sleep
2589 ):
2590 """Created_items dict does not have volume-id."""
2591 elapsed_time = 10
2592 created_items = {}
2593
2594 self.vimconn.cinder.volumes.get.side_effect = [None]
2595
2596 result = self.vimconn._wait_for_created_volumes_availability(
2597 elapsed_time, created_items
2598 )
2599 self.assertEqual(result, 10)
2600 self.vimconn.cinder.volumes.get.assert_not_called()
2601 mock_sleep.assert_not_called()
2602
2603 @patch("time.sleep")
2604 def test_wait_for_existing_volumes_availability_no_volume_in_existing_vim_volumes(
2605 self, mock_sleep
2606 ):
2607 """Existing_vim_volumes list does not have volume."""
2608 elapsed_time = 10
2609 existing_vim_volumes = []
2610
2611 self.vimconn.cinder.volumes.get.side_effect = [None]
2612
2613 result = self.vimconn._wait_for_existing_volumes_availability(
2614 elapsed_time, existing_vim_volumes
2615 )
2616 self.assertEqual(result, 10)
2617 self.vimconn.cinder.volumes.get.assert_not_called()
2618 mock_sleep.assert_not_called()
2619
2620 @patch.object(vimconnector, "_prepare_persistent_root_volumes")
2621 @patch.object(vimconnector, "_prepare_non_root_persistent_volumes")
2622 @patch.object(vimconnector, "_wait_for_created_volumes_availability")
2623 @patch.object(vimconnector, "_wait_for_existing_volumes_availability")
2624 def test_prepare_disk_for_vm_instance(
2625 self,
2626 mock_existing_vol_availability,
2627 mock_created_vol_availability,
2628 mock_non_root_volumes,
2629 mock_root_volumes,
2630 ):
2631 """Prepare disks for VM instance successfully."""
2632 existing_vim_volumes = []
2633 created_items = {}
2634 vm_av_zone = ["nova"]
2635
2636 mock_root_volumes.return_value = root_vol_id
2637 mock_created_vol_availability.return_value = 10
2638 mock_existing_vol_availability.return_value = 15
2639 self.vimconn.cinder = CopyingMock()
2640
2641 self.vimconn._prepare_disk_for_vminstance(
2642 name, existing_vim_volumes, created_items, vm_av_zone, disk_list2
2643 )
2644 self.vimconn.cinder.volumes.set_bootable.assert_called_once_with(
2645 root_vol_id, True
2646 )
2647 mock_created_vol_availability.assert_called_once_with(0, created_items)
2648 mock_existing_vol_availability.assert_called_once_with(10, existing_vim_volumes)
2649 self.assertEqual(mock_root_volumes.call_count, 1)
2650 self.assertEqual(mock_non_root_volumes.call_count, 1)
2651 mock_root_volumes.assert_called_once_with(
2652 name="basicvm",
2653 vm_av_zone=["nova"],
2654 disk={"size": 10, "image_id": image_id},
2655 base_disk_index=97,
2656 block_device_mapping={},
2657 existing_vim_volumes=[],
2658 created_items={},
2659 )
2660 mock_non_root_volumes.assert_called_once_with(
2661 name="basicvm",
2662 disk={"size": 20},
2663 vm_av_zone=["nova"],
2664 base_disk_index=98,
2665 block_device_mapping={},
2666 existing_vim_volumes=[],
2667 created_items={},
2668 )
2669
2670 @patch.object(vimconnector, "_prepare_persistent_root_volumes")
2671 @patch.object(vimconnector, "_prepare_non_root_persistent_volumes")
2672 @patch.object(vimconnector, "_wait_for_created_volumes_availability")
2673 @patch.object(vimconnector, "_wait_for_existing_volumes_availability")
2674 def test_prepare_disk_for_vm_instance_timeout_exceeded(
2675 self,
2676 mock_existing_vol_availability,
2677 mock_created_vol_availability,
2678 mock_non_root_volumes,
2679 mock_root_volumes,
2680 ):
2681 """Timeout exceeded while waiting for disks."""
2682 existing_vim_volumes = []
2683 created_items = {}
2684 vm_av_zone = ["nova"]
2685
2686 mock_root_volumes.return_value = root_vol_id
2687 mock_created_vol_availability.return_value = 1700
2688 mock_existing_vol_availability.return_value = 1900
2689
2690 with self.assertRaises(VimConnException) as err:
2691 self.vimconn._prepare_disk_for_vminstance(
2692 name, existing_vim_volumes, created_items, vm_av_zone, disk_list2
2693 )
2694 self.assertEqual(
2695 str(err.exception), "Timeout creating volumes for instance basicvm"
2696 )
2697 self.vimconn.cinder.volumes.set_bootable.assert_not_called()
2698 mock_created_vol_availability.assert_called_once_with(0, created_items)
2699 mock_existing_vol_availability.assert_called_once_with(
2700 1700, existing_vim_volumes
2701 )
2702 self.assertEqual(mock_root_volumes.call_count, 1)
2703 self.assertEqual(mock_non_root_volumes.call_count, 1)
2704 mock_root_volumes.assert_called_once_with(
2705 name="basicvm",
2706 vm_av_zone=["nova"],
2707 disk={"size": 10, "image_id": image_id},
2708 base_disk_index=97,
2709 block_device_mapping={},
2710 existing_vim_volumes=[],
2711 created_items={},
2712 )
2713 mock_non_root_volumes.assert_called_once_with(
2714 name="basicvm",
2715 disk={"size": 20},
2716 vm_av_zone=["nova"],
2717 base_disk_index=98,
2718 block_device_mapping={},
2719 existing_vim_volumes=[],
2720 created_items={},
2721 )
2722
2723 @patch.object(vimconnector, "_prepare_persistent_root_volumes")
2724 @patch.object(vimconnector, "_prepare_non_root_persistent_volumes")
2725 @patch.object(vimconnector, "_wait_for_created_volumes_availability")
2726 @patch.object(vimconnector, "_wait_for_existing_volumes_availability")
2727 def test_prepare_disk_for_vm_instance_empty_disk_list(
2728 self,
2729 mock_existing_vol_availability,
2730 mock_created_vol_availability,
2731 mock_non_root_volumes,
2732 mock_root_volumes,
2733 ):
2734 """Disk list is empty."""
2735 existing_vim_volumes = []
2736 created_items = {}
2737 vm_av_zone = ["nova"]
2738 mock_created_vol_availability.return_value = 2
2739 mock_existing_vol_availability.return_value = 3
2740
2741 self.vimconn._prepare_disk_for_vminstance(
2742 name, existing_vim_volumes, created_items, vm_av_zone, disk_list
2743 )
2744 self.vimconn.cinder.volumes.set_bootable.assert_not_called()
2745 mock_created_vol_availability.assert_called_once_with(0, created_items)
2746 mock_existing_vol_availability.assert_called_once_with(2, existing_vim_volumes)
2747 mock_root_volumes.assert_not_called()
2748 mock_non_root_volumes.assert_not_called()
2749
2750 @patch.object(vimconnector, "_prepare_persistent_root_volumes")
2751 @patch.object(vimconnector, "_prepare_non_root_persistent_volumes")
2752 @patch.object(vimconnector, "_wait_for_created_volumes_availability")
2753 @patch.object(vimconnector, "_wait_for_existing_volumes_availability")
2754 def test_prepare_disk_for_vm_instance_persistent_root_volume_error(
2755 self,
2756 mock_existing_vol_availability,
2757 mock_created_vol_availability,
2758 mock_non_root_volumes,
2759 mock_root_volumes,
2760 ):
2761 """Persistent root volumes preparation raises error."""
2762 existing_vim_volumes = []
2763 created_items = {}
2764 vm_av_zone = ["nova"]
2765
2766 mock_root_volumes.side_effect = Exception()
2767 mock_created_vol_availability.return_value = 10
2768 mock_existing_vol_availability.return_value = 15
2769
2770 with self.assertRaises(Exception):
2771 self.vimconn._prepare_disk_for_vminstance(
2772 name, existing_vim_volumes, created_items, vm_av_zone, disk_list2
2773 )
2774 self.vimconn.cinder.volumes.set_bootable.assert_not_called()
2775 mock_created_vol_availability.assert_not_called()
2776 mock_existing_vol_availability.assert_not_called()
2777 mock_root_volumes.assert_called_once_with(
2778 name="basicvm",
2779 vm_av_zone=["nova"],
2780 disk={"size": 10, "image_id": image_id},
2781 base_disk_index=97,
2782 block_device_mapping={},
2783 existing_vim_volumes=[],
2784 created_items={},
2785 )
2786 mock_non_root_volumes.assert_not_called()
2787
2788 @patch.object(vimconnector, "_prepare_persistent_root_volumes")
2789 @patch.object(vimconnector, "_prepare_non_root_persistent_volumes")
2790 @patch.object(vimconnector, "_wait_for_created_volumes_availability")
2791 @patch.object(vimconnector, "_wait_for_existing_volumes_availability")
2792 def test_prepare_disk_for_vm_instance_non_root_volume_error(
2793 self,
2794 mock_existing_vol_availability,
2795 mock_created_vol_availability,
2796 mock_non_root_volumes,
2797 mock_root_volumes,
2798 ):
2799 """Non-root volumes preparation raises error."""
2800 existing_vim_volumes = []
2801 created_items = {}
2802 vm_av_zone = ["nova"]
2803
2804 mock_root_volumes.return_value = root_vol_id
2805 mock_non_root_volumes.side_effect = Exception
2806
2807 with self.assertRaises(Exception):
2808 self.vimconn._prepare_disk_for_vminstance(
2809 name, existing_vim_volumes, created_items, vm_av_zone, disk_list2
2810 )
2811 self.vimconn.cinder.volumes.set_bootable.assert_not_called()
2812 mock_created_vol_availability.assert_not_called()
2813 mock_existing_vol_availability.assert_not_called()
2814 self.assertEqual(mock_root_volumes.call_count, 1)
2815 self.assertEqual(mock_non_root_volumes.call_count, 1)
2816 mock_root_volumes.assert_called_once_with(
2817 name="basicvm",
2818 vm_av_zone=["nova"],
2819 disk={"size": 10, "image_id": image_id},
2820 base_disk_index=97,
2821 block_device_mapping={},
2822 existing_vim_volumes=[],
2823 created_items={},
2824 )
2825 mock_non_root_volumes.assert_called_once_with(
2826 name="basicvm",
2827 disk={"size": 20},
2828 vm_av_zone=["nova"],
2829 base_disk_index=98,
2830 block_device_mapping={},
2831 existing_vim_volumes=[],
2832 created_items={},
2833 )
2834
2835 def test_find_external_network_for_floating_ip_no_external_network(self):
2836 """External network could not be found."""
2837 self.vimconn.neutron.list_networks.return_value = {
2838 "networks": [
2839 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": False}
2840 ]
2841 }
2842 with self.assertRaises(VimConnException) as err:
2843 self.vimconn._find_the_external_network_for_floating_ip()
2844 self.assertEqual(
2845 str(err.exception),
2846 "Cannot create floating_ip automatically since no external network is present",
2847 )
2848
2849 def test_find_external_network_for_floating_one_external_network(self):
2850 """One external network has been found."""
2851 self.vimconn.neutron.list_networks.return_value = {
2852 "networks": [
2853 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True}
2854 ]
2855 }
2856 expected_result = "408b73-r9cc-5a6a-a270-82cc4811bd4a"
2857 result = self.vimconn._find_the_external_network_for_floating_ip()
2858 self.assertEqual(result, expected_result)
2859
2860 def test_find_external_network_for_floating_neutron_raises_exception(self):
2861 """Neutron list networks raises exception."""
2862 self.vimconn.neutron.list_networks.side_effect = Exception
2863 with self.assertRaises(Exception):
2864 self.vimconn._find_the_external_network_for_floating_ip()
2865
2866 def test_find_external_network_for_floating_several_external_network(self):
2867 """Several exernal networks has been found."""
2868 self.vimconn.neutron.list_networks.return_value = {
2869 "networks": [
2870 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True},
2871 {"id": "608b73-y9cc-5a6a-a270-12cc4811bd4a", "router:external": True},
2872 ]
2873 }
2874 with self.assertRaises(VimConnException) as err:
2875 self.vimconn._find_the_external_network_for_floating_ip()
2876 self.assertEqual(
2877 str(err.exception),
2878 "Cannot create floating_ip automatically since multiple external networks are present",
2879 )
2880
2881 def test_neutron_create_float_ip(self):
2882 """Floating ip creation is successful."""
2883 param = {"net_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a"}
2884 created_items = {}
2885 self.vimconn.neutron.create_floatingip.return_value = {
2886 "floatingip": {"id": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
2887 }
2888 expected_created_items = {
2889 "floating_ip:308b73-t9cc-1a6a-a270-12cc4811bd4a": True
2890 }
2891 self.vimconn._neutron_create_float_ip(param, created_items)
2892 self.assertEqual(created_items, expected_created_items)
2893
2894 def test_neutron_create_float_ip_exception_occured(self):
2895 """Floating ip could not be created."""
2896 param = {
2897 "floatingip": {
2898 "floating_network_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a",
2899 "tenant_id": "308b73-19cc-8a6a-a270-02cc4811bd9a",
2900 }
2901 }
2902 created_items = {}
2903 self.vimconn.neutron = CopyingMock()
2904 self.vimconn.neutron.create_floatingip.side_effect = Exception(
2905 "Neutron floating ip create exception occured."
2906 )
2907 with self.assertRaises(VimConnException) as err:
2908 self.vimconn._neutron_create_float_ip(param, created_items)
2909 self.assertEqual(created_items, {})
2910 self.assertEqual(
2911 str(err.exception),
2912 "Exception: Cannot create new floating_ip Neutron floating ip create exception occured.",
2913 )
2914
2915 @patch.object(vimconnector, "_neutron_create_float_ip")
2916 @patch.object(vimconnector, "_find_the_external_network_for_floating_ip")
2917 def test_create_floating_ip_pool_id_available(
2918 self, mock_find_ext_network, mock_create_float_ip
2919 ):
2920 """Floating ip creation, ip pool is available."""
2921 floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
2922 created_items = {}
2923 expected_param = {
2924 "floatingip": {
2925 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2926 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
2927 }
2928 }
2929 self.vimconn._create_floating_ip(floating_network, self.server, created_items)
2930 mock_find_ext_network.assert_not_called()
2931 mock_create_float_ip.assert_called_once_with(expected_param, {})
2932
2933 @patch.object(vimconnector, "_neutron_create_float_ip")
2934 @patch.object(vimconnector, "_find_the_external_network_for_floating_ip")
2935 def test_create_floating_ip_finding_pool_id(
2936 self, mock_find_ext_network, mock_create_float_ip
2937 ):
2938 """Floating ip creation, pool id need to be found."""
2939 floating_network = {"floating_ip": True}
2940 created_items = {}
2941 mock_find_ext_network.return_value = "308b73-t9cc-1a6a-a270-12cc4811bd4a"
2942 expected_param = {
2943 "floatingip": {
2944 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2945 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
2946 }
2947 }
2948 self.vimconn._create_floating_ip(floating_network, self.server, created_items)
2949 mock_find_ext_network.assert_called_once()
2950 mock_create_float_ip.assert_called_once_with(expected_param, {})
2951
2952 @patch.object(vimconnector, "_neutron_create_float_ip")
2953 @patch.object(vimconnector, "_find_the_external_network_for_floating_ip")
2954 def test_create_floating_ip_neutron_create_floating_ip_exception(
2955 self, mock_find_ext_network, mock_create_float_ip
2956 ):
2957 """Neutron creat floating ip raises error."""
2958 floating_network = {"floating_ip": True}
2959 created_items = {}
2960 mock_create_float_ip.side_effect = VimConnException(
2961 "Can not create floating ip."
2962 )
2963 mock_find_ext_network.return_value = "308b73-t9cc-1a6a-a270-12cc4811bd4a"
2964 expected_param = {
2965 "floatingip": {
2966 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2967 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
2968 }
2969 }
2970
2971 with self.assertRaises(VimConnException) as err:
2972 self.vimconn._create_floating_ip(
2973 floating_network, self.server, created_items
2974 )
2975 self.assertEqual(str(err.exception), "Can not create floating ip.")
2976 mock_find_ext_network.assert_called_once()
2977 mock_create_float_ip.assert_called_once_with(expected_param, {})
2978
2979 @patch.object(vimconnector, "_neutron_create_float_ip")
2980 @patch.object(vimconnector, "_find_the_external_network_for_floating_ip")
2981 def test_create_floating_ip_can_not_find_pool_id(
2982 self, mock_find_ext_network, mock_create_float_ip
2983 ):
2984 """Floating ip creation, pool id could not be found."""
2985 floating_network = {"floating_ip": True}
2986 created_items = {}
2987 mock_find_ext_network.side_effect = VimConnException(
2988 "Cannot create floating_ip automatically since no external network is present"
2989 )
2990 with self.assertRaises(VimConnException) as err:
2991 self.vimconn._create_floating_ip(
2992 floating_network, self.server, created_items
2993 )
2994 self.assertEqual(
2995 str(err.exception),
2996 "Cannot create floating_ip automatically since no external network is present",
2997 )
2998 mock_find_ext_network.assert_called_once()
2999 mock_create_float_ip.assert_not_called()
3000
3001 def test_find_floating_ip_get_free_floating_ip(self):
3002 """Get free floating ips successfully."""
3003 floating_ips = [
3004 {
3005 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
3006 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3007 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3008 }
3009 ]
3010 floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3011 expected_result = "508b73-o9cc-5a6a-a270-72cc4811bd8"
3012
3013 result = self.vimconn._find_floating_ip(
3014 self.server, floating_ips, floating_network
3015 )
3016 self.assertEqual(result, expected_result)
3017
3018 def test_find_floating_ip_different_floating_network_id(self):
3019 """Floating network id is different with floating_ip of floating network."""
3020 floating_ips = [
3021 {
3022 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3023 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3024 }
3025 ]
3026 floating_network = {"floating_ip": "508b73-t9cc-1a6a-a270-12cc4811bd4a"}
3027
3028 result = self.vimconn._find_floating_ip(
3029 self.server, floating_ips, floating_network
3030 )
3031 self.assertEqual(result, None)
3032
3033 def test_find_floating_ip_different_fip_tenant(self):
3034 """Items in floating_ips has port_id, tenant_is is not same with server tenant id."""
3035 floating_ips = [
3036 {
3037 "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3038 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3039 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3040 "tenant_id": self.server.id,
3041 }
3042 ]
3043 floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3044 mock_create_floating_ip = CopyingMock()
3045 with patch.object(vimconnector, "_create_floating_ip", mock_create_floating_ip):
3046 result = self.vimconn._find_floating_ip(
3047 self.server, floating_ips, floating_network
3048 )
3049 self.assertEqual(result, None)
3050
3051 @patch("time.sleep")
3052 def test_assign_floating_ip(self, mock_sleep):
3053 """Assign floating ip successfully."""
3054 free_floating_ip = "508b73-o9cc-5a6a-a270-72cc4811bd8"
3055 floating_network = {"vim_id": floating_network_vim_id}
3056 fip = {
3057 "port_id": floating_network_vim_id,
3058 "floating_network_id": "p08b73-e9cc-5a6a-t270-82cc4811bd4a",
3059 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3060 "tenant_id": "k08b73-e9cc-5a6a-t270-82cc4811bd4a",
3061 }
3062 self.vimconn.neutron.update_floatingip.side_effect = None
3063 self.vimconn.neutron.show_floatingip.return_value = fip
3064 expected_result = fip
3065
3066 result = self.vimconn._assign_floating_ip(free_floating_ip, floating_network)
3067 self.assertEqual(result, expected_result)
3068 self.vimconn.neutron.update_floatingip.assert_called_once_with(
3069 free_floating_ip,
3070 {"floatingip": {"port_id": floating_network_vim_id}},
3071 )
3072 mock_sleep.assert_called_once_with(5)
3073 self.vimconn.neutron.show_floatingip.assert_called_once_with(free_floating_ip)
3074
3075 @patch("time.sleep")
3076 def test_assign_floating_ip_update_floating_ip_exception(self, mock_sleep):
3077 """Neutron update floating ip raises exception."""
3078 free_floating_ip = "508b73-o9cc-5a6a-a270-72cc4811bd8"
3079 floating_network = {"vim_id": floating_network_vim_id}
3080 self.vimconn.neutron = CopyingMock()
3081 self.vimconn.neutron.update_floatingip.side_effect = Exception(
3082 "Floating ip is not updated."
3083 )
3084
3085 with self.assertRaises(Exception) as err:
3086 result = self.vimconn._assign_floating_ip(
3087 free_floating_ip, floating_network
3088 )
3089 self.assertEqual(result, None)
3090 self.assertEqual(str(err.exception), "Floating ip is not updated.")
3091
3092 self.vimconn.neutron.update_floatingip.assert_called_once_with(
3093 free_floating_ip,
3094 {"floatingip": {"port_id": floating_network_vim_id}},
3095 )
3096 mock_sleep.assert_not_called()
3097 self.vimconn.neutron.show_floatingip.assert_not_called()
3098
3099 @patch("time.sleep")
3100 def test_assign_floating_ip_show_floating_ip_exception(self, mock_sleep):
3101 """Neutron show floating ip raises exception."""
3102 free_floating_ip = "508b73-o9cc-5a6a-a270-72cc4811bd8"
3103 floating_network = {"vim_id": floating_network_vim_id}
3104 self.vimconn.neutron.update_floatingip.side_effect = None
3105 self.vimconn.neutron.show_floatingip.side_effect = Exception(
3106 "Floating ip could not be shown."
3107 )
3108
3109 with self.assertRaises(Exception) as err:
3110 result = self.vimconn._assign_floating_ip(
3111 free_floating_ip, floating_network
3112 )
3113 self.assertEqual(result, None)
3114 self.assertEqual(str(err.exception), "Floating ip could not be shown.")
3115 self.vimconn.neutron.update_floatingip.assert_called_once_with(
3116 free_floating_ip,
3117 {"floatingip": {"port_id": floating_network_vim_id}},
3118 )
3119 mock_sleep.assert_called_once_with(5)
3120 self.vimconn.neutron.show_floatingip.assert_called_once_with(free_floating_ip)
3121
3122 @patch("random.shuffle")
3123 @patch.object(vimconnector, "_find_floating_ip")
3124 def test_get_free_floating_ip(self, mock_find_floating_ip, mock_shuffle):
3125 """Get free floating ip successfully."""
3126 floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3127 created_items = {}
3128 floating_ips = [
3129 {
3130 "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3131 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3132 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3133 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3134 },
3135 {
3136 "port_id": "508b73-r9cc-5a6a-5270-o2cc4811bd4a",
3137 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3138 "id": "208b73-o9cc-5a6a-a270-52cc4811bd8",
3139 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3140 },
3141 ]
3142 self.vimconn.neutron.list_floatingips.return_value = {
3143 "floatingips": floating_ips
3144 }
3145 mock_find_floating_ip.return_value = "508b73-o9cc-5a6a-a270-72cc4811bd8"
3146 expected_result = "508b73-o9cc-5a6a-a270-72cc4811bd8"
3147
3148 result = self.vimconn._get_free_floating_ip(
3149 self.server, floating_network, created_items
3150 )
3151 self.assertEqual(result, expected_result)
3152 mock_shuffle.assert_called_once_with(floating_ips)
3153 mock_find_floating_ip.assert_called_once_with(
3154 self.server, floating_ips, floating_network, created_items
3155 )
3156
3157 @patch("random.shuffle")
3158 @patch.object(vimconnector, "_find_floating_ip")
3159 def test_get_free_floating_ip_list_floating_ip_exception(
3160 self, mock_find_floating_ip, mock_shuffle
3161 ):
3162 """Neutron list floating IPs raises exception."""
3163 floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3164 created_items = {}
3165 self.vimconn.neutron = CopyingMock()
3166 self.vimconn.neutron.list_floatingips.side_effect = Exception(
3167 "Floating ips could not be listed."
3168 )
3169 with self.assertRaises(Exception) as err:
3170 result = self.vimconn._get_free_floating_ip(
3171 self.server, floating_network, created_items
3172 )
3173 self.assertEqual(result, None)
3174 self.assertEqual(str(err.exception), "Floating ips could not be listed.")
3175 mock_shuffle.assert_not_called()
3176 mock_find_floating_ip.assert_not_called()
3177
3178 @patch("random.shuffle")
3179 @patch.object(vimconnector, "_find_floating_ip")
3180 def test_get_free_floating_ip_find_floating_ip_exception(
3181 self, mock_find_floating_ip, mock_shuffle
3182 ):
3183 """_find_floating_ip method raises exception."""
3184 floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3185 created_items = {}
3186 floating_ips = [
3187 {
3188 "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3189 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3190 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3191 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3192 },
3193 {
3194 "port_id": "508b73-r9cc-5a6a-5270-o2cc4811bd4a",
3195 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3196 "id": "208b73-o9cc-5a6a-a270-52cc4811bd8",
3197 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3198 },
3199 ]
3200 self.vimconn.neutron = CopyingMock()
3201 self.vimconn.neutron.list_floatingips.return_value = {
3202 "floatingips": floating_ips
3203 }
3204 mock_find_floating_ip.side_effect = Exception(
3205 "Free floating ip could not be found."
3206 )
3207
3208 with self.assertRaises(Exception) as err:
3209 result = self.vimconn._get_free_floating_ip(
3210 self.server, floating_network, created_items
3211 )
3212 self.assertEqual(result, None)
3213 self.assertEqual(str(err.exception), "Free floating ip could not be found.")
3214 mock_shuffle.assert_called_once_with(floating_ips)
3215 mock_find_floating_ip.assert_called_once_with(
3216 self.server, floating_ips, floating_network, created_items
3217 )
3218
3219 @patch.object(vimconnector, "_create_floating_ip")
3220 @patch.object(vimconnector, "_get_free_floating_ip")
3221 @patch.object(vimconnector, "_assign_floating_ip")
3222 def test_prepare_external_network_for_vm_instance(
3223 self,
3224 mock_assign_floating_ip,
3225 mock_get_free_floating_ip,
3226 mock_create_floating_ip,
3227 ):
3228 """Prepare external network successfully."""
3229 external_network = [
3230 {
3231 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3232 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3233 },
3234 ]
3235 created_items = {}
3236 vm_start_time = time_return_value
3237 mock_get_free_floating_ip.side_effect = ["y08b73-o9cc-1a6a-a270-12cc4811bd4u"]
3238 mock_assign_floating_ip.return_value = {
3239 "floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}
3240 }
3241 self.vimconn.neutron = CopyingMock()
3242 self.vimconn.nova = CopyingMock()
3243 self.vimconn.neutron.show_floatingip.return_value = {
3244 "floatingip": {"port_id": ""}
3245 }
3246
3247 self.vimconn._prepare_external_network_for_vminstance(
3248 external_network, self.server, created_items, vm_start_time
3249 )
3250
3251 self.assertEqual(mock_get_free_floating_ip.call_count, 1)
3252 mock_get_free_floating_ip.assert_called_once_with(
3253 self.server,
3254 {
3255 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3256 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3257 },
3258 created_items,
3259 )
3260 self.vimconn.neutron.show_floatingip.assert_called_once_with(
3261 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
3262 )
3263 self.vimconn.nova.servers.get.assert_not_called()
3264 mock_create_floating_ip.assert_not_called()
3265 mock_assign_floating_ip.assert_called_once_with(
3266 "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3267 {
3268 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3269 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3270 },
3271 )
3272
3273 @patch("time.time")
3274 @patch("time.sleep")
3275 @patch.object(vimconnector, "_create_floating_ip")
3276 @patch.object(vimconnector, "_get_free_floating_ip")
3277 @patch.object(vimconnector, "_assign_floating_ip")
3278 def test_prepare_external_network_for_vm_instance_no_free_floating_ip(
3279 self,
3280 mock_assign_floating_ip,
3281 mock_get_free_floating_ip,
3282 mock_create_floating_ip,
3283 mock_sleep,
3284 mock_time,
3285 ):
3286 """There is not any free floating ip."""
3287 floating_network = {
3288 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3289 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3290 }
3291 external_network = [floating_network]
3292
3293 created_items = {}
3294 vm_start_time = time_return_value
3295 mock_get_free_floating_ip.return_value = None
3296 mock_assign_floating_ip.return_value = {}
3297 self.vimconn.nova.servers.get.return_value.status = "ERROR"
3298 self.vimconn.neutron.show_floatingip.return_value = {}
3299
3300 with self.assertRaises(KeyError):
3301 self.vimconn._prepare_external_network_for_vminstance(
3302 external_network, self.server, created_items, vm_start_time
3303 )
3304
3305 self.assertEqual(mock_get_free_floating_ip.call_count, 4)
3306 mock_get_free_floating_ip.assert_called_with(
3307 self.server,
3308 {
3309 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3310 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3311 },
3312 created_items,
3313 )
3314 self.vimconn.neutron.show_floatingip.assert_called_with(None)
3315 mock_sleep.assert_not_called()
3316 mock_time.assert_not_called()
3317 self.assertEqual(self.vimconn.nova.servers.get.call_count, 4)
3318 mock_create_floating_ip.assert_called_with(
3319 floating_network, self.server, created_items
3320 )
3321 self.assertEqual(mock_create_floating_ip.call_count, 4)
3322 mock_assign_floating_ip.assert_not_called()
3323 self.vimconn.nova.servers.get.assert_called_with(self.server.id)
3324
3325 @patch("time.time")
3326 @patch("time.sleep")
3327 @patch.object(vimconnector, "_create_floating_ip")
3328 @patch.object(vimconnector, "_get_free_floating_ip")
3329 @patch.object(vimconnector, "_assign_floating_ip")
3330 def test_prepare_external_network_for_vm_instance_no_free_fip_can_not_create_fip_exit_on_error_false(
3331 self,
3332 mock_assign_floating_ip,
3333 mock_get_free_floating_ip,
3334 mock_create_floating_ip,
3335 mock_sleep,
3336 mock_time,
3337 ):
3338 """There is not any free floating ip, create_floating ip method raise exception
3339 exit_on_floating_ip_error set to False."""
3340 floating_network = {
3341 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3342 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3343 "exit_on_floating_ip_error": False,
3344 }
3345 external_network = [floating_network]
3346
3347 created_items = {}
3348 vm_start_time = time_return_value
3349 mock_get_free_floating_ip.return_value = None
3350 mock_assign_floating_ip.return_value = {}
3351 mock_create_floating_ip.side_effect = VimConnException(
3352 "Can not create floating ip."
3353 )
3354 self.vimconn.nova.servers.get.return_value.status = "ERROR"
3355 self.vimconn.neutron.show_floatingip.return_value = {}
3356
3357 self.vimconn._prepare_external_network_for_vminstance(
3358 external_network, self.server, created_items, vm_start_time
3359 )
3360 self.assertEqual(mock_get_free_floating_ip.call_count, 1)
3361 mock_get_free_floating_ip.assert_called_with(
3362 self.server,
3363 {
3364 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3365 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3366 "exit_on_floating_ip_error": False,
3367 },
3368 created_items,
3369 )
3370 self.vimconn.neutron.show_floatingip.assert_not_called()
3371 mock_sleep.assert_not_called()
3372 mock_time.assert_not_called()
3373 self.vimconn.nova.servers.get.assert_not_called()
3374 mock_create_floating_ip.assert_called_with(
3375 floating_network, self.server, created_items
3376 )
3377 self.assertEqual(mock_create_floating_ip.call_count, 1)
3378 mock_assign_floating_ip.assert_not_called()
3379
3380 @patch("time.time")
3381 @patch("time.sleep")
3382 @patch.object(vimconnector, "_create_floating_ip")
3383 @patch.object(vimconnector, "_get_free_floating_ip")
3384 @patch.object(vimconnector, "_assign_floating_ip")
3385 def test_prepare_external_network_for_vm_instance_no_free_fip_can_not_create_fip_exit_on_error_true(
3386 self,
3387 mock_assign_floating_ip,
3388 mock_get_free_floating_ip,
3389 mock_create_floating_ip,
3390 mock_sleep,
3391 mock_time,
3392 ):
3393 """There is not any free floating ip, create_floating ip method raise exception
3394 exit_on_floating_ip_error set to False."""
3395 floating_network = {
3396 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3397 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3398 "exit_on_floating_ip_error": True,
3399 }
3400 external_network = [floating_network]
3401
3402 created_items = {}
3403 vm_start_time = time_return_value
3404 mock_get_free_floating_ip.return_value = None
3405 mock_assign_floating_ip.return_value = {}
3406 mock_create_floating_ip.side_effect = VimConnException(
3407 "Can not create floating ip."
3408 )
3409 self.vimconn.nova.servers.get.return_value.status = "ERROR"
3410 self.vimconn.neutron.show_floatingip.return_value = {}
3411 with self.assertRaises(VimConnException):
3412 self.vimconn._prepare_external_network_for_vminstance(
3413 external_network, self.server, created_items, vm_start_time
3414 )
3415 self.assertEqual(mock_get_free_floating_ip.call_count, 1)
3416 mock_get_free_floating_ip.assert_called_with(
3417 self.server,
3418 {
3419 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3420 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3421 "exit_on_floating_ip_error": True,
3422 },
3423 created_items,
3424 )
3425 self.vimconn.neutron.show_floatingip.assert_not_called()
3426 mock_sleep.assert_not_called()
3427 mock_time.assert_not_called()
3428 self.vimconn.nova.servers.get.assert_not_called()
3429 mock_create_floating_ip.assert_called_with(
3430 floating_network, self.server, created_items
3431 )
3432 self.assertEqual(mock_create_floating_ip.call_count, 1)
3433 mock_assign_floating_ip.assert_not_called()
3434
3435 @patch.object(vimconnector, "_create_floating_ip")
3436 @patch.object(vimconnector, "_get_free_floating_ip")
3437 @patch.object(vimconnector, "_assign_floating_ip")
3438 def test_prepare_external_network_for_vm_instance_fip_has_port_id(
3439 self,
3440 mock_assign_floating_ip,
3441 mock_get_free_floating_ip,
3442 mock_create_floating_ip,
3443 ):
3444 """Neutron show floating ip return the fip with port_id and floating network vim_id
3445 is different from port_id."""
3446 floating_network = {
3447 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3448 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3449 }
3450 external_network = [floating_network]
3451 created_items = {}
3452 vm_start_time = 150
3453 mock_get_free_floating_ip.side_effect = [
3454 "t08b73-o9cc-1a6a-a270-12cc4811bd4u",
3455 "r08b73-o9cc-1a6a-a270-12cc4811bd4u",
3456 "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3457 ]
3458 mock_assign_floating_ip.side_effect = [
3459 {"floatingip": {"port_id": "k08b73-r9cc-5a6a-a270-82cc4811bd4a"}},
3460 {"floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}},
3461 ]
3462 self.vimconn.neutron = CopyingMock()
3463 self.vimconn.nova = CopyingMock()
3464 self.vimconn.neutron.show_floatingip.side_effect = [
3465 {"floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}},
3466 {"floatingip": {"port_id": ""}},
3467 {"floatingip": {"port_id": ""}},
3468 ]
3469 self.vimconn._prepare_external_network_for_vminstance(
3470 external_network, self.server, created_items, vm_start_time
3471 )
3472 self.assertEqual(mock_get_free_floating_ip.call_count, 3)
3473 _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list
3474 self.assertEqual(
3475 _call_mock_get_free_floating_ip[0][0],
3476 (
3477 self.server,
3478 floating_network,
3479 created_items,
3480 ),
3481 )
3482 self.assertEqual(
3483 _call_mock_get_free_floating_ip[1][0],
3484 (
3485 self.server,
3486 floating_network,
3487 created_items,
3488 ),
3489 )
3490 self.assertEqual(
3491 _call_mock_get_free_floating_ip[2][0],
3492 (
3493 self.server,
3494 floating_network,
3495 created_items,
3496 ),
3497 )
3498 self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 3)
3499 self.vimconn.nova.servers.get.assert_not_called()
3500 mock_create_floating_ip.assert_not_called()
3501 self.assertEqual(mock_assign_floating_ip.call_count, 2)
3502 _call_mock_assign_floating_ip = mock_assign_floating_ip.call_args_list
3503 self.assertEqual(
3504 _call_mock_assign_floating_ip[0][0],
3505 ("r08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network),
3506 )
3507 self.assertEqual(
3508 _call_mock_assign_floating_ip[1][0],
3509 ("y08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network),
3510 )
3511
3512 @patch("time.time")
3513 @patch("time.sleep")
3514 @patch.object(vimconnector, "_create_floating_ip")
3515 @patch.object(vimconnector, "_get_free_floating_ip")
3516 @patch.object(vimconnector, "_assign_floating_ip")
3517 def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_vm_status_in_error(
3518 self,
3519 mock_assign_floating_ip,
3520 mock_get_free_floating_ip,
3521 mock_create_floating_ip,
3522 mock_sleep,
3523 mock_time,
3524 ):
3525 """Neutron show floating ip gives exception, exit_on_floating_ip_error set to True,
3526 VM status is in error."""
3527 floating_network = {
3528 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3529 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3530 "exit_on_floating_ip_error": True,
3531 }
3532 external_network = [floating_network]
3533 created_items = {}
3534 vm_start_time = time_return_value
3535
3536 mock_time.side_effect = [156570150, 156570800, 156571200]
3537
3538 self.vimconn.nova.servers.get.return_value.status = "ERROR"
3539 self.vimconn.neutron.show_floatingip.side_effect = [
3540 Exception("Floating ip could not be shown.")
3541 ] * 4
3542 with self.assertRaises(Exception) as err:
3543 self.vimconn._prepare_external_network_for_vminstance(
3544 external_network, self.server, created_items, vm_start_time
3545 )
3546 self.assertEqual(
3547 str(err.exception),
3548 "Cannot create floating_ip: Exception Floating ip could not be shown.",
3549 )
3550
3551 self.assertEqual(mock_get_free_floating_ip.call_count, 4)
3552 _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list
3553 self.assertEqual(
3554 _call_mock_get_free_floating_ip[0][0],
3555 (
3556 self.server,
3557 floating_network,
3558 created_items,
3559 ),
3560 )
3561 self.assertEqual(
3562 _call_mock_get_free_floating_ip[1][0],
3563 (
3564 self.server,
3565 floating_network,
3566 created_items,
3567 ),
3568 )
3569 self.assertEqual(
3570 _call_mock_get_free_floating_ip[2][0],
3571 (
3572 self.server,
3573 floating_network,
3574 created_items,
3575 ),
3576 )
3577 self.assertEqual(
3578 _call_mock_get_free_floating_ip[3][0],
3579 (
3580 self.server,
3581 floating_network,
3582 created_items,
3583 ),
3584 )
3585
3586 self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 4)
3587 self.vimconn.nova.servers.get.assert_called_with(self.server.id)
3588 mock_create_floating_ip.assert_not_called()
3589 mock_assign_floating_ip.assert_not_called()
3590 mock_time.assert_not_called()
3591 mock_sleep.assert_not_called()
3592
3593 @patch("time.time")
3594 @patch("time.sleep")
3595 @patch.object(vimconnector, "_create_floating_ip")
3596 @patch.object(vimconnector, "_get_free_floating_ip")
3597 @patch.object(vimconnector, "_assign_floating_ip")
3598 def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_vm_status_in_active(
3599 self,
3600 mock_assign_floating_ip,
3601 mock_get_free_floating_ip,
3602 mock_create_floating_ip,
3603 mock_sleep,
3604 mock_time,
3605 ):
3606 """Neutron show floating ip gives exception, exit_on_floating_ip_error is set to False,
3607 VM status is in active."""
3608 floating_network = {
3609 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3610 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3611 "exit_on_floating_ip_error": False,
3612 }
3613 external_network = [floating_network]
3614 created_items = {}
3615 vm_start_time = time_return_value
3616
3617 mock_time.side_effect = [156570150, 156570800, 156571200]
3618
3619 self.vimconn.nova.servers.get.return_value.status = "ACTIVE"
3620 self.vimconn.neutron.show_floatingip.side_effect = [
3621 Exception("Floating ip could not be shown.")
3622 ] * 4
3623
3624 self.vimconn._prepare_external_network_for_vminstance(
3625 external_network, self.server, created_items, vm_start_time
3626 )
3627 # self.assertEqual(str(err.exception), "Cannot create floating_ip")
3628
3629 self.assertEqual(mock_get_free_floating_ip.call_count, 4)
3630 _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list
3631 self.assertEqual(
3632 _call_mock_get_free_floating_ip[0][0],
3633 (
3634 self.server,
3635 floating_network,
3636 created_items,
3637 ),
3638 )
3639 self.assertEqual(
3640 _call_mock_get_free_floating_ip[1][0],
3641 (
3642 self.server,
3643 floating_network,
3644 created_items,
3645 ),
3646 )
3647 self.assertEqual(
3648 _call_mock_get_free_floating_ip[2][0],
3649 (
3650 self.server,
3651 floating_network,
3652 created_items,
3653 ),
3654 )
3655 self.assertEqual(
3656 _call_mock_get_free_floating_ip[3][0],
3657 (
3658 self.server,
3659 floating_network,
3660 created_items,
3661 ),
3662 )
3663
3664 self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 4)
3665 self.vimconn.nova.servers.get.assert_called_with(self.server.id)
3666 mock_create_floating_ip.assert_not_called()
3667 mock_assign_floating_ip.assert_not_called()
3668 mock_time.assert_not_called()
3669 mock_sleep.assert_not_called()
3670
3671 @patch("time.time")
3672 @patch("time.sleep")
3673 @patch.object(vimconnector, "_create_floating_ip")
3674 @patch.object(vimconnector, "_get_free_floating_ip")
3675 @patch.object(vimconnector, "_assign_floating_ip")
3676 def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_exit_on_error(
3677 self,
3678 mock_assign_floating_ip,
3679 mock_get_free_floating_ip,
3680 mock_create_floating_ip,
3681 mock_sleep,
3682 mock_time,
3683 ):
3684 """Neutron show floating ip gives exception, but exit_on_floating_ip_error is set to True.
3685 VM status is not ACTIVE or ERROR, server timeout happened."""
3686 floating_network = {
3687 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3688 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3689 "exit_on_floating_ip_error": True,
3690 }
3691 external_network = [floating_network]
3692 created_items = {}
3693 vm_start_time = time_return_value
3694 mock_get_free_floating_ip.side_effect = None
3695 mock_time.side_effect = [156571790, 156571795, 156571800, 156571805]
3696 self.vimconn.nova.servers.get.return_value.status = "OTHER"
3697 self.vimconn.neutron.show_floatingip.side_effect = [
3698 Exception("Floating ip could not be shown.")
3699 ] * 5
3700
3701 with self.assertRaises(VimConnException) as err:
3702 self.vimconn._prepare_external_network_for_vminstance(
3703 external_network, self.server, created_items, vm_start_time
3704 )
3705 self.assertEqual(
3706 str(err.exception),
3707 "Cannot create floating_ip: Exception Floating ip could not be shown.",
3708 )
3709
3710 self.assertEqual(mock_get_free_floating_ip.call_count, 3)
3711 _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list
3712 self.assertEqual(
3713 _call_mock_get_free_floating_ip[0][0],
3714 (
3715 self.server,
3716 floating_network,
3717 created_items,
3718 ),
3719 )
3720 self.assertEqual(
3721 _call_mock_get_free_floating_ip[1][0],
3722 (
3723 self.server,
3724 floating_network,
3725 created_items,
3726 ),
3727 )
3728 self.assertEqual(
3729 _call_mock_get_free_floating_ip[2][0],
3730 (
3731 self.server,
3732 floating_network,
3733 created_items,
3734 ),
3735 )
3736
3737 self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 3)
3738 self.vimconn.nova.servers.get.assert_called_with(self.server.id)
3739 mock_create_floating_ip.assert_not_called()
3740 mock_assign_floating_ip.assert_not_called()
3741 self.assertEqual(mock_time.call_count, 3)
3742 self.assertEqual(mock_sleep.call_count, 2)
3743
3744 @patch("time.time")
3745 @patch("time.sleep")
3746 @patch.object(vimconnector, "_create_floating_ip")
3747 @patch.object(vimconnector, "_get_free_floating_ip")
3748 @patch.object(vimconnector, "_assign_floating_ip")
3749 def test_prepare_external_network_for_vm_instance_assign_floating_ip_exception_exit_on_error(
3750 self,
3751 mock_assign_floating_ip,
3752 mock_get_free_floating_ip,
3753 mock_create_floating_ip,
3754 mock_sleep,
3755 mock_time,
3756 ):
3757 """Assign floating ip method gives exception, exit_on_floating_ip_error is set to True.
3758 VM status is in ERROR."""
3759 floating_network = {
3760 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3761 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3762 "exit_on_floating_ip_error": True,
3763 }
3764 external_network = [floating_network]
3765 created_items = {}
3766 vm_start_time = time_return_value
3767
3768 mock_get_free_floating_ip.side_effect = [
3769 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
3770 ] * 4
3771
3772 mock_time.side_effect = [156571790, 156571795, 156571800, 156571805]
3773
3774 mock_assign_floating_ip.side_effect = [
3775 Exception("Floating ip could not be assigned.")
3776 ] * 4
3777
3778 self.vimconn.nova.servers.get.return_value.status = "ERROR"
3779 self.vimconn.neutron.show_floatingip.side_effect = [
3780 {"floatingip": {"port_id": ""}}
3781 ] * 4
3782
3783 with self.assertRaises(VimConnException) as err:
3784 self.vimconn._prepare_external_network_for_vminstance(
3785 external_network, self.server, created_items, vm_start_time
3786 )
3787 self.assertEqual(
3788 str(err.exception),
3789 "Cannot create floating_ip: Exception Floating ip could not be assigned.",
3790 )
3791
3792 self.assertEqual(mock_get_free_floating_ip.call_count, 4)
3793 _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list
3794 self.assertEqual(
3795 _call_mock_get_free_floating_ip[0][0],
3796 (
3797 self.server,
3798 floating_network,
3799 created_items,
3800 ),
3801 )
3802 self.assertEqual(
3803 _call_mock_get_free_floating_ip[1][0],
3804 (
3805 self.server,
3806 floating_network,
3807 created_items,
3808 ),
3809 )
3810 self.assertEqual(
3811 _call_mock_get_free_floating_ip[2][0],
3812 (
3813 self.server,
3814 floating_network,
3815 created_items,
3816 ),
3817 )
3818
3819 self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 4)
3820 self.vimconn.neutron.show_floatingip.assert_called_with(
3821 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
3822 )
3823 self.assertEqual(self.vimconn.nova.servers.get.call_count, 4)
3824 self.vimconn.nova.servers.get.assert_called_with(self.server.id)
3825 mock_time.assert_not_called()
3826 mock_sleep.assert_not_called()
3827 mock_create_floating_ip.assert_not_called()
3828
3829 @patch("time.time")
3830 @patch("time.sleep")
3831 @patch.object(vimconnector, "_create_floating_ip")
3832 @patch.object(vimconnector, "_get_free_floating_ip")
3833 @patch.object(vimconnector, "_assign_floating_ip")
3834 def test_prepare_external_network_for_vm_instance_empty_external_network_list(
3835 self,
3836 mock_assign_floating_ip,
3837 mock_get_free_floating_ip,
3838 mock_create_floating_ip,
3839 mock_sleep,
3840 mock_time,
3841 ):
3842 """External network list is empty."""
3843 external_network = []
3844 created_items = {}
3845 vm_start_time = time_return_value
3846
3847 self.vimconn._prepare_external_network_for_vminstance(
3848 external_network, self.server, created_items, vm_start_time
3849 )
3850 mock_create_floating_ip.assert_not_called()
3851 mock_time.assert_not_called()
3852 mock_sleep.assert_not_called()
3853 mock_assign_floating_ip.assert_not_called()
3854 mock_get_free_floating_ip.assert_not_called()
3855 self.vimconn.neutron.show.show_floatingip.assert_not_called()
3856 self.vimconn.nova.servers.get.assert_not_called()
3857
3858 @patch.object(vimconnector, "_vimconnector__wait_for_vm")
3859 def test_update_port_security_for_vm_instance(self, mock_wait_for_vm):
3860 """no_secured_ports has port and the port has allow-address-pairs."""
3861 no_secured_ports = [(port2_id, "allow-address-pairs")]
3862
3863 self.vimconn._update_port_security_for_vminstance(no_secured_ports, self.server)
3864
3865 mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE")
3866
3867 self.vimconn.neutron.update_port.assert_called_once_with(
3868 port2_id,
3869 {"port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}},
3870 )
3871
3872 @patch.object(vimconnector, "_vimconnector__wait_for_vm")
3873 def test_update_port_security_for_vm_instance_no_allowed_address_pairs(
3874 self, mock_wait_for_vm
3875 ):
3876 """no_secured_ports has port and the port does not have allow-address-pairs."""
3877 no_secured_ports = [(port2_id, "something")]
3878
3879 self.vimconn._update_port_security_for_vminstance(no_secured_ports, self.server)
3880
3881 mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE")
3882
3883 self.vimconn.neutron.update_port.assert_called_once_with(
3884 port2_id,
3885 {"port": {"port_security_enabled": False, "security_groups": None}},
3886 )
3887
3888 @patch.object(vimconnector, "_vimconnector__wait_for_vm")
3889 def test_update_port_security_for_vm_instance_wait_for_vm_raise_exception(
3890 self, mock_wait_for_vm
3891 ):
3892 """__wait_for_vm raises timeout exception."""
3893 no_secured_ports = [(port2_id, "something")]
3894
3895 mock_wait_for_vm.side_effect = VimConnException("Timeout waiting for instance.")
3896
3897 with self.assertRaises(VimConnException) as err:
3898 self.vimconn._update_port_security_for_vminstance(
3899 no_secured_ports, self.server
3900 )
3901 self.assertEqual(str(err.exception), "Timeout waiting for instance.")
3902
3903 mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE")
3904
3905 self.vimconn.neutron.update_port.assert_not_called()
3906
3907 @patch.object(vimconnector, "_vimconnector__wait_for_vm")
3908 def test_update_port_security_for_vm_instance_neutron_update_port_raise_exception(
3909 self, mock_wait_for_vm
3910 ):
3911 """neutron_update_port method raises exception."""
3912 no_secured_ports = [(port2_id, "something")]
3913
3914 self.vimconn.neutron.update_port.side_effect = Exception(
3915 "Port security could not be updated."
3916 )
3917
3918 with self.assertRaises(VimConnException) as err:
3919 self.vimconn._update_port_security_for_vminstance(
3920 no_secured_ports, self.server
3921 )
3922 self.assertEqual(
3923 str(err.exception),
3924 "It was not possible to disable port security for port 17472685-f67f-49fd-8722-eabb7692fc22",
3925 )
3926 mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE")
3927
3928 self.vimconn.neutron.update_port.assert_called_once_with(
3929 port2_id,
3930 {"port": {"port_security_enabled": False, "security_groups": None}},
3931 )
3932
3933 @patch.object(vimconnector, "_vimconnector__wait_for_vm")
3934 def test_update_port_security_for_vm_instance_empty_port_list(
3935 self, mock_wait_for_vm
3936 ):
3937 """no_secured_ports list does not have any ports."""
3938 no_secured_ports = []
3939
3940 self.vimconn._update_port_security_for_vminstance(no_secured_ports, self.server)
3941
3942 mock_wait_for_vm.assert_not_called()
3943
3944 self.vimconn.neutron.update_port.assert_not_called()
3945
3946 @patch("time.time")
3947 @patch.object(vimconnector, "_reload_connection")
3948 @patch.object(vimconnector, "_prepare_network_for_vminstance")
3949 @patch.object(vimconnector, "_create_user_data")
3950 @patch.object(vimconnector, "_get_vm_availability_zone")
3951 @patch.object(vimconnector, "_prepare_disk_for_vminstance")
3952 @patch.object(vimconnector, "_update_port_security_for_vminstance")
3953 @patch.object(vimconnector, "_prepare_external_network_for_vminstance")
3954 @patch.object(vimconnector, "delete_vminstance")
3955 @patch.object(vimconnector, "_format_exception")
3956 def test_new_vm_instance(
3957 self,
3958 mock_format_exception,
3959 mock_delete_vm_instance,
3960 mock_prepare_external_network,
3961 mock_update_port_security,
3962 mock_prepare_disk_for_vm_instance,
3963 mock_get_vm_availability_zone,
3964 mock_create_user_data,
3965 mock_prepare_network_for_vm_instance,
3966 mock_reload_connection,
3967 mock_time,
3968 ):
3969 """New VM instance creation is successful."""
3970
3971 mock_create_user_data.return_value = True, "userdata"
3972
3973 mock_get_vm_availability_zone.return_value = "nova"
3974
3975 self.vimconn.nova.servers.create.return_value = self.server
3976
3977 mock_time.return_value = time_return_value
3978
3979 expected_result = self.server.id, {}
3980
3981 result = self.vimconn.new_vminstance(
3982 name,
3983 description,
3984 start,
3985 image_id,
3986 flavor_id,
3987 affinity_group_list,
3988 net_list,
3989 cloud_config,
3990 disk_list2,
3991 availability_zone_index,
3992 availability_zone_list,
3993 )
3994 self.assertEqual(result, expected_result)
3995
3996 mock_reload_connection.assert_called_once()
3997 mock_prepare_network_for_vm_instance.assert_called_once_with(
3998 name=name,
3999 net_list=net_list,
4000 created_items={},
4001 net_list_vim=[],
4002 external_network=[],
4003 no_secured_ports=[],
4004 )
4005 mock_create_user_data.assert_called_once_with(cloud_config)
4006 mock_get_vm_availability_zone.assert_called_once_with(
4007 availability_zone_index, availability_zone_list
4008 )
4009 mock_prepare_disk_for_vm_instance.assert_called_once_with(
4010 name=name,
4011 existing_vim_volumes=[],
4012 created_items={},
4013 vm_av_zone="nova",
4014 disk_list=disk_list2,
4015 )
4016 self.vimconn.nova.servers.create.assert_called_once_with(
4017 name=name,
4018 image=image_id,
4019 flavor=flavor_id,
4020 nics=[],
4021 security_groups="default",
4022 availability_zone="nova",
4023 key_name="my_keypair",
4024 userdata="userdata",
4025 config_drive=True,
4026 block_device_mapping=None,
4027 scheduler_hints={},
4028 )
4029 mock_time.assert_called_once()
4030 mock_update_port_security.assert_called_once_with([], self.server)
4031 mock_prepare_external_network.assert_called_once_with(
4032 external_network=[],
4033 server=self.server,
4034 created_items={},
4035 vm_start_time=time_return_value,
4036 )
4037 mock_delete_vm_instance.assert_not_called()
4038 mock_format_exception.assert_not_called()
4039
4040 @patch("time.time")
4041 @patch.object(vimconnector, "_reload_connection")
4042 @patch.object(vimconnector, "_prepare_network_for_vminstance")
4043 @patch.object(vimconnector, "_create_user_data")
4044 @patch.object(vimconnector, "_get_vm_availability_zone")
4045 @patch.object(vimconnector, "_prepare_disk_for_vminstance")
4046 @patch.object(vimconnector, "_update_port_security_for_vminstance")
4047 @patch.object(vimconnector, "_prepare_external_network_for_vminstance")
4048 @patch.object(vimconnector, "delete_vminstance")
4049 @patch.object(vimconnector, "_format_exception")
4050 def test_new_vm_instance_create_user_data_fails(
4051 self,
4052 mock_format_exception,
4053 mock_delete_vm_instance,
4054 mock_prepare_external_network,
4055 mock_update_port_security,
4056 mock_prepare_disk_for_vm_instance,
4057 mock_get_vm_availability_zone,
4058 mock_create_user_data,
4059 mock_prepare_network_for_vm_instance,
4060 mock_reload_connection,
4061 mock_time,
4062 ):
4063 """New VM instance creation failed because of user data creation failure."""
4064
4065 mock_create_user_data.side_effect = Exception(
4066 "User data could not be retrieved."
4067 )
4068
4069 mock_get_vm_availability_zone.return_value = "nova"
4070
4071 self.vimconn.nova.servers.create.return_value = self.server
4072
4073 mock_time.return_value = time_return_value
4074
4075 self.vimconn.new_vminstance(
4076 name,
4077 description,
4078 start,
4079 image_id,
4080 flavor_id,
4081 affinity_group_list,
4082 net_list,
4083 cloud_config,
4084 disk_list,
4085 availability_zone_index,
4086 availability_zone_list,
4087 )
4088
4089 mock_reload_connection.assert_called_once()
4090 mock_prepare_network_for_vm_instance.assert_called_once_with(
4091 name=name,
4092 net_list=net_list,
4093 created_items={},
4094 net_list_vim=[],
4095 external_network=[],
4096 no_secured_ports=[],
4097 )
4098 mock_create_user_data.assert_called_once_with(cloud_config)
4099 mock_get_vm_availability_zone.assert_not_called()
4100 mock_prepare_disk_for_vm_instance.assert_not_called()
4101 self.vimconn.nova.servers.create.assert_not_called()
4102 mock_time.assert_not_called()
4103 mock_update_port_security.assert_not_called()
4104 mock_prepare_external_network.assert_not_called()
4105 mock_delete_vm_instance.assert_called_once_with(None, {})
4106 mock_format_exception.assert_called_once()
4107 arg = mock_format_exception.call_args[0][0]
4108 self.assertEqual(str(arg), "User data could not be retrieved.")
4109
4110 @patch("time.time")
4111 @patch.object(vimconnector, "_reload_connection")
4112 @patch.object(vimconnector, "_prepare_network_for_vminstance")
4113 @patch.object(vimconnector, "_create_user_data")
4114 @patch.object(vimconnector, "_get_vm_availability_zone")
4115 @patch.object(vimconnector, "_prepare_disk_for_vminstance")
4116 @patch.object(vimconnector, "_update_port_security_for_vminstance")
4117 @patch.object(vimconnector, "_prepare_external_network_for_vminstance")
4118 @patch.object(vimconnector, "delete_vminstance")
4119 @patch.object(vimconnector, "_format_exception")
4120 def test_new_vm_instance_external_network_exception(
4121 self,
4122 mock_format_exception,
4123 mock_delete_vm_instance,
4124 mock_prepare_external_network,
4125 mock_update_port_security,
4126 mock_prepare_disk_for_vm_instance,
4127 mock_get_vm_availability_zone,
4128 mock_create_user_data,
4129 mock_prepare_network_for_vm_instance,
4130 mock_reload_connection,
4131 mock_time,
4132 ):
4133 """New VM instance creation, external network connection has failed as floating
4134 ip could not be created."""
4135
4136 mock_create_user_data.return_value = True, "userdata"
4137
4138 mock_get_vm_availability_zone.return_value = "nova"
4139
4140 self.vimconn.nova.servers.create.return_value = self.server
4141
4142 mock_time.return_value = time_return_value
4143
4144 mock_prepare_external_network.side_effect = VimConnException(
4145 "Can not create floating ip."
4146 )
4147
4148 self.vimconn.new_vminstance(
4149 name,
4150 description,
4151 start,
4152 image_id,
4153 flavor_id,
4154 affinity_group_list,
4155 net_list,
4156 cloud_config,
4157 disk_list2,
4158 availability_zone_index,
4159 availability_zone_list,
4160 )
4161
4162 mock_reload_connection.assert_called_once()
4163 mock_prepare_network_for_vm_instance.assert_called_once_with(
4164 name=name,
4165 net_list=net_list,
4166 created_items={},
4167 net_list_vim=[],
4168 external_network=[],
4169 no_secured_ports=[],
4170 )
4171 mock_create_user_data.assert_called_once_with(cloud_config)
4172 mock_get_vm_availability_zone.assert_called_once_with(
4173 availability_zone_index, availability_zone_list
4174 )
4175 mock_prepare_disk_for_vm_instance.assert_called_once_with(
4176 name=name,
4177 existing_vim_volumes=[],
4178 created_items={},
4179 vm_av_zone="nova",
4180 disk_list=disk_list2,
4181 )
4182 self.vimconn.nova.servers.create.assert_called_once_with(
4183 name=name,
4184 image=image_id,
4185 flavor=flavor_id,
4186 nics=[],
4187 security_groups="default",
4188 availability_zone="nova",
4189 key_name="my_keypair",
4190 userdata="userdata",
4191 config_drive=True,
4192 block_device_mapping=None,
4193 scheduler_hints={},
4194 )
4195 mock_time.assert_called_once()
4196 mock_update_port_security.assert_called_once_with([], self.server)
4197 mock_prepare_external_network.assert_called_once_with(
4198 external_network=[],
4199 server=self.server,
4200 created_items={},
4201 vm_start_time=time_return_value,
4202 )
4203 mock_delete_vm_instance.assert_called_once_with(self.server.id, {})
4204 mock_format_exception.assert_called_once()
4205 arg = mock_format_exception.call_args[0][0]
4206 self.assertEqual(str(arg), "Can not create floating ip.")
4207
4208 @patch("time.time")
4209 @patch.object(vimconnector, "_reload_connection")
4210 @patch.object(vimconnector, "_prepare_network_for_vminstance")
4211 @patch.object(vimconnector, "_create_user_data")
4212 @patch.object(vimconnector, "_get_vm_availability_zone")
4213 @patch.object(vimconnector, "_prepare_disk_for_vminstance")
4214 @patch.object(vimconnector, "_update_port_security_for_vminstance")
4215 @patch.object(vimconnector, "_prepare_external_network_for_vminstance")
4216 @patch.object(vimconnector, "delete_vminstance")
4217 @patch.object(vimconnector, "_format_exception")
4218 def test_new_vm_instance_with_affinity_group(
4219 self,
4220 mock_format_exception,
4221 mock_delete_vm_instance,
4222 mock_prepare_external_network,
4223 mock_update_port_security,
4224 mock_prepare_disk_for_vm_instance,
4225 mock_get_vm_availability_zone,
4226 mock_create_user_data,
4227 mock_prepare_network_for_vm_instance,
4228 mock_reload_connection,
4229 mock_time,
4230 ):
4231 """New VM creation with affinity group."""
4232 affinity_group_list = [
4233 {"affinity_group_id": "38b73-e9cc-5a6a-t270-82cc4811bd4a"}
4234 ]
4235 mock_create_user_data.return_value = True, "userdata"
4236 mock_get_vm_availability_zone.return_value = "nova"
4237 self.vimconn.nova.servers.create.return_value = self.server
4238 mock_time.return_value = time_return_value
4239 expected_result = self.server.id, {}
4240
4241 result = self.vimconn.new_vminstance(
4242 name,
4243 description,
4244 start,
4245 image_id,
4246 flavor_id,
4247 affinity_group_list,
4248 net_list,
4249 cloud_config,
4250 disk_list2,
4251 availability_zone_index,
4252 availability_zone_list,
4253 )
4254 self.assertEqual(result, expected_result)
4255
4256 mock_reload_connection.assert_called_once()
4257 mock_prepare_network_for_vm_instance.assert_called_once_with(
4258 name=name,
4259 net_list=net_list,
4260 created_items={},
4261 net_list_vim=[],
4262 external_network=[],
4263 no_secured_ports=[],
4264 )
4265 mock_create_user_data.assert_called_once_with(cloud_config)
4266 mock_get_vm_availability_zone.assert_called_once_with(
4267 availability_zone_index, availability_zone_list
4268 )
4269 mock_prepare_disk_for_vm_instance.assert_called_once_with(
4270 name=name,
4271 existing_vim_volumes=[],
4272 created_items={},
4273 vm_av_zone="nova",
4274 disk_list=disk_list2,
4275 )
4276 self.vimconn.nova.servers.create.assert_called_once_with(
4277 name=name,
4278 image=image_id,
4279 flavor=flavor_id,
4280 nics=[],
4281 security_groups="default",
4282 availability_zone="nova",
4283 key_name="my_keypair",
4284 userdata="userdata",
4285 config_drive=True,
4286 block_device_mapping=None,
4287 scheduler_hints={"group": "38b73-e9cc-5a6a-t270-82cc4811bd4a"},
4288 )
4289 mock_time.assert_called_once()
4290 mock_update_port_security.assert_called_once_with([], self.server)
4291 mock_prepare_external_network.assert_called_once_with(
4292 external_network=[],
4293 server=self.server,
4294 created_items={},
4295 vm_start_time=time_return_value,
4296 )
4297 mock_delete_vm_instance.assert_not_called()
4298 mock_format_exception.assert_not_called()
4299
4300 @patch("time.time")
4301 @patch.object(vimconnector, "_reload_connection")
4302 @patch.object(vimconnector, "_prepare_network_for_vminstance")
4303 @patch.object(vimconnector, "_create_user_data")
4304 @patch.object(vimconnector, "_get_vm_availability_zone")
4305 @patch.object(vimconnector, "_prepare_disk_for_vminstance")
4306 @patch.object(vimconnector, "_update_port_security_for_vminstance")
4307 @patch.object(vimconnector, "_prepare_external_network_for_vminstance")
4308 @patch.object(vimconnector, "delete_vminstance")
4309 @patch.object(vimconnector, "_format_exception")
4310 def test_new_vm_instance_nova_server_create_failed(
4311 self,
4312 mock_format_exception,
4313 mock_delete_vm_instance,
4314 mock_prepare_external_network,
4315 mock_update_port_security,
4316 mock_prepare_disk_for_vm_instance,
4317 mock_get_vm_availability_zone,
4318 mock_create_user_data,
4319 mock_prepare_network_for_vm_instance,
4320 mock_reload_connection,
4321 mock_time,
4322 ):
4323 """New VM(server) creation failed."""
4324
4325 mock_create_user_data.return_value = True, "userdata"
4326
4327 mock_get_vm_availability_zone.return_value = "nova"
4328
4329 self.vimconn.nova.servers.create.side_effect = Exception(
4330 "Server could not be created."
4331 )
4332
4333 mock_time.return_value = time_return_value
4334
4335 self.vimconn.new_vminstance(
4336 name,
4337 description,
4338 start,
4339 image_id,
4340 flavor_id,
4341 affinity_group_list,
4342 net_list,
4343 cloud_config,
4344 disk_list2,
4345 availability_zone_index,
4346 availability_zone_list,
4347 )
4348
4349 mock_reload_connection.assert_called_once()
4350 mock_prepare_network_for_vm_instance.assert_called_once_with(
4351 name=name,
4352 net_list=net_list,
4353 created_items={},
4354 net_list_vim=[],
4355 external_network=[],
4356 no_secured_ports=[],
4357 )
4358 mock_create_user_data.assert_called_once_with(cloud_config)
4359 mock_get_vm_availability_zone.assert_called_once_with(
4360 availability_zone_index, availability_zone_list
4361 )
4362 mock_prepare_disk_for_vm_instance.assert_called_once_with(
4363 name=name,
4364 existing_vim_volumes=[],
4365 created_items={},
4366 vm_av_zone="nova",
4367 disk_list=disk_list2,
4368 )
4369
4370 self.vimconn.nova.servers.create.assert_called_once_with(
4371 name=name,
4372 image=image_id,
4373 flavor=flavor_id,
4374 nics=[],
4375 security_groups="default",
4376 availability_zone="nova",
4377 key_name="my_keypair",
4378 userdata="userdata",
4379 config_drive=True,
4380 block_device_mapping=None,
4381 scheduler_hints={},
4382 )
4383 mock_time.assert_not_called()
4384 mock_update_port_security.assert_not_called()
4385 mock_prepare_external_network.assert_not_called()
4386 mock_delete_vm_instance.assert_called_once_with(None, {})
4387 mock_format_exception.assert_called_once()
4388 arg = mock_format_exception.call_args[0][0]
4389 self.assertEqual(str(arg), "Server could not be created.")
4390
4391 @patch("time.time")
4392 @patch.object(vimconnector, "_reload_connection")
4393 @patch.object(vimconnector, "_prepare_network_for_vminstance")
4394 @patch.object(vimconnector, "_create_user_data")
4395 @patch.object(vimconnector, "_get_vm_availability_zone")
4396 @patch.object(vimconnector, "_prepare_disk_for_vminstance")
4397 @patch.object(vimconnector, "_update_port_security_for_vminstance")
4398 @patch.object(vimconnector, "_prepare_external_network_for_vminstance")
4399 @patch.object(vimconnector, "delete_vminstance")
4400 @patch.object(vimconnector, "_format_exception")
4401 def test_new_vm_instance_connection_exception(
4402 self,
4403 mock_format_exception,
4404 mock_delete_vm_instance,
4405 mock_prepare_external_network,
4406 mock_update_port_security,
4407 mock_prepare_disk_for_vm_instance,
4408 mock_get_vm_availability_zone,
4409 mock_create_user_data,
4410 mock_prepare_network_for_vm_instance,
4411 mock_reload_connection,
4412 mock_time,
4413 ):
4414 """Connection to Cloud API has failed."""
4415 mock_reload_connection.side_effect = Exception("Can not connect to Cloud APIs.")
4416 mock_create_user_data.return_value = True, "userdata"
4417 mock_get_vm_availability_zone.return_value = "nova"
4418 self.vimconn.nova.servers.create.return_value = self.server
4419 mock_time.return_value = time_return_value
4420
4421 self.vimconn.new_vminstance(
4422 name,
4423 description,
4424 start,
4425 image_id,
4426 flavor_id,
4427 affinity_group_list,
4428 net_list,
4429 cloud_config,
4430 disk_list,
4431 availability_zone_index,
4432 availability_zone_list,
4433 )
4434 mock_format_exception.assert_called_once()
4435 arg = mock_format_exception.call_args[0][0]
4436 self.assertEqual(str(arg), "Can not connect to Cloud APIs.")
4437 mock_reload_connection.assert_called_once()
4438 mock_prepare_network_for_vm_instance.assert_not_called()
4439 mock_create_user_data.assert_not_called()
4440 mock_get_vm_availability_zone.assert_not_called()
4441 mock_prepare_disk_for_vm_instance.assert_not_called()
4442 self.vimconn.nova.servers.create.assert_not_called()
4443 mock_time.assert_not_called()
4444 mock_update_port_security.assert_not_called()
4445 mock_prepare_external_network.assert_not_called()
4446 mock_delete_vm_instance.assert_called_once_with(None, {})
4447
4448
4449 if __name__ == "__main__":
4450 unittest.main()