2016-02-01 09:50:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-10-11 08:07:35 +00:00
|
|
|
import difflib
|
|
|
|
import errno
|
|
|
|
import imp
|
2016-02-26 09:31:43 +00:00
|
|
|
import os
|
2018-10-11 08:07:35 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
import mock
|
|
|
|
import six
|
|
|
|
from kobo.rpmlib import parse_nvr
|
|
|
|
|
2016-05-25 11:39:02 +00:00
|
|
|
try:
|
|
|
|
import unittest2 as unittest
|
|
|
|
except ImportError:
|
|
|
|
import unittest
|
2016-02-01 09:50:28 +00:00
|
|
|
|
2017-04-27 13:21:36 +00:00
|
|
|
from pungi.util import get_arch_variant_data
|
2018-03-16 06:15:01 +00:00
|
|
|
from pungi import paths, checks, Modulemd
|
2016-02-11 14:15:36 +00:00
|
|
|
|
|
|
|
|
2018-10-11 08:07:35 +00:00
|
|
|
class BaseTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def assertFilesEqual(self, fn1, fn2):
|
|
|
|
with open(fn1, 'rb') as f1:
|
|
|
|
lines1 = f1.read().decode('utf-8').splitlines()
|
|
|
|
with open(fn2, 'rb') as f2:
|
|
|
|
lines2 = f2.read().decode('utf-8').splitlines()
|
|
|
|
diff = '\n'.join(difflib.unified_diff(lines1, lines2,
|
|
|
|
fromfile='EXPECTED', tofile='ACTUAL'))
|
|
|
|
self.assertEqual(diff, '', 'Files differ:\n' + diff)
|
|
|
|
|
2018-10-12 12:50:46 +00:00
|
|
|
def assertFileContent(self, fn, expected):
|
|
|
|
with open(fn, 'rb') as f:
|
|
|
|
lines = f.read().decode('utf-8').splitlines()
|
|
|
|
diff = '\n'.join(difflib.unified_diff(
|
|
|
|
lines, expected.splitlines(), fromfile='EXPECTED', tofile='ACTUAL')
|
|
|
|
)
|
|
|
|
self.assertEqual(diff, '', 'Files differ:\n' + diff)
|
|
|
|
|
2018-10-11 08:07:35 +00:00
|
|
|
|
|
|
|
class PungiTestCase(BaseTestCase):
|
2016-02-11 14:15:36 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.topdir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2016-04-05 06:40:54 +00:00
|
|
|
try:
|
|
|
|
shutil.rmtree(self.topdir)
|
|
|
|
except OSError as err:
|
|
|
|
if err.errno != errno.ENOENT:
|
|
|
|
raise
|
2016-02-01 09:50:28 +00:00
|
|
|
|
2016-12-07 14:57:35 +00:00
|
|
|
def assertValidConfig(self, conf):
|
2018-11-21 09:56:22 +00:00
|
|
|
self.assertEqual(checks.validate(conf, offline=True), ([], []))
|
2016-12-07 14:57:35 +00:00
|
|
|
|
2016-02-01 09:50:28 +00:00
|
|
|
|
2016-11-23 08:54:34 +00:00
|
|
|
class MockVariant(mock.Mock):
|
2018-07-19 12:47:06 +00:00
|
|
|
def __init__(self, is_empty=False, name=None, *args, **kwargs):
|
2017-04-26 11:54:00 +00:00
|
|
|
super(MockVariant, self).__init__(*args, is_empty=is_empty, **kwargs)
|
2017-01-26 08:44:45 +00:00
|
|
|
self.parent = kwargs.get('parent', None)
|
2017-05-04 18:46:06 +00:00
|
|
|
self.arch_mmds = {}
|
2018-03-20 07:52:49 +00:00
|
|
|
self.module_uid_to_koji_tag = {}
|
2017-04-26 11:54:00 +00:00
|
|
|
self.variants = {}
|
2018-03-07 11:35:33 +00:00
|
|
|
self.pkgset = mock.Mock(rpms_by_arch={})
|
2018-03-14 07:17:50 +00:00
|
|
|
self.modules = None
|
2018-07-19 12:47:06 +00:00
|
|
|
self.name = name
|
2018-10-01 13:38:14 +00:00
|
|
|
self.nsvc_to_pkgset = defaultdict(lambda: mock.Mock(rpms_by_arch={}))
|
2017-01-26 08:44:45 +00:00
|
|
|
|
2016-11-23 08:54:34 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.uid
|
|
|
|
|
2017-04-26 11:54:00 +00:00
|
|
|
def get_variants(self, arch=None, types=None):
|
2017-09-05 08:01:21 +00:00
|
|
|
return [v for v in list(self.variants.values())
|
2017-04-26 11:54:00 +00:00
|
|
|
if (not arch or arch in v.arches) and (not types or v.type in types)]
|
|
|
|
|
2017-08-30 08:21:41 +00:00
|
|
|
def get_modules(self, arch=None, types=None):
|
|
|
|
return []
|
|
|
|
|
2018-03-20 09:37:43 +00:00
|
|
|
def get_modular_koji_tags(self, arch=None, types=None):
|
|
|
|
return []
|
|
|
|
|
2019-05-10 13:37:14 +00:00
|
|
|
def add_fake_module(self, nsvc, rpm_nvrs=None, with_artifacts=False, mmd_arch=None):
|
2018-03-16 06:15:01 +00:00
|
|
|
if not Modulemd:
|
|
|
|
# No support for modules
|
2018-03-13 13:29:13 +00:00
|
|
|
return
|
|
|
|
name, stream, version, context = nsvc.split(":")
|
2019-07-01 10:11:39 +00:00
|
|
|
module_stream = Modulemd.ModuleStreamV2.new(name, stream)
|
|
|
|
module_stream.set_version(int(version))
|
|
|
|
module_stream.set_context(context)
|
|
|
|
module_stream.set_summary("foo")
|
|
|
|
module_stream.set_description("foo")
|
|
|
|
module_stream.add_module_license("GPL")
|
2018-03-13 13:29:13 +00:00
|
|
|
|
|
|
|
if rpm_nvrs:
|
|
|
|
for rpm_nvr in rpm_nvrs:
|
|
|
|
rpm_name = parse_nvr(rpm_nvr)["name"]
|
2019-07-01 10:11:39 +00:00
|
|
|
component = Modulemd.ComponentRpm.new(rpm_nvr)
|
2018-03-13 13:29:13 +00:00
|
|
|
component.set_name(rpm_name)
|
|
|
|
component.set_rationale("Needed for test")
|
2019-07-01 10:11:39 +00:00
|
|
|
module_stream.add_component(component)
|
|
|
|
if with_artifacts:
|
|
|
|
module_stream.add_rpm_artifact(rpm_nvr)
|
2018-03-13 13:29:13 +00:00
|
|
|
|
|
|
|
if self.modules is None:
|
|
|
|
self.modules = []
|
|
|
|
self.modules.append(":".join([name, stream, version]))
|
2019-05-10 13:37:14 +00:00
|
|
|
if mmd_arch:
|
2019-07-01 10:11:39 +00:00
|
|
|
nsvc = module_stream.get_nsvc()
|
|
|
|
self.arch_mmds.setdefault(mmd_arch, {})[nsvc] = module_stream
|
|
|
|
return module_stream
|
2018-03-13 13:29:13 +00:00
|
|
|
|
2016-11-23 08:54:34 +00:00
|
|
|
|
2017-02-22 02:48:05 +00:00
|
|
|
class IterableMock(mock.Mock):
|
|
|
|
def __iter__(self):
|
|
|
|
return iter([])
|
|
|
|
|
|
|
|
|
2016-02-22 14:58:34 +00:00
|
|
|
class DummyCompose(object):
|
2016-02-11 14:15:36 +00:00
|
|
|
def __init__(self, topdir, config):
|
2016-02-11 15:45:56 +00:00
|
|
|
self.supported = True
|
2016-02-01 09:50:28 +00:00
|
|
|
self.compose_date = '20151203'
|
|
|
|
self.compose_type_suffix = '.t'
|
2016-04-04 07:38:48 +00:00
|
|
|
self.compose_type = 'test'
|
2016-02-01 09:50:28 +00:00
|
|
|
self.compose_respin = 0
|
|
|
|
self.compose_id = 'Test-20151203.0.t'
|
2016-03-16 07:33:43 +00:00
|
|
|
self.compose_label = None
|
2016-04-04 07:38:48 +00:00
|
|
|
self.compose_label_major_version = None
|
2016-03-22 07:56:51 +00:00
|
|
|
self.image_release = '20151203.t.0'
|
2016-08-30 07:51:36 +00:00
|
|
|
self.image_version = '25'
|
2016-02-01 09:50:28 +00:00
|
|
|
self.ci_base = mock.Mock(
|
|
|
|
release_id='Test-1.0',
|
|
|
|
release=mock.Mock(
|
|
|
|
short='test',
|
|
|
|
version='1.0',
|
2016-02-29 11:58:21 +00:00
|
|
|
is_layered=False,
|
2017-11-07 10:13:49 +00:00
|
|
|
type_suffix=''
|
2016-02-01 09:50:28 +00:00
|
|
|
),
|
|
|
|
)
|
2016-02-11 14:15:36 +00:00
|
|
|
self.topdir = topdir
|
2016-08-22 14:08:25 +00:00
|
|
|
self.conf = load_config(PKGSET_REPOS, **config)
|
2018-11-21 09:56:22 +00:00
|
|
|
checks.validate(self.conf, offline=True)
|
2016-02-11 14:15:36 +00:00
|
|
|
self.paths = paths.Paths(self)
|
2018-04-17 12:49:46 +00:00
|
|
|
self.has_comps = True
|
2016-02-01 09:50:28 +00:00
|
|
|
self.variants = {
|
2016-11-23 08:54:34 +00:00
|
|
|
'Server': MockVariant(uid='Server', arches=['x86_64', 'amd64'],
|
2018-07-19 12:47:06 +00:00
|
|
|
type='variant', id='Server', name='Server'),
|
2016-11-23 08:54:34 +00:00
|
|
|
'Client': MockVariant(uid='Client', arches=['amd64'],
|
2018-07-19 12:47:06 +00:00
|
|
|
type='variant', id='Client', name='Client'),
|
2016-11-23 08:54:34 +00:00
|
|
|
'Everything': MockVariant(uid='Everything', arches=['x86_64', 'amd64'],
|
2018-07-19 12:47:06 +00:00
|
|
|
type='variant', id='Everything', name='Everything'),
|
2016-02-01 09:50:28 +00:00
|
|
|
}
|
2016-11-09 09:20:55 +00:00
|
|
|
self.all_variants = self.variants.copy()
|
2016-11-18 20:33:03 +00:00
|
|
|
|
|
|
|
# for PhaseLoggerMixin
|
2018-11-14 14:41:36 +00:00
|
|
|
self._logger = mock.Mock(name="compose._logger")
|
2016-11-18 20:33:03 +00:00
|
|
|
self._logger.handlers = [mock.Mock()]
|
|
|
|
|
2016-02-26 09:31:43 +00:00
|
|
|
self.log_info = mock.Mock()
|
2016-02-01 09:50:28 +00:00
|
|
|
self.log_error = mock.Mock()
|
2016-02-11 15:45:56 +00:00
|
|
|
self.log_debug = mock.Mock()
|
2016-02-29 08:21:49 +00:00
|
|
|
self.log_warning = mock.Mock()
|
2016-02-01 09:50:28 +00:00
|
|
|
self.get_image_name = mock.Mock(return_value='image-name')
|
2019-02-12 11:09:08 +00:00
|
|
|
self.image = mock.Mock(
|
|
|
|
path='Client/i386/iso/image.iso', can_fail=False, size=123, _max_size=None,
|
|
|
|
)
|
2016-06-06 08:29:40 +00:00
|
|
|
self.im = mock.Mock(images={'Client': {'amd64': [self.image]}})
|
2016-02-29 11:58:21 +00:00
|
|
|
self.old_composes = []
|
2016-03-10 12:22:11 +00:00
|
|
|
self.config_dir = '/home/releng/config'
|
2016-04-06 08:41:07 +00:00
|
|
|
self.notifier = None
|
2016-04-13 11:44:17 +00:00
|
|
|
self.attempt_deliverable = mock.Mock()
|
|
|
|
self.fail_deliverable = mock.Mock()
|
|
|
|
self.require_deliverable = mock.Mock()
|
2018-05-18 08:17:41 +00:00
|
|
|
self.should_create_yum_database = True
|
2018-08-15 07:20:40 +00:00
|
|
|
self.cache_region = None
|
2016-02-01 09:50:28 +00:00
|
|
|
|
2016-11-09 12:39:01 +00:00
|
|
|
def setup_optional(self):
|
2016-11-23 08:54:34 +00:00
|
|
|
self.all_variants['Server-optional'] = MockVariant(
|
2017-04-26 11:54:00 +00:00
|
|
|
uid='Server-optional', arches=['x86_64'], type='optional')
|
2016-11-24 13:53:30 +00:00
|
|
|
self.all_variants['Server-optional'].parent = self.variants['Server']
|
2017-04-26 11:54:00 +00:00
|
|
|
self.variants['Server'].variants['optional'] = self.all_variants['Server-optional']
|
|
|
|
|
|
|
|
def setup_addon(self):
|
|
|
|
self.all_variants['Server-HA'] = MockVariant(
|
|
|
|
uid='Server-HA', arches=['x86_64'], type='addon', is_empty=False)
|
|
|
|
self.all_variants['Server-HA'].parent = self.variants['Server']
|
|
|
|
self.variants['Server'].variants['HA'] = self.all_variants['Server-HA']
|
2016-11-09 12:39:01 +00:00
|
|
|
|
|
|
|
def get_variants(self, arch=None, types=None):
|
2017-09-05 08:01:21 +00:00
|
|
|
return [v for v in list(self.all_variants.values())
|
2017-04-26 11:54:00 +00:00
|
|
|
if (not arch or arch in v.arches) and (not types or v.type in types)]
|
2016-02-01 09:50:28 +00:00
|
|
|
|
|
|
|
def can_fail(self, variant, arch, deliverable):
|
|
|
|
failable = get_arch_variant_data(self.conf, 'failable_deliverables', arch, variant)
|
|
|
|
return deliverable in failable
|
2016-02-11 15:45:56 +00:00
|
|
|
|
|
|
|
def get_arches(self):
|
|
|
|
result = set()
|
2017-09-05 08:01:21 +00:00
|
|
|
for variant in list(self.variants.values()):
|
2016-02-11 15:45:56 +00:00
|
|
|
result |= set(variant.arches)
|
2016-05-12 11:41:53 +00:00
|
|
|
return sorted(result)
|
2016-02-26 09:31:43 +00:00
|
|
|
|
2017-01-09 07:40:24 +00:00
|
|
|
def mkdtemp(self, suffix="", prefix="tmp"):
|
2017-04-27 13:21:36 +00:00
|
|
|
return tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=self.topdir)
|
2017-01-09 07:40:24 +00:00
|
|
|
|
2016-02-26 09:31:43 +00:00
|
|
|
|
2016-02-29 12:35:55 +00:00
|
|
|
def touch(path, content=None):
|
2016-02-26 09:31:43 +00:00
|
|
|
"""Helper utility that creates an dummy file in given location. Directories
|
|
|
|
will be created."""
|
2016-02-29 12:35:55 +00:00
|
|
|
content = content or (path + '\n')
|
2016-02-26 09:31:43 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(path))
|
|
|
|
except OSError:
|
|
|
|
pass
|
2017-09-05 08:01:21 +00:00
|
|
|
if not isinstance(content, six.binary_type):
|
|
|
|
content = content.encode()
|
2017-08-24 13:01:33 +00:00
|
|
|
with open(path, 'wb') as f:
|
2016-02-29 12:35:55 +00:00
|
|
|
f.write(content)
|
2016-10-12 13:42:22 +00:00
|
|
|
return path
|
2016-02-29 08:21:49 +00:00
|
|
|
|
|
|
|
|
2016-04-22 09:15:06 +00:00
|
|
|
FIXTURE_DIR = os.path.join(os.path.dirname(__file__), 'fixtures')
|
|
|
|
|
|
|
|
|
2016-02-29 08:21:49 +00:00
|
|
|
def copy_fixture(fixture_name, dest):
|
2016-04-22 09:15:06 +00:00
|
|
|
src = os.path.join(FIXTURE_DIR, fixture_name)
|
2016-02-29 08:21:49 +00:00
|
|
|
touch(dest)
|
|
|
|
shutil.copy2(src, dest)
|
2016-03-10 12:22:11 +00:00
|
|
|
|
|
|
|
|
2016-03-23 09:40:16 +00:00
|
|
|
def boom(*args, **kwargs):
|
|
|
|
raise Exception('BOOM')
|
2016-08-22 14:08:25 +00:00
|
|
|
|
|
|
|
|
2017-07-27 09:59:03 +00:00
|
|
|
def mk_boom(cls=Exception, msg='BOOM'):
|
|
|
|
def b(*args, **kwargs):
|
|
|
|
raise cls(msg)
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
2016-08-22 14:08:25 +00:00
|
|
|
PKGSET_REPOS = dict(
|
|
|
|
pkgset_source='repos',
|
|
|
|
pkgset_repos={},
|
|
|
|
)
|
|
|
|
|
|
|
|
BASE_CONFIG = dict(
|
|
|
|
release_short='test',
|
|
|
|
release_name='Test',
|
|
|
|
release_version='1.0',
|
|
|
|
variants_file='variants.xml',
|
|
|
|
createrepo_checksum='sha256',
|
|
|
|
gather_method='deps',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(data={}, **kwargs):
|
|
|
|
conf = dict()
|
|
|
|
conf.update(BASE_CONFIG)
|
|
|
|
conf.update(data)
|
|
|
|
conf.update(kwargs)
|
|
|
|
return conf
|
2017-09-11 06:15:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_bin(name):
|
2017-10-05 13:58:02 +00:00
|
|
|
return imp.load_source('pungi_cli_fake_' + name, os.path.dirname(__file__) + "/../bin/" + name)
|