@@ -3383,6 +3383,155 @@ def test_build_network_info_model_empty(
3383
3383
mocked_client .list_ports .assert_called_once_with (
3384
3384
tenant_id = uuids .fake , device_id = uuids .instance )
3385
3385
3386
+ @mock .patch .object (
3387
+ neutronapi .API ,
3388
+ '_get_physnet_tunneled_info' ,
3389
+ new = mock .Mock (return_value = (None , False )))
3390
+ @mock .patch .object (
3391
+ neutronapi .API ,
3392
+ '_get_preexisting_port_ids' ,
3393
+ new = mock .Mock (return_value = []))
3394
+ @mock .patch .object (
3395
+ neutronapi .API ,
3396
+ '_get_subnets_from_port' ,
3397
+ new = mock .Mock (return_value = [model .Subnet (cidr = '1.0.0.0/8' )]))
3398
+ @mock .patch .object (
3399
+ neutronapi .API ,
3400
+ '_get_floating_ips_by_fixed_and_port' ,
3401
+ new = mock .Mock (return_value = [{'floating_ip_address' : '10.0.0.1' }]))
3402
+ @mock .patch .object (neutronapi , 'get_client' )
3403
+ def test_build_network_info_model_full_vnic_type_change (
3404
+ self , mock_get_client
3405
+ ):
3406
+ mocked_client = mock .create_autospec (client .Client )
3407
+ mock_get_client .return_value = mocked_client
3408
+ fake_inst = objects .Instance ()
3409
+ fake_inst .project_id = uuids .fake
3410
+ fake_inst .uuid = uuids .instance
3411
+ fake_ports = [
3412
+ {
3413
+ "id" : "port1" ,
3414
+ "network_id" : "net-id" ,
3415
+ "tenant_id" : uuids .fake ,
3416
+ "admin_state_up" : True ,
3417
+ "status" : "ACTIVE" ,
3418
+ "fixed_ips" : [{"ip_address" : "1.1.1.1" }],
3419
+ "mac_address" : "de:ad:be:ef:00:01" ,
3420
+ "binding:vif_type" : model .VIF_TYPE_BRIDGE ,
3421
+ "binding:vnic_type" : model .VNIC_TYPE_DIRECT ,
3422
+ "binding:vif_details" : {},
3423
+ },
3424
+ ]
3425
+ mocked_client .list_ports .return_value = {'ports' : fake_ports }
3426
+ fake_inst .info_cache = objects .InstanceInfoCache .new (
3427
+ self .context , uuids .instance )
3428
+ fake_inst .info_cache .network_info = model .NetworkInfo .hydrate ([])
3429
+
3430
+ # build the network info first
3431
+ nw_infos = self .api ._build_network_info_model (
3432
+ self .context ,
3433
+ fake_inst ,
3434
+ force_refresh = True ,
3435
+ )
3436
+
3437
+ self .assertEqual (1 , len (nw_infos ))
3438
+ fake_inst .info_cache .network_info = nw_infos
3439
+
3440
+ # change the vnic_type of the port and rebuild the network info
3441
+ fake_ports [0 ]["binding:vnic_type" ] = model .VNIC_TYPE_MACVTAP
3442
+ with mock .patch (
3443
+ "nova.network.neutron.API._log_error_if_vnic_type_changed"
3444
+ ) as mock_log :
3445
+ nw_infos = self .api ._build_network_info_model (
3446
+ self .context ,
3447
+ fake_inst ,
3448
+ force_refresh = True ,
3449
+ )
3450
+
3451
+ mock_log .assert_called_once_with (
3452
+ fake_ports [0 ]["id" ], "direct" , "macvtap" , fake_inst )
3453
+ self .assertEqual (1 , len (nw_infos ))
3454
+
3455
+ @mock .patch .object (
3456
+ neutronapi .API ,
3457
+ '_get_physnet_tunneled_info' ,
3458
+ new = mock .Mock (return_value = (None , False )))
3459
+ @mock .patch .object (
3460
+ neutronapi .API ,
3461
+ '_get_preexisting_port_ids' ,
3462
+ new = mock .Mock (return_value = []))
3463
+ @mock .patch .object (
3464
+ neutronapi .API ,
3465
+ '_get_subnets_from_port' ,
3466
+ new = mock .Mock (return_value = [model .Subnet (cidr = '1.0.0.0/8' )]))
3467
+ @mock .patch .object (
3468
+ neutronapi .API ,
3469
+ '_get_floating_ips_by_fixed_and_port' ,
3470
+ new = mock .Mock (return_value = [{'floating_ip_address' : '10.0.0.1' }]))
3471
+ @mock .patch .object (neutronapi , 'get_client' )
3472
+ def test_build_network_info_model_single_vnic_type_change (
3473
+ self , mock_get_client
3474
+ ):
3475
+ mocked_client = mock .create_autospec (client .Client )
3476
+ mock_get_client .return_value = mocked_client
3477
+ fake_inst = objects .Instance ()
3478
+ fake_inst .project_id = uuids .fake
3479
+ fake_inst .uuid = uuids .instance
3480
+ fake_ports = [
3481
+ {
3482
+ "id" : "port1" ,
3483
+ "network_id" : "net-id" ,
3484
+ "tenant_id" : uuids .fake ,
3485
+ "admin_state_up" : True ,
3486
+ "status" : "ACTIVE" ,
3487
+ "fixed_ips" : [{"ip_address" : "1.1.1.1" }],
3488
+ "mac_address" : "de:ad:be:ef:00:01" ,
3489
+ "binding:vif_type" : model .VIF_TYPE_BRIDGE ,
3490
+ "binding:vnic_type" : model .VNIC_TYPE_DIRECT ,
3491
+ "binding:vif_details" : {},
3492
+ },
3493
+ ]
3494
+ fake_nets = [
3495
+ {
3496
+ "id" : "net-id" ,
3497
+ "name" : "foo" ,
3498
+ "tenant_id" : uuids .fake ,
3499
+ }
3500
+ ]
3501
+ mocked_client .list_ports .return_value = {'ports' : fake_ports }
3502
+ fake_inst .info_cache = objects .InstanceInfoCache .new (
3503
+ self .context , uuids .instance )
3504
+ fake_inst .info_cache .network_info = model .NetworkInfo .hydrate ([])
3505
+
3506
+ # build the network info first
3507
+ nw_infos = self .api ._build_network_info_model (
3508
+ self .context ,
3509
+ fake_inst ,
3510
+ fake_nets ,
3511
+ [fake_ports [0 ]["id" ]],
3512
+ refresh_vif_id = fake_ports [0 ]["id" ],
3513
+ )
3514
+
3515
+ self .assertEqual (1 , len (nw_infos ))
3516
+ fake_inst .info_cache .network_info = nw_infos
3517
+
3518
+ # change the vnic_type of the port and rebuild the network info
3519
+ fake_ports [0 ]["binding:vnic_type" ] = model .VNIC_TYPE_MACVTAP
3520
+ with mock .patch (
3521
+ "nova.network.neutron.API._log_error_if_vnic_type_changed"
3522
+ ) as mock_log :
3523
+ nw_infos = self .api ._build_network_info_model (
3524
+ self .context ,
3525
+ fake_inst ,
3526
+ fake_nets ,
3527
+ [fake_ports [0 ]["id" ]],
3528
+ refresh_vif_id = fake_ports [0 ]["id" ],
3529
+ )
3530
+
3531
+ mock_log .assert_called_once_with (
3532
+ fake_ports [0 ]["id" ], "direct" , "macvtap" , fake_inst )
3533
+ self .assertEqual (1 , len (nw_infos ))
3534
+
3386
3535
@mock .patch .object (neutronapi , 'get_client' )
3387
3536
def test_get_subnets_from_port (self , mock_get_client ):
3388
3537
mocked_client = mock .create_autospec (client .Client )
0 commit comments