3232from neutron_lib import context
3333from neutron_lib import exceptions as n_exc
3434from neutron_lib .plugins import directory
35+ from neutron_lib .plugins import utils as p_utils
3536from neutron_lib .tests import tools
3637from neutron_lib .utils import net as n_net
3738from oslo_concurrency import processutils
@@ -1736,7 +1737,8 @@ def test_update_subnet_postcommit_enable_dhcp(self):
17361737 self .mech_driver .update_subnet_postcommit (context )
17371738 esd .assert_called_once_with (
17381739 context .current , context .network .current , mock .ANY )
1739- umd .assert_called_once_with (mock .ANY , 'id' , subnet = subnet )
1740+ umd .assert_called_once_with (mock .ANY , context .network .current ,
1741+ subnet = subnet )
17401742
17411743 def test_update_subnet_postcommit_disable_dhcp (self ):
17421744 self .mech_driver .nb_ovn .get_subnet_dhcp_options .return_value = {
@@ -1752,7 +1754,8 @@ def test_update_subnet_postcommit_disable_dhcp(self):
17521754 'update_metadata_port' ) as umd :
17531755 self .mech_driver .update_subnet_postcommit (context )
17541756 dsd .assert_called_once_with (context .current ['id' ], mock .ANY )
1755- umd .assert_called_once_with (mock .ANY , 'id' , subnet = subnet )
1757+ umd .assert_called_once_with (mock .ANY , context .network .current ,
1758+ subnet = subnet )
17561759
17571760 def test_update_subnet_postcommit_update_dhcp (self ):
17581761 self .mech_driver .nb_ovn .get_subnet_dhcp_options .return_value = {
@@ -1769,7 +1772,8 @@ def test_update_subnet_postcommit_update_dhcp(self):
17691772 self .mech_driver .update_subnet_postcommit (context )
17701773 usd .assert_called_once_with (
17711774 context .current , context .network .current , mock .ANY )
1772- umd .assert_called_once_with (mock .ANY , 'id' , subnet = subnet )
1775+ umd .assert_called_once_with (mock .ANY , context .network .current ,
1776+ subnet = subnet )
17731777
17741778 def test__get_port_options (self ):
17751779 with mock .patch .object (self .mech_driver ._plugin , 'get_subnets' ) as \
@@ -1955,9 +1959,10 @@ def test_update_metadata_port_with_subnet(self):
19551959 mock_metaport .return_value = {'fixed_ips' : fixed_ips ,
19561960 'id' : 'metadata_id' }
19571961 mock_get_subnets .return_value = [{'id' : 'subnet1' }]
1962+ network = {'id' : 'net_id' }
19581963 subnet = {'id' : 'subnet1' , 'enable_dhcp' : True }
19591964 self .mech_driver ._ovn_client .update_metadata_port (
1960- self .context , 'net_id' , subnet = subnet )
1965+ self .context , network , subnet = subnet )
19611966 mock_update_port .assert_not_called ()
19621967
19631968 # Subnet without DHCP, present in port.
@@ -1967,7 +1972,7 @@ def test_update_metadata_port_with_subnet(self):
19671972 mock_get_subnets .return_value = [{'id' : 'subnet1' }]
19681973 subnet = {'id' : 'subnet1' , 'enable_dhcp' : False }
19691974 self .mech_driver ._ovn_client .update_metadata_port (
1970- self .context , 'net_id' , subnet = subnet )
1975+ self .context , network , subnet = subnet )
19711976 port = {'id' : 'metadata_id' ,
19721977 'port' : {'network_id' : 'net_id' , 'fixed_ips' : []}}
19731978 mock_update_port .assert_called_once_with (mock .ANY , 'metadata_id' ,
@@ -1980,7 +1985,7 @@ def test_update_metadata_port_with_subnet(self):
19801985 mock_get_subnets .return_value = []
19811986 subnet = {'id' : 'subnet1' , 'enable_dhcp' : True }
19821987 self .mech_driver ._ovn_client .update_metadata_port (
1983- self .context , 'net_id' , subnet = subnet )
1988+ self .context , network , subnet = subnet )
19841989 fixed_ips = [{'subnet_id' : 'subnet1' }]
19851990 port = {'id' : 'metadata_id' ,
19861991 'port' : {'network_id' : 'net_id' , 'fixed_ips' : fixed_ips }}
@@ -1994,7 +1999,7 @@ def test_update_metadata_port_with_subnet(self):
19941999 mock_get_subnets .return_value = []
19952000 subnet = {'id' : 'subnet1' , 'enable_dhcp' : False }
19962001 self .mech_driver ._ovn_client .update_metadata_port (
1997- self .context , 'net_id' , subnet = subnet )
2002+ self .context , network , subnet = subnet )
19982003 mock_update_port .assert_not_called ()
19992004
20002005 def test_update_metadata_port_no_subnet (self ):
@@ -2011,10 +2016,11 @@ def test_update_metadata_port_no_subnet(self):
20112016 mock_get_subnets .return_value = [{'id' : 'subnet1' },
20122017 {'id' : 'subnet2' }]
20132018 fixed_ips = [{'subnet_id' : 'subnet1' , 'ip_address' : 'ip_add1' }]
2019+ network = {'id' : 'net_id' }
20142020 mock_metaport .return_value = {'fixed_ips' : fixed_ips ,
20152021 'id' : 'metadata_id' }
20162022 self .mech_driver ._ovn_client .update_metadata_port (self .context ,
2017- 'net_id' )
2023+ network )
20182024 port = {'id' : 'metadata_id' ,
20192025 'port' : {'network_id' : 'net_id' , 'fixed_ips' : fixed_ips }}
20202026 fixed_ips .append ({'subnet_id' : 'subnet2' })
@@ -2025,10 +2031,11 @@ def test_update_metadata_port_no_subnet(self):
20252031 # Port with IP in subnet1; subnet1 with DHCP, subnet2 without DHCP.
20262032 mock_get_subnets .return_value = [{'id' : 'subnet1' }]
20272033 fixed_ips = [{'subnet_id' : 'subnet1' , 'ip_address' : 'ip_add1' }]
2034+ network = {'id' : 'net_id' }
20282035 mock_metaport .return_value = {'fixed_ips' : fixed_ips ,
20292036 'id' : 'metadata_id' }
20302037 self .mech_driver ._ovn_client .update_metadata_port (self .context ,
2031- 'net_id' )
2038+ network )
20322039 mock_update_port .assert_not_called ()
20332040
20342041 # Port with IP in subnet1; subnet1 without DHCP.
@@ -2037,13 +2044,51 @@ def test_update_metadata_port_no_subnet(self):
20372044 mock_metaport .return_value = {'fixed_ips' : fixed_ips ,
20382045 'id' : 'metadata_id' }
20392046 self .mech_driver ._ovn_client .update_metadata_port (self .context ,
2040- 'net_id' )
2047+ network )
20412048 port = {'id' : 'metadata_id' ,
20422049 'port' : {'network_id' : 'net_id' , 'fixed_ips' : []}}
20432050 mock_update_port .assert_called_once_with (
20442051 mock .ANY , 'metadata_id' , port )
20452052 mock_update_port .reset_mock ()
20462053
2054+ def test_update_metadata_port_no_port (self ):
2055+ ovn_conf .cfg .CONF .set_override ('ovn_metadata_enabled' , True ,
2056+ group = 'ovn' )
2057+
2058+ with mock .patch .object (
2059+ self .mech_driver ._ovn_client , '_find_metadata_port' ) as \
2060+ mock_find_metaport , \
2061+ mock .patch .object (self .mech_driver ._plugin , 'get_subnets' ) as \
2062+ mock_get_subnets , \
2063+ mock .patch .object (p_utils , 'create_port' ) as \
2064+ mock_create_port :
2065+ # Subnet with DHCP, no port, port created.
2066+ network = {'id' : 'net_id' , 'project_id' : 'project_id-foo' }
2067+ subnet = {'id' : 'subnet1' , 'enable_dhcp' : True }
2068+ fixed_ips = [{'subnet_id' : 'subnet1' , 'ip_address' : 'ip_add1' }]
2069+ port = {'id' : 'metadata_id' ,
2070+ 'network_id' : 'net_id' ,
2071+ 'device_owner' : const .DEVICE_OWNER_DISTRIBUTED ,
2072+ 'device_id' : 'ovnmeta-%s' % 'net_id' ,
2073+ 'fixed_ips' : fixed_ips }
2074+ mock_get_subnets .return_value = [subnet ]
2075+ mock_find_metaport .return_value = None
2076+
2077+ # Subnet with DHCP, no port, port create failure.
2078+ mock_create_port .return_value = None
2079+ ret_status = self .mech_driver ._ovn_client .update_metadata_port (
2080+ self .context , network , subnet = subnet )
2081+ self .assertFalse (ret_status )
2082+ mock_create_port .assert_called_once ()
2083+
2084+ # Subnet with DHCP, no port, port created successfully.
2085+ mock_create_port .reset_mock ()
2086+ mock_create_port .return_value = port
2087+ ret_status = self .mech_driver ._ovn_client .update_metadata_port (
2088+ self .context , network , subnet = subnet )
2089+ self .assertTrue (ret_status )
2090+ mock_create_port .assert_called_once ()
2091+
20472092 @mock .patch .object (provisioning_blocks , 'is_object_blocked' )
20482093 @mock .patch .object (provisioning_blocks , 'provisioning_complete' )
20492094 def test_notify_dhcp_updated (self , mock_prov_complete , mock_is_obj_block ):
0 commit comments