From 8661d294ab84ea0e7e31eba8c4979a5d7db9556f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Mon, 1 Feb 2016 10:50:28 +0100 Subject: [PATCH] [tests] Move dummy testing compose into separate module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This greatly reduces duplication, as a single copy is now shared among all test cases. Signed-off-by: Lubomír Sedlář --- tests/helpers.py | 74 ++++++++++++++++++++++++++++++++ tests/test_imagebuildphase.py | 57 +----------------------- tests/test_imagechecksumphase.py | 28 +----------- tests/test_liveimagesphase.py | 66 ++++------------------------ tests/test_livemediaphase.py | 63 +++------------------------ 5 files changed, 93 insertions(+), 195 deletions(-) create mode 100644 tests/helpers.py diff --git a/tests/helpers.py b/tests/helpers.py new file mode 100644 index 00000000..d7c8d35e --- /dev/null +++ b/tests/helpers.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- + +import mock +import os + +from pungi.util import get_arch_variant_data + + +class _DummyCompose(object): + def __init__(self, config): + self.compose_date = '20151203' + self.compose_type_suffix = '.t' + self.compose_respin = 0 + self.compose_id = 'Test-20151203.0.t' + self.ci_base = mock.Mock( + release_id='Test-1.0', + release=mock.Mock( + short='test', + version='1.0', + ), + ) + self.conf = config + self.paths = mock.Mock( + compose=mock.Mock( + topdir=mock.Mock(return_value='/a/b'), + os_tree=mock.Mock( + side_effect=lambda arch, variant, create_dir=False: os.path.join('/ostree', arch, variant.uid) + ), + repository=mock.Mock( + side_effect=lambda arch, variant, create_dir=False: os.path.join('/repo', arch, variant.uid) + ), + image_dir=mock.Mock( + side_effect=lambda variant, relative=False, symlink_to=None: os.path.join( + '' if relative else '/', 'image_dir', variant.uid, '%(arch)s' + ) + ), + iso_dir=mock.Mock( + side_effect=lambda arch, variant, symlink_to=None, relative=False: os.path.join( + '' if relative else '/', 'iso_dir', arch, variant.uid + ) + ), + iso_path=mock.Mock( + side_effect=lambda arch, variant, filename, symlink_to: os.path.join( + '/iso_dir', arch, variant.uid, filename + ) + ) + ), + work=mock.Mock( + image_build_conf=mock.Mock( + side_effect=lambda variant, image_name, image_type: + '-'.join([variant.uid, image_name, image_type]) + ) + ), + log=mock.Mock( + log_file=mock.Mock(return_value='/a/b/log/log_file') + ) + ) + self._logger = mock.Mock() + self.variants = { + 'Server': mock.Mock(uid='Server', arches=['x86_64', 'amd64'], is_empty=False), + 'Client': mock.Mock(uid='Client', arches=['amd64'], is_empty=False), + 'Everything': mock.Mock(uid='Everything', arches=['x86_64', 'amd64'], is_empty=False), + } + self.log_error = mock.Mock() + self.get_image_name = mock.Mock(return_value='image-name') + self.image = mock.Mock(path='Client/i386/iso/image.iso') + self.im = mock.Mock(images={'Client': {'i386': [self.image]}}) + + def get_variants(self, arch=None, types=None): + return [v for v in self.variants.values() if not arch or arch in v.arches] + + def can_fail(self, variant, arch, deliverable): + failable = get_arch_variant_data(self.conf, 'failable_deliverables', arch, variant) + return deliverable in failable diff --git a/tests/test_imagebuildphase.py b/tests/test_imagebuildphase.py index 89a735cb..fc4eb329 100755 --- a/tests/test_imagebuildphase.py +++ b/tests/test_imagebuildphase.py @@ -11,62 +11,7 @@ import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from pungi.phases.image_build import ImageBuildPhase, CreateImageBuildThread -from pungi.util import get_arch_variant_data - - -class _DummyCompose(object): - def __init__(self, config): - self.compose_date = '20151203' - self.compose_type_suffix = '.t' - self.compose_respin = 0 - self.ci_base = mock.Mock( - release_id='Test-1.0', - release=mock.Mock( - short='test', - version='1.0', - ), - ) - self.conf = config - self.paths = mock.Mock( - compose=mock.Mock( - topdir=mock.Mock(return_value='/a/b'), - os_tree=mock.Mock( - side_effect=lambda arch, variant, create_dir=False: os.path.join('/ostree', arch, variant.uid) - ), - image_dir=mock.Mock( - side_effect=lambda variant, relative=False: os.path.join( - '' if relative else '/', 'image_dir', variant.uid, '%(arch)s' - ) - ) - ), - work=mock.Mock( - image_build_conf=mock.Mock( - side_effect=lambda variant, image_name, image_type: - '-'.join([variant.uid, image_name, image_type]) - ) - ), - log=mock.Mock( - log_file=mock.Mock(return_value='/a/b/log/log_file') - ) - ) - self._logger = mock.Mock() - self.variants = { - 'Server': mock.Mock(uid='Server', arches=['x86_64', 'amd64'], is_empty=False), - 'Client': mock.Mock(uid='Client', arches=['amd64'], is_empty=False), - 'Everything': mock.Mock(uid='Everything', arches=['x86_64', 'amd64'], is_empty=False), - } - self.im = mock.Mock() - self.log_error = mock.Mock() - - def get_arches(self): - return ['x86_64', 'amd64'] - - def get_variants(self, arch=None, types=None): - return [v for v in self.variants.values() if not arch or arch in v.arches] - - def can_fail(self, variant, arch, deliverable): - failable = get_arch_variant_data(self.conf, 'failable_deliverables', arch, variant) - return deliverable in failable +from tests.helpers import _DummyCompose class TestImageBuildPhase(unittest.TestCase): diff --git a/tests/test_imagechecksumphase.py b/tests/test_imagechecksumphase.py index 628508a7..634e44ec 100755 --- a/tests/test_imagechecksumphase.py +++ b/tests/test_imagechecksumphase.py @@ -12,32 +12,8 @@ import shutil sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) -from pungi.phases.image_checksum import (ImageChecksumPhase, - dump_checksums) - - -class _DummyCompose(object): - def __init__(self, config): - self.compose_date = '20151203' - self.compose_type_suffix = '.t' - self.compose_respin = 0 - self.ci_base = mock.Mock( - release_id='Test-1.0', - release=mock.Mock( - short='test', - version='1.0', - ), - ) - self.conf = config - self.paths = mock.Mock( - compose=mock.Mock( - topdir=mock.Mock(return_value='/a/b') - ) - ) - self.image = mock.Mock( - path='Client/i386/iso/image.iso', - ) - self.im = mock.Mock(images={'Client': {'i386': [self.image]}}) +from pungi.phases.image_checksum import ImageChecksumPhase, dump_checksums +from tests.helpers import _DummyCompose class TestImageChecksumPhase(unittest.TestCase): diff --git a/tests/test_liveimagesphase.py b/tests/test_liveimagesphase.py index abf4a29e..8dc65d31 100755 --- a/tests/test_liveimagesphase.py +++ b/tests/test_liveimagesphase.py @@ -11,55 +11,7 @@ import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from pungi.phases.live_images import LiveImagesPhase, CreateLiveImageThread -from pungi.util import get_arch_variant_data - - -class _DummyCompose(object): - def __init__(self, config): - self.compose_id = 'Test-20151203.0.t' - self.compose_date = '20151203' - self.compose_respin = '0' - self.conf = config - self.paths = mock.Mock( - compose=mock.Mock( - topdir=mock.Mock(return_value='/top'), - repository=mock.Mock( - side_effect=lambda arch, variant, create_dir=False: os.path.join('/repo', arch, variant.uid) - ), - iso_dir=mock.Mock( - side_effect=lambda arch, variant, symlink_to: os.path.join( - '/top/iso_dir', arch, variant.uid - ) - ), - image_dir=mock.Mock( - side_effect=lambda variant, symlink_to: os.path.join( - '/top/image_dir/%(arch)s', variant.uid - ) - ), - ), - log=mock.Mock( - log_file=mock.Mock(return_value='/a/b/log/log_file') - ) - ) - self._logger = mock.Mock() - self.variants = { - 'Server': mock.Mock(uid='Server', arches=['x86_64', 'amd64'], is_empty=False), - 'Client': mock.Mock(uid='Client', arches=['amd64'], is_empty=False), - 'Everything': mock.Mock(uid='Everything', arches=['x86_64', 'amd64'], is_empty=False), - } - self.log_error = mock.Mock() - self.get_image_name = mock.Mock(return_value='image-name') - self.im = mock.Mock() - - def get_arches(self): - return ['x86_64', 'amd64'] - - def get_variants(self, arch=None, types=None): - return [v for v in self.variants.values() if not arch or arch in v.arches] - - def can_fail(self, variant, arch, deliverable): - failable = get_arch_variant_data(self.conf, 'failable_deliverables', arch, variant) - return deliverable in failable +from tests.helpers import _DummyCompose class TestLiveImagesPhase(unittest.TestCase): @@ -90,7 +42,7 @@ class TestLiveImagesPhase(unittest.TestCase): [mock.call((compose, {'ks_file': 'test.ks', 'build_arch': 'amd64', - 'dest_dir': '/top/iso_dir/amd64/Client', + 'dest_dir': '/iso_dir/amd64/Client', 'scratch': False, 'repos': ['/repo/amd64/Client', 'http://example.com/repo/', @@ -134,7 +86,7 @@ class TestLiveImagesPhase(unittest.TestCase): [mock.call((compose, {'ks_file': 'test.ks', 'build_arch': 'amd64', - 'dest_dir': '/top/iso_dir/amd64/Client', + 'dest_dir': '/iso_dir/amd64/Client', 'scratch': False, 'repos': ['/repo/amd64/Client', 'http://example.com/repo/', @@ -180,7 +132,7 @@ class TestLiveImagesPhase(unittest.TestCase): [mock.call((compose, {'ks_file': 'test.ks', 'build_arch': 'amd64', - 'dest_dir': '/top/iso_dir/amd64/Client', + 'dest_dir': '/iso_dir/amd64/Client', 'scratch': False, 'repos': ['/repo/amd64/Client', 'http://example.com/repo/', @@ -199,7 +151,7 @@ class TestLiveImagesPhase(unittest.TestCase): mock.call((compose, {'ks_file': 'another.ks', 'build_arch': 'amd64', - 'dest_dir': '/top/iso_dir/amd64/Client', + 'dest_dir': '/iso_dir/amd64/Client', 'scratch': False, 'repos': ['/repo/amd64/Client', 'http://example.com/repo/', @@ -246,7 +198,7 @@ class TestLiveImagesPhase(unittest.TestCase): [mock.call((compose, {'ks_file': 'test.ks', 'build_arch': 'amd64', - 'dest_dir': '/top/image_dir/amd64/Client', + 'dest_dir': '/image_dir/Client/amd64', 'scratch': False, 'repos': ['/repo/amd64/Client', 'http://example.com/repo/', @@ -335,7 +287,7 @@ class TestCreateLiveImageThread(unittest.TestCase): ksurl='https://git.example.com/kickstarts.git?#CAFEBABE')]) self.assertEqual(Image.return_value.type, 'live') self.assertEqual(Image.return_value.format, 'iso') - self.assertEqual(Image.return_value.path, 'iso_dir/amd64/Client/image-name') + self.assertEqual(Image.return_value.path, '../../top/iso_dir/amd64/Client/image-name') self.assertEqual(Image.return_value.size, 1024) self.assertEqual(Image.return_value.mtime, 13579) self.assertEqual(Image.return_value.arch, 'amd64') @@ -417,7 +369,7 @@ class TestCreateLiveImageThread(unittest.TestCase): self.assertEqual(Image.return_value.type, 'live') self.assertEqual(Image.return_value.format, 'iso') - self.assertEqual(Image.return_value.path, 'iso_dir/amd64/Client/image.iso') + self.assertEqual(Image.return_value.path, '../../top/iso_dir/amd64/Client/image.iso') self.assertEqual(Image.return_value.size, 1024) self.assertEqual(Image.return_value.mtime, 13579) self.assertEqual(Image.return_value.arch, 'amd64') @@ -495,7 +447,7 @@ class TestCreateLiveImageThread(unittest.TestCase): self.assertEqual(Image.return_value.type, 'appliance') self.assertEqual(Image.return_value.format, 'raw.xz') - self.assertEqual(Image.return_value.path, 'iso_dir/amd64/Client/image-name') + self.assertEqual(Image.return_value.path, '../../top/iso_dir/amd64/Client/image-name') self.assertEqual(Image.return_value.size, 1024) self.assertEqual(Image.return_value.mtime, 13579) self.assertEqual(Image.return_value.arch, 'amd64') diff --git a/tests/test_livemediaphase.py b/tests/test_livemediaphase.py index 7b49af57..eecee799 100755 --- a/tests/test_livemediaphase.py +++ b/tests/test_livemediaphase.py @@ -10,56 +10,7 @@ import os sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from pungi.phases.livemedia_phase import LiveMediaPhase, LiveMediaThread -from pungi.util import get_arch_variant_data - - -class _DummyCompose(object): - def __init__(self, config): - self.compose_date = '20151203' - self.compose_type_suffix = '.t' - self.compose_respin = 0 - self.ci_base = mock.Mock( - release_id='Test-1.0', - release=mock.Mock( - short='test', - version='1.0', - ), - ) - self.conf = config - self.paths = mock.Mock( - compose=mock.Mock( - topdir=mock.Mock(return_value='/a/b'), - os_tree=mock.Mock( - side_effect=lambda arch, variant, create_dir=False: os.path.join('/ostree', arch, variant.uid) - ), - repository=mock.Mock( - side_effect=lambda arch, variant, create_dir=False: os.path.join('/repo', arch, variant.uid) - ), - iso_dir=mock.Mock( - side_effect=lambda arch, variant, relative=False: os.path.join( - '' if relative else '/', 'iso_dir', variant.uid, arch - ) - ) - ), - log=mock.Mock( - log_file=mock.Mock(return_value='/a/b/log/log_file') - ) - ) - self._logger = mock.Mock() - self.variants = { - 'Server': mock.Mock(uid='Server', arches=['x86_64', 'amd64'], is_empty=False), - 'Client': mock.Mock(uid='Client', arches=['amd64'], is_empty=False), - 'Everything': mock.Mock(uid='Everything', arches=['x86_64', 'amd64'], is_empty=False), - } - self.im = mock.Mock() - self.log_error = mock.Mock() - - def get_variants(self, arch=None, types=None): - return [v for v in self.variants.values() if not arch or arch in v.arches] - - def can_fail(self, variant, arch, deliverable): - failable = get_arch_variant_data(self.conf, 'failable_deliverables', arch, variant) - return deliverable in failable +from tests.helpers import _DummyCompose class TestLiveMediaPhase(unittest.TestCase): @@ -278,20 +229,20 @@ class TestCreateImageBuildThread(unittest.TestCase): self.assertEqual(get_image_paths.mock_calls, [mock.call(1234)]) self.assertItemsEqual(makedirs.mock_calls, - [mock.call('/iso_dir/Server/x86_64'), - mock.call('/iso_dir/Server/amd64')]) + [mock.call('/iso_dir/x86_64/Server'), + mock.call('/iso_dir/amd64/Server')]) link = Linker.return_value.link self.assertItemsEqual(link.mock_calls, [mock.call('/koji/task/1235/Live-20160103.amd64.iso', - '/iso_dir/Server/amd64/Live-20160103.amd64.iso', + '/iso_dir/amd64/Server/Live-20160103.amd64.iso', link_type='hardlink-or-copy'), mock.call('/koji/task/1235/Live-20160103.x86_64.iso', - '/iso_dir/Server/x86_64/Live-20160103.x86_64.iso', + '/iso_dir/x86_64/Server/Live-20160103.x86_64.iso', link_type='hardlink-or-copy')]) image_relative_paths = [ - 'iso_dir/Server/amd64/Live-20160103.amd64.iso', - 'iso_dir/Server/x86_64/Live-20160103.x86_64.iso' + 'iso_dir/amd64/Server/Live-20160103.amd64.iso', + 'iso_dir/x86_64/Server/Live-20160103.x86_64.iso' ] self.assertEqual(len(compose.im.add.call_args_list), 2)