# Path: f5lbaasdriver/v2/bigip/neutron_client.py
# class F5NetworksNeutronClient(object):
# 
#     def __init__(self, plugin):
#         self.plugin = plugin
# 
#     @log_helpers.log_method_call
#     def create_port_on_subnet(self, context, subnet_id=None,
#                               mac_address=None, ip_address=None,
#                               name="", fixed_address_count=1, host=""):
#         """Create port on subnet."""
#         port = None
# 
#         if not mac_address:
#             mac_address = neutron_const.ATTR_NOT_SPECIFIED
# 
#         if subnet_id:
#             try:
#                 subnet = self.plugin.db._core_plugin.get_subnet(
#                     context,
#                     subnet_id
#                 )
#                 fixed_ip = {'subnet_id': subnet['id']}
#                 if ip_address:
#                     fixed_ip['ip_address'] = ip_address
#                 fixed_ips = [fixed_ip]
# 
#                 port_data = {
#                     'tenant_id': subnet['tenant_id'],
#                     'name': name,
#                     'network_id': subnet['network_id'],
#                     'mac_address': mac_address,
#                     'admin_state_up': True,
#                     'device_id': "",
#                     'device_owner': 'network:f5lbaasv2',
#                     'status': neutron_const.PORT_STATUS_ACTIVE,
#                     'fixed_ips': fixed_ips
#                 }
# 
#                 if ('binding:capabilities' in
#                         portbindings.EXTENDED_ATTRIBUTES_2_0['ports']):
#                     port_data['binding:capabilities'] = {
#                         'port_filter': False}
#                 port = self.plugin.db._core_plugin.create_port(
#                     context, {'port': port_data})
# 
#                 # Because ML2 marks ports DOWN by default on creation
#                 update_data = {
#                     'status': neutron_const.PORT_STATUS_ACTIVE
#                 }
#                 self.plugin.db._core_plugin.update_port(
#                     context, port['id'], {'port': update_data})
# 
#             except Exception as e:
#                 LOG.error("Exception: create_port_on_subnet: %s",
#                           e.message)
#             return port
# 
#     @log_helpers.log_method_call
#     def delete_port(self, context, port_id=None, mac_address=None):
#         """Delete port."""
#         if port_id:
#             self.plugin.db._core_plugin.delete_port(context, port_id)
#         elif mac_address:
#             filters = {'mac_address': [mac_address]}
#             ports = self.plugin.db._core_plugin.get_ports(
#                 context,
#                 filters=filters
#             )
#             for port in ports:
#                 self.plugin.db._core_plugin.delete_port(
#                     context,
#                     port['id']
#                 )


from f5lbaasdriver.v2.bigip.neutron_client import F5NetworksNeutronClient
import mock
import pytest


def test_create_port_on_subnet(f5_neutron_client):
    mock_ctx = mock.MagicMock(name='context')
    f5_neutron_client.create_port_on_subnet(mock_ctx)
    assert not mock_ctx.session.begin.called


@mock.patch('f5lbaasdriver.v2.bigip.neutron_client.LOG')
def test_create_port_on_subnet_error(mock_log):
    mock_plugin = mock.MagicMock(name='plugin')
    mock_plugin.db._core_plugin.update_port.side_effect = Exception('error')
    nc = F5NetworksNeutronClient(mock_plugin)
    mock_ctx = mock.MagicMock(name='context')
    nc.create_port_on_subnet(mock_ctx, subnet_id='id')
    assert mock_log.error.call_args_list == [
        mock.call('Exception: create_port_on_subnet: %s', 'error')]
    assert not mock_ctx.session.begin.called


def test_delete_port(f5_neutron_client):
    mock_ctx = mock.MagicMock(name='context')
    f5_neutron_client.delete_port(mock_ctx, port_id='id')
    assert not mock_ctx.session.begin.called


@mock.patch('f5lbaasdriver.v2.bigip.neutron_client.LOG')
def test_delete_port_error(mock_log):
    mock_plugin = mock.MagicMock(name='plugin')
    mock_plugin.db._core_plugin.delete_port.side_effect = Exception('error')

# Path: f5lbaasdriver/v2/bigip/neutron_client.py
# class F5NetworksNeutronClient(object):
# 
#     def __init__(self, plugin):
#         self.plugin = plugin
# 
#     @log_helpers.log_method_call
#     def create_port_on_subnet(self, context, subnet_id=None,
#                               mac_address=None, ip_address=None,
#                               name="", fixed_address_count=1, host=""):
#         """Create port on subnet."""
#         port = None
# 
#         if not mac_address:
#             mac_address = neutron_const.ATTR_NOT_SPECIFIED
# 
#         if subnet_id:
#             try:
#                 subnet = self.plugin.db._core_plugin.get_subnet(
#                     context,
#                     subnet_id
#                 )
#                 fixed_ip = {'subnet_id': subnet['id']}
#                 if ip_address:
#                     fixed_ip['ip_address'] = ip_address
#                 fixed_ips = [fixed_ip]
# 
#                 port_data = {
#                     'tenant_id': subnet['tenant_id'],
#                     'name': name,
#                     'network_id': subnet['network_id'],
#                     'mac_address': mac_address,
#                     'admin_state_up': True,
#                     'device_id': "",
#                     'device_owner': 'network:f5lbaasv2',
#                     'status': neutron_const.PORT_STATUS_ACTIVE,
#                     'fixed_ips': fixed_ips
#                 }
# 
#                 if ('binding:capabilities' in
#                         portbindings.EXTENDED_ATTRIBUTES_2_0['ports']):
#                     port_data['binding:capabilities'] = {
#                         'port_filter': False}
#                 port = self.plugin.db._core_plugin.create_port(
#                     context, {'port': port_data})
# 
#                 # Because ML2 marks ports DOWN by default on creation
#                 update_data = {
#                     'status': neutron_const.PORT_STATUS_ACTIVE
#                 }
#                 self.plugin.db._core_plugin.update_port(
#                     context, port['id'], {'port': update_data})
# 
#             except Exception as e:
#                 LOG.error("Exception: create_port_on_subnet: %s",
#                           e.message)
#             return port
# 
#     @log_helpers.log_method_call
#     def delete_port(self, context, port_id=None, mac_address=None):
#         """Delete port."""
#         if port_id:
#             self.plugin.db._core_plugin.delete_port(context, port_id)
#         elif mac_address:
#             filters = {'mac_address': [mac_address]}
#             ports = self.plugin.db._core_plugin.get_ports(
#                 context,
#                 filters=filters
#             )
#             for port in ports:
#                 self.plugin.db._core_plugin.delete_port(
#                     context,
#                     port['id']
#                 )


# Path: f5lbaasdriver/v2/bigip/test/test_neutron_client.py
from f5lbaasdriver.v2.bigip.neutron_client import F5NetworksNeutronClient
import mock
import pytest



def test_create_port_on_subnet(f5_neutron_client):
    mock_ctx = mock.MagicMock(name='context')
    f5_neutron_client.create_port_on_subnet(mock_ctx)
    assert not mock_ctx.session.begin.called


@mock.patch('f5lbaasdriver.v2.bigip.neutron_client.LOG')
def test_create_port_on_subnet_error(mock_log):
    mock_plugin = mock.MagicMock(name='plugin')
    mock_plugin.db._core_plugin.update_port.side_effect = Exception('error')
    nc = F5NetworksNeutronClient(mock_plugin)
    mock_ctx = mock.MagicMock(name='context')
    nc.create_port_on_subnet(mock_ctx, subnet_id='id')
    assert mock_log.error.call_args_list == [
        mock.call('Exception: create_port_on_subnet: %s', 'error')]
    assert not mock_ctx.session.begin.called


def test_delete_port(f5_neutron_client):
    mock_ctx = mock.MagicMock(name='context')
    f5_neutron_client.delete_port(mock_ctx, port_id='id')
    assert not mock_ctx.session.begin.called


@mock.patch('f5lbaasdriver.v2.bigip.neutron_client.LOG')
def test_delete_port_error(mock_log):
    mock_plugin = mock.MagicMock(name='plugin')
    mock_plugin.db._core_plugin.delete_port.side_effect = Exception('error')

    nc = F5NetworksNeutronClient(mock_plugin)