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