def test_setup_hm_port(self):
    self.patch('subprocess.check_output', 'check_output')
    self.patch('subprocess.check_call', 'check_call')
    self.patch_object(api_crud, 'get_hm_port')
    self.patch_object(api_crud, 'toggle_hm_port')
    identity_service = mock.MagicMock()
    octavia_charm = mock.MagicMock()
    port_uuid = 'fake-port-uuid'
    port_mac_address = 'fake-mac-address'
    self.get_hm_port.return_value = {'id': port_uuid, 'mac_address': port_mac_address, 'admin_state_up': False, 'binding:vif_type': 'binding_failed', 'status': 'DOWN'}
    e = subprocess.CalledProcessError(returncode=1, cmd=None)
    e.output = 'Device "{}" does not exist.'.format(api_crud.octavia.OCTAVIA_MGMT_INTF)
    self.check_output.side_effect = e
    api_crud.setup_hm_port(identity_service, octavia_charm)
    self.get_hm_port.assert_called_with(identity_service, octavia_charm.local_unit_name, octavia_charm.local_address, host_id=None)
    self.check_output.assert_called_with(['ip', 'link', 'show', api_crud.octavia.OCTAVIA_MGMT_INTF], stderr=(- 2), universal_newlines=True)
    self.check_call.assert_has_calls([mock.call(['ovs-vsctl', '--', 'add-port', api_crud.octavia.OCTAVIA_INT_BRIDGE, api_crud.octavia.OCTAVIA_MGMT_INTF, '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'type=internal', '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:iface-status=active', '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:attached-mac={}'.format(port_mac_address), '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:iface-id={}'.format(port_uuid), '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:skip_cleanup=true']), mock.call(['ip', 'link', 'set', 'o-hm0', 'up', 'address', 'fake-mac-address'])])
    self.check_call.assert_called_with(['ip', 'link', 'set', api_crud.octavia.OCTAVIA_MGMT_INTF, 'up', 'address', port_mac_address])
    self.toggle_hm_port.assert_called
def test_setup_hm_port(self):
    self.patch('subprocess.check_output', 'check_output')
    self.patch('subprocess.check_call', 'check_call')
    self.patch_object(api_crud, 'get_hm_port')
    self.patch_object(api_crud, 'toggle_hm_port')
    identity_service = mock.MagicMock()
    octavia_charm = mock.MagicMock()
    port_uuid = 'fake-port-uuid'
    port_mac_address = 'fake-mac-address'
    self.get_hm_port.return_value = {'id': port_uuid, 'mac_address': port_mac_address, 'admin_state_up': False, 'binding:vif_type': 'binding_failed', 'status': 'DOWN'}
    e = subprocess.CalledProcessError(returncode=1, cmd=None)
    e.output = 'Device "{}" does not exist.'.format(api_crud.octavia.OCTAVIA_MGMT_INTF)
    self.check_output.side_effect = e
    api_crud.setup_hm_port(identity_service, octavia_charm)
    self.get_hm_port.assert_called_with(identity_service, octavia_charm.local_unit_name, octavia_charm.local_address, host_id=None)
    self.check_output.assert_called_with(['ip', 'link', 'show', api_crud.octavia.OCTAVIA_MGMT_INTF], stderr=(- 2), universal_newlines=True)
    self.check_call.assert_has_calls([mock.call(['ovs-vsctl', '--', 'add-port', api_crud.octavia.OCTAVIA_INT_BRIDGE, api_crud.octavia.OCTAVIA_MGMT_INTF, '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'type=internal', '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:iface-status=active', '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:attached-mac={}'.format(port_mac_address), '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:iface-id={}'.format(port_uuid), '--', 'set', 'Interface', api_crud.octavia.OCTAVIA_MGMT_INTF, 'external-ids:skip_cleanup=true']), mock.call(['ip', 'link', 'set', 'o-hm0', 'up', 'address', 'fake-mac-address'])])
    self.check_call.assert_called_with(['ip', 'link', 'set', api_crud.octavia.OCTAVIA_MGMT_INTF, 'up', 'address', port_mac_address])
    self.toggle_hm_port.assert_called