@@ -7294,7 +7294,9 @@ def do_confirm_resize(mock_save, mock_drop, mock_delete,
7294
7294
mock_confirm , mock_nwapi , mock_notify ,
7295
7295
mock_mig_save , mock_mig_get , mock_inst_get ):
7296
7296
self ._mock_rt ()
7297
- self .instance .migration_context = objects .MigrationContext ()
7297
+ self .instance .migration_context = objects .MigrationContext (
7298
+ new_pci_devices = None ,
7299
+ old_pci_devices = None )
7298
7300
self .migration .source_compute = self .instance ['host' ]
7299
7301
self .migration .source_node = self .instance ['node' ]
7300
7302
self .migration .status = 'confirming'
@@ -7310,6 +7312,7 @@ def do_confirm_resize(mock_save, mock_drop, mock_delete,
7310
7312
7311
7313
do_confirm_resize ()
7312
7314
7315
+ @mock .patch ('nova.objects.MigrationContext.get_pci_mapping_for_migration' )
7313
7316
@mock .patch ('nova.compute.utils.add_instance_fault_from_exc' )
7314
7317
@mock .patch ('nova.objects.Migration.get_by_id' )
7315
7318
@mock .patch ('nova.objects.Instance.get_by_uuid' )
@@ -7318,24 +7321,27 @@ def do_confirm_resize(mock_save, mock_drop, mock_delete,
7318
7321
@mock .patch ('nova.objects.Instance.save' )
7319
7322
def test_confirm_resize_driver_confirm_migration_fails (
7320
7323
self , instance_save , notify_action , notify_usage ,
7321
- instance_get_by_uuid , migration_get_by_id , add_fault ):
7324
+ instance_get_by_uuid , migration_get_by_id , add_fault , get_mapping ):
7322
7325
"""Tests the scenario that driver.confirm_migration raises some error
7323
7326
to make sure the error is properly handled, like the instance and
7324
7327
migration status is set to 'error'.
7325
7328
"""
7326
7329
self .migration .status = 'confirming'
7327
7330
migration_get_by_id .return_value = self .migration
7328
7331
instance_get_by_uuid .return_value = self .instance
7332
+ self .instance .migration_context = objects .MigrationContext ()
7329
7333
7330
7334
error = exception .HypervisorUnavailable (
7331
7335
host = self .migration .source_compute )
7332
7336
with test .nested (
7333
7337
mock .patch .object (self .compute , 'network_api' ),
7334
7338
mock .patch .object (self .compute .driver , 'confirm_migration' ,
7335
7339
side_effect = error ),
7336
- mock .patch .object (self .compute , '_delete_allocation_after_move' )
7340
+ mock .patch .object (self .compute , '_delete_allocation_after_move' ),
7341
+ mock .patch .object (self .compute ,
7342
+ '_get_updated_nw_info_with_pci_mapping' )
7337
7343
) as (
7338
- network_api , confirm_migration , delete_allocation
7344
+ network_api , confirm_migration , delete_allocation , pci_mapping
7339
7345
):
7340
7346
self .assertRaises (exception .HypervisorUnavailable ,
7341
7347
self .compute .confirm_resize ,
@@ -7362,6 +7368,59 @@ def test_confirm_resize_driver_confirm_migration_fails(
7362
7368
instance_get_by_uuid .assert_called_once ()
7363
7369
migration_get_by_id .assert_called_once ()
7364
7370
7371
+ def test_confirm_resize_calls_virt_driver_with_old_pci (self ):
7372
+ @mock .patch .object (self .migration , 'save' )
7373
+ @mock .patch .object (self .compute , '_notify_about_instance_usage' )
7374
+ @mock .patch .object (self .compute , 'network_api' )
7375
+ @mock .patch .object (self .compute .driver , 'confirm_migration' )
7376
+ @mock .patch .object (self .compute , '_delete_allocation_after_move' )
7377
+ @mock .patch .object (self .instance , 'drop_migration_context' )
7378
+ @mock .patch .object (self .instance , 'save' )
7379
+ def do_confirm_resize (mock_save , mock_drop , mock_delete ,
7380
+ mock_confirm , mock_nwapi , mock_notify ,
7381
+ mock_mig_save ):
7382
+ # Mock virt driver confirm_resize() to save the provided
7383
+ # network_info, we will check it later.
7384
+ updated_nw_info = []
7385
+
7386
+ def driver_confirm_resize (* args , ** kwargs ):
7387
+ if 'network_info' in kwargs :
7388
+ nw_info = kwargs ['network_info' ]
7389
+ else :
7390
+ nw_info = args [3 ]
7391
+ updated_nw_info .extend (nw_info )
7392
+
7393
+ mock_confirm .side_effect = driver_confirm_resize
7394
+ self ._mock_rt ()
7395
+ old_devs = objects .PciDeviceList (
7396
+ objects = [objects .PciDevice (
7397
+ address = '0000:04:00.2' ,
7398
+ request_id = uuids .pcidev1 )])
7399
+ new_devs = objects .PciDeviceList (
7400
+ objects = [objects .PciDevice (
7401
+ address = '0000:05:00.3' ,
7402
+ request_id = uuids .pcidev1 )])
7403
+ self .instance .migration_context = objects .MigrationContext (
7404
+ new_pci_devices = new_devs ,
7405
+ old_pci_devices = old_devs )
7406
+ # Create VIF with new_devs[0] PCI address.
7407
+ nw_info = network_model .NetworkInfo ([
7408
+ network_model .VIF (
7409
+ id = uuids .port1 ,
7410
+ vnic_type = network_model .VNIC_TYPE_DIRECT ,
7411
+ profile = {'pci_slot' : new_devs [0 ].address })])
7412
+ mock_nwapi .get_instance_nw_info .return_value = nw_info
7413
+ self .migration .source_compute = self .instance ['host' ]
7414
+ self .migration .source_node = self .instance ['node' ]
7415
+ self .compute ._confirm_resize (self .context , self .instance ,
7416
+ self .migration )
7417
+ # Assert virt driver confirm_migration() was called
7418
+ # with the updated nw_info object.
7419
+ self .assertEqual (old_devs [0 ].address ,
7420
+ updated_nw_info [0 ]['profile' ]['pci_slot' ])
7421
+
7422
+ do_confirm_resize ()
7423
+
7365
7424
def test_delete_allocation_after_move_confirm_by_migration (self ):
7366
7425
with mock .patch .object (self .compute , 'reportclient' ) as mock_report :
7367
7426
mock_report .delete_allocation_for_instance .return_value = True
@@ -8757,6 +8816,24 @@ def doit(mock_pr, mock_r):
8757
8816
8758
8817
doit ()
8759
8818
8819
+ def test_get_updated_nw_info_with_pci_mapping (self ):
8820
+ old_dev = objects .PciDevice (address = '0000:04:00.2' )
8821
+ new_dev = objects .PciDevice (address = '0000:05:00.3' )
8822
+ pci_mapping = {old_dev .address : new_dev }
8823
+ nw_info = network_model .NetworkInfo ([
8824
+ network_model .VIF (
8825
+ id = uuids .port1 ,
8826
+ vnic_type = network_model .VNIC_TYPE_NORMAL ),
8827
+ network_model .VIF (
8828
+ id = uuids .port2 ,
8829
+ vnic_type = network_model .VNIC_TYPE_DIRECT ,
8830
+ profile = {'pci_slot' : old_dev .address })])
8831
+ updated_nw_info = self .compute ._get_updated_nw_info_with_pci_mapping (
8832
+ nw_info , pci_mapping )
8833
+ self .assertDictEqual (nw_info [0 ], updated_nw_info [0 ])
8834
+ self .assertEqual (new_dev .address ,
8835
+ updated_nw_info [1 ]['profile' ]['pci_slot' ])
8836
+
8760
8837
8761
8838
class ComputeManagerInstanceUsageAuditTestCase (test .TestCase ):
8762
8839
def setUp (self ):
0 commit comments