From d4e71a37947a15229991cee610d1c678b77969f1 Mon Sep 17 00:00:00 2001 From: Dongcan Ye Date: Mon, 6 Mar 2017 13:15:54 +0800 Subject: [PATCH] Add unit tests for nested binding driver Add unit test for binding drivers: IPVLAN, MACVLAN, and VLAN. Change-Id: I8d25630b1543f9760cc34f331711804c7a1dde0f Closes-Bug: #1644403 --- .../tests/unit/binding/drivers/test_ipvlan.py | 61 ++++++++++++++++ .../unit/binding/drivers/test_macvlan.py | 69 +++++++++++++++++++ .../tests/unit/binding/drivers/test_nested.py | 41 +++++++++++ kuryr/tests/unit/binding/drivers/test_vlan.py | 59 ++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 kuryr/tests/unit/binding/drivers/test_ipvlan.py create mode 100644 kuryr/tests/unit/binding/drivers/test_macvlan.py create mode 100644 kuryr/tests/unit/binding/drivers/test_nested.py create mode 100644 kuryr/tests/unit/binding/drivers/test_vlan.py diff --git a/kuryr/tests/unit/binding/drivers/test_ipvlan.py b/kuryr/tests/unit/binding/drivers/test_ipvlan.py new file mode 100644 index 00000000..b668cc1e --- /dev/null +++ b/kuryr/tests/unit/binding/drivers/test_ipvlan.py @@ -0,0 +1,61 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import mock +from oslo_utils import uuidutils + +from kuryr.lib.binding.drivers import ipvlan +from kuryr.lib import constants +from kuryr.lib import utils +from kuryr.tests.unit import base + + +mock_create = mock.MagicMock() +mock_interface = mock.MagicMock() + + +class TestIpvlanDriver(base.TestCase): + """Unit tests for nested IPVLAN driver""" + + @mock.patch('kuryr.lib.binding.drivers.utils._configure_container_iface') + @mock.patch('pyroute2.ipdb.interface.InterfacesDict.__getattribute__', + return_value=mock_create) + @mock.patch('pyroute2.ipdb.interface.InterfacesDict.__getitem__', + return_value=mock_interface) + def test_port_bind(self, mock_getitem, mock_getattribute, + mock_configure_container_iface): + fake_mtu = 1450 + fake_docker_endpoint_id = utils.get_hash() + fake_docker_network_id = utils.get_hash() + fake_port_id = uuidutils.generate_uuid() + fake_neutron_v4_subnet_id = uuidutils.generate_uuid() + fake_neutron_v6_subnet_id = uuidutils.generate_uuid() + fake_vif_details = {"port_filter": True, "ovs_hybrid_plug": False} + fake_vif_type = "ovs" + fake_port = self._get_fake_port( + fake_docker_endpoint_id, fake_docker_network_id, + fake_port_id, constants.PORT_STATUS_ACTIVE, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, + vif_details=fake_vif_details, vif_type=fake_vif_type) + fake_subnets = self._get_fake_subnets( + fake_docker_endpoint_id, fake_docker_network_id, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id) + fake_network = self._get_fake_networks(fake_docker_network_id) + fake_network['networks'][0]['mtu'] = fake_mtu + + ipvlan.port_bind(fake_docker_endpoint_id, + fake_port['port'], + fake_subnets['subnets'], + fake_network['networks'][0]) + + mock_configure_container_iface.assert_called_once() diff --git a/kuryr/tests/unit/binding/drivers/test_macvlan.py b/kuryr/tests/unit/binding/drivers/test_macvlan.py new file mode 100644 index 00000000..11ad4096 --- /dev/null +++ b/kuryr/tests/unit/binding/drivers/test_macvlan.py @@ -0,0 +1,69 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import mock +from oslo_utils import uuidutils + +from kuryr.lib.binding.drivers import macvlan +from kuryr.lib import constants +from kuryr.lib import utils +from kuryr.tests.unit import base + + +mock_create = mock.MagicMock() +mock_interface = mock.MagicMock() + + +class TestMacvlanDriver(base.TestCase): + """Unit tests for nested MACVLAN driver""" + + @mock.patch('kuryr.lib.binding.drivers.utils._configure_container_iface') + @mock.patch('pyroute2.ipdb.interface.InterfacesDict.__getattribute__', + return_value=mock_create) + @mock.patch('pyroute2.ipdb.interface.InterfacesDict.__getitem__', + return_value=mock_interface) + def test_port_bind(self, mock_getitem, mock_getattribute, + mock_configure_container_iface): + fake_mtu = 1450 + fake_docker_endpoint_id = utils.get_hash() + fake_docker_network_id = utils.get_hash() + fake_port_id = uuidutils.generate_uuid() + fake_nova_instance_port_id = uuidutils.generate_uuid() + fake_neutron_v4_subnet_id = uuidutils.generate_uuid() + fake_neutron_v6_subnet_id = uuidutils.generate_uuid() + fake_vif_details = {"port_filter": True, "ovs_hybrid_plug": False} + fake_vif_type = "ovs" + fake_neutron_port = self._get_fake_port( + fake_docker_endpoint_id, fake_docker_network_id, + fake_port_id, constants.PORT_STATUS_ACTIVE, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, + vif_details=fake_vif_details, vif_type=fake_vif_type) + fake_nova_instance_port = self._get_fake_port( + "nova-port", fake_docker_network_id, + fake_nova_instance_port_id, constants.PORT_STATUS_ACTIVE, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, + "192.168.1.3", "fe80::f816:3eff:fe20:57c5", + vif_details=fake_vif_details, vif_type=fake_vif_type) + fake_subnets = self._get_fake_subnets( + fake_docker_endpoint_id, fake_docker_network_id, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id) + fake_network = self._get_fake_networks(fake_docker_network_id) + fake_network['networks'][0]['mtu'] = fake_mtu + + macvlan.port_bind(fake_docker_endpoint_id, + fake_neutron_port['port'], + fake_subnets['subnets'], + fake_network['networks'][0], + fake_nova_instance_port['port']) + + mock_configure_container_iface.assert_called_once() diff --git a/kuryr/tests/unit/binding/drivers/test_nested.py b/kuryr/tests/unit/binding/drivers/test_nested.py new file mode 100644 index 00000000..bdd3b823 --- /dev/null +++ b/kuryr/tests/unit/binding/drivers/test_nested.py @@ -0,0 +1,41 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import mock +from oslo_utils import uuidutils + +from kuryr.lib.binding.drivers import nested +from kuryr.lib import constants +from kuryr.lib import utils +from kuryr.tests.unit import base + + +class TestNestedDriver(base.TestCase): + """Unit tests for nested driver""" + + @mock.patch('kuryr.lib.binding.drivers.utils.remove_device') + def test_port_unbind(self, mock_remove_device): + fake_docker_endpoint_id = utils.get_hash() + fake_docker_network_id = utils.get_hash() + fake_port_id = uuidutils.generate_uuid() + fake_neutron_v4_subnet_id = uuidutils.generate_uuid() + fake_neutron_v6_subnet_id = uuidutils.generate_uuid() + fake_vif_details = {"port_filter": True, "ovs_hybrid_plug": False} + fake_vif_type = "ovs" + fake_port = self._get_fake_port( + fake_docker_endpoint_id, fake_docker_network_id, + fake_port_id, constants.PORT_STATUS_ACTIVE, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, + vif_details=fake_vif_details, vif_type=fake_vif_type) + nested.port_unbind(fake_docker_endpoint_id, fake_port['port']) + mock_remove_device.assert_called_once() diff --git a/kuryr/tests/unit/binding/drivers/test_vlan.py b/kuryr/tests/unit/binding/drivers/test_vlan.py new file mode 100644 index 00000000..d2a5ac1b --- /dev/null +++ b/kuryr/tests/unit/binding/drivers/test_vlan.py @@ -0,0 +1,59 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import mock +from oslo_utils import uuidutils + +from kuryr.lib.binding.drivers import vlan +from kuryr.lib import constants +from kuryr.lib import utils +from kuryr.tests.unit import base + + +mock_create = mock.MagicMock() +mock_interface = mock.MagicMock() + + +class TestVlanDriver(base.TestCase): + """Unit tests for nested vlan driver""" + + @mock.patch('kuryr.lib.binding.drivers.utils._configure_container_iface') + @mock.patch('pyroute2.ipdb.interface.InterfacesDict.__getattribute__', + return_value=mock_create) + @mock.patch('pyroute2.ipdb.interface.InterfacesDict.__getitem__', + return_value=mock_interface) + def test_port_bind(self, mock_getitem, mock_getattribute, + mock_configure_container_iface): + fake_segmentation_id = 100 + fake_docker_endpoint_id = utils.get_hash() + fake_docker_network_id = utils.get_hash() + fake_port_id = uuidutils.generate_uuid() + fake_neutron_v4_subnet_id = uuidutils.generate_uuid() + fake_neutron_v6_subnet_id = uuidutils.generate_uuid() + fake_vif_details = {"port_filter": True, "ovs_hybrid_plug": False} + fake_vif_type = "ovs" + fake_port = self._get_fake_port( + fake_docker_endpoint_id, fake_docker_network_id, + fake_port_id, constants.PORT_STATUS_ACTIVE, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, + vif_details=fake_vif_details, vif_type=fake_vif_type) + fake_subnets = self._get_fake_subnets( + fake_docker_endpoint_id, fake_docker_network_id, + fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id) + + vlan.port_bind(fake_docker_endpoint_id, + fake_port['port'], + fake_subnets['subnets'], + fake_segmentation_id) + + mock_configure_container_iface.assert_called_once()