From 7dc7c7cdb06826e3cb83a8133dff85f2e30e6de4 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 2 Oct 2015 01:49:11 +0900 Subject: [PATCH] Start writing tests Also, sync requirements with Neutron. Partial-Bug: #1501792 Change-Id: Ib5f70646ddcbc807d45c8d352219029a8d9530d1 --- .testr.conf | 2 +- neutron_taas/tests/base.py | 23 ----- neutron_taas/tests/test_neutron_taas.py | 28 ------ neutron_taas/tests/unit/__init__.py | 0 neutron_taas/tests/unit/services/__init__.py | 0 .../tests/unit/services/taas/__init__.py | 0 .../unit/services/taas/test_taas_plugin.py | 96 +++++++++++++++++++ requirements.txt | 3 +- test-requirements.txt | 5 +- tox.ini | 13 ++- 10 files changed, 110 insertions(+), 60 deletions(-) delete mode 100644 neutron_taas/tests/base.py delete mode 100644 neutron_taas/tests/test_neutron_taas.py create mode 100644 neutron_taas/tests/unit/__init__.py create mode 100644 neutron_taas/tests/unit/services/__init__.py create mode 100644 neutron_taas/tests/unit/services/taas/__init__.py create mode 100644 neutron_taas/tests/unit/services/taas/test_taas_plugin.py diff --git a/.testr.conf b/.testr.conf index 6d83b3c4..d56c4781 100644 --- a/.testr.conf +++ b/.testr.conf @@ -2,6 +2,6 @@ test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \ OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \ OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \ - ${PYTHON:-python} -m subunit.run discover -t ./ . $LISTOPT $IDOPTION + ${PYTHON:-python} -m subunit.run discover -t ./ ${OS_TEST_PATH:-./neutron_taas/tests/unit} $LISTOPT $IDOPTION test_id_option=--load-list $IDFILE test_list_option=--list diff --git a/neutron_taas/tests/base.py b/neutron_taas/tests/base.py deleted file mode 100644 index 1c30cdb5..00000000 --- a/neutron_taas/tests/base.py +++ /dev/null @@ -1,23 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright 2010-2011 OpenStack Foundation -# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. -# -# 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. - -from oslotest import base - - -class TestCase(base.BaseTestCase): - - """Test case base class for all unit tests.""" diff --git a/neutron_taas/tests/test_neutron_taas.py b/neutron_taas/tests/test_neutron_taas.py deleted file mode 100644 index 7933f09d..00000000 --- a/neutron_taas/tests/test_neutron_taas.py +++ /dev/null @@ -1,28 +0,0 @@ -# -*- coding: utf-8 -*- - -# 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. - -""" -test_neutron_taas ----------------------------------- - -Tests for `neutron_taas` module. -""" - -from neutron_taas.tests import base - - -class TestNeutron_taas(base.TestCase): - - def test_something(self): - pass diff --git a/neutron_taas/tests/unit/__init__.py b/neutron_taas/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/neutron_taas/tests/unit/services/__init__.py b/neutron_taas/tests/unit/services/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/neutron_taas/tests/unit/services/taas/__init__.py b/neutron_taas/tests/unit/services/taas/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/neutron_taas/tests/unit/services/taas/test_taas_plugin.py b/neutron_taas/tests/unit/services/taas/test_taas_plugin.py new file mode 100644 index 00000000..da406375 --- /dev/null +++ b/neutron_taas/tests/unit/services/taas/test_taas_plugin.py @@ -0,0 +1,96 @@ +# Copyright (C) 2015 Midokura SARL. +# All Rights Reserved. +# +# 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 +import testtools + +from oslo_utils import uuidutils + +import neutron.common.rpc as n_rpc +from neutron import context +from neutron.tests.unit import testlib_api + +import neutron_taas.db.taas_db # noqa +import neutron_taas.extensions.taas as taas_ext +from neutron_taas.services.taas import taas_plugin + + +class TestTaasPlugin(testlib_api.SqlTestCaseLight): + def setUp(self): + super(TestTaasPlugin, self).setUp() + mock.patch.object(n_rpc, 'create_connection', auto_spec=True).start() + mock.patch.object(taas_plugin, 'TaasCallbacks', auto_spec=True).start() + mock.patch.object(taas_plugin, 'TaasAgentApi', auto_spec=True).start() + self._plugin = taas_plugin.TaasPlugin() + self._context = context.get_admin_context() + + def test_create_tap_service(self): + tenant_id = 'tenant-X' + network_id = uuidutils.generate_uuid() + host_id = 'host-A' + port_id = uuidutils.generate_uuid() + port_details = { + 'tenant_id': tenant_id, + 'binding:host_id': host_id, + } + tap_service = { + 'tenant_id': tenant_id, + 'name': 'MyTap', + 'description': 'This is my tap service', + 'port_id': port_id, + 'network_id': network_id, + } + req = { + 'tap_service': tap_service, + } + with mock.patch.object(self._plugin, '_get_port_details', + return_value=port_details): + self._plugin.create_tap_service(self._context, req) + tap_service['id'] = mock.ANY + expected_msg = { + 'tap_service': tap_service, + 'taas_id': mock.ANY, + 'port': port_details, + } + self.assertEqual( + [ + mock.call.create_tap_service(self._context, expected_msg, + host_id) + ], self._plugin.agent_rpc.mock_calls) + + def test_create_tap_service_wrong_tenant_id(self): + tenant_id = 'tenant-X' + network_id = uuidutils.generate_uuid() + host_id = 'host-A' + port_id = uuidutils.generate_uuid() + port_details = { + 'tenant_id': 'other-tenant', + 'binding:host_id': host_id, + } + tap_service = { + 'tenant_id': tenant_id, + 'name': 'MyTap', + 'description': 'This is my tap service', + 'port_id': port_id, + 'network_id': network_id, + } + req = { + 'tap_service': tap_service, + } + with mock.patch.object(self._plugin, '_get_port_details', + return_value=port_details), \ + testtools.ExpectedException(taas_ext.PortDoesNotBelongToTenant): + self._plugin.create_tap_service(self._context, req) + self.assertEqual([], self._plugin.agent_rpc.mock_calls) diff --git a/requirements.txt b/requirements.txt index 95137a6f..f69ec56e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,6 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -pbr>=0.6,!=0.7,<1.0 +pbr>=1.6 Babel>=1.3 +-e git://git.openstack.org/openstack/neutron.git@master#egg=neutron diff --git a/test-requirements.txt b/test-requirements.txt index a1901e50..50f6d301 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,9 +6,10 @@ hacking<0.11,>=0.10.0 coverage>=3.6 python-subunit>=0.0.18 -sphinx>=1.1.2,!=1.2.0,!=1.3b1,<1.3 +sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 oslosphinx>=2.2.0 # Apache-2.0 -oslotest>=1.2.0 # Apache-2.0 +oslotest>=1.10.0 # Apache-2.0 +os-testr>=0.1.0 testrepository>=0.0.18 testscenarios>=0.4 testtools>=0.9.36,!=1.2.0 diff --git a/tox.ini b/tox.ini index c4464b30..2f37e2a4 100644 --- a/tox.ini +++ b/tox.ini @@ -1,16 +1,19 @@ [tox] -minversion = 1.6 -envlist = py27,pep8 +envlist = docs,py27,py34,pep8 +minversion = 1.8 skipsdist = True [testenv] +setenv = VIRTUAL_ENV={envdir} usedevelop = True install_command = pip install -U {opts} {packages} -setenv = - VIRTUAL_ENV={envdir} deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt -commands = python setup.py test --slowest --testr-args='{posargs}' +commands = ostestr --regex '{posargs}' + +[tox:jenkins] +sitepackages = True +downloadcache = ~/cache/pip [testenv:pep8] commands = flake8