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