@@ -1818,6 +1818,22 @@ def setUp(self):
18181818 def _sync_resources (self ):
18191819 self .sb_synchronizer .sync_hostname_and_physical_networks (self .ctx )
18201820
1821+ def create_agent (self , host , bridge_mappings = None , agent_type = None ):
1822+ if agent_type is None :
1823+ agent_type = ovn_const .OVN_CONTROLLER_AGENT
1824+ if bridge_mappings is None :
1825+ bridge_mappings = {}
1826+ agent = {
1827+ 'host' : host ,
1828+ 'agent_type' : agent_type ,
1829+ 'binary' : '/bin/test' ,
1830+ 'topic' : 'test_topic' ,
1831+ 'configurations' : {'bridge_mappings' : bridge_mappings }
1832+ }
1833+ _ , status = self .plugin .create_or_update_agent (self .context , agent )
1834+
1835+ return status ['id' ]
1836+
18211837 def create_segment (self , network_id , physical_network , segmentation_id ):
18221838 segment_data = {'network_id' : network_id ,
18231839 'physical_network' : physical_network ,
@@ -1858,6 +1874,7 @@ def test_ovn_sb_sync_delete_stale_host(self):
18581874 segment = self .create_segment (network_id , 'physnet1' , 50 )
18591875 segments_db .update_segment_host_mapping (
18601876 self .ctx , 'host1' , {segment ['id' ]})
1877+ _ = self .create_agent ('host1' , bridge_mappings = {'physnet1' : 'eth0' })
18611878 segment_hosts = segments_db .get_hosts_mapped_with_segments (self .ctx )
18621879 self .assertEqual ({'host1' }, segment_hosts )
18631880 # Since there is no chassis in the sb DB, host1 is the stale host
@@ -1866,6 +1883,36 @@ def test_ovn_sb_sync_delete_stale_host(self):
18661883 segment_hosts = segments_db .get_hosts_mapped_with_segments (self .ctx )
18671884 self .assertFalse (segment_hosts )
18681885
1886+ def test_ovn_sb_sync_host_with_no_agent_not_deleted (self ):
1887+ with self .network () as network :
1888+ network_id = network ['network' ]['id' ]
1889+ segment = self .create_segment (network_id , 'physnet1' , 50 )
1890+ segments_db .update_segment_host_mapping (
1891+ self .ctx , 'host1' , {segment ['id' ]})
1892+ _ = self .create_agent ('host1' , bridge_mappings = {'physnet1' : 'eth0' },
1893+ agent_type = "Not OVN Agent" )
1894+ segment_hosts = segments_db .get_hosts_mapped_with_segments (self .ctx )
1895+ self .assertEqual ({'host1' }, segment_hosts )
1896+ # There is no chassis in the sb DB, host1 does not have an agent
1897+ # so it is not deleted.
1898+ self ._sync_resources ()
1899+ segment_hosts = segments_db .get_hosts_mapped_with_segments (self .ctx )
1900+ self .assertEqual ({'host1' }, segment_hosts )
1901+
1902+ def test_ovn_sb_sync_host_with_other_agent_type_not_deleted (self ):
1903+ with self .network () as network :
1904+ network_id = network ['network' ]['id' ]
1905+ segment = self .create_segment (network_id , 'physnet1' , 50 )
1906+ segments_db .update_segment_host_mapping (
1907+ self .ctx , 'host1' , {segment ['id' ]})
1908+ segment_hosts = segments_db .get_hosts_mapped_with_segments (self .ctx )
1909+ self .assertEqual ({'host1' }, segment_hosts )
1910+ # There is no chassis in the sb DB, host1 does not have an agent
1911+ # so it is not deleted.
1912+ self ._sync_resources ()
1913+ segment_hosts = segments_db .get_hosts_mapped_with_segments (self .ctx )
1914+ self .assertEqual ({'host1' }, segment_hosts )
1915+
18691916 def test_ovn_sb_sync (self ):
18701917 with self .network () as network :
18711918 network_id = network ['network' ]['id' ]
@@ -1878,6 +1925,9 @@ def test_ovn_sb_sync(self):
18781925 segments_db .update_segment_host_mapping (
18791926 self .ctx , 'host3' , {seg1 ['id' ]})
18801927 segment_hosts = segments_db .get_hosts_mapped_with_segments (self .ctx )
1928+ _ = self .create_agent ('host1' )
1929+ _ = self .create_agent ('host2' , bridge_mappings = {'physnet2' : 'eth0' })
1930+ _ = self .create_agent ('host3' , bridge_mappings = {'physnet3' : 'eth0' })
18811931 self .assertEqual ({'host1' , 'host2' , 'host3' }, segment_hosts )
18821932 self .add_fake_chassis ('host2' , ['physnet2' ])
18831933 self .add_fake_chassis ('host3' , ['physnet3' ])
0 commit comments