From: Matthew Booth Date: Tue, 22 Nov 2016 12:02:18 +0000 (+0000) Subject: libvirt: Mock imagebackend template funcs in ImageBackendFixture X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=6edf11aff59d95afe733ad8a7db908826a53271c;p=osstest%2Fopenstack-nova.git libvirt: Mock imagebackend template funcs in ImageBackendFixture This represents a small change to how we test the arguments passed to a template function. Most tests which test cache() currently don't directly test the callback function. Some test the callback function which was passed to cache(), but this is undesirable as: * It breaks untestably if you replace it with a wrapper * You can't test the arguments which were passed to it To make this easier to test, and because a subsequent change alters this slightly in ways we want to make obvious, we update ImageBackendFixture to execute the callback function when cache() is called. We pre-emptively mock all callback methods so they are not actually called. Test can assert on these mocks to check that the intended callback was called, and the arguments used. Implements: bp/libvirt-imagebackend-refactor Change-Id: Ifef488bcc3d8b7bb11aa7ec8255f9a27f57c3291 --- diff --git a/nova/tests/unit/virt/libvirt/fake_imagebackend.py b/nova/tests/unit/virt/libvirt/fake_imagebackend.py index 71a2ca4166..7ec02205b5 100644 --- a/nova/tests/unit/virt/libvirt/fake_imagebackend.py +++ b/nova/tests/unit/virt/libvirt/fake_imagebackend.py @@ -22,6 +22,7 @@ import mock import six from nova.virt.libvirt import config +from nova.virt.libvirt import driver from nova.virt.libvirt import imagebackend from nova.virt.libvirt import utils as libvirt_utils @@ -50,6 +51,31 @@ class ImageBackendFixture(fixtures.Fixture): def setUp(self): super(ImageBackendFixture, self).setUp() + + # Mock template functions passed to cache + self.mock_fetch_image = mock.create_autospec(libvirt_utils.fetch_image) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.utils.fetch_image', self.mock_fetch_image)) + + self.mock_fetch_raw_image = \ + mock.create_autospec(libvirt_utils.fetch_raw_image) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.utils.fetch_raw_image', + self.mock_fetch_raw_image)) + + self.mock_create_ephemeral = \ + mock.create_autospec(driver.LibvirtDriver._create_ephemeral) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.driver.LibvirtDriver._create_ephemeral', + self.mock_create_ephemeral)) + + self.mock_create_swap = \ + mock.create_autospec(driver.LibvirtDriver._create_swap) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.driver.LibvirtDriver._create_swap', + self.mock_create_swap)) + + # Backend.backend creates all Image objects self.useFixture(fixtures.MonkeyPatch( 'nova.virt.libvirt.imagebackend.Backend.backend', self._mock_backend)) @@ -158,6 +184,10 @@ class ImageBackendFixture(fixtures.Fixture): return image_init def _fake_cache(self, fetch_func, filename, size=None, *args, **kwargs): + # Execute the template function so we can test the arguments it was + # called with. + fetch_func(target=filename, *args, **kwargs) + # For legacy tests which use got_files if self.got_files is not None: self.got_files.append({'filename': filename, 'size': size}) diff --git a/nova/tests/unit/virt/libvirt/test_driver.py b/nova/tests/unit/virt/libvirt/test_driver.py index 81a1d39d78..5ce658d672 100644 --- a/nova/tests/unit/virt/libvirt/test_driver.py +++ b/nova/tests/unit/virt/libvirt/test_driver.py @@ -20,7 +20,6 @@ import copy import datetime import ddt import errno -import functools import glob import os import random @@ -9362,12 +9361,13 @@ class LibvirtConnTestCase(test.NoDBTestCase): 'ephemeral_foo') ] - m_args, m_kwargs = create_ephemeral_mock.call_args_list[0] - self.assertEqual(ephemeral_backing, m_kwargs['target']) - self.assertEqual(len(fetch_image_mock.call_args_list), 1) + create_ephemeral_mock.assert_called_once_with( + ephemeral_size=1, fs_label='ephemeral_foo', + os_type='linux', target=ephemeral_backing) - m_args, m_kwargs = fetch_image_mock.call_args_list[0] - self.assertEqual(root_backing, m_kwargs['target']) + fetch_image_mock.assert_called_once_with( + context=self.context, image_id=instance.image_ref, + target=root_backing) verify_base_size_mock.assert_has_calls([ mock.call(root_backing, instance.flavor.root_gb * units.Gi), @@ -10589,10 +10589,12 @@ class LibvirtConnTestCase(test.NoDBTestCase): drvr._create_image(self.context, instance, disk_info['mapping'], block_device_info=bdi, **create_image_kwargs) + backend.mock_create_swap.assert_called_once_with( + target='swap_%i' % expected, swap_mb=expected, + context=self.context) backend.disks['disk.swap'].cache.assert_called_once_with( - fetch_func=drvr._create_swap, context=self.context, - filename='swap_%i' % expected, size=expected * units.Mi, - swap_mb=expected) + fetch_func=mock.ANY, filename='swap_%i' % expected, + size=expected * units.Mi, context=self.context, swap_mb=expected) @mock.patch.object(nova.virt.libvirt.imagebackend.Image, 'cache', side_effect=exception.ImageNotFound(image_id='fake-id')) @@ -10655,18 +10657,15 @@ class LibvirtConnTestCase(test.NoDBTestCase): drvr._create_image(self.context, instance, disk_info['mapping'], block_device_info=bdi) + filename = 'ephemeral_100_%s' % mock.sentinel.file_ext + backend.mock_create_ephemeral.assert_called_once_with( + target=filename, ephemeral_size=100, fs_label='ephemeral0', + is_block_dev=mock.sentinel.is_block_dev, os_type='linux', + specified_fs=None, context=self.context) backend.disks['disk.eph0'].cache.assert_called_once_with( fetch_func=mock.ANY, context=self.context, - filename=('ephemeral_100_%s' % mock.sentinel.file_ext), - size=100 * units.Gi, ephemeral_size=100, specified_fs=None) - fetch_func = (backend.disks['disk.eph0'].cache. - mock_calls[0][2]['fetch_func']) - self.assertIsInstance(fetch_func, functools.partial) - self.assertEqual(drvr._create_ephemeral, fetch_func.func) - self.assertEqual( - dict(fs_label='ephemeral0', os_type=instance.os_type, - is_block_dev=mock.sentinel.is_block_dev), - fetch_func.keywords) + filename=filename, size=100 * units.Gi, ephemeral_size=mock.ANY, + specified_fs=None) @mock.patch.object(nova.virt.libvirt.imagebackend.Image, 'cache') def test_create_image_resize_snap_backend(self, mock_cache): @@ -16171,12 +16170,10 @@ class LibvirtDriverTestCase(test.NoDBTestCase): self.assertFalse(func(instance, disk_mapping)) @mock.patch('nova.virt.libvirt.driver.imagebackend') - @mock.patch( - 'nova.virt.libvirt.driver.LibvirtDriver._try_fetch_image_cache') @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._inject_data') @mock.patch('nova.virt.libvirt.driver.imagecache') def test_data_not_injects_with_configdrive(self, mock_image, mock_inject, - mock_fetch, mock_backend): + mock_backend): self.flags(inject_partition=-1, group='libvirt') drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)