1 # -*- coding: utf-8 -*-
4 # Copyright 2017 Intel Corporation.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
23 This module contains unit tests for the OpenStack VIM connector
24 Run this directly with python2 or python3.
27 from copy
import deepcopy
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
38 __author__
= "Igor D.C."
39 __date__
= "$23-aug-2017 23:59:59$"
41 # Variables Used in TestNewVmInstance Class
43 description
= "my firewall"
45 image_id
= "408b73-e9cc-5a6a-t270-82cc4811bd4a"
46 flavor_id
= "208b73-e9cc-5a6a-t270-82cc4811bd4a"
47 affinity_group_list
= []
52 {"size": 10, "image_id": image_id
},
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"
66 volume_id
= "ac408b73-b9cc-4a6a-a270-82cc4811bd4a"
67 volume_id2
= "o4e0e83-b9uu-4akk-a234-89cc4811bd4a"
68 volume_id3
= "44e0e83-t9uu-4akk-a234-p9cc4811bd4a"
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(
94 ingress_ports
=["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
95 egress_ports
=["230cdf1b-de37-4891-bc07-f9010cf1f967"],
97 # input to VIM connector
101 # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
103 if sfc_encap
is not None:
107 # what OpenStack is assumed to respond (patch OpenStack"s return value)
108 dict_from_neutron
= {
110 "id": "3d7ddc13-923c-4332-971e-708ed82902ce",
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
},
120 create_sfc_port_pair
.return_value
= dict_from_neutron
122 # what the VIM connector is expected to
123 # send to OpenStack based on the input
127 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
128 "egress": "230cdf1b-de37-4891-bc07-f9010cf1f967",
129 "service_function_parameters": {"correlation": correlation
},
133 # call the VIM connector
134 if sfc_encap
is None:
135 result
= self
.vimconn
.new_sfi(name
, ingress_ports
, egress_ports
)
137 result
= self
.vimconn
.new_sfi(name
, ingress_ports
, egress_ports
, sfc_encap
)
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"])
144 def _test_new_sf(self
, create_sfc_port_pair_group
):
145 # input to VIM connector
148 "bbd01220-cf72-41f2-9e70-0669c2e5c4cd",
149 "12ba215e-3987-4892-bd3a-d0fd91eecf98",
150 "e25a7c79-14c8-469a-9ae1-f601c9371ffd",
153 # what OpenStack is assumed to respond (patch OpenStack"s return value)
154 dict_from_neutron
= {
156 "id": "3d7ddc13-923c-4332-971e-708ed82902ce",
159 "tenant_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
160 "project_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
161 "port_pairs": instances
,
163 "port_pair_group_parameters": {
165 "ppg_n_tuple_mapping": {
166 "ingress_n_tuple": {},
167 "egress_n_tuple": {},
172 create_sfc_port_pair_group
.return_value
= dict_from_neutron
174 # what the VIM connector is expected to
175 # send to OpenStack based on the input
180 "bbd01220-cf72-41f2-9e70-0669c2e5c4cd",
181 "12ba215e-3987-4892-bd3a-d0fd91eecf98",
182 "e25a7c79-14c8-469a-9ae1-f601c9371ffd",
187 # call the VIM connector
188 result
= self
.vimconn
.new_sf(name
, instances
)
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"])
195 def _test_new_sfp(self
, create_sfc_port_chain
, sfc_encap
, spi
):
196 # input to VIM connector
199 "2bd2a2e5-c5fd-4eac-a297-d5e255c35c19",
200 "00f23389-bdfa-43c2-8b16-5815f2582fa8",
203 "2314daec-c262-414a-86e3-69bb6fa5bc16",
204 "d8bfdb5d-195e-4f34-81aa-6135705317df",
207 # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
213 # what OpenStack is assumed to respond (patch OpenStack"s return value)
214 dict_from_neutron
= {
216 "id": "5bc05721-079b-4b6e-a235-47cac331cbb6",
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
},
227 create_sfc_port_chain
.return_value
= dict_from_neutron
229 # what the VIM connector is expected to
230 # send to OpenStack based on the input
234 "flow_classifiers": [
235 "2bd2a2e5-c5fd-4eac-a297-d5e255c35c19",
236 "00f23389-bdfa-43c2-8b16-5815f2582fa8",
238 "port_pair_groups": [
239 "2314daec-c262-414a-86e3-69bb6fa5bc16",
240 "d8bfdb5d-195e-4f34-81aa-6135705317df",
242 "chain_parameters": {"correlation": correlation
},
246 dict_to_neutron
["port_chain"]["chain_id"] = spi
248 # call the VIM connector
249 if sfc_encap
is None:
250 dict_to_neutron
["port_chain"]["chain_parameters"] = {"correlation": "mpls"}
252 result
= self
.vimconn
.new_sfp(
253 name
, classifications
, sfs
, sfc_encap
=False
256 result
= self
.vimconn
.new_sfp(
257 name
, classifications
, sfs
, sfc_encap
=False, spi
=spi
261 result
= self
.vimconn
.new_sfp(name
, classifications
, sfs
, sfc_encap
)
263 result
= self
.vimconn
.new_sfp(
264 name
, classifications
, sfs
, sfc_encap
, spi
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"])
272 def _test_new_classification(self
, create_sfc_flow_classifier
, ctype
):
273 # input to VIM connector
274 name
= "osm_classification"
277 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
279 "source_ip_prefix": "192.168.2.0/24",
280 "source_port_range_max": 99,
281 "source_port_range_min": 50,
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"][
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"][
293 ] = "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c"
294 dict_from_neutron
["flow_classifier"][
296 ] = "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c"
297 create_sfc_flow_classifier
.return_value
= dict_from_neutron
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"
304 # call the VIM connector
305 result
= self
.vimconn
.new_classification(name
, ctype
, definition
)
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"])
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"
318 @mock.patch
.object(Client
, "create_sfc_flow_classifier")
319 def test_new_classification_unsupported_type(self
, create_sfc_flow_classifier
):
321 vimconn
.VimConnNotSupportedException
,
322 self
._test
_new
_classification
,
323 create_sfc_flow_classifier
,
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)
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)
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)
339 @mock.patch
.object(Client
, "create_sfc_port_pair")
340 def test_new_sfi_bad_ingress_ports(self
, create_sfc_port_pair
):
342 "5311c75d-d718-4369-bbda-cdcc6da60fcc",
343 "a0273f64-82c9-11e7-b08f-6328e53f0fa7",
346 vimconn
.VimConnNotSupportedException
,
348 create_sfc_port_pair
,
350 ingress_ports
=ingress_ports
,
354 vimconn
.VimConnNotSupportedException
,
356 create_sfc_port_pair
,
358 ingress_ports
=ingress_ports
,
361 @mock.patch
.object(Client
, "create_sfc_port_pair")
362 def test_new_sfi_bad_egress_ports(self
, create_sfc_port_pair
):
364 "230cdf1b-de37-4891-bc07-f9010cf1f967",
365 "b41228fe-82c9-11e7-9b44-17504174320b",
368 vimconn
.VimConnNotSupportedException
,
370 create_sfc_port_pair
,
372 egress_ports
=egress_ports
,
376 vimconn
.VimConnNotSupportedException
,
378 create_sfc_port_pair
,
380 egress_ports
=egress_ports
,
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
)
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"}
394 vimconn
.VimConnNotSupportedException
,
396 create_sfc_port_pair_group
,
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)
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)
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)
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)
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)
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": [
426 "source_port_range_min": 2000,
427 "destination_ip_prefix": "192.168.3.0/24",
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",
446 # call the VIM connector
447 filter_dict
= {"protocol": "tcp", "ethertype": "IPv4"}
448 result
= self
.vimconn
.get_classification_list(filter_dict
.copy())
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
458 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
461 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
462 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
463 "ctype": "legacy_flow_classifier",
465 "source_port_range_min": 2000,
466 "destination_ip_prefix": "192.168.3.0/24",
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",
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
= {
486 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
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",
498 # call the VIM connector
499 filter_dict
= {"name": "osm_sfi", "description": ""}
500 result
= self
.vimconn
.get_sfi_list(filter_dict
.copy())
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
510 "ingress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
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",
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)
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)
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": [
537 "08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2",
538 "0d63799c-82d6-11e7-8deb-a746bb3ae9f5",
541 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
542 "port_pair_group_parameters": {},
543 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
544 "id": "f4a0bde8-82d5-11e7-90e1-a72b762fa27f",
550 # call the VIM connector
551 filter_dict
= {"name": "osm_sf", "description": ""}
552 result
= self
.vimconn
.get_sf_list(filter_dict
.copy())
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
563 "08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2",
564 "0d63799c-82d6-11e7-8deb-a746bb3ae9f5",
567 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
568 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
569 "id": "f4a0bde8-82d5-11e7-90e1-a72b762fa27f",
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
= {
580 "port_pair_groups": [
581 "7d8e3bf8-82d6-11e7-a032-8ff028839d25",
582 "7dc9013e-82d6-11e7-a5a6-a3a8d78a5518",
584 "flow_classifiers": [
585 "1333c2f4-82d7-11e7-a5df-9327f33d104e",
586 "1387ab44-82d7-11e7-9bb0-476337183905",
589 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
590 "chain_parameters": {"correlation": correlation
},
592 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
593 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
599 # call the VIM connector
600 filter_dict
= {"name": "osm_sfp", "description": ""}
601 result
= self
.vimconn
.get_sfp_list(filter_dict
.copy())
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
611 "service_functions": [
612 "7d8e3bf8-82d6-11e7-a032-8ff028839d25",
613 "7dc9013e-82d6-11e7-a5a6-a3a8d78a5518",
616 "1333c2f4-82d7-11e7-a5df-9327f33d104e",
617 "1387ab44-82d7-11e7-9bb0-476337183905",
620 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
621 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
622 "sfc_encap": sfc_encap
,
624 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
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)
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)
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": [
644 "source_port_range_min": 2000,
645 "destination_ip_prefix": "192.168.3.0/24",
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",
664 # call the VIM connector
665 result
= self
.vimconn
.get_classification("22198366-d4e8-4d6b-b4d2-637d5d6cbb7d")
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"
671 # assert that VIM connector successfully returned the OpenStack result
675 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
678 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
679 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
680 "ctype": "legacy_flow_classifier",
682 "source_port_range_min": 2000,
683 "destination_ip_prefix": "192.168.3.0/24",
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",
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": [
703 "source_port_range_min": 2000,
704 "destination_ip_prefix": "192.168.3.0/24",
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",
721 "source_port_range_min": 1000,
722 "destination_ip_prefix": "192.168.3.0/24",
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",
741 # call the VIM connector
743 vimconn
.VimConnConflictException
,
744 self
.vimconn
.get_classification
,
745 "3196bafc-82dd-11e7-a205-9bf6c14b0721",
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"
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": []}
758 # call the VIM connector
760 vimconn
.VimConnNotFoundException
,
761 self
.vimconn
.get_classification
,
762 "3196bafc-82dd-11e7-a205-9bf6c14b0721",
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"
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
= {
776 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
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",
788 # call the VIM connector
789 result
= self
.vimconn
.get_sfi("c121ebdd-7f2d-4213-b933-3325298a6966")
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"
795 # assert the VIM connector successfully returned the OpenStack result
799 "ingress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
800 "egress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
803 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
804 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
805 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
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
= {
816 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
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",
826 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
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",
838 # call the VIM connector
840 vimconn
.VimConnConflictException
,
841 self
.vimconn
.get_sfi
,
842 "c0436d92-82db-11e7-8f9c-5fa535f1261f",
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"
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": []}
855 # call the VIM connector
857 vimconn
.VimConnNotFoundException
,
858 self
.vimconn
.get_sfi
,
859 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
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"
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": [
873 "port_pairs": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
875 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
876 "port_pair_group_parameters": {},
877 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
878 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
884 # call the VIM connector
885 result
= self
.vimconn
.get_sf("b22892fc-82d9-11e7-ae85-0fea6a3b3757")
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"
891 # assert that VIM connector successfully returned the OpenStack result
896 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
897 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
898 "sfis": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
899 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
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": [
910 "port_pairs": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
912 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
913 "port_pair_group_parameters": {},
914 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
915 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
919 "port_pairs": ["0d63799c-82d6-11e7-8deb-a746bb3ae9f5"],
921 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
922 "port_pair_group_parameters": {},
923 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
924 "id": "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
930 # call the VIM connector
932 vimconn
.VimConnConflictException
,
934 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
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"
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": []}
947 # call the VIM connector
949 vimconn
.VimConnNotFoundException
,
951 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
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"
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
= {
965 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
966 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
968 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
969 "chain_parameters": {"correlation": "nsh"},
971 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
972 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
978 # call the VIM connector
979 result
= self
.vimconn
.get_sfp("821bc9be-82d7-11e7-8ce3-23a08a27ab47")
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"
985 # assert that VIM connector successfully returned the OpenStack result
989 "service_functions": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
990 "classifications": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
992 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
993 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
996 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
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
= {
1007 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
1008 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
1010 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1011 "chain_parameters": {"correlation": "nsh"},
1013 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1014 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
1018 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
1019 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
1021 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1022 "chain_parameters": {"correlation": "nsh"},
1024 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
1025 "id": "5d002f38-82de-11e7-a770-f303f11ce66a",
1031 # call the VIM connector
1033 vimconn
.VimConnConflictException
,
1034 self
.vimconn
.get_sfp
,
1035 "5d002f38-82de-11e7-a770-f303f11ce66a",
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"
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": []}
1048 # call the VIM connector
1050 vimconn
.VimConnNotFoundException
,
1051 self
.vimconn
.get_sfp
,
1052 "5d002f38-82de-11e7-a770-f303f11ce66a",
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"
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"
1065 delete_sfc_flow_classifier
.assert_called_with(
1066 "638f957c-82df-11e7-b7c8-132706021464"
1068 self
.assertEqual(result
, "638f957c-82df-11e7-b7c8-132706021464")
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")
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"
1082 self
.assertEqual(result
, "638f957c-82df-11e7-b7c8-132706021464")
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")
1092 def __init__(self
, s
):
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
)
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(
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"
1135 @patch.object(vimconnector
, "_get_ids_from_name")
1136 def test_prepare_port_dict_security_security_groups_exists_in_config(
1139 """In VIM config security_groups exists, net port_security is True
1140 no_port_security_extension does not exist.
1142 self
.vimconn
.config
= {"security_groups": "example_security_group"}
1143 net
= {"port_security": True}
1145 result_dict
= {"security_groups": "12345"}
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()
1151 @patch.object(vimconnector
, "_get_ids_from_name")
1152 def test_prepare_port_dict_security_security_groups_exists_in_config_no_security_groups_id(
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.
1158 self
.vimconn
.config
= {"security_groups": "example_security_group"}
1159 self
.vimconn
.security_groups_id
= None
1160 net
= {"port_security": True}
1162 result_dict
= {"security_groups": None}
1164 self
.vimconn
._prepare
_port
_dict
_security
_groups
(net
, port_dict
)
1165 self
.assertDictEqual(result_dict
, port_dict
)
1166 mock_get_ids
.assert_called()
1168 @patch.object(vimconnector
, "_get_ids_from_name")
1169 def test_prepare_port_dict_security_security_groups_exists_security_extension_true_in_config(
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.
1175 self
.vimconn
.config
= {
1176 "security_groups": "example_security_group",
1177 "no_port_security_extension": True,
1179 net
= {"port_security": True}
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()
1187 @patch.object(vimconnector
, "_get_ids_from_name")
1188 def test_prepare_port_dict_security_no_security_groups_in_config(
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}
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()
1202 @patch.object(vimconnector
, "_get_ids_from_name")
1203 def test_prepare_port_dict_security_no_security_groups_security_extension_true_in_config(
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}
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()
1217 @patch.object(vimconnector
, "_get_ids_from_name")
1218 def test_prepare_port_dict_security_security_groups_exists_net_port_security_false(
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}
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()
1232 @patch.object(vimconnector
, "_get_ids_from_name")
1233 def test_prepare_port_dict_security_net_port_security_false_port_security_extension_true(
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,
1242 net
= {"port_security": False}
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()
1250 def test_prepare_port_dict_binding_net_type_virtual(self
):
1251 """net type is virtual."""
1252 net
= {"type": "virtual"}
1255 self
.vimconn
._prepare
_port
_dict
_binding
(net
, port_dict
)
1256 self
.assertDictEqual(result_dict
, port_dict
)
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
1263 result_dict
= {"binding:vnic_type": "direct"}
1264 self
.vimconn
._prepare
_port
_dict
_binding
(net
, port_dict
)
1265 self
.assertDictEqual(port_dict
, result_dict
)
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"
1273 "binding:vnic_type": "direct",
1274 "port_security_enabled": False,
1275 "provider_security_groups": [],
1276 "security_groups": [],
1278 self
.vimconn
._prepare
_port
_dict
_binding
(net
, port_dict
)
1279 self
.assertDictEqual(port_dict
, result_dict
)
1281 def test_prepare_port_dict_binding_net_type_passthrough(self
):
1282 """net type is pci-passthrough."""
1283 net
= {"type": "PCI-PASSTHROUGH"}
1286 "binding:vnic_type": "direct-physical",
1288 self
.vimconn
._prepare
_port
_dict
_binding
(net
, port_dict
)
1289 self
.assertDictEqual(port_dict
, result_dict
)
1291 def test_prepare_port_dict_binding_no_net_type(self
):
1292 """net type is missing."""
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.")
1299 def test_set_fixed_ip(self
):
1300 """new_port has fixed ip."""
1304 "fixed_ips": [{"ip_address": "10.1.2.3"}, {"ip_address": "20.1.2.3"}]
1307 result
= {"ip": "10.1.2.3"}
1308 self
.vimconn
._set
_fixed
_ip
(new_port
, net
)
1309 self
.assertDictEqual(net
, result
)
1311 def test_set_fixed_ip_no_fixed_ip(self
):
1312 """new_port does not have fixed ip."""
1314 new_port
= {"port": {}}
1315 result
= {"ip": None}
1316 self
.vimconn
._set
_fixed
_ip
(new_port
, net
)
1317 self
.assertDictEqual(net
, result
)
1319 def test_set_fixed_ip_raise_exception(self
):
1320 """new_port does not have port details."""
1323 with self
.assertRaises(Exception) as err
:
1324 self
.vimconn
._set
_fixed
_ip
(new_port
, net
)
1325 self
.assertEqual(type(err
.exception
), KeyError)
1327 def test_prepare_port_dict_mac_ip_addr(self
):
1328 """mac address and ip address exist."""
1330 "mac_address": mac_address
,
1331 "ip_address": "10.0.1.5",
1335 "mac_address": mac_address
,
1336 "fixed_ips": [{"ip_address": "10.0.1.5"}],
1338 self
.vimconn
._prepare
_port
_dict
_mac
_ip
_addr
(net
, port_dict
)
1339 self
.assertDictEqual(port_dict
, result_dict
)
1341 def test_prepare_port_dict_mac_ip_addr_no_mac_and_ip(self
):
1342 """mac address and ip address does not exist."""
1346 self
.vimconn
._prepare
_port
_dict
_mac
_ip
_addr
(net
, port_dict
)
1347 self
.assertDictEqual(port_dict
, result_dict
)
1349 def test_create_new_port(self
):
1350 """new port has id and mac address."""
1354 "mac_address": mac_address
,
1357 self
.vimconn
.neutron
.create_port
.return_value
= new_port
1358 net
, port_dict
, created_items
= {}, {}, {}
1359 expected_result
= new_port
1361 "mac_adress": mac_address
,
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
})
1371 def test_create_new_port_without_mac_or_id(self
):
1372 """new port does not have mac address or ID."""
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
})
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."
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
})
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(
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
,
1403 """Net has name, type, net-id."""
1407 "name": "management",
1414 "mac_address": mac_address
,
1415 "name": "management",
1416 "fixed_ips": [{"ip_address": ip_addr1
}],
1419 mock_create_new_port
.return_value
= new_port
1422 "tag": "management",
1425 "network_id": net_id
,
1426 "name": "management",
1427 "admin_state_up": True,
1430 new_port_result
, port_result
= self
.vimconn
._create
_port
(
1431 net
, name
, created_items
1434 self
.assertDictEqual(new_port_result
, new_port
)
1435 self
.assertDictEqual(port_result
, expected_port
)
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
)
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(
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
,
1456 """Net has no name."""
1465 "mac_address": mac_address
,
1467 "fixed_ips": [{"ip_address": ip_addr1
}],
1470 mock_create_new_port
.return_value
= new_port
1476 "network_id": net_id
,
1477 "admin_state_up": True,
1481 new_port_result
, port_result
= self
.vimconn
._create
_port
(
1482 net
, name
, created_items
1485 self
.assertDictEqual(new_port_result
, new_port
)
1486 self
.assertDictEqual(port_result
, expected_port
)
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
)
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(
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
,
1507 """Nova api version is smaller than 2.32."""
1508 self
.vimconn
.nova
.api_version
.get_string
.return_value
= "2.30"
1517 "mac_address": mac_address
,
1519 "fixed_ips": [{"ip_address": ip_addr1
}],
1522 mock_create_new_port
.return_value
= new_port
1527 "network_id": net_id
,
1528 "admin_state_up": True,
1532 new_port_result
, port_result
= self
.vimconn
._create
_port
(
1533 net
, name
, created_items
1536 self
.assertDictEqual(new_port_result
, new_port
)
1537 self
.assertDictEqual(port_result
, expected_port
)
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
)
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(
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
,
1558 """_create_new_port method raises exception."""
1564 mock_create_new_port
.side_effect
= Exception
1566 "network_id": net_id
,
1567 "admin_state_up": True,
1571 with self
.assertRaises(Exception):
1572 self
.vimconn
._create
_port
(net
, name
, created_items
)
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()
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(
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
,
1593 """_prepare_port_dict_security_groups method raises exception."""
1599 mock_prepare_port_dict_security_groups
.side_effect
= Exception
1601 "network_id": net_id
,
1602 "admin_state_up": True,
1606 with self
.assertRaises(Exception):
1607 self
.vimconn
._create
_port
(net
, name
, created_items
)
1609 mock_prepare_port_dict_security_groups
.assert_called_once_with(net
, port_dict
)
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()
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(
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
,
1629 """_prepare_port_dict_binding method raises exception."""
1636 mock_prepare_port_dict_binding
.side_effect
= Exception
1638 "network_id": net_id
,
1639 "admin_state_up": True,
1643 with self
.assertRaises(Exception):
1644 self
.vimconn
._create
_port
(net
, name
, created_items
)
1646 mock_prepare_port_dict_security_groups
.assert_called_once_with(net
, port_dict
)
1648 mock_prepare_port_dict_binding
.assert_called_once_with(net
, port_dict
)
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()
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(
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
,
1667 """prepare_port_dict_mac_ip_addr method raises exception."""
1673 mock_prepare_port_dict_mac_ip_addr
.side_effect
= Exception
1675 "network_id": net_id
,
1676 "admin_state_up": True,
1680 with self
.assertRaises(Exception):
1681 self
.vimconn
._create
_port
(net
, name
, created_items
)
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
)
1687 mock_create_new_port
.assert_not_called()
1688 mock_set_fixed_ip
.assert_not_called()
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(
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
,
1703 """_set_fixed_ip method raises exception."""
1709 mock_set_fixed_ip
.side_effect
= VimConnException(
1710 "Port detail is missing in new_port."
1713 "network_id": net_id
,
1714 "admin_state_up": True,
1720 "mac_address": mac_address
,
1722 "fixed_ips": [{"ip_address": ip_addr1
}],
1725 mock_create_new_port
.return_value
= new_port
1727 with self
.assertRaises(VimConnException
):
1728 self
.vimconn
._create
_port
(net
, name
, created_items
)
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
)
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
1741 """Nets do not have net_id"""
1742 mock_reload_connection
.side_effect
= None
1747 "port_security": False,
1748 "exit_on_floating_ip_error": False,
1749 "port_security_disable_strategy": "full",
1752 "port_security": True,
1753 "exit_on_floating_ip_error": False,
1754 "floating_ip": True,
1758 external_network
, no_secured_ports
= [], []
1759 expected_external_network
, expected_no_secured_ports
= [], []
1760 expected_net_list_vim
= []
1762 self
.vimconn
._prepare
_network
_for
_vminstance
(
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
)
1774 mock_create_port
.assert_not_called()
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
1781 """Net list is empty."""
1782 mock_reload_connection
.side_effect
= None
1785 external_network
, no_secured_ports
= [], []
1786 expected_external_network
, expected_no_secured_ports
= [], []
1787 expected_net_list_vim
= []
1789 self
.vimconn
._prepare
_network
_for
_vminstance
(
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
)
1801 mock_create_port
.assert_not_called()
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
1808 """Nets have net-id, floating_ip False, mgmt network."""
1809 mock_reload_connection
.side_effect
= None
1814 "floating_ip": False,
1819 mock_create_port
.side_effect
= [
1824 "mac_address": mac_address
,
1828 {"port-dict": port2_id
},
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
(
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
)
1846 mock_create_port
.assert_called_once_with(
1849 "floating_ip": False,
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
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
1868 "floating_ip": True,
1873 mock_create_port
.side_effect
= [
1878 "mac_address": mac_address
,
1882 {"port-dict": port2_id
},
1885 external_network
, no_secured_ports
= [], []
1886 expected_external_network
= [
1889 "floating_ip": True,
1891 "exit_on_floating_ip_error": True,
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
(
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
)
1909 mock_create_port
.assert_called_once_with(
1912 "floating_ip": True,
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
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
1934 "port_security": False,
1935 "exit_on_floating_ip_error": False,
1936 "port_security_disable_strategy": "full",
1940 mock_create_port
.side_effect
= [
1945 "mac_address": mac_address
,
1949 {"port-dict": port2_id
},
1952 external_network
, no_secured_ports
= [], []
1953 expected_external_network
= [
1957 "port_security": False,
1958 "exit_on_floating_ip_error": False,
1959 "port_security_disable_strategy": "full",
1960 "floating_ip": True,
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
(
1975 mock_create_port
.assert_called_once_with(
1979 "port_security": False,
1980 "exit_on_floating_ip_error": False,
1981 "port_security_disable_strategy": "full",
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
)
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
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
2005 "port_security": False,
2006 "port_security_disable_strategy": "full",
2010 mock_create_port
.side_effect
= [
2015 "mac_address": mac_address
,
2019 {"port-dict": port2_id
},
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
(
2036 mock_create_port
.assert_called_once_with(
2040 "port_security": False,
2041 "port_security_disable_strategy": "full",
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
)
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
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
2065 "port_security": True,
2066 "port_security_disable_strategy": "full",
2070 mock_create_port
.side_effect
= [
2075 "mac_address": mac_address
,
2079 {"port-dict": port2_id
},
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
(
2096 mock_create_port
.assert_called_once_with(
2100 "port_security": True,
2101 "port_security_disable_strategy": "full",
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
)
2110 @patch.object(vimconnector
, "_reload_connection")
2111 def test_prepare_network_for_vm_instance_create_port_raise_exception(
2112 self
, mock_reload_connection
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
2125 "port_security": True,
2126 "port_security_disable_strategy": "full",
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
(
2146 self
.assertEqual(type(err
.exception
), KeyError)
2148 mock_create_port
.assert_called_once_with(
2152 "port_security": True,
2153 "port_security_disable_strategy": "full",
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
)
2162 @patch.object(vimconnector
, "_reload_connection")
2163 def test_prepare_network_for_vm_instance_reload_connection_raise_exception(
2164 self
, mock_reload_connection
2166 """_reload_connection method raises exception."""
2167 mock_create_port
= CopyingMock()
2168 mock_reload_connection
.side_effect
= VimConnConnectionException(
2169 "Connection failed."
2171 self
.vimconn
.config
["use_floating_ip"] = True
2172 self
.vimconn
.config
["no_port_security_extension"] = True
2179 "port_security": True,
2180 "port_security_disable_strategy": "full",
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
(
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
)
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
= []
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
(
2224 block_device_mapping
,
2225 existing_vim_volumes
,
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()
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
= []
2241 expected_block_device_mapping
= {"vdb": volume_id
}
2242 expected_existing_vim_volumes
= [{"id": volume_id
}]
2243 self
.vimconn
._prepare
_non
_root
_persistent
_volumes
(
2247 block_device_mapping
,
2249 existing_vim_volumes
,
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()
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
= []
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
(
2272 block_device_mapping
,
2273 existing_vim_volumes
,
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()
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
= []
2290 expected_block_device_mapping
= {"vdb": volume_id
}
2291 expected_existing_vim_volumes
= [{"id": volume_id
}]
2292 self
.vimconn
._prepare
_non
_root
_persistent
_volumes
(
2296 block_device_mapping
,
2298 existing_vim_volumes
,
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()
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
= []
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
(
2323 block_device_mapping
,
2324 existing_vim_volumes
,
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(
2334 availability_zone
=["nova"],
2336 self
.assertEqual(created_items
, {f
"volume:{volume_id2}": True})
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")
2345 block_device_mapping
= {}
2346 existing_vim_volumes
= []
2348 expected_block_device_mapping
= {"vda": volume_id2
}
2349 expected_existing_vim_volumes
= []
2350 self
.vimconn
._prepare
_non
_root
_persistent
_volumes
(
2354 block_device_mapping
,
2356 existing_vim_volumes
,
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"]
2365 self
.assertEqual(created_items
, {f
"volume:{volume_id2}": True})
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
= []
2377 with self
.assertRaises(Exception):
2378 result
= self
.vimconn
._prepare
_persistent
_root
_volumes
(
2383 block_device_mapping
,
2384 existing_vim_volumes
,
2388 self
.assertEqual(result
, None)
2390 self
.vimconn
.cinder
.volumes
.create
.assert_called_once_with(
2394 availability_zone
=["nova"],
2396 self
.assertEqual(existing_vim_volumes
, [])
2397 self
.assertEqual(block_device_mapping
, {})
2398 self
.assertEqual(created_items
, {})
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")
2406 block_device_mapping
= {}
2407 existing_vim_volumes
= []
2410 with self
.assertRaises(Exception):
2411 self
.vimconn
._prepare
_non
_root
_persistent
_volumes
(
2415 block_device_mapping
,
2417 existing_vim_volumes
,
2421 self
.vimconn
.cinder
.volumes
.create
.assert_called_once_with(
2422 size
=10, name
="basicvmvdb", availability_zone
=["nova"]
2424 self
.assertEqual(existing_vim_volumes
, [])
2425 self
.assertEqual(block_device_mapping
, {})
2426 self
.assertEqual(created_items
, {})
2428 @patch("time.sleep")
2429 def test_wait_for_created_volumes_availability_volume_status_available(
2432 """Created volume status is available."""
2434 created_items
= {f
"volume:{volume_id2}": True}
2435 self
.vimconn
.cinder
.volumes
.get
.return_value
.status
= "available"
2437 result
= self
.vimconn
._wait
_for
_created
_volumes
_availability
(
2438 elapsed_time
, created_items
2440 self
.assertEqual(result
, elapsed_time
)
2441 self
.vimconn
.cinder
.volumes
.get
.assert_called_with(volume_id2
)
2442 mock_sleep
.assert_not_called()
2444 @patch("time.sleep")
2445 def test_wait_for_existing_volumes_availability_volume_status_available(
2448 """Existing volume status is available."""
2450 existing_vim_volumes
= [{"id": volume_id2
}]
2451 self
.vimconn
.cinder
.volumes
.get
.return_value
.status
= "available"
2453 result
= self
.vimconn
._wait
_for
_existing
_volumes
_availability
(
2454 elapsed_time
, existing_vim_volumes
2456 self
.assertEqual(result
, elapsed_time
)
2457 self
.vimconn
.cinder
.volumes
.get
.assert_called_with(volume_id2
)
2458 mock_sleep
.assert_not_called()
2460 @patch("time.sleep")
2461 def test_wait_for_created_volumes_availability_status_processing_multiple_volumes(
2464 """Created volume status is processing."""
2467 f
"volume:{volume_id2}": True,
2468 f
"volume:{volume_id3}": True,
2470 self
.vimconn
.cinder
.volumes
.get
.side_effect
= [
2471 Status("processing"),
2472 Status("available"),
2473 Status("available"),
2476 result
= self
.vimconn
._wait
_for
_created
_volumes
_availability
(
2477 elapsed_time
, created_items
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
)
2487 @patch("time.sleep")
2488 def test_wait_for_existing_volumes_availability_status_processing_multiple_volumes(
2491 """Existing volume status is processing."""
2493 existing_vim_volumes
= [
2495 {"id": "44e0e83-b9uu-4akk-t234-p9cc4811bd4a"},
2497 self
.vimconn
.cinder
.volumes
.get
.side_effect
= [
2498 Status("processing"),
2499 Status("available"),
2500 Status("available"),
2503 result
= self
.vimconn
._wait
_for
_existing
_volumes
_availability
(
2504 elapsed_time
, existing_vim_volumes
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
,))
2511 _call_mock_get_volumes
[2][0], ("44e0e83-b9uu-4akk-t234-p9cc4811bd4a",)
2513 mock_sleep
.assert_called_with(5)
2514 self
.assertEqual(1, mock_sleep
.call_count
)
2516 @patch("time.sleep")
2517 def test_wait_for_created_volumes_availability_volume_status_processing_timeout(
2520 """Created volume status is processing, elapsed time greater than timeout (1800)."""
2522 created_items
= {f
"volume:{volume_id2}": True}
2523 self
.vimconn
.cinder
.volumes
.get
.side_effect
= [
2524 Status("processing"),
2525 Status("processing"),
2527 with
patch("time.sleep", mock_sleep
):
2528 result
= self
.vimconn
._wait
_for
_created
_volumes
_availability
(
2529 elapsed_time
, created_items
2531 self
.assertEqual(result
, 1805)
2532 self
.vimconn
.cinder
.volumes
.get
.assert_not_called()
2533 mock_sleep
.assert_not_called()
2535 @patch("time.sleep")
2536 def test_wait_for_existing_volumes_availability_volume_status_processing_timeout(
2539 """Exsiting volume status is processing, elapsed time greater than timeout (1800)."""
2541 existing_vim_volumes
= [{"id": volume_id2
}]
2542 self
.vimconn
.cinder
.volumes
.get
.side_effect
= [
2543 Status("processing"),
2544 Status("processing"),
2547 result
= self
.vimconn
._wait
_for
_existing
_volumes
_availability
(
2548 elapsed_time
, existing_vim_volumes
2550 self
.assertEqual(result
, 1805)
2551 self
.vimconn
.cinder
.volumes
.get
.assert_not_called()
2552 mock_sleep
.assert_not_called()
2554 @patch("time.sleep")
2555 def test_wait_for_created_volumes_availability_cinder_raise_exception(
2558 """Cinder get volumes raises exception for created volumes."""
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
2566 self
.assertEqual(result
, 1000)
2567 self
.vimconn
.cinder
.volumes
.get
.assert_called_with(volume_id2
)
2568 mock_sleep
.assert_not_called()
2570 @patch("time.sleep")
2571 def test_wait_for_existing_volumes_availability_cinder_raise_exception(
2574 """Cinder get volumes raises exception for existing volumes."""
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
2582 self
.assertEqual(result
, 1000)
2583 self
.vimconn
.cinder
.volumes
.get
.assert_called_with(volume_id2
)
2584 mock_sleep
.assert_not_called()
2586 @patch("time.sleep")
2587 def test_wait_for_created_volumes_availability_no_volume_in_created_items(
2590 """Created_items dict does not have volume-id."""
2594 self
.vimconn
.cinder
.volumes
.get
.side_effect
= [None]
2596 result
= self
.vimconn
._wait
_for
_created
_volumes
_availability
(
2597 elapsed_time
, created_items
2599 self
.assertEqual(result
, 10)
2600 self
.vimconn
.cinder
.volumes
.get
.assert_not_called()
2601 mock_sleep
.assert_not_called()
2603 @patch("time.sleep")
2604 def test_wait_for_existing_volumes_availability_no_volume_in_existing_vim_volumes(
2607 """Existing_vim_volumes list does not have volume."""
2609 existing_vim_volumes
= []
2611 self
.vimconn
.cinder
.volumes
.get
.side_effect
= [None]
2613 result
= self
.vimconn
._wait
_for
_existing
_volumes
_availability
(
2614 elapsed_time
, existing_vim_volumes
2616 self
.assertEqual(result
, 10)
2617 self
.vimconn
.cinder
.volumes
.get
.assert_not_called()
2618 mock_sleep
.assert_not_called()
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(
2626 mock_existing_vol_availability
,
2627 mock_created_vol_availability
,
2628 mock_non_root_volumes
,
2631 """Prepare disks for VM instance successfully."""
2632 existing_vim_volumes
= []
2634 vm_av_zone
= ["nova"]
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()
2641 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2642 name
, existing_vim_volumes
, created_items
, vm_av_zone
, disk_list2
2644 self
.vimconn
.cinder
.volumes
.set_bootable
.assert_called_once_with(
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(
2653 vm_av_zone
=["nova"],
2654 disk
={"size": 10, "image_id": image_id
},
2656 block_device_mapping
={},
2657 existing_vim_volumes
=[],
2660 mock_non_root_volumes
.assert_called_once_with(
2663 vm_av_zone
=["nova"],
2665 block_device_mapping
={},
2666 existing_vim_volumes
=[],
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(
2676 mock_existing_vol_availability
,
2677 mock_created_vol_availability
,
2678 mock_non_root_volumes
,
2681 """Timeout exceeded while waiting for disks."""
2682 existing_vim_volumes
= []
2684 vm_av_zone
= ["nova"]
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
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
2695 str(err
.exception
), "Timeout creating volumes for instance basicvm"
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
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(
2706 vm_av_zone
=["nova"],
2707 disk
={"size": 10, "image_id": image_id
},
2709 block_device_mapping
={},
2710 existing_vim_volumes
=[],
2713 mock_non_root_volumes
.assert_called_once_with(
2716 vm_av_zone
=["nova"],
2718 block_device_mapping
={},
2719 existing_vim_volumes
=[],
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(
2729 mock_existing_vol_availability
,
2730 mock_created_vol_availability
,
2731 mock_non_root_volumes
,
2734 """Disk list is empty."""
2735 existing_vim_volumes
= []
2737 vm_av_zone
= ["nova"]
2738 mock_created_vol_availability
.return_value
= 2
2739 mock_existing_vol_availability
.return_value
= 3
2741 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2742 name
, existing_vim_volumes
, created_items
, vm_av_zone
, disk_list
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()
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(
2756 mock_existing_vol_availability
,
2757 mock_created_vol_availability
,
2758 mock_non_root_volumes
,
2761 """Persistent root volumes preparation raises error."""
2762 existing_vim_volumes
= []
2764 vm_av_zone
= ["nova"]
2766 mock_root_volumes
.side_effect
= Exception()
2767 mock_created_vol_availability
.return_value
= 10
2768 mock_existing_vol_availability
.return_value
= 15
2770 with self
.assertRaises(Exception):
2771 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2772 name
, existing_vim_volumes
, created_items
, vm_av_zone
, disk_list2
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(
2779 vm_av_zone
=["nova"],
2780 disk
={"size": 10, "image_id": image_id
},
2782 block_device_mapping
={},
2783 existing_vim_volumes
=[],
2786 mock_non_root_volumes
.assert_not_called()
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(
2794 mock_existing_vol_availability
,
2795 mock_created_vol_availability
,
2796 mock_non_root_volumes
,
2799 """Non-root volumes preparation raises error."""
2800 existing_vim_volumes
= []
2802 vm_av_zone
= ["nova"]
2804 mock_root_volumes
.return_value
= root_vol_id
2805 mock_non_root_volumes
.side_effect
= Exception
2807 with self
.assertRaises(Exception):
2808 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2809 name
, existing_vim_volumes
, created_items
, vm_av_zone
, disk_list2
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(
2818 vm_av_zone
=["nova"],
2819 disk
={"size": 10, "image_id": image_id
},
2821 block_device_mapping
={},
2822 existing_vim_volumes
=[],
2825 mock_non_root_volumes
.assert_called_once_with(
2828 vm_av_zone
=["nova"],
2830 block_device_mapping
={},
2831 existing_vim_volumes
=[],
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
= {
2839 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": False}
2842 with self
.assertRaises(VimConnException
) as err
:
2843 self
.vimconn
._find
_the
_external
_network
_for
_floating
_ip
()
2846 "Cannot create floating_ip automatically since no external network is present",
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
= {
2853 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True}
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
)
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
()
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
= {
2870 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True},
2871 {"id": "608b73-y9cc-5a6a-a270-12cc4811bd4a", "router:external": True},
2874 with self
.assertRaises(VimConnException
) as err
:
2875 self
.vimconn
._find
_the
_external
_network
_for
_floating
_ip
()
2878 "Cannot create floating_ip automatically since multiple external networks are present",
2881 def test_neutron_create_float_ip(self
):
2882 """Floating ip creation is successful."""
2883 param
= {"net_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a"}
2885 self
.vimconn
.neutron
.create_floatingip
.return_value
= {
2886 "floatingip": {"id": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
2888 expected_created_items
= {
2889 "floating_ip:308b73-t9cc-1a6a-a270-12cc4811bd4a": True
2891 self
.vimconn
._neutron
_create
_float
_ip
(param
, created_items
)
2892 self
.assertEqual(created_items
, expected_created_items
)
2894 def test_neutron_create_float_ip_exception_occured(self
):
2895 """Floating ip could not be created."""
2898 "floating_network_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a",
2899 "tenant_id": "308b73-19cc-8a6a-a270-02cc4811bd9a",
2903 self
.vimconn
.neutron
= CopyingMock()
2904 self
.vimconn
.neutron
.create_floatingip
.side_effect
= Exception(
2905 "Neutron floating ip create exception occured."
2907 with self
.assertRaises(VimConnException
) as err
:
2908 self
.vimconn
._neutron
_create
_float
_ip
(param
, created_items
)
2909 self
.assertEqual(created_items
, {})
2912 "Exception: Cannot create new floating_ip Neutron floating ip create exception occured.",
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
2920 """Floating ip creation, ip pool is available."""
2921 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
2925 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2926 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
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
, {})
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
2938 """Floating ip creation, pool id need to be found."""
2939 floating_network
= {"floating_ip": True}
2941 mock_find_ext_network
.return_value
= "308b73-t9cc-1a6a-a270-12cc4811bd4a"
2944 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2945 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
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
, {})
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
2957 """Neutron creat floating ip raises error."""
2958 floating_network
= {"floating_ip": True}
2960 mock_create_float_ip
.side_effect
= VimConnException(
2961 "Can not create floating ip."
2963 mock_find_ext_network
.return_value
= "308b73-t9cc-1a6a-a270-12cc4811bd4a"
2966 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2967 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
2971 with self
.assertRaises(VimConnException
) as err
:
2972 self
.vimconn
._create
_floating
_ip
(
2973 floating_network
, self
.server
, created_items
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
, {})
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
2984 """Floating ip creation, pool id could not be found."""
2985 floating_network
= {"floating_ip": True}
2987 mock_find_ext_network
.side_effect
= VimConnException(
2988 "Cannot create floating_ip automatically since no external network is present"
2990 with self
.assertRaises(VimConnException
) as err
:
2991 self
.vimconn
._create
_floating
_ip
(
2992 floating_network
, self
.server
, created_items
2996 "Cannot create floating_ip automatically since no external network is present",
2998 mock_find_ext_network
.assert_called_once()
2999 mock_create_float_ip
.assert_not_called()
3001 def test_find_floating_ip_get_free_floating_ip(self
):
3002 """Get free floating ips successfully."""
3005 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
3006 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3007 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3010 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3011 expected_result
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3013 result
= self
.vimconn
._find
_floating
_ip
(
3014 self
.server
, floating_ips
, floating_network
3016 self
.assertEqual(result
, expected_result
)
3018 def test_find_floating_ip_different_floating_network_id(self
):
3019 """Floating network id is different with floating_ip of floating network."""
3022 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3023 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3026 floating_network
= {"floating_ip": "508b73-t9cc-1a6a-a270-12cc4811bd4a"}
3028 result
= self
.vimconn
._find
_floating
_ip
(
3029 self
.server
, floating_ips
, floating_network
3031 self
.assertEqual(result
, None)
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."""
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,
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
3049 self
.assertEqual(result
, None)
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
}
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",
3062 self
.vimconn
.neutron
.update_floatingip
.side_effect
= None
3063 self
.vimconn
.neutron
.show_floatingip
.return_value
= fip
3064 expected_result
= fip
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(
3070 {"floatingip": {"port_id": floating_network_vim_id
}},
3072 mock_sleep
.assert_called_once_with(5)
3073 self
.vimconn
.neutron
.show_floatingip
.assert_called_once_with(free_floating_ip
)
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."
3085 with self
.assertRaises(Exception) as err
:
3086 result
= self
.vimconn
._assign
_floating
_ip
(
3087 free_floating_ip
, floating_network
3089 self
.assertEqual(result
, None)
3090 self
.assertEqual(str(err
.exception
), "Floating ip is not updated.")
3092 self
.vimconn
.neutron
.update_floatingip
.assert_called_once_with(
3094 {"floatingip": {"port_id": floating_network_vim_id
}},
3096 mock_sleep
.assert_not_called()
3097 self
.vimconn
.neutron
.show_floatingip
.assert_not_called()
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."
3109 with self
.assertRaises(Exception) as err
:
3110 result
= self
.vimconn
._assign
_floating
_ip
(
3111 free_floating_ip
, floating_network
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(
3117 {"floatingip": {"port_id": floating_network_vim_id
}},
3119 mock_sleep
.assert_called_once_with(5)
3120 self
.vimconn
.neutron
.show_floatingip
.assert_called_once_with(free_floating_ip
)
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"}
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",
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",
3142 self
.vimconn
.neutron
.list_floatingips
.return_value
= {
3143 "floatingips": floating_ips
3145 mock_find_floating_ip
.return_value
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3146 expected_result
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3148 result
= self
.vimconn
._get
_free
_floating
_ip
(
3149 self
.server
, floating_network
, created_items
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
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
3162 """Neutron list floating IPs raises exception."""
3163 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3165 self
.vimconn
.neutron
= CopyingMock()
3166 self
.vimconn
.neutron
.list_floatingips
.side_effect
= Exception(
3167 "Floating ips could not be listed."
3169 with self
.assertRaises(Exception) as err
:
3170 result
= self
.vimconn
._get
_free
_floating
_ip
(
3171 self
.server
, floating_network
, created_items
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()
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
3183 """_find_floating_ip method raises exception."""
3184 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
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",
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",
3200 self
.vimconn
.neutron
= CopyingMock()
3201 self
.vimconn
.neutron
.list_floatingips
.return_value
= {
3202 "floatingips": floating_ips
3204 mock_find_floating_ip
.side_effect
= Exception(
3205 "Free floating ip could not be found."
3208 with self
.assertRaises(Exception) as err
:
3209 result
= self
.vimconn
._get
_free
_floating
_ip
(
3210 self
.server
, floating_network
, created_items
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
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(
3224 mock_assign_floating_ip
,
3225 mock_get_free_floating_ip
,
3226 mock_create_floating_ip
,
3228 """Prepare external network successfully."""
3229 external_network
= [
3231 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3232 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
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"}
3241 self
.vimconn
.neutron
= CopyingMock()
3242 self
.vimconn
.nova
= CopyingMock()
3243 self
.vimconn
.neutron
.show_floatingip
.return_value
= {
3244 "floatingip": {"port_id": ""}
3247 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3248 external_network
, self
.server
, created_items
, vm_start_time
3251 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 1)
3252 mock_get_free_floating_ip
.assert_called_once_with(
3255 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3256 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3260 self
.vimconn
.neutron
.show_floatingip
.assert_called_once_with(
3261 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
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",
3268 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3269 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
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(
3280 mock_assign_floating_ip
,
3281 mock_get_free_floating_ip
,
3282 mock_create_floating_ip
,
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",
3291 external_network
= [floating_network
]
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
= {}
3300 with self
.assertRaises(KeyError):
3301 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3302 external_network
, self
.server
, created_items
, vm_start_time
3305 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 4)
3306 mock_get_free_floating_ip
.assert_called_with(
3309 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3310 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
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
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)
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(
3332 mock_assign_floating_ip
,
3333 mock_get_free_floating_ip
,
3334 mock_create_floating_ip
,
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,
3345 external_network
= [floating_network
]
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."
3354 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ERROR"
3355 self
.vimconn
.neutron
.show_floatingip
.return_value
= {}
3357 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3358 external_network
, self
.server
, created_items
, vm_start_time
3360 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 1)
3361 mock_get_free_floating_ip
.assert_called_with(
3364 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3365 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3366 "exit_on_floating_ip_error": False,
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
3377 self
.assertEqual(mock_create_floating_ip
.call_count
, 1)
3378 mock_assign_floating_ip
.assert_not_called()
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(
3387 mock_assign_floating_ip
,
3388 mock_get_free_floating_ip
,
3389 mock_create_floating_ip
,
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,
3400 external_network
= [floating_network
]
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."
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
3415 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 1)
3416 mock_get_free_floating_ip
.assert_called_with(
3419 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3420 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3421 "exit_on_floating_ip_error": True,
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
3432 self
.assertEqual(mock_create_floating_ip
.call_count
, 1)
3433 mock_assign_floating_ip
.assert_not_called()
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(
3440 mock_assign_floating_ip
,
3441 mock_get_free_floating_ip
,
3442 mock_create_floating_ip
,
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",
3450 external_network
= [floating_network
]
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",
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"}},
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": ""}},
3469 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3470 external_network
, self
.server
, created_items
, vm_start_time
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
3475 _call_mock_get_free_floating_ip
[0][0],
3483 _call_mock_get_free_floating_ip
[1][0],
3491 _call_mock_get_free_floating_ip
[2][0],
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
3504 _call_mock_assign_floating_ip
[0][0],
3505 ("r08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network
),
3508 _call_mock_assign_floating_ip
[1][0],
3509 ("y08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network
),
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(
3519 mock_assign_floating_ip
,
3520 mock_get_free_floating_ip
,
3521 mock_create_floating_ip
,
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,
3532 external_network
= [floating_network
]
3534 vm_start_time
= time_return_value
3536 mock_time
.side_effect
= [156570150, 156570800, 156571200]
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.")
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
3548 "Cannot create floating_ip: Exception Floating ip could not be shown.",
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
3554 _call_mock_get_free_floating_ip
[0][0],
3562 _call_mock_get_free_floating_ip
[1][0],
3570 _call_mock_get_free_floating_ip
[2][0],
3578 _call_mock_get_free_floating_ip
[3][0],
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()
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(
3600 mock_assign_floating_ip
,
3601 mock_get_free_floating_ip
,
3602 mock_create_floating_ip
,
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,
3613 external_network
= [floating_network
]
3615 vm_start_time
= time_return_value
3617 mock_time
.side_effect
= [156570150, 156570800, 156571200]
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.")
3624 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3625 external_network
, self
.server
, created_items
, vm_start_time
3627 # self.assertEqual(str(err.exception), "Cannot create floating_ip")
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
3632 _call_mock_get_free_floating_ip
[0][0],
3640 _call_mock_get_free_floating_ip
[1][0],
3648 _call_mock_get_free_floating_ip
[2][0],
3656 _call_mock_get_free_floating_ip
[3][0],
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()
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(
3678 mock_assign_floating_ip
,
3679 mock_get_free_floating_ip
,
3680 mock_create_floating_ip
,
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,
3691 external_network
= [floating_network
]
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.")
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
3707 "Cannot create floating_ip: Exception Floating ip could not be shown.",
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
3713 _call_mock_get_free_floating_ip
[0][0],
3721 _call_mock_get_free_floating_ip
[1][0],
3729 _call_mock_get_free_floating_ip
[2][0],
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)
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(
3751 mock_assign_floating_ip
,
3752 mock_get_free_floating_ip
,
3753 mock_create_floating_ip
,
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,
3764 external_network
= [floating_network
]
3766 vm_start_time
= time_return_value
3768 mock_get_free_floating_ip
.side_effect
= [
3769 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
3772 mock_time
.side_effect
= [156571790, 156571795, 156571800, 156571805]
3774 mock_assign_floating_ip
.side_effect
= [
3775 Exception("Floating ip could not be assigned.")
3778 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ERROR"
3779 self
.vimconn
.neutron
.show_floatingip
.side_effect
= [
3780 {"floatingip": {"port_id": ""}}
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
3789 "Cannot create floating_ip: Exception Floating ip could not be assigned.",
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
3795 _call_mock_get_free_floating_ip
[0][0],
3803 _call_mock_get_free_floating_ip
[1][0],
3811 _call_mock_get_free_floating_ip
[2][0],
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"
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()
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(
3836 mock_assign_floating_ip
,
3837 mock_get_free_floating_ip
,
3838 mock_create_floating_ip
,
3842 """External network list is empty."""
3843 external_network
= []
3845 vm_start_time
= time_return_value
3847 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3848 external_network
, self
.server
, created_items
, vm_start_time
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()
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")]
3863 self
.vimconn
._update
_port
_security
_for
_vminstance
(no_secured_ports
, self
.server
)
3865 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3867 self
.vimconn
.neutron
.update_port
.assert_called_once_with(
3869 {"port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}},
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
3876 """no_secured_ports has port and the port does not have allow-address-pairs."""
3877 no_secured_ports
= [(port2_id
, "something")]
3879 self
.vimconn
._update
_port
_security
_for
_vminstance
(no_secured_ports
, self
.server
)
3881 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3883 self
.vimconn
.neutron
.update_port
.assert_called_once_with(
3885 {"port": {"port_security_enabled": False, "security_groups": None}},
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
3892 """__wait_for_vm raises timeout exception."""
3893 no_secured_ports
= [(port2_id
, "something")]
3895 mock_wait_for_vm
.side_effect
= VimConnException("Timeout waiting for instance.")
3897 with self
.assertRaises(VimConnException
) as err
:
3898 self
.vimconn
._update
_port
_security
_for
_vminstance
(
3899 no_secured_ports
, self
.server
3901 self
.assertEqual(str(err
.exception
), "Timeout waiting for instance.")
3903 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3905 self
.vimconn
.neutron
.update_port
.assert_not_called()
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
3911 """neutron_update_port method raises exception."""
3912 no_secured_ports
= [(port2_id
, "something")]
3914 self
.vimconn
.neutron
.update_port
.side_effect
= Exception(
3915 "Port security could not be updated."
3918 with self
.assertRaises(VimConnException
) as err
:
3919 self
.vimconn
._update
_port
_security
_for
_vminstance
(
3920 no_secured_ports
, self
.server
3924 "It was not possible to disable port security for port 17472685-f67f-49fd-8722-eabb7692fc22",
3926 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3928 self
.vimconn
.neutron
.update_port
.assert_called_once_with(
3930 {"port": {"port_security_enabled": False, "security_groups": None}},
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
3937 """no_secured_ports list does not have any ports."""
3938 no_secured_ports
= []
3940 self
.vimconn
._update
_port
_security
_for
_vminstance
(no_secured_ports
, self
.server
)
3942 mock_wait_for_vm
.assert_not_called()
3944 self
.vimconn
.neutron
.update_port
.assert_not_called()
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(
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
,
3969 """New VM instance creation is successful."""
3971 mock_create_user_data
.return_value
= True, "userdata"
3973 mock_get_vm_availability_zone
.return_value
= "nova"
3975 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
3977 mock_time
.return_value
= time_return_value
3979 expected_result
= self
.server
.id, {}
3981 result
= self
.vimconn
.new_vminstance(
3987 affinity_group_list
,
3991 availability_zone_index
,
3992 availability_zone_list
,
3994 self
.assertEqual(result
, expected_result
)
3996 mock_reload_connection
.assert_called_once()
3997 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4002 external_network
=[],
4003 no_secured_ports
=[],
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
4009 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4011 existing_vim_volumes
=[],
4014 disk_list
=disk_list2
,
4016 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4021 security_groups
="default",
4022 availability_zone
="nova",
4023 key_name
="my_keypair",
4024 userdata
="userdata",
4026 block_device_mapping
=None,
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
=[],
4035 vm_start_time
=time_return_value
,
4037 mock_delete_vm_instance
.assert_not_called()
4038 mock_format_exception
.assert_not_called()
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(
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
,
4063 """New VM instance creation failed because of user data creation failure."""
4065 mock_create_user_data
.side_effect
= Exception(
4066 "User data could not be retrieved."
4069 mock_get_vm_availability_zone
.return_value
= "nova"
4071 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
4073 mock_time
.return_value
= time_return_value
4075 self
.vimconn
.new_vminstance(
4081 affinity_group_list
,
4085 availability_zone_index
,
4086 availability_zone_list
,
4089 mock_reload_connection
.assert_called_once()
4090 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4095 external_network
=[],
4096 no_secured_ports
=[],
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.")
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(
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
,
4133 """New VM instance creation, external network connection has failed as floating
4134 ip could not be created."""
4136 mock_create_user_data
.return_value
= True, "userdata"
4138 mock_get_vm_availability_zone
.return_value
= "nova"
4140 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
4142 mock_time
.return_value
= time_return_value
4144 mock_prepare_external_network
.side_effect
= VimConnException(
4145 "Can not create floating ip."
4148 self
.vimconn
.new_vminstance(
4154 affinity_group_list
,
4158 availability_zone_index
,
4159 availability_zone_list
,
4162 mock_reload_connection
.assert_called_once()
4163 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4168 external_network
=[],
4169 no_secured_ports
=[],
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
4175 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4177 existing_vim_volumes
=[],
4180 disk_list
=disk_list2
,
4182 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4187 security_groups
="default",
4188 availability_zone
="nova",
4189 key_name
="my_keypair",
4190 userdata
="userdata",
4192 block_device_mapping
=None,
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
=[],
4201 vm_start_time
=time_return_value
,
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.")
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(
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
,
4231 """New VM creation with affinity group."""
4232 affinity_group_list
= [
4233 {"affinity_group_id": "38b73-e9cc-5a6a-t270-82cc4811bd4a"}
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, {}
4241 result
= self
.vimconn
.new_vminstance(
4247 affinity_group_list
,
4251 availability_zone_index
,
4252 availability_zone_list
,
4254 self
.assertEqual(result
, expected_result
)
4256 mock_reload_connection
.assert_called_once()
4257 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4262 external_network
=[],
4263 no_secured_ports
=[],
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
4269 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4271 existing_vim_volumes
=[],
4274 disk_list
=disk_list2
,
4276 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4281 security_groups
="default",
4282 availability_zone
="nova",
4283 key_name
="my_keypair",
4284 userdata
="userdata",
4286 block_device_mapping
=None,
4287 scheduler_hints
={"group": "38b73-e9cc-5a6a-t270-82cc4811bd4a"},
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
=[],
4295 vm_start_time
=time_return_value
,
4297 mock_delete_vm_instance
.assert_not_called()
4298 mock_format_exception
.assert_not_called()
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(
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
,
4323 """New VM(server) creation failed."""
4325 mock_create_user_data
.return_value
= True, "userdata"
4327 mock_get_vm_availability_zone
.return_value
= "nova"
4329 self
.vimconn
.nova
.servers
.create
.side_effect
= Exception(
4330 "Server could not be created."
4333 mock_time
.return_value
= time_return_value
4335 self
.vimconn
.new_vminstance(
4341 affinity_group_list
,
4345 availability_zone_index
,
4346 availability_zone_list
,
4349 mock_reload_connection
.assert_called_once()
4350 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4355 external_network
=[],
4356 no_secured_ports
=[],
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
4362 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4364 existing_vim_volumes
=[],
4367 disk_list
=disk_list2
,
4370 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4375 security_groups
="default",
4376 availability_zone
="nova",
4377 key_name
="my_keypair",
4378 userdata
="userdata",
4380 block_device_mapping
=None,
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.")
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(
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
,
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
4421 self
.vimconn
.new_vminstance(
4427 affinity_group_list
,
4431 availability_zone_index
,
4432 availability_zone_list
,
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, {})
4449 if __name__
== "__main__":