def test_get_hm_port(self):
    self.patch_object(api_crud, 'session_from_identity_service')
    self.patch_object(api_crud, 'init_neutron_client')
    nc = mock.MagicMock()
    self.init_neutron_client.return_value = nc
    network_uuid = 'fake-network-uuid'
    nc.list_networks.return_value = {'networks': [{'id': network_uuid}]}
    health_secgrp_uuid = 'fake-secgrp-uuid'
    nc.list_security_groups.return_value = {'security_groups': [{'id': health_secgrp_uuid}]}
    self.patch_object(api_crud.socket, 'gethostname')
    self.gethostname.return_value = 'fakehostname'
    port_uuid = 'fake-port-uuid'
    port_mac_address = 'fake-mac-address'
    nc.create_port.return_value = {'port': {'id': port_uuid, 'mac_address': port_mac_address}}
    self.patch('subprocess.check_output', 'check_output')
    self.patch('charms.reactive.set_flag', 'set_flag')
    identity_service = mock.MagicMock()
    self.patch_object(api_crud, 'neutron_lib')
    self.neutron_lib.constants.DEVICE_OWNER_LOADBALANCERV2 = 'fakeowner'
    self.patch_object(api_crud, 'lookup_hm_port')
    self.lookup_hm_port.return_value = None
    result = api_crud.get_hm_port(identity_service, 'fake-unit-name', '192.0.2.42')
    self.init_neutron_client.assert_called_once_with(self.session_from_identity_service())
    nc.list_networks.assert_called_with(tags='charm-octavia')
    nc.list_security_groups.assert_called_with(tags='charm-octavia-health')
    self.lookup_hm_port.assert_called_once_with(nc, 'fake-unit-name')
    nc.create_port.assert_called_once_with({'port': {'admin_state_up': False, 'binding:host_id': 'fakehostname', 'device_owner': 'fakeowner', 'security_groups': ['fake-secgrp-uuid'], 'name': 'octavia-health-manager-fake-unit-name-listen-port', 'network_id': 'fake-network-uuid'}})
    nc.add_tag.assert_called_with('ports', port_uuid, 'charm-octavia')
    self.assertEqual(result, {'id': 'fake-port-uuid', 'mac_address': 'fake-mac-address'})
    nc.create_port.reset_mock()
    result = api_crud.get_hm_port(identity_service, 'fake-unit-name', '192.0.2.42', host_id='fake-unit-name.fqdn')
    nc.create_port.assert_called_once_with({'port': {'admin_state_up': False, 'binding:host_id': 'fake-unit-name.fqdn', 'device_owner': 'fakeowner', 'security_groups': ['fake-secgrp-uuid'], 'name': 'octavia-health-manager-fake-unit-name-listen-port', 'network_id': 'fake-network-uuid'}})
    self.assertEqual(result, {'id': 'fake-port-uuid', 'mac_address': 'fake-mac-address'})
def test_get_hm_port(self):
    self.patch_object(api_crud, 'session_from_identity_service')
    self.patch_object(api_crud, 'init_neutron_client')
    nc = mock.MagicMock()
    self.init_neutron_client.return_value = nc
    network_uuid = 'fake-network-uuid'
    nc.list_networks.return_value = {'networks': [{'id': network_uuid}]}
    health_secgrp_uuid = 'fake-secgrp-uuid'
    nc.list_security_groups.return_value = {'security_groups': [{'id': health_secgrp_uuid}]}
    self.patch_object(api_crud.socket, 'gethostname')
    self.gethostname.return_value = 'fakehostname'
    port_uuid = 'fake-port-uuid'
    port_mac_address = 'fake-mac-address'
    nc.create_port.return_value = {'port': {'id': port_uuid, 'mac_address': port_mac_address}}
    self.patch('subprocess.check_output', 'check_output')
    self.patch('charms.reactive.set_flag', 'set_flag')
    identity_service = mock.MagicMock()
    self.patch_object(api_crud, 'neutron_lib')
    self.neutron_lib.constants.DEVICE_OWNER_LOADBALANCERV2 = 'fakeowner'
    self.patch_object(api_crud, 'lookup_hm_port')
    self.lookup_hm_port.return_value = None
    result = api_crud.get_hm_port(identity_service, 'fake-unit-name', '192.0.2.42')
    self.init_neutron_client.assert_called_once_with(self.session_from_identity_service())
    nc.list_networks.assert_called_with(tags='charm-octavia')
    nc.list_security_groups.assert_called_with(tags='charm-octavia-health')
    self.lookup_hm_port.assert_called_once_with(nc, 'fake-unit-name')
    nc.create_port.assert_called_once_with({'port': {'admin_state_up': False, 'binding:host_id': 'fakehostname', 'device_owner': 'fakeowner', 'security_groups': ['fake-secgrp-uuid'], 'name': 'octavia-health-manager-fake-unit-name-listen-port', 'network_id': 'fake-network-uuid'}})
    nc.add_tag.assert_called_with('ports', port_uuid, 'charm-octavia')
    self.assertEqual(result, {'id': 'fake-port-uuid', 'mac_address': 'fake-mac-address'})
    nc.create_port.reset_mock()
    result = api_crud.get_hm_port(identity_service, 'fake-unit-name', '192.0.2.42', host_id='fake-unit-name.fqdn')
    nc.create_port.assert_called_once_with({'port': {'admin_state_up': False, 'binding:host_id': 'fake-unit-name.fqdn', 'device_owner': 'fakeowner', 'security_groups': ['fake-secgrp-uuid'], 'name': 'octavia-health-manager-fake-unit-name-listen-port', 'network_id': 'fake-network-uuid'}})
    self.assertEqual(result, {'id': 'fake-port-uuid', 'mac_address': 'fake-mac-address'})