Fix unit tests with py27

Fix building unit tests with py27.

Signed-off-by: Chuck Short <chuck.short@canonical.com>
This commit is contained in:
Chuck Short 2016-05-11 12:57:49 -04:00
parent 3457fa29b4
commit 88e7fdd568
13 changed files with 180 additions and 315 deletions

View File

@ -2,6 +2,8 @@
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 ./nova/tests/unit/virt/lxd/ ./nova/tests/unit/virt/lxd \
$LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list
test_list_option=--list

View File

@ -1,46 +0,0 @@
# Copyright 2015 Canonical Ltd
# 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 ddt
import mock
from nova import test
from nova_lxd.nova.virt.lxd import session
from nova_lxd.tests import stubs
@ddt.ddt
class SessionEventTest(test.NoDBTestCase):
def setUp(self):
super(SessionEventTest, self).setUp()
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
def test_container_wait(self):
instance = stubs._fake_instance()
operation_id = mock.Mock()
self.ml.wait_container_operation.return_value = True
self.assertEqual(None,
self.session.operation_wait(operation_id, instance))
self.ml.wait_container_operation.assert_called_with(operation_id,
200, -1)

View File

@ -1,54 +0,0 @@
# Copyright 2015 Canonical Ltd
# 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 ddt
import mock
from nova import test
from nova_lxd.nova.virt.lxd import session
from nova_lxd.tests import stubs
@ddt.ddt
class SessionImageTest(test.NoDBTestCase):
def setUp(self):
super(SessionImageTest, self).setUp()
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
def test_image_defined(self):
"""Test the image is defined in the LXD hypervisor."""
instance = stubs._fake_instance()
self.ml.alias_defined.return_value = True
self.assertTrue(self.session.image_defined(instance))
calls = [mock.call.alias_defined(instance.image_ref)]
self.assertEqual(calls, self.ml.method_calls)
def test_alias_create(self):
"""Test the alias is created."""
instance = stubs._fake_instance()
alias = mock.Mock()
self.ml.alias_create.return_value = True
self.assertTrue(self.session.create_alias(alias, instance))
calls = [mock.call.alias_create(alias)]
self.assertEqual(calls, self.ml.method_calls)

View File

@ -1,38 +0,0 @@
# Copyright 2015 Canonical Ltd
# 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 ddt
import mock
from nova import test
from nova_lxd.nova.virt.lxd import session
from nova_lxd.tests import stubs
@ddt.ddt
class SessionMigrateTest(test.NoDBTestCase):
def setUp(self):
super(SessionMigrateTest, self).setUp()
"""This is so we can mock out pylxd API calls."""
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()

View File

@ -1,79 +0,0 @@
# Copyright 2015 Canonical Ltd
# 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 ddt
import mock
from nova import exception
from nova import test
from pylxd.deprecated import exceptions as lxd_exceptions
from nova_lxd.nova.virt.lxd import session
from nova_lxd.tests import fake_api
from nova_lxd.tests import stubs
@ddt.ddt
class SessionProfileTest(test.NoDBTestCase):
def setUp(self):
super(SessionProfileTest, self).setUp()
"""This is so we can mock out pylxd API calls."""
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
@stubs.annotated_data(
('empty', [], []),
('valid', ['test'], ['test']),
)
def test_profile_list(self, tag, side_effect, expected):
self.ml.profile_list.return_value = side_effect
self.assertEqual(expected,
self.session.profile_list())
def test_profile_list_fail(self):
self.ml.profile_list.side_effect = (
lxd_exceptions.APIError('Fake', 500))
self.assertRaises(
exception.NovaException,
self.session.profile_list)
def test_profile_create(self):
instance = stubs._fake_instance()
config = mock.Mock()
self.ml.profile_defined.return_value = True
self.ml.profile_create.return_value = \
(200, fake_api.fake_standard_return())
self.assertEqual((200, fake_api.fake_standard_return()),
self.session.profile_create(config,
instance))
calls = [mock.call.profile_list(),
mock.call.profile_create(config)]
self.assertEqual(calls, self.ml.method_calls)
def test_profile_delete(self):
instance = stubs._fake_instance()
self.ml.profile_defined.return_value = True
self.ml.profile_delete.return_value = \
(200, fake_api.fake_standard_return())
self.assertEqual(None,
self.session.profile_delete(instance))

View File

@ -1,81 +0,0 @@
# Copyright 2015 Canonical Ltd
# 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 ddt
import mock
from nova import exception
from nova import test
from pylxd.deprecated import exceptions as lxd_exceptions
from nova_lxd.nova.virt.lxd import session
from nova_lxd.tests import fake_api
from nova_lxd.tests import stubs
@ddt.ddt
class SessionSnapshotTest(test.NoDBTestCase):
def setUp(self):
super(SessionSnapshotTest, self).setUp()
"""This is so we can mock out pylxd API calls."""
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
@stubs.annotated_data(
('1,', (200, fake_api.fake_operation_info_ok()))
)
def test_container_snapshot(self, tag, side_effect):
snapshot = mock.Mock()
instance = stubs._fake_instance()
self.ml.container_snapshot_create.return_value = side_effect
self.assertEqual(None,
self.session.container_snapshot(snapshot, instance))
calls = [
mock.call.container_snapshot_create(instance.name, snapshot),
mock.call.wait_container_operation(
'/1.0/operation/1234', 200, -1)]
self.assertEqual(calls, self.ml.method_calls)
@stubs.annotated_data(
('api_fail', lxd_exceptions.APIError(500, 'Fake'),
exception.NovaException)
)
def test_container_snapshot_fail(self, tag, side_effect, expected):
snapshot = mock.Mock()
instance = stubs._fake_instance()
self.ml.container_snapshot_create.side_effect = side_effect
self.assertRaises(expected,
self.session.container_snapshot,
instance.name, snapshot)
@stubs.annotated_data(
(1, (200, fake_api.fake_operation_info_ok()))
)
def test_container_publish(self, tag, side_effect):
image = mock.Mock()
instance = stubs._fake_instance()
self.ml.image_export.return_value = True
self.assertTrue(
self.session.container_publish(image, instance))
calls = [
mock.call.container_publish(image)]
self.assertEqual(calls, self.ml.method_calls)

View File

@ -15,20 +15,17 @@
import mock
import nova.conf
from nova import test
from nova.virt import fake
from oslo_config import cfg
from nova.virt.lxd import config
from nova.virt.lxd import migrate
from nova.virt.lxd import operations
from nova.virt.lxd import session
import stubs
CONF = cfg.CONF
CONF.import_opt('my_ip', 'nova.netconf')
CONF = nova.conf.CONF
class LXDTestContainerMigrate(test.NoDBTestCase):

View File

@ -28,9 +28,9 @@ from nova import exception
from nova import test
from pylxd.deprecated import exceptions as lxd_exceptions
from nova_lxd.nova.virt.lxd import session
from nova_lxd.tests import fake_api
from nova_lxd.tests import stubs
from nova.virt.lxd import session
import fake_api
import stubs
@ddt.ddt
@ -548,3 +548,164 @@ class SessionContainerTest(test.NoDBTestCase):
self.assertRaises(expected,
self.session.container_init, config,
instance)
@ddt.ddt
class SessionEventTest(test.NoDBTestCase):
def setUp(self):
super(SessionEventTest, self).setUp()
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
def test_container_wait(self):
instance = stubs._fake_instance()
operation_id = mock.Mock()
self.ml.wait_container_operation.return_value = True
self.assertEqual(None,
self.session.operation_wait(operation_id, instance))
self.ml.wait_container_operation.assert_called_with(operation_id,
200, -1)
@ddt.ddt
class SessionImageTest(test.NoDBTestCase):
def setUp(self):
super(SessionImageTest, self).setUp()
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
def test_image_defined(self):
"""Test the image is defined in the LXD hypervisor."""
instance = stubs._fake_instance()
self.ml.alias_defined.return_value = True
self.assertTrue(self.session.image_defined(instance))
calls = [mock.call.alias_defined(instance.image_ref)]
self.assertEqual(calls, self.ml.method_calls)
def test_alias_create(self):
"""Test the alias is created."""
instance = stubs._fake_instance()
alias = mock.Mock()
self.ml.alias_create.return_value = True
self.assertTrue(self.session.create_alias(alias, instance))
calls = [mock.call.alias_create(alias)]
self.assertEqual(calls, self.ml.method_calls)
@ddt.ddt
class SessionProfileTest(test.NoDBTestCase):
def setUp(self):
super(SessionProfileTest, self).setUp()
"""This is so we can mock out pylxd API calls."""
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
@stubs.annotated_data(
('empty', [], []),
('valid', ['test'], ['test']),
)
def test_profile_list(self, tag, side_effect, expected):
self.ml.profile_list.return_value = side_effect
self.assertEqual(expected,
self.session.profile_list())
def test_profile_list_fail(self):
self.ml.profile_list.side_effect = (
lxd_exceptions.APIError('Fake', 500))
self.assertRaises(
exception.NovaException,
self.session.profile_list)
def test_profile_create(self):
instance = stubs._fake_instance()
config = mock.Mock()
self.ml.profile_defined.return_value = True
self.ml.profile_create.return_value = \
(200, fake_api.fake_standard_return())
self.assertEqual((200, fake_api.fake_standard_return()),
self.session.profile_create(config,
instance))
calls = [mock.call.profile_list(),
mock.call.profile_create(config)]
self.assertEqual(calls, self.ml.method_calls)
def test_profile_delete(self):
instance = stubs._fake_instance()
self.ml.profile_defined.return_value = True
self.ml.profile_delete.return_value = \
(200, fake_api.fake_standard_return())
self.assertEqual(None,
self.session.profile_delete(instance))
@ddt.ddt
class SessionSnapshotTest(test.NoDBTestCase):
def setUp(self):
super(SessionSnapshotTest, self).setUp()
"""This is so we can mock out pylxd API calls."""
self.ml = stubs.lxd_mock()
lxd_patcher = mock.patch('pylxd.api.API',
mock.Mock(return_value=self.ml))
lxd_patcher.start()
self.addCleanup(lxd_patcher.stop)
self.session = session.LXDAPISession()
@stubs.annotated_data(
('1,', (200, fake_api.fake_operation_info_ok()))
)
def test_container_snapshot(self, tag, side_effect):
snapshot = mock.Mock()
instance = stubs._fake_instance()
self.ml.container_snapshot_create.return_value = side_effect
self.assertEqual(None,
self.session.container_snapshot(snapshot, instance))
calls = [
mock.call.container_snapshot_create(instance.name, snapshot),
mock.call.wait_container_operation(
'/1.0/operation/1234', 200, -1)]
self.assertEqual(calls, self.ml.method_calls)
@stubs.annotated_data(
('api_fail', lxd_exceptions.APIError(500, 'Fake'),
exception.NovaException)
)
def test_container_snapshot_fail(self, tag, side_effect, expected):
snapshot = mock.Mock()
instance = stubs._fake_instance()
self.ml.container_snapshot_create.side_effect = side_effect
self.assertRaises(expected,
self.session.container_snapshot,
instance.name, snapshot)
@stubs.annotated_data(
(1, (200, fake_api.fake_operation_info_ok()))
)
def test_container_publish(self, tag, side_effect):
image = mock.Mock()
instance = stubs._fake_instance()
self.ml.image_export.return_value = True
self.assertTrue(
self.session.container_publish(image, instance))
calls = [
mock.call.container_publish(image)]
self.assertEqual(calls, self.ml.method_calls)

View File

@ -16,11 +16,11 @@
import socket
import nova.conf
from nova import exception
from nova import i18n
from nova.virt import configdrive
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import excutils
@ -32,8 +32,7 @@ _ = i18n._
_LE = i18n._LE
_LI = i18n._LI
CONF = cfg.CONF
CONF.import_opt('my_ip', 'nova.netconf')
CONF = nova.conf.CONF
LOG = logging.getLogger(__name__)

View File

@ -15,6 +15,7 @@
import os
import nova.conf
from nova import exception
from nova import i18n
from nova import utils
@ -35,8 +36,7 @@ _ = i18n._
_LE = i18n._LE
_LI = i18n._LI
CONF = cfg.CONF
CONF.import_opt('my_ip', 'nova.netconf')
CONF = nova.conf.CONF
LOG = logging.getLogger(__name__)

View File

@ -14,6 +14,7 @@
# the License for the specific language governing permissions and
# limitations under the License.
import nova.conf
from nova import context as nova_context
from nova import exception
from nova import i18n
@ -37,8 +38,7 @@ _ = i18n._
_LE = i18n._LE
_LI = i18n._LI
CONF = cfg.CONF
CONF.import_opt('host', 'nova.netconf')
CONF = nova.conf.CONF
LOG = logging.getLogger(__name__)

View File

@ -18,6 +18,10 @@ deps = -r{toxinidir}/requirements.txt
-egit+https://github.com/openstack/nova#egg=nova
commands = ostestr {posargs}
[testenv:py27]
commands = /bin/cp -r {toxinidir}/nova/virt/lxd/ {toxinidir}/.tox/py27/src/nova/nova/virt/
python setup.py testr --slowest --testr-args='{posargs}'
[testenv:pep8]
commands = flake8