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 block_device_mapping
= {}
2635 vm_av_zone
= ["nova"]
2637 mock_root_volumes
.return_value
= root_vol_id
2638 mock_created_vol_availability
.return_value
= 10
2639 mock_existing_vol_availability
.return_value
= 15
2640 self
.vimconn
.cinder
= CopyingMock()
2642 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2644 existing_vim_volumes
,
2647 block_device_mapping
,
2650 self
.vimconn
.cinder
.volumes
.set_bootable
.assert_called_once_with(
2653 mock_created_vol_availability
.assert_called_once_with(0, created_items
)
2654 mock_existing_vol_availability
.assert_called_once_with(10, existing_vim_volumes
)
2655 self
.assertEqual(mock_root_volumes
.call_count
, 1)
2656 self
.assertEqual(mock_non_root_volumes
.call_count
, 1)
2657 mock_root_volumes
.assert_called_once_with(
2659 vm_av_zone
=["nova"],
2660 disk
={"size": 10, "image_id": image_id
},
2662 block_device_mapping
={},
2663 existing_vim_volumes
=[],
2666 mock_non_root_volumes
.assert_called_once_with(
2669 vm_av_zone
=["nova"],
2671 block_device_mapping
={},
2672 existing_vim_volumes
=[],
2676 @patch.object(vimconnector
, "_prepare_persistent_root_volumes")
2677 @patch.object(vimconnector
, "_prepare_non_root_persistent_volumes")
2678 @patch.object(vimconnector
, "_wait_for_created_volumes_availability")
2679 @patch.object(vimconnector
, "_wait_for_existing_volumes_availability")
2680 def test_prepare_disk_for_vm_instance_timeout_exceeded(
2682 mock_existing_vol_availability
,
2683 mock_created_vol_availability
,
2684 mock_non_root_volumes
,
2687 """Timeout exceeded while waiting for disks."""
2688 existing_vim_volumes
= []
2690 vm_av_zone
= ["nova"]
2691 block_device_mapping
= {}
2693 mock_root_volumes
.return_value
= root_vol_id
2694 mock_created_vol_availability
.return_value
= 1700
2695 mock_existing_vol_availability
.return_value
= 1900
2697 with self
.assertRaises(VimConnException
) as err
:
2698 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2700 existing_vim_volumes
,
2703 block_device_mapping
,
2707 str(err
.exception
), "Timeout creating volumes for instance basicvm"
2709 self
.vimconn
.cinder
.volumes
.set_bootable
.assert_not_called()
2710 mock_created_vol_availability
.assert_called_once_with(0, created_items
)
2711 mock_existing_vol_availability
.assert_called_once_with(
2712 1700, existing_vim_volumes
2714 self
.assertEqual(mock_root_volumes
.call_count
, 1)
2715 self
.assertEqual(mock_non_root_volumes
.call_count
, 1)
2716 mock_root_volumes
.assert_called_once_with(
2718 vm_av_zone
=["nova"],
2719 disk
={"size": 10, "image_id": image_id
},
2721 block_device_mapping
={},
2722 existing_vim_volumes
=[],
2725 mock_non_root_volumes
.assert_called_once_with(
2728 vm_av_zone
=["nova"],
2730 block_device_mapping
={},
2731 existing_vim_volumes
=[],
2735 @patch.object(vimconnector
, "_prepare_persistent_root_volumes")
2736 @patch.object(vimconnector
, "_prepare_non_root_persistent_volumes")
2737 @patch.object(vimconnector
, "_wait_for_created_volumes_availability")
2738 @patch.object(vimconnector
, "_wait_for_existing_volumes_availability")
2739 def test_prepare_disk_for_vm_instance_empty_disk_list(
2741 mock_existing_vol_availability
,
2742 mock_created_vol_availability
,
2743 mock_non_root_volumes
,
2746 """Disk list is empty."""
2747 existing_vim_volumes
= []
2749 block_device_mapping
= {}
2750 vm_av_zone
= ["nova"]
2751 mock_created_vol_availability
.return_value
= 2
2752 mock_existing_vol_availability
.return_value
= 3
2754 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2756 existing_vim_volumes
,
2759 block_device_mapping
,
2762 self
.vimconn
.cinder
.volumes
.set_bootable
.assert_not_called()
2763 mock_created_vol_availability
.assert_called_once_with(0, created_items
)
2764 mock_existing_vol_availability
.assert_called_once_with(2, existing_vim_volumes
)
2765 mock_root_volumes
.assert_not_called()
2766 mock_non_root_volumes
.assert_not_called()
2768 @patch.object(vimconnector
, "_prepare_persistent_root_volumes")
2769 @patch.object(vimconnector
, "_prepare_non_root_persistent_volumes")
2770 @patch.object(vimconnector
, "_wait_for_created_volumes_availability")
2771 @patch.object(vimconnector
, "_wait_for_existing_volumes_availability")
2772 def test_prepare_disk_for_vm_instance_persistent_root_volume_error(
2774 mock_existing_vol_availability
,
2775 mock_created_vol_availability
,
2776 mock_non_root_volumes
,
2779 """Persistent root volumes preparation raises error."""
2780 existing_vim_volumes
= []
2782 vm_av_zone
= ["nova"]
2783 block_device_mapping
= {}
2785 mock_root_volumes
.side_effect
= Exception()
2786 mock_created_vol_availability
.return_value
= 10
2787 mock_existing_vol_availability
.return_value
= 15
2789 with self
.assertRaises(Exception):
2790 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2792 existing_vim_volumes
,
2795 block_device_mapping
,
2798 self
.vimconn
.cinder
.volumes
.set_bootable
.assert_not_called()
2799 mock_created_vol_availability
.assert_not_called()
2800 mock_existing_vol_availability
.assert_not_called()
2801 mock_root_volumes
.assert_called_once_with(
2803 vm_av_zone
=["nova"],
2804 disk
={"size": 10, "image_id": image_id
},
2806 block_device_mapping
={},
2807 existing_vim_volumes
=[],
2810 mock_non_root_volumes
.assert_not_called()
2812 @patch.object(vimconnector
, "_prepare_persistent_root_volumes")
2813 @patch.object(vimconnector
, "_prepare_non_root_persistent_volumes")
2814 @patch.object(vimconnector
, "_wait_for_created_volumes_availability")
2815 @patch.object(vimconnector
, "_wait_for_existing_volumes_availability")
2816 def test_prepare_disk_for_vm_instance_non_root_volume_error(
2818 mock_existing_vol_availability
,
2819 mock_created_vol_availability
,
2820 mock_non_root_volumes
,
2823 """Non-root volumes preparation raises error."""
2824 existing_vim_volumes
= []
2826 vm_av_zone
= ["nova"]
2827 block_device_mapping
= {}
2829 mock_root_volumes
.return_value
= root_vol_id
2830 mock_non_root_volumes
.side_effect
= Exception
2832 with self
.assertRaises(Exception):
2833 self
.vimconn
._prepare
_disk
_for
_vminstance
(
2835 existing_vim_volumes
,
2838 block_device_mapping
,
2841 self
.vimconn
.cinder
.volumes
.set_bootable
.assert_not_called()
2842 mock_created_vol_availability
.assert_not_called()
2843 mock_existing_vol_availability
.assert_not_called()
2844 self
.assertEqual(mock_root_volumes
.call_count
, 1)
2845 self
.assertEqual(mock_non_root_volumes
.call_count
, 1)
2846 mock_root_volumes
.assert_called_once_with(
2848 vm_av_zone
=["nova"],
2849 disk
={"size": 10, "image_id": image_id
},
2851 block_device_mapping
={},
2852 existing_vim_volumes
=[],
2855 mock_non_root_volumes
.assert_called_once_with(
2858 vm_av_zone
=["nova"],
2860 block_device_mapping
={},
2861 existing_vim_volumes
=[],
2865 def test_find_external_network_for_floating_ip_no_external_network(self
):
2866 """External network could not be found."""
2867 self
.vimconn
.neutron
.list_networks
.return_value
= {
2869 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": False}
2872 with self
.assertRaises(VimConnException
) as err
:
2873 self
.vimconn
._find
_the
_external
_network
_for
_floating
_ip
()
2876 "Cannot create floating_ip automatically since no external network is present",
2879 def test_find_external_network_for_floating_one_external_network(self
):
2880 """One external network has been found."""
2881 self
.vimconn
.neutron
.list_networks
.return_value
= {
2883 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True}
2886 expected_result
= "408b73-r9cc-5a6a-a270-82cc4811bd4a"
2887 result
= self
.vimconn
._find
_the
_external
_network
_for
_floating
_ip
()
2888 self
.assertEqual(result
, expected_result
)
2890 def test_find_external_network_for_floating_neutron_raises_exception(self
):
2891 """Neutron list networks raises exception."""
2892 self
.vimconn
.neutron
.list_networks
.side_effect
= Exception
2893 with self
.assertRaises(Exception):
2894 self
.vimconn
._find
_the
_external
_network
_for
_floating
_ip
()
2896 def test_find_external_network_for_floating_several_external_network(self
):
2897 """Several exernal networks has been found."""
2898 self
.vimconn
.neutron
.list_networks
.return_value
= {
2900 {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True},
2901 {"id": "608b73-y9cc-5a6a-a270-12cc4811bd4a", "router:external": True},
2904 with self
.assertRaises(VimConnException
) as err
:
2905 self
.vimconn
._find
_the
_external
_network
_for
_floating
_ip
()
2908 "Cannot create floating_ip automatically since multiple external networks are present",
2911 def test_neutron_create_float_ip(self
):
2912 """Floating ip creation is successful."""
2913 param
= {"net_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a"}
2915 self
.vimconn
.neutron
.create_floatingip
.return_value
= {
2916 "floatingip": {"id": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
2918 expected_created_items
= {
2919 "floating_ip:308b73-t9cc-1a6a-a270-12cc4811bd4a": True
2921 self
.vimconn
._neutron
_create
_float
_ip
(param
, created_items
)
2922 self
.assertEqual(created_items
, expected_created_items
)
2924 def test_neutron_create_float_ip_exception_occured(self
):
2925 """Floating ip could not be created."""
2928 "floating_network_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a",
2929 "tenant_id": "308b73-19cc-8a6a-a270-02cc4811bd9a",
2933 self
.vimconn
.neutron
= CopyingMock()
2934 self
.vimconn
.neutron
.create_floatingip
.side_effect
= Exception(
2935 "Neutron floating ip create exception occured."
2937 with self
.assertRaises(VimConnException
) as err
:
2938 self
.vimconn
._neutron
_create
_float
_ip
(param
, created_items
)
2939 self
.assertEqual(created_items
, {})
2942 "Exception: Cannot create new floating_ip Neutron floating ip create exception occured.",
2945 @patch.object(vimconnector
, "_neutron_create_float_ip")
2946 @patch.object(vimconnector
, "_find_the_external_network_for_floating_ip")
2947 def test_create_floating_ip_pool_id_available(
2948 self
, mock_find_ext_network
, mock_create_float_ip
2950 """Floating ip creation, ip pool is available."""
2951 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
2955 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2956 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
2959 self
.vimconn
._create
_floating
_ip
(floating_network
, self
.server
, created_items
)
2960 mock_find_ext_network
.assert_not_called()
2961 mock_create_float_ip
.assert_called_once_with(expected_param
, {})
2963 @patch.object(vimconnector
, "_neutron_create_float_ip")
2964 @patch.object(vimconnector
, "_find_the_external_network_for_floating_ip")
2965 def test_create_floating_ip_finding_pool_id(
2966 self
, mock_find_ext_network
, mock_create_float_ip
2968 """Floating ip creation, pool id need to be found."""
2969 floating_network
= {"floating_ip": True}
2971 mock_find_ext_network
.return_value
= "308b73-t9cc-1a6a-a270-12cc4811bd4a"
2974 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2975 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
2978 self
.vimconn
._create
_floating
_ip
(floating_network
, self
.server
, created_items
)
2979 mock_find_ext_network
.assert_called_once()
2980 mock_create_float_ip
.assert_called_once_with(expected_param
, {})
2982 @patch.object(vimconnector
, "_neutron_create_float_ip")
2983 @patch.object(vimconnector
, "_find_the_external_network_for_floating_ip")
2984 def test_create_floating_ip_neutron_create_floating_ip_exception(
2985 self
, mock_find_ext_network
, mock_create_float_ip
2987 """Neutron creat floating ip raises error."""
2988 floating_network
= {"floating_ip": True}
2990 mock_create_float_ip
.side_effect
= VimConnException(
2991 "Can not create floating ip."
2993 mock_find_ext_network
.return_value
= "308b73-t9cc-1a6a-a270-12cc4811bd4a"
2996 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
2997 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
3001 with self
.assertRaises(VimConnException
) as err
:
3002 self
.vimconn
._create
_floating
_ip
(
3003 floating_network
, self
.server
, created_items
3005 self
.assertEqual(str(err
.exception
), "Can not create floating ip.")
3006 mock_find_ext_network
.assert_called_once()
3007 mock_create_float_ip
.assert_called_once_with(expected_param
, {})
3009 @patch.object(vimconnector
, "_neutron_create_float_ip")
3010 @patch.object(vimconnector
, "_find_the_external_network_for_floating_ip")
3011 def test_create_floating_ip_can_not_find_pool_id(
3012 self
, mock_find_ext_network
, mock_create_float_ip
3014 """Floating ip creation, pool id could not be found."""
3015 floating_network
= {"floating_ip": True}
3017 mock_find_ext_network
.side_effect
= VimConnException(
3018 "Cannot create floating_ip automatically since no external network is present"
3020 with self
.assertRaises(VimConnException
) as err
:
3021 self
.vimconn
._create
_floating
_ip
(
3022 floating_network
, self
.server
, created_items
3026 "Cannot create floating_ip automatically since no external network is present",
3028 mock_find_ext_network
.assert_called_once()
3029 mock_create_float_ip
.assert_not_called()
3031 def test_find_floating_ip_get_free_floating_ip(self
):
3032 """Get free floating ips successfully."""
3035 "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a",
3036 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3037 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3040 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3041 expected_result
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3043 result
= self
.vimconn
._find
_floating
_ip
(
3044 self
.server
, floating_ips
, floating_network
3046 self
.assertEqual(result
, expected_result
)
3048 def test_find_floating_ip_different_floating_network_id(self
):
3049 """Floating network id is different with floating_ip of floating network."""
3052 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3053 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3056 floating_network
= {"floating_ip": "508b73-t9cc-1a6a-a270-12cc4811bd4a"}
3058 result
= self
.vimconn
._find
_floating
_ip
(
3059 self
.server
, floating_ips
, floating_network
3061 self
.assertEqual(result
, None)
3063 def test_find_floating_ip_different_fip_tenant(self
):
3064 """Items in floating_ips has port_id, tenant_is is not same with server tenant id."""
3067 "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3068 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3069 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3070 "tenant_id": self
.server
.id,
3073 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3074 mock_create_floating_ip
= CopyingMock()
3075 with patch
.object(vimconnector
, "_create_floating_ip", mock_create_floating_ip
):
3076 result
= self
.vimconn
._find
_floating
_ip
(
3077 self
.server
, floating_ips
, floating_network
3079 self
.assertEqual(result
, None)
3081 @patch("time.sleep")
3082 def test_assign_floating_ip(self
, mock_sleep
):
3083 """Assign floating ip successfully."""
3084 free_floating_ip
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3085 floating_network
= {"vim_id": floating_network_vim_id
}
3087 "port_id": floating_network_vim_id
,
3088 "floating_network_id": "p08b73-e9cc-5a6a-t270-82cc4811bd4a",
3089 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3090 "tenant_id": "k08b73-e9cc-5a6a-t270-82cc4811bd4a",
3092 self
.vimconn
.neutron
.update_floatingip
.side_effect
= None
3093 self
.vimconn
.neutron
.show_floatingip
.return_value
= fip
3094 expected_result
= fip
3096 result
= self
.vimconn
._assign
_floating
_ip
(free_floating_ip
, floating_network
)
3097 self
.assertEqual(result
, expected_result
)
3098 self
.vimconn
.neutron
.update_floatingip
.assert_called_once_with(
3100 {"floatingip": {"port_id": floating_network_vim_id
}},
3102 mock_sleep
.assert_called_once_with(5)
3103 self
.vimconn
.neutron
.show_floatingip
.assert_called_once_with(free_floating_ip
)
3105 @patch("time.sleep")
3106 def test_assign_floating_ip_update_floating_ip_exception(self
, mock_sleep
):
3107 """Neutron update floating ip raises exception."""
3108 free_floating_ip
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3109 floating_network
= {"vim_id": floating_network_vim_id
}
3110 self
.vimconn
.neutron
= CopyingMock()
3111 self
.vimconn
.neutron
.update_floatingip
.side_effect
= Exception(
3112 "Floating ip is not updated."
3115 with self
.assertRaises(Exception) as err
:
3116 result
= self
.vimconn
._assign
_floating
_ip
(
3117 free_floating_ip
, floating_network
3119 self
.assertEqual(result
, None)
3120 self
.assertEqual(str(err
.exception
), "Floating ip is not updated.")
3122 self
.vimconn
.neutron
.update_floatingip
.assert_called_once_with(
3124 {"floatingip": {"port_id": floating_network_vim_id
}},
3126 mock_sleep
.assert_not_called()
3127 self
.vimconn
.neutron
.show_floatingip
.assert_not_called()
3129 @patch("time.sleep")
3130 def test_assign_floating_ip_show_floating_ip_exception(self
, mock_sleep
):
3131 """Neutron show floating ip raises exception."""
3132 free_floating_ip
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3133 floating_network
= {"vim_id": floating_network_vim_id
}
3134 self
.vimconn
.neutron
.update_floatingip
.side_effect
= None
3135 self
.vimconn
.neutron
.show_floatingip
.side_effect
= Exception(
3136 "Floating ip could not be shown."
3139 with self
.assertRaises(Exception) as err
:
3140 result
= self
.vimconn
._assign
_floating
_ip
(
3141 free_floating_ip
, floating_network
3143 self
.assertEqual(result
, None)
3144 self
.assertEqual(str(err
.exception
), "Floating ip could not be shown.")
3145 self
.vimconn
.neutron
.update_floatingip
.assert_called_once_with(
3147 {"floatingip": {"port_id": floating_network_vim_id
}},
3149 mock_sleep
.assert_called_once_with(5)
3150 self
.vimconn
.neutron
.show_floatingip
.assert_called_once_with(free_floating_ip
)
3152 @patch("random.shuffle")
3153 @patch.object(vimconnector
, "_find_floating_ip")
3154 def test_get_free_floating_ip(self
, mock_find_floating_ip
, mock_shuffle
):
3155 """Get free floating ip successfully."""
3156 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3160 "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3161 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3162 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3163 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3166 "port_id": "508b73-r9cc-5a6a-5270-o2cc4811bd4a",
3167 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3168 "id": "208b73-o9cc-5a6a-a270-52cc4811bd8",
3169 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3172 self
.vimconn
.neutron
.list_floatingips
.return_value
= {
3173 "floatingips": floating_ips
3175 mock_find_floating_ip
.return_value
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3176 expected_result
= "508b73-o9cc-5a6a-a270-72cc4811bd8"
3178 result
= self
.vimconn
._get
_free
_floating
_ip
(
3179 self
.server
, floating_network
, created_items
3181 self
.assertEqual(result
, expected_result
)
3182 mock_shuffle
.assert_called_once_with(floating_ips
)
3183 mock_find_floating_ip
.assert_called_once_with(
3184 self
.server
, floating_ips
, floating_network
, created_items
3187 @patch("random.shuffle")
3188 @patch.object(vimconnector
, "_find_floating_ip")
3189 def test_get_free_floating_ip_list_floating_ip_exception(
3190 self
, mock_find_floating_ip
, mock_shuffle
3192 """Neutron list floating IPs raises exception."""
3193 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3195 self
.vimconn
.neutron
= CopyingMock()
3196 self
.vimconn
.neutron
.list_floatingips
.side_effect
= Exception(
3197 "Floating ips could not be listed."
3199 with self
.assertRaises(Exception) as err
:
3200 result
= self
.vimconn
._get
_free
_floating
_ip
(
3201 self
.server
, floating_network
, created_items
3203 self
.assertEqual(result
, None)
3204 self
.assertEqual(str(err
.exception
), "Floating ips could not be listed.")
3205 mock_shuffle
.assert_not_called()
3206 mock_find_floating_ip
.assert_not_called()
3208 @patch("random.shuffle")
3209 @patch.object(vimconnector
, "_find_floating_ip")
3210 def test_get_free_floating_ip_find_floating_ip_exception(
3211 self
, mock_find_floating_ip
, mock_shuffle
3213 """_find_floating_ip method raises exception."""
3214 floating_network
= {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"}
3218 "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3219 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3220 "id": "508b73-o9cc-5a6a-a270-72cc4811bd8",
3221 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3224 "port_id": "508b73-r9cc-5a6a-5270-o2cc4811bd4a",
3225 "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a",
3226 "id": "208b73-o9cc-5a6a-a270-52cc4811bd8",
3227 "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a",
3230 self
.vimconn
.neutron
= CopyingMock()
3231 self
.vimconn
.neutron
.list_floatingips
.return_value
= {
3232 "floatingips": floating_ips
3234 mock_find_floating_ip
.side_effect
= Exception(
3235 "Free floating ip could not be found."
3238 with self
.assertRaises(Exception) as err
:
3239 result
= self
.vimconn
._get
_free
_floating
_ip
(
3240 self
.server
, floating_network
, created_items
3242 self
.assertEqual(result
, None)
3243 self
.assertEqual(str(err
.exception
), "Free floating ip could not be found.")
3244 mock_shuffle
.assert_called_once_with(floating_ips
)
3245 mock_find_floating_ip
.assert_called_once_with(
3246 self
.server
, floating_ips
, floating_network
, created_items
3249 @patch.object(vimconnector
, "_create_floating_ip")
3250 @patch.object(vimconnector
, "_get_free_floating_ip")
3251 @patch.object(vimconnector
, "_assign_floating_ip")
3252 def test_prepare_external_network_for_vm_instance(
3254 mock_assign_floating_ip
,
3255 mock_get_free_floating_ip
,
3256 mock_create_floating_ip
,
3258 """Prepare external network successfully."""
3259 external_network
= [
3261 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3262 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3266 vm_start_time
= time_return_value
3267 mock_get_free_floating_ip
.side_effect
= ["y08b73-o9cc-1a6a-a270-12cc4811bd4u"]
3268 mock_assign_floating_ip
.return_value
= {
3269 "floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}
3271 self
.vimconn
.neutron
= CopyingMock()
3272 self
.vimconn
.nova
= CopyingMock()
3273 self
.vimconn
.neutron
.show_floatingip
.return_value
= {
3274 "floatingip": {"port_id": ""}
3277 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3278 external_network
, self
.server
, created_items
, vm_start_time
3281 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 1)
3282 mock_get_free_floating_ip
.assert_called_once_with(
3285 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3286 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3290 self
.vimconn
.neutron
.show_floatingip
.assert_called_once_with(
3291 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
3293 self
.vimconn
.nova
.servers
.get
.assert_not_called()
3294 mock_create_floating_ip
.assert_not_called()
3295 mock_assign_floating_ip
.assert_called_once_with(
3296 "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3298 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3299 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3304 @patch("time.sleep")
3305 @patch.object(vimconnector
, "_create_floating_ip")
3306 @patch.object(vimconnector
, "_get_free_floating_ip")
3307 @patch.object(vimconnector
, "_assign_floating_ip")
3308 def test_prepare_external_network_for_vm_instance_no_free_floating_ip(
3310 mock_assign_floating_ip
,
3311 mock_get_free_floating_ip
,
3312 mock_create_floating_ip
,
3316 """There is not any free floating ip."""
3317 floating_network
= {
3318 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3319 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3321 external_network
= [floating_network
]
3324 vm_start_time
= time_return_value
3325 mock_get_free_floating_ip
.return_value
= None
3326 mock_assign_floating_ip
.return_value
= {}
3327 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ERROR"
3328 self
.vimconn
.neutron
.show_floatingip
.return_value
= {}
3330 with self
.assertRaises(KeyError):
3331 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3332 external_network
, self
.server
, created_items
, vm_start_time
3335 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 4)
3336 mock_get_free_floating_ip
.assert_called_with(
3339 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3340 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3344 self
.vimconn
.neutron
.show_floatingip
.assert_called_with(None)
3345 mock_sleep
.assert_not_called()
3346 mock_time
.assert_not_called()
3347 self
.assertEqual(self
.vimconn
.nova
.servers
.get
.call_count
, 4)
3348 mock_create_floating_ip
.assert_called_with(
3349 floating_network
, self
.server
, created_items
3351 self
.assertEqual(mock_create_floating_ip
.call_count
, 4)
3352 mock_assign_floating_ip
.assert_not_called()
3353 self
.vimconn
.nova
.servers
.get
.assert_called_with(self
.server
.id)
3356 @patch("time.sleep")
3357 @patch.object(vimconnector
, "_create_floating_ip")
3358 @patch.object(vimconnector
, "_get_free_floating_ip")
3359 @patch.object(vimconnector
, "_assign_floating_ip")
3360 def test_prepare_external_network_for_vm_instance_no_free_fip_can_not_create_fip_exit_on_error_false(
3362 mock_assign_floating_ip
,
3363 mock_get_free_floating_ip
,
3364 mock_create_floating_ip
,
3368 """There is not any free floating ip, create_floating ip method raise exception
3369 exit_on_floating_ip_error set to False."""
3370 floating_network
= {
3371 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3372 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3373 "exit_on_floating_ip_error": False,
3375 external_network
= [floating_network
]
3378 vm_start_time
= time_return_value
3379 mock_get_free_floating_ip
.return_value
= None
3380 mock_assign_floating_ip
.return_value
= {}
3381 mock_create_floating_ip
.side_effect
= VimConnException(
3382 "Can not create floating ip."
3384 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ERROR"
3385 self
.vimconn
.neutron
.show_floatingip
.return_value
= {}
3387 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3388 external_network
, self
.server
, created_items
, vm_start_time
3390 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 1)
3391 mock_get_free_floating_ip
.assert_called_with(
3394 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3395 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3396 "exit_on_floating_ip_error": False,
3400 self
.vimconn
.neutron
.show_floatingip
.assert_not_called()
3401 mock_sleep
.assert_not_called()
3402 mock_time
.assert_not_called()
3403 self
.vimconn
.nova
.servers
.get
.assert_not_called()
3404 mock_create_floating_ip
.assert_called_with(
3405 floating_network
, self
.server
, created_items
3407 self
.assertEqual(mock_create_floating_ip
.call_count
, 1)
3408 mock_assign_floating_ip
.assert_not_called()
3411 @patch("time.sleep")
3412 @patch.object(vimconnector
, "_create_floating_ip")
3413 @patch.object(vimconnector
, "_get_free_floating_ip")
3414 @patch.object(vimconnector
, "_assign_floating_ip")
3415 def test_prepare_external_network_for_vm_instance_no_free_fip_can_not_create_fip_exit_on_error_true(
3417 mock_assign_floating_ip
,
3418 mock_get_free_floating_ip
,
3419 mock_create_floating_ip
,
3423 """There is not any free floating ip, create_floating ip method raise exception
3424 exit_on_floating_ip_error set to False."""
3425 floating_network
= {
3426 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3427 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3428 "exit_on_floating_ip_error": True,
3430 external_network
= [floating_network
]
3433 vm_start_time
= time_return_value
3434 mock_get_free_floating_ip
.return_value
= None
3435 mock_assign_floating_ip
.return_value
= {}
3436 mock_create_floating_ip
.side_effect
= VimConnException(
3437 "Can not create floating ip."
3439 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ERROR"
3440 self
.vimconn
.neutron
.show_floatingip
.return_value
= {}
3441 with self
.assertRaises(VimConnException
):
3442 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3443 external_network
, self
.server
, created_items
, vm_start_time
3445 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 1)
3446 mock_get_free_floating_ip
.assert_called_with(
3449 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3450 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3451 "exit_on_floating_ip_error": True,
3455 self
.vimconn
.neutron
.show_floatingip
.assert_not_called()
3456 mock_sleep
.assert_not_called()
3457 mock_time
.assert_not_called()
3458 self
.vimconn
.nova
.servers
.get
.assert_not_called()
3459 mock_create_floating_ip
.assert_called_with(
3460 floating_network
, self
.server
, created_items
3462 self
.assertEqual(mock_create_floating_ip
.call_count
, 1)
3463 mock_assign_floating_ip
.assert_not_called()
3465 @patch.object(vimconnector
, "_create_floating_ip")
3466 @patch.object(vimconnector
, "_get_free_floating_ip")
3467 @patch.object(vimconnector
, "_assign_floating_ip")
3468 def test_prepare_external_network_for_vm_instance_fip_has_port_id(
3470 mock_assign_floating_ip
,
3471 mock_get_free_floating_ip
,
3472 mock_create_floating_ip
,
3474 """Neutron show floating ip return the fip with port_id and floating network vim_id
3475 is different from port_id."""
3476 floating_network
= {
3477 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3478 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3480 external_network
= [floating_network
]
3483 mock_get_free_floating_ip
.side_effect
= [
3484 "t08b73-o9cc-1a6a-a270-12cc4811bd4u",
3485 "r08b73-o9cc-1a6a-a270-12cc4811bd4u",
3486 "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3488 mock_assign_floating_ip
.side_effect
= [
3489 {"floatingip": {"port_id": "k08b73-r9cc-5a6a-a270-82cc4811bd4a"}},
3490 {"floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}},
3492 self
.vimconn
.neutron
= CopyingMock()
3493 self
.vimconn
.nova
= CopyingMock()
3494 self
.vimconn
.neutron
.show_floatingip
.side_effect
= [
3495 {"floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}},
3496 {"floatingip": {"port_id": ""}},
3497 {"floatingip": {"port_id": ""}},
3499 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3500 external_network
, self
.server
, created_items
, vm_start_time
3502 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 3)
3503 _call_mock_get_free_floating_ip
= mock_get_free_floating_ip
.call_args_list
3505 _call_mock_get_free_floating_ip
[0][0],
3513 _call_mock_get_free_floating_ip
[1][0],
3521 _call_mock_get_free_floating_ip
[2][0],
3528 self
.assertEqual(self
.vimconn
.neutron
.show_floatingip
.call_count
, 3)
3529 self
.vimconn
.nova
.servers
.get
.assert_not_called()
3530 mock_create_floating_ip
.assert_not_called()
3531 self
.assertEqual(mock_assign_floating_ip
.call_count
, 2)
3532 _call_mock_assign_floating_ip
= mock_assign_floating_ip
.call_args_list
3534 _call_mock_assign_floating_ip
[0][0],
3535 ("r08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network
),
3538 _call_mock_assign_floating_ip
[1][0],
3539 ("y08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network
),
3543 @patch("time.sleep")
3544 @patch.object(vimconnector
, "_create_floating_ip")
3545 @patch.object(vimconnector
, "_get_free_floating_ip")
3546 @patch.object(vimconnector
, "_assign_floating_ip")
3547 def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_vm_status_in_error(
3549 mock_assign_floating_ip
,
3550 mock_get_free_floating_ip
,
3551 mock_create_floating_ip
,
3555 """Neutron show floating ip gives exception, exit_on_floating_ip_error set to True,
3556 VM status is in error."""
3557 floating_network
= {
3558 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3559 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3560 "exit_on_floating_ip_error": True,
3562 external_network
= [floating_network
]
3564 vm_start_time
= time_return_value
3566 mock_time
.side_effect
= [156570150, 156570800, 156571200]
3568 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ERROR"
3569 self
.vimconn
.neutron
.show_floatingip
.side_effect
= [
3570 Exception("Floating ip could not be shown.")
3572 with self
.assertRaises(Exception) as err
:
3573 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3574 external_network
, self
.server
, created_items
, vm_start_time
3578 "Cannot create floating_ip: Exception Floating ip could not be shown.",
3581 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 4)
3582 _call_mock_get_free_floating_ip
= mock_get_free_floating_ip
.call_args_list
3584 _call_mock_get_free_floating_ip
[0][0],
3592 _call_mock_get_free_floating_ip
[1][0],
3600 _call_mock_get_free_floating_ip
[2][0],
3608 _call_mock_get_free_floating_ip
[3][0],
3616 self
.assertEqual(self
.vimconn
.neutron
.show_floatingip
.call_count
, 4)
3617 self
.vimconn
.nova
.servers
.get
.assert_called_with(self
.server
.id)
3618 mock_create_floating_ip
.assert_not_called()
3619 mock_assign_floating_ip
.assert_not_called()
3620 mock_time
.assert_not_called()
3621 mock_sleep
.assert_not_called()
3624 @patch("time.sleep")
3625 @patch.object(vimconnector
, "_create_floating_ip")
3626 @patch.object(vimconnector
, "_get_free_floating_ip")
3627 @patch.object(vimconnector
, "_assign_floating_ip")
3628 def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_vm_status_in_active(
3630 mock_assign_floating_ip
,
3631 mock_get_free_floating_ip
,
3632 mock_create_floating_ip
,
3636 """Neutron show floating ip gives exception, exit_on_floating_ip_error is set to False,
3637 VM status is in active."""
3638 floating_network
= {
3639 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3640 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3641 "exit_on_floating_ip_error": False,
3643 external_network
= [floating_network
]
3645 vm_start_time
= time_return_value
3647 mock_time
.side_effect
= [156570150, 156570800, 156571200]
3649 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ACTIVE"
3650 self
.vimconn
.neutron
.show_floatingip
.side_effect
= [
3651 Exception("Floating ip could not be shown.")
3654 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3655 external_network
, self
.server
, created_items
, vm_start_time
3657 # self.assertEqual(str(err.exception), "Cannot create floating_ip")
3659 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 4)
3660 _call_mock_get_free_floating_ip
= mock_get_free_floating_ip
.call_args_list
3662 _call_mock_get_free_floating_ip
[0][0],
3670 _call_mock_get_free_floating_ip
[1][0],
3678 _call_mock_get_free_floating_ip
[2][0],
3686 _call_mock_get_free_floating_ip
[3][0],
3694 self
.assertEqual(self
.vimconn
.neutron
.show_floatingip
.call_count
, 4)
3695 self
.vimconn
.nova
.servers
.get
.assert_called_with(self
.server
.id)
3696 mock_create_floating_ip
.assert_not_called()
3697 mock_assign_floating_ip
.assert_not_called()
3698 mock_time
.assert_not_called()
3699 mock_sleep
.assert_not_called()
3702 @patch("time.sleep")
3703 @patch.object(vimconnector
, "_create_floating_ip")
3704 @patch.object(vimconnector
, "_get_free_floating_ip")
3705 @patch.object(vimconnector
, "_assign_floating_ip")
3706 def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_exit_on_error(
3708 mock_assign_floating_ip
,
3709 mock_get_free_floating_ip
,
3710 mock_create_floating_ip
,
3714 """Neutron show floating ip gives exception, but exit_on_floating_ip_error is set to True.
3715 VM status is not ACTIVE or ERROR, server timeout happened."""
3716 floating_network
= {
3717 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3718 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3719 "exit_on_floating_ip_error": True,
3721 external_network
= [floating_network
]
3723 vm_start_time
= time_return_value
3724 mock_get_free_floating_ip
.side_effect
= None
3725 mock_time
.side_effect
= [156571790, 156571795, 156571800, 156571805]
3726 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "OTHER"
3727 self
.vimconn
.neutron
.show_floatingip
.side_effect
= [
3728 Exception("Floating ip could not be shown.")
3731 with self
.assertRaises(VimConnException
) as err
:
3732 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3733 external_network
, self
.server
, created_items
, vm_start_time
3737 "Cannot create floating_ip: Exception Floating ip could not be shown.",
3740 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 3)
3741 _call_mock_get_free_floating_ip
= mock_get_free_floating_ip
.call_args_list
3743 _call_mock_get_free_floating_ip
[0][0],
3751 _call_mock_get_free_floating_ip
[1][0],
3759 _call_mock_get_free_floating_ip
[2][0],
3767 self
.assertEqual(self
.vimconn
.neutron
.show_floatingip
.call_count
, 3)
3768 self
.vimconn
.nova
.servers
.get
.assert_called_with(self
.server
.id)
3769 mock_create_floating_ip
.assert_not_called()
3770 mock_assign_floating_ip
.assert_not_called()
3771 self
.assertEqual(mock_time
.call_count
, 3)
3772 self
.assertEqual(mock_sleep
.call_count
, 2)
3775 @patch("time.sleep")
3776 @patch.object(vimconnector
, "_create_floating_ip")
3777 @patch.object(vimconnector
, "_get_free_floating_ip")
3778 @patch.object(vimconnector
, "_assign_floating_ip")
3779 def test_prepare_external_network_for_vm_instance_assign_floating_ip_exception_exit_on_error(
3781 mock_assign_floating_ip
,
3782 mock_get_free_floating_ip
,
3783 mock_create_floating_ip
,
3787 """Assign floating ip method gives exception, exit_on_floating_ip_error is set to True.
3788 VM status is in ERROR."""
3789 floating_network
= {
3790 "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u",
3791 "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a",
3792 "exit_on_floating_ip_error": True,
3794 external_network
= [floating_network
]
3796 vm_start_time
= time_return_value
3798 mock_get_free_floating_ip
.side_effect
= [
3799 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
3802 mock_time
.side_effect
= [156571790, 156571795, 156571800, 156571805]
3804 mock_assign_floating_ip
.side_effect
= [
3805 Exception("Floating ip could not be assigned.")
3808 self
.vimconn
.nova
.servers
.get
.return_value
.status
= "ERROR"
3809 self
.vimconn
.neutron
.show_floatingip
.side_effect
= [
3810 {"floatingip": {"port_id": ""}}
3813 with self
.assertRaises(VimConnException
) as err
:
3814 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3815 external_network
, self
.server
, created_items
, vm_start_time
3819 "Cannot create floating_ip: Exception Floating ip could not be assigned.",
3822 self
.assertEqual(mock_get_free_floating_ip
.call_count
, 4)
3823 _call_mock_get_free_floating_ip
= mock_get_free_floating_ip
.call_args_list
3825 _call_mock_get_free_floating_ip
[0][0],
3833 _call_mock_get_free_floating_ip
[1][0],
3841 _call_mock_get_free_floating_ip
[2][0],
3849 self
.assertEqual(self
.vimconn
.neutron
.show_floatingip
.call_count
, 4)
3850 self
.vimconn
.neutron
.show_floatingip
.assert_called_with(
3851 "y08b73-o9cc-1a6a-a270-12cc4811bd4u"
3853 self
.assertEqual(self
.vimconn
.nova
.servers
.get
.call_count
, 4)
3854 self
.vimconn
.nova
.servers
.get
.assert_called_with(self
.server
.id)
3855 mock_time
.assert_not_called()
3856 mock_sleep
.assert_not_called()
3857 mock_create_floating_ip
.assert_not_called()
3860 @patch("time.sleep")
3861 @patch.object(vimconnector
, "_create_floating_ip")
3862 @patch.object(vimconnector
, "_get_free_floating_ip")
3863 @patch.object(vimconnector
, "_assign_floating_ip")
3864 def test_prepare_external_network_for_vm_instance_empty_external_network_list(
3866 mock_assign_floating_ip
,
3867 mock_get_free_floating_ip
,
3868 mock_create_floating_ip
,
3872 """External network list is empty."""
3873 external_network
= []
3875 vm_start_time
= time_return_value
3877 self
.vimconn
._prepare
_external
_network
_for
_vminstance
(
3878 external_network
, self
.server
, created_items
, vm_start_time
3880 mock_create_floating_ip
.assert_not_called()
3881 mock_time
.assert_not_called()
3882 mock_sleep
.assert_not_called()
3883 mock_assign_floating_ip
.assert_not_called()
3884 mock_get_free_floating_ip
.assert_not_called()
3885 self
.vimconn
.neutron
.show
.show_floatingip
.assert_not_called()
3886 self
.vimconn
.nova
.servers
.get
.assert_not_called()
3888 @patch.object(vimconnector
, "_vimconnector__wait_for_vm")
3889 def test_update_port_security_for_vm_instance(self
, mock_wait_for_vm
):
3890 """no_secured_ports has port and the port has allow-address-pairs."""
3891 no_secured_ports
= [(port2_id
, "allow-address-pairs")]
3893 self
.vimconn
._update
_port
_security
_for
_vminstance
(no_secured_ports
, self
.server
)
3895 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3897 self
.vimconn
.neutron
.update_port
.assert_called_once_with(
3899 {"port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}},
3902 @patch.object(vimconnector
, "_vimconnector__wait_for_vm")
3903 def test_update_port_security_for_vm_instance_no_allowed_address_pairs(
3904 self
, mock_wait_for_vm
3906 """no_secured_ports has port and the port does not have allow-address-pairs."""
3907 no_secured_ports
= [(port2_id
, "something")]
3909 self
.vimconn
._update
_port
_security
_for
_vminstance
(no_secured_ports
, self
.server
)
3911 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3913 self
.vimconn
.neutron
.update_port
.assert_called_once_with(
3915 {"port": {"port_security_enabled": False, "security_groups": None}},
3918 @patch.object(vimconnector
, "_vimconnector__wait_for_vm")
3919 def test_update_port_security_for_vm_instance_wait_for_vm_raise_exception(
3920 self
, mock_wait_for_vm
3922 """__wait_for_vm raises timeout exception."""
3923 no_secured_ports
= [(port2_id
, "something")]
3925 mock_wait_for_vm
.side_effect
= VimConnException("Timeout waiting for instance.")
3927 with self
.assertRaises(VimConnException
) as err
:
3928 self
.vimconn
._update
_port
_security
_for
_vminstance
(
3929 no_secured_ports
, self
.server
3931 self
.assertEqual(str(err
.exception
), "Timeout waiting for instance.")
3933 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3935 self
.vimconn
.neutron
.update_port
.assert_not_called()
3937 @patch.object(vimconnector
, "_vimconnector__wait_for_vm")
3938 def test_update_port_security_for_vm_instance_neutron_update_port_raise_exception(
3939 self
, mock_wait_for_vm
3941 """neutron_update_port method raises exception."""
3942 no_secured_ports
= [(port2_id
, "something")]
3944 self
.vimconn
.neutron
.update_port
.side_effect
= Exception(
3945 "Port security could not be updated."
3948 with self
.assertRaises(VimConnException
) as err
:
3949 self
.vimconn
._update
_port
_security
_for
_vminstance
(
3950 no_secured_ports
, self
.server
3954 "It was not possible to disable port security for port 17472685-f67f-49fd-8722-eabb7692fc22",
3956 mock_wait_for_vm
.assert_called_once_with(self
.server
.id, "ACTIVE")
3958 self
.vimconn
.neutron
.update_port
.assert_called_once_with(
3960 {"port": {"port_security_enabled": False, "security_groups": None}},
3963 @patch.object(vimconnector
, "_vimconnector__wait_for_vm")
3964 def test_update_port_security_for_vm_instance_empty_port_list(
3965 self
, mock_wait_for_vm
3967 """no_secured_ports list does not have any ports."""
3968 no_secured_ports
= []
3970 self
.vimconn
._update
_port
_security
_for
_vminstance
(no_secured_ports
, self
.server
)
3972 mock_wait_for_vm
.assert_not_called()
3974 self
.vimconn
.neutron
.update_port
.assert_not_called()
3977 @patch.object(vimconnector
, "_reload_connection")
3978 @patch.object(vimconnector
, "_prepare_network_for_vminstance")
3979 @patch.object(vimconnector
, "_create_user_data")
3980 @patch.object(vimconnector
, "_get_vm_availability_zone")
3981 @patch.object(vimconnector
, "_prepare_disk_for_vminstance")
3982 @patch.object(vimconnector
, "_update_port_security_for_vminstance")
3983 @patch.object(vimconnector
, "_prepare_external_network_for_vminstance")
3984 @patch.object(vimconnector
, "delete_vminstance")
3985 @patch.object(vimconnector
, "_format_exception")
3986 def test_new_vm_instance(
3988 mock_format_exception
,
3989 mock_delete_vm_instance
,
3990 mock_prepare_external_network
,
3991 mock_update_port_security
,
3992 mock_prepare_disk_for_vm_instance
,
3993 mock_get_vm_availability_zone
,
3994 mock_create_user_data
,
3995 mock_prepare_network_for_vm_instance
,
3996 mock_reload_connection
,
3999 """New VM instance creation is successful."""
4001 mock_create_user_data
.return_value
= True, "userdata"
4003 mock_get_vm_availability_zone
.return_value
= "nova"
4005 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
4007 mock_time
.return_value
= time_return_value
4009 expected_result
= self
.server
.id, {}
4011 result
= self
.vimconn
.new_vminstance(
4017 affinity_group_list
,
4021 availability_zone_index
,
4022 availability_zone_list
,
4024 self
.assertEqual(result
, expected_result
)
4026 mock_reload_connection
.assert_called_once()
4027 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4032 external_network
=[],
4033 no_secured_ports
=[],
4035 mock_create_user_data
.assert_called_once_with(cloud_config
)
4036 mock_get_vm_availability_zone
.assert_called_once_with(
4037 availability_zone_index
, availability_zone_list
4039 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4041 existing_vim_volumes
=[],
4044 block_device_mapping
={},
4045 disk_list
=disk_list2
,
4047 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4052 security_groups
="default",
4053 availability_zone
="nova",
4054 key_name
="my_keypair",
4055 userdata
="userdata",
4057 block_device_mapping
={},
4060 mock_time
.assert_called_once()
4061 mock_update_port_security
.assert_called_once_with([], self
.server
)
4062 mock_prepare_external_network
.assert_called_once_with(
4063 external_network
=[],
4066 vm_start_time
=time_return_value
,
4068 mock_delete_vm_instance
.assert_not_called()
4069 mock_format_exception
.assert_not_called()
4072 @patch.object(vimconnector
, "_reload_connection")
4073 @patch.object(vimconnector
, "_prepare_network_for_vminstance")
4074 @patch.object(vimconnector
, "_create_user_data")
4075 @patch.object(vimconnector
, "_get_vm_availability_zone")
4076 @patch.object(vimconnector
, "_prepare_disk_for_vminstance")
4077 @patch.object(vimconnector
, "_update_port_security_for_vminstance")
4078 @patch.object(vimconnector
, "_prepare_external_network_for_vminstance")
4079 @patch.object(vimconnector
, "delete_vminstance")
4080 @patch.object(vimconnector
, "_format_exception")
4081 def test_new_vm_instance_create_user_data_fails(
4083 mock_format_exception
,
4084 mock_delete_vm_instance
,
4085 mock_prepare_external_network
,
4086 mock_update_port_security
,
4087 mock_prepare_disk_for_vm_instance
,
4088 mock_get_vm_availability_zone
,
4089 mock_create_user_data
,
4090 mock_prepare_network_for_vm_instance
,
4091 mock_reload_connection
,
4094 """New VM instance creation failed because of user data creation failure."""
4096 mock_create_user_data
.side_effect
= Exception(
4097 "User data could not be retrieved."
4100 mock_get_vm_availability_zone
.return_value
= "nova"
4102 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
4104 mock_time
.return_value
= time_return_value
4106 self
.vimconn
.new_vminstance(
4112 affinity_group_list
,
4116 availability_zone_index
,
4117 availability_zone_list
,
4120 mock_reload_connection
.assert_called_once()
4121 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4126 external_network
=[],
4127 no_secured_ports
=[],
4129 mock_create_user_data
.assert_called_once_with(cloud_config
)
4130 mock_get_vm_availability_zone
.assert_not_called()
4131 mock_prepare_disk_for_vm_instance
.assert_not_called()
4132 self
.vimconn
.nova
.servers
.create
.assert_not_called()
4133 mock_time
.assert_not_called()
4134 mock_update_port_security
.assert_not_called()
4135 mock_prepare_external_network
.assert_not_called()
4136 mock_delete_vm_instance
.assert_called_once_with(None, {})
4137 mock_format_exception
.assert_called_once()
4138 arg
= mock_format_exception
.call_args
[0][0]
4139 self
.assertEqual(str(arg
), "User data could not be retrieved.")
4142 @patch.object(vimconnector
, "_reload_connection")
4143 @patch.object(vimconnector
, "_prepare_network_for_vminstance")
4144 @patch.object(vimconnector
, "_create_user_data")
4145 @patch.object(vimconnector
, "_get_vm_availability_zone")
4146 @patch.object(vimconnector
, "_prepare_disk_for_vminstance")
4147 @patch.object(vimconnector
, "_update_port_security_for_vminstance")
4148 @patch.object(vimconnector
, "_prepare_external_network_for_vminstance")
4149 @patch.object(vimconnector
, "delete_vminstance")
4150 @patch.object(vimconnector
, "_format_exception")
4151 def test_new_vm_instance_external_network_exception(
4153 mock_format_exception
,
4154 mock_delete_vm_instance
,
4155 mock_prepare_external_network
,
4156 mock_update_port_security
,
4157 mock_prepare_disk_for_vm_instance
,
4158 mock_get_vm_availability_zone
,
4159 mock_create_user_data
,
4160 mock_prepare_network_for_vm_instance
,
4161 mock_reload_connection
,
4164 """New VM instance creation, external network connection has failed as floating
4165 ip could not be created."""
4167 mock_create_user_data
.return_value
= True, "userdata"
4169 mock_get_vm_availability_zone
.return_value
= "nova"
4171 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
4173 mock_time
.return_value
= time_return_value
4175 mock_prepare_external_network
.side_effect
= VimConnException(
4176 "Can not create floating ip."
4179 self
.vimconn
.new_vminstance(
4185 affinity_group_list
,
4189 availability_zone_index
,
4190 availability_zone_list
,
4193 mock_reload_connection
.assert_called_once()
4194 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4199 external_network
=[],
4200 no_secured_ports
=[],
4202 mock_create_user_data
.assert_called_once_with(cloud_config
)
4203 mock_get_vm_availability_zone
.assert_called_once_with(
4204 availability_zone_index
, availability_zone_list
4206 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4208 existing_vim_volumes
=[],
4211 block_device_mapping
={},
4212 disk_list
=disk_list2
,
4214 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4219 security_groups
="default",
4220 availability_zone
="nova",
4221 key_name
="my_keypair",
4222 userdata
="userdata",
4224 block_device_mapping
={},
4227 mock_time
.assert_called_once()
4228 mock_update_port_security
.assert_called_once_with([], self
.server
)
4229 mock_prepare_external_network
.assert_called_once_with(
4230 external_network
=[],
4233 vm_start_time
=time_return_value
,
4235 mock_delete_vm_instance
.assert_called_once_with(self
.server
.id, {})
4236 mock_format_exception
.assert_called_once()
4237 arg
= mock_format_exception
.call_args
[0][0]
4238 self
.assertEqual(str(arg
), "Can not create floating ip.")
4241 @patch.object(vimconnector
, "_reload_connection")
4242 @patch.object(vimconnector
, "_prepare_network_for_vminstance")
4243 @patch.object(vimconnector
, "_create_user_data")
4244 @patch.object(vimconnector
, "_get_vm_availability_zone")
4245 @patch.object(vimconnector
, "_prepare_disk_for_vminstance")
4246 @patch.object(vimconnector
, "_update_port_security_for_vminstance")
4247 @patch.object(vimconnector
, "_prepare_external_network_for_vminstance")
4248 @patch.object(vimconnector
, "delete_vminstance")
4249 @patch.object(vimconnector
, "_format_exception")
4250 def test_new_vm_instance_with_affinity_group(
4252 mock_format_exception
,
4253 mock_delete_vm_instance
,
4254 mock_prepare_external_network
,
4255 mock_update_port_security
,
4256 mock_prepare_disk_for_vm_instance
,
4257 mock_get_vm_availability_zone
,
4258 mock_create_user_data
,
4259 mock_prepare_network_for_vm_instance
,
4260 mock_reload_connection
,
4263 """New VM creation with affinity group."""
4264 affinity_group_list
= [
4265 {"affinity_group_id": "38b73-e9cc-5a6a-t270-82cc4811bd4a"}
4267 mock_create_user_data
.return_value
= True, "userdata"
4268 mock_get_vm_availability_zone
.return_value
= "nova"
4269 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
4270 mock_time
.return_value
= time_return_value
4271 expected_result
= self
.server
.id, {}
4273 result
= self
.vimconn
.new_vminstance(
4279 affinity_group_list
,
4283 availability_zone_index
,
4284 availability_zone_list
,
4286 self
.assertEqual(result
, expected_result
)
4288 mock_reload_connection
.assert_called_once()
4289 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4294 external_network
=[],
4295 no_secured_ports
=[],
4297 mock_create_user_data
.assert_called_once_with(cloud_config
)
4298 mock_get_vm_availability_zone
.assert_called_once_with(
4299 availability_zone_index
, availability_zone_list
4301 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4303 existing_vim_volumes
=[],
4306 block_device_mapping
={},
4307 disk_list
=disk_list2
,
4309 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4314 security_groups
="default",
4315 availability_zone
="nova",
4316 key_name
="my_keypair",
4317 userdata
="userdata",
4319 block_device_mapping
={},
4320 scheduler_hints
={"group": "38b73-e9cc-5a6a-t270-82cc4811bd4a"},
4322 mock_time
.assert_called_once()
4323 mock_update_port_security
.assert_called_once_with([], self
.server
)
4324 mock_prepare_external_network
.assert_called_once_with(
4325 external_network
=[],
4328 vm_start_time
=time_return_value
,
4330 mock_delete_vm_instance
.assert_not_called()
4331 mock_format_exception
.assert_not_called()
4334 @patch.object(vimconnector
, "_reload_connection")
4335 @patch.object(vimconnector
, "_prepare_network_for_vminstance")
4336 @patch.object(vimconnector
, "_create_user_data")
4337 @patch.object(vimconnector
, "_get_vm_availability_zone")
4338 @patch.object(vimconnector
, "_prepare_disk_for_vminstance")
4339 @patch.object(vimconnector
, "_update_port_security_for_vminstance")
4340 @patch.object(vimconnector
, "_prepare_external_network_for_vminstance")
4341 @patch.object(vimconnector
, "delete_vminstance")
4342 @patch.object(vimconnector
, "_format_exception")
4343 def test_new_vm_instance_nova_server_create_failed(
4345 mock_format_exception
,
4346 mock_delete_vm_instance
,
4347 mock_prepare_external_network
,
4348 mock_update_port_security
,
4349 mock_prepare_disk_for_vm_instance
,
4350 mock_get_vm_availability_zone
,
4351 mock_create_user_data
,
4352 mock_prepare_network_for_vm_instance
,
4353 mock_reload_connection
,
4356 """New VM(server) creation failed."""
4358 mock_create_user_data
.return_value
= True, "userdata"
4360 mock_get_vm_availability_zone
.return_value
= "nova"
4362 self
.vimconn
.nova
.servers
.create
.side_effect
= Exception(
4363 "Server could not be created."
4366 mock_time
.return_value
= time_return_value
4368 self
.vimconn
.new_vminstance(
4374 affinity_group_list
,
4378 availability_zone_index
,
4379 availability_zone_list
,
4382 mock_reload_connection
.assert_called_once()
4383 mock_prepare_network_for_vm_instance
.assert_called_once_with(
4388 external_network
=[],
4389 no_secured_ports
=[],
4391 mock_create_user_data
.assert_called_once_with(cloud_config
)
4392 mock_get_vm_availability_zone
.assert_called_once_with(
4393 availability_zone_index
, availability_zone_list
4395 mock_prepare_disk_for_vm_instance
.assert_called_once_with(
4397 existing_vim_volumes
=[],
4400 block_device_mapping
={},
4401 disk_list
=disk_list2
,
4404 self
.vimconn
.nova
.servers
.create
.assert_called_once_with(
4409 security_groups
="default",
4410 availability_zone
="nova",
4411 key_name
="my_keypair",
4412 userdata
="userdata",
4414 block_device_mapping
={},
4417 mock_time
.assert_not_called()
4418 mock_update_port_security
.assert_not_called()
4419 mock_prepare_external_network
.assert_not_called()
4420 mock_delete_vm_instance
.assert_called_once_with(None, {})
4421 mock_format_exception
.assert_called_once()
4422 arg
= mock_format_exception
.call_args
[0][0]
4423 self
.assertEqual(str(arg
), "Server could not be created.")
4426 @patch.object(vimconnector
, "_reload_connection")
4427 @patch.object(vimconnector
, "_prepare_network_for_vminstance")
4428 @patch.object(vimconnector
, "_create_user_data")
4429 @patch.object(vimconnector
, "_get_vm_availability_zone")
4430 @patch.object(vimconnector
, "_prepare_disk_for_vminstance")
4431 @patch.object(vimconnector
, "_update_port_security_for_vminstance")
4432 @patch.object(vimconnector
, "_prepare_external_network_for_vminstance")
4433 @patch.object(vimconnector
, "delete_vminstance")
4434 @patch.object(vimconnector
, "_format_exception")
4435 def test_new_vm_instance_connection_exception(
4437 mock_format_exception
,
4438 mock_delete_vm_instance
,
4439 mock_prepare_external_network
,
4440 mock_update_port_security
,
4441 mock_prepare_disk_for_vm_instance
,
4442 mock_get_vm_availability_zone
,
4443 mock_create_user_data
,
4444 mock_prepare_network_for_vm_instance
,
4445 mock_reload_connection
,
4448 """Connection to Cloud API has failed."""
4449 mock_reload_connection
.side_effect
= Exception("Can not connect to Cloud APIs.")
4450 mock_create_user_data
.return_value
= True, "userdata"
4451 mock_get_vm_availability_zone
.return_value
= "nova"
4452 self
.vimconn
.nova
.servers
.create
.return_value
= self
.server
4453 mock_time
.return_value
= time_return_value
4455 self
.vimconn
.new_vminstance(
4461 affinity_group_list
,
4465 availability_zone_index
,
4466 availability_zone_list
,
4468 mock_format_exception
.assert_called_once()
4469 arg
= mock_format_exception
.call_args
[0][0]
4470 self
.assertEqual(str(arg
), "Can not connect to Cloud APIs.")
4471 mock_reload_connection
.assert_called_once()
4472 mock_prepare_network_for_vm_instance
.assert_not_called()
4473 mock_create_user_data
.assert_not_called()
4474 mock_get_vm_availability_zone
.assert_not_called()
4475 mock_prepare_disk_for_vm_instance
.assert_not_called()
4476 self
.vimconn
.nova
.servers
.create
.assert_not_called()
4477 mock_time
.assert_not_called()
4478 mock_update_port_security
.assert_not_called()
4479 mock_prepare_external_network
.assert_not_called()
4480 mock_delete_vm_instance
.assert_called_once_with(None, {})
4483 if __name__
== "__main__":