|
| 1 | +import mock |
| 2 | +import pytest |
| 3 | + |
| 4 | +import neutron.api.v2.attributes |
| 5 | +from neutron_lib import constants as neutron_const |
| 6 | + |
| 7 | +import f5lbaasdriver.v2.bigip.plugin_rpc |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +@mock.patch("f5lbaasdriver.v2.bigip.plugin_rpc.LBaaSv2PluginCallbacksRPC." |
| 12 | + "__init__") |
| 13 | +def fully_mocked_target(init): |
| 14 | + init.return_value = None |
| 15 | + target = f5lbaasdriver.v2.bigip.plugin_rpc.LBaaSv2PluginCallbacksRPC() |
| 16 | + return target |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def neutron_attributes(request): |
| 21 | + hold_attributes = neutron.api.v2.attributes |
| 22 | + def finalizer(): |
| 23 | + neutron.api.v2.attributes = hold_attributes |
| 24 | + request.addfinalizer(finalizer) |
| 25 | + neutron.api.v2.attributes = mock.Mock() |
| 26 | + return neutron.api.v2.attributes |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def neutron_extensions(request): |
| 30 | + hold_extensions = neutron.extensions |
| 31 | + def finalizer(): |
| 32 | + neutron.extensions = hold_extensions |
| 33 | + request.addfinalizer(finalizer) |
| 34 | + neutron.extensions = mock.Mock() |
| 35 | + return neutron.extensions |
| 36 | + |
| 37 | +def test_create_port_on_subnet(fully_mocked_target, neutron_attributes, |
| 38 | + neutron_extensions): |
| 39 | + portbindings = neutron_extensions.portbindings |
| 40 | + target = fully_mocked_target |
| 41 | + context = mock.Mock() |
| 42 | + # fake data manipulations.... |
| 43 | + # args... |
| 44 | + fake_args = ['subnet_id', 'mac_address', 'name', 'host', 'device_id', |
| 45 | + 'vnic_type', 'binding_profile'] |
| 46 | + portbindings_attrs = ['VNIC_NORMAL', 'HOST_ID', 'VNIC_TYPE', 'PROFILE'] |
| 47 | + fake_args = {x: x for x in fake_args} |
| 48 | + fake_args['binding_profile'] = {} |
| 49 | + # port bindings... |
| 50 | + for attr in portbindings_attrs: |
| 51 | + setattr(portbindings, attr, attr) |
| 52 | + fake_args['vnic_type'] = portbindings.VNIC_NORMAL |
| 53 | + # fake validation data... |
| 54 | + subnet = {'id': 'subnet_id', 'tenant_id': 'tenant_id', |
| 55 | + 'network_id': 'network_id'} |
| 56 | + port = {'id': 'id'} |
| 57 | + device_ips = [{'subnet_id': subnet['id']}] |
| 58 | + port_data = { |
| 59 | + 'tenant_id': subnet['tenant_id'], |
| 60 | + 'name': fake_args['name'], |
| 61 | + 'network_id': subnet['network_id'], |
| 62 | + 'mac_address': fake_args['mac_address'], |
| 63 | + 'admin_state_up': True, |
| 64 | + 'device_owner': 'network:f5lbaasv2', |
| 65 | + 'status': neutron_const.PORT_STATUS_ACTIVE, |
| 66 | + 'fixed_ips': device_ips, |
| 67 | + 'device_id': fake_args['device_id'], |
| 68 | + 'binding:host_id': fake_args['host'], |
| 69 | + 'binding:vnic_type': portbindings.VNIC_NORMAL, |
| 70 | + 'binding:profile': {}} |
| 71 | + update_data = {'status': neutron_const.PORT_STATUS_ACTIVE} |
| 72 | + expected_create_port_args = (context, {'port': port_data}) |
| 73 | + expected_update_port_args = \ |
| 74 | + (context, port['id'], {'port': update_data}) |
| 75 | + # attach our mock interactions... |
| 76 | + target.driver = mock.Mock() |
| 77 | + target.driver.plugin.db._core_plugin.get_subnet.return_value = subnet |
| 78 | + target.driver.plugin.db._core_plugin.create_port.return_value = port |
| 79 | + # test... |
| 80 | + assert port == target.create_port_on_subnet(context, **fake_args) |
| 81 | + # validate... |
| 82 | + assert target.driver.plugin.db._core_plugin.get_subnet.call_count |
| 83 | + assert target.driver.plugin.db._core_plugin.create_port.call_count |
| 84 | + assert target.driver.plugin.db._core_plugin.create_port.\ |
| 85 | + call_args_list[0][0] == expected_create_port_args |
| 86 | + assert target.driver.plugin.db._core_plugin.update_port.\ |
| 87 | + call_args_list[0][0] == expected_update_port_args |
0 commit comments