From 96fc6fb11eb4c6d6fcf89c967589da9cebf98226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Wed, 23 Nov 2016 09:54:34 +0100 Subject: [PATCH] createrepo: Add tests for retrieving product certificates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lubomír Sedlář --- tests/helpers.py | 19 ++++-- tests/test_createrepophase.py | 124 +++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 8 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index 216289b1..c707f314 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -26,6 +26,11 @@ class PungiTestCase(unittest.TestCase): raise +class MockVariant(mock.Mock): + def __str__(self): + return self.uid + + class DummyCompose(object): def __init__(self, topdir, config): self.supported = True @@ -52,12 +57,12 @@ class DummyCompose(object): self.paths = paths.Paths(self) self._logger = mock.Mock() self.variants = { - 'Server': mock.Mock(uid='Server', arches=['x86_64', 'amd64'], - type='variant', is_empty=False), - 'Client': mock.Mock(uid='Client', arches=['amd64'], - type='variant', is_empty=False), - 'Everything': mock.Mock(uid='Everything', arches=['x86_64', 'amd64'], - type='variant', is_empty=False), + 'Server': MockVariant(uid='Server', arches=['x86_64', 'amd64'], + type='variant', is_empty=False), + 'Client': MockVariant(uid='Client', arches=['amd64'], + type='variant', is_empty=False), + 'Everything': MockVariant(uid='Everything', arches=['x86_64', 'amd64'], + type='variant', is_empty=False), } self.all_variants = self.variants.copy() @@ -80,7 +85,7 @@ class DummyCompose(object): self.require_deliverable = mock.Mock() def setup_optional(self): - self.all_variants['Server-optional'] = mock.Mock( + self.all_variants['Server-optional'] = MockVariant( uid='Server-optional', arches=['x86_64'], type='optional', is_empty=False, parent=self.variants['Server']) self.variants['Server'].variants = {'optional': self.all_variants['Server-optional']} diff --git a/tests/test_createrepophase.py b/tests/test_createrepophase.py index 2a894f9d..bbf25e0c 100644 --- a/tests/test_createrepophase.py +++ b/tests/test_createrepophase.py @@ -8,12 +8,15 @@ except ImportError: import unittest import mock +import glob import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) -from pungi.phases.createrepo import CreaterepoPhase, create_variant_repo +from pungi.phases.createrepo import (CreaterepoPhase, + create_variant_repo, + get_productids_from_scm) from tests.helpers import DummyCompose, PungiTestCase, copy_fixture, touch @@ -360,5 +363,124 @@ class TestCreateRepoThread(PungiTestCase): self.assertEqual(f.read(), 'Packages/b/bash-debuginfo-4.3.30-2.fc21.x86_64.rpm\n') +class ANYSingleton(object): + """An object that is equal to anything.""" + def __eq__(self, another): + return True + + def __repr__(self): + return u'ANY' + +ANY = ANYSingleton() + + +class TestGetProductIds(PungiTestCase): + def mock_get(self, filenames): + def _mock_get(scm, dest): + for filename in filenames: + touch(os.path.join(dest, filename)) + return _mock_get + + def assertProductIds(self, mapping): + pids = glob.glob(self.compose.paths.work.product_id('*', '*')) + expected = set() + for variant, arches in mapping.iteritems(): + for arch in arches: + expected.add(os.path.join(self.topdir, 'work', arch, + 'product_id', + '%s.%s.pem' % (variant, arch), + 'productid')) + self.assertItemsEqual(pids, expected) + + @mock.patch('pungi.phases.createrepo.get_dir_from_scm') + def test_not_configured(self, get_dir_from_scm): + self.compose = DummyCompose(self.topdir, {}) + get_productids_from_scm(self.compose) + self.assertEqual(get_dir_from_scm.call_args_list, []) + self.assertProductIds({}) + + @mock.patch('pungi.phases.createrepo.get_dir_from_scm') + def test_correct(self, get_dir_from_scm): + cfg = mock.Mock() + self.compose = DummyCompose(self.topdir, { + 'product_id': cfg, + }) + get_dir_from_scm.side_effect = self.mock_get([ + 'Client-amd64-cert.pem', + 'Everything-amd64-cert.pem', + 'Server-amd64-cert.pem', + 'Everything-x86_64-cert.pem', + 'Server-x86_64-cert.pem', + ]) + + get_productids_from_scm(self.compose) + + self.assertEqual(get_dir_from_scm.call_args_list, [mock.call(cfg, ANY)]) + self.assertProductIds({ + 'Client': ['amd64'], + 'Everything': ['amd64', 'x86_64'], + 'Server': ['amd64', 'x86_64'], + }) + + @mock.patch('pungi.phases.createrepo.get_dir_from_scm') + def test_allow_missing(self, get_dir_from_scm): + cfg = mock.Mock() + self.compose = DummyCompose(self.topdir, { + 'product_id': cfg, + 'product_id_allow_missing': True, + }) + get_dir_from_scm.side_effect = self.mock_get([ + 'Server-amd64-cert.pem', + 'Server-x86_64-cert.pem', + ]) + + get_productids_from_scm(self.compose) + + self.assertEqual(get_dir_from_scm.call_args_list, [mock.call(cfg, ANY)]) + self.assertProductIds({ + 'Server': ['amd64', 'x86_64'], + }) + + @mock.patch('pungi.phases.createrepo.get_dir_from_scm') + def test_missing_fails(self, get_dir_from_scm): + cfg = mock.Mock() + self.compose = DummyCompose(self.topdir, { + 'product_id': cfg, + }) + get_dir_from_scm.side_effect = self.mock_get([ + 'Server-amd64-cert.pem', + 'Server-x86_64-cert.pem', + ]) + + with self.assertRaises(RuntimeError) as ctx: + get_productids_from_scm(self.compose) + + self.assertEqual(get_dir_from_scm.call_args_list, [mock.call(cfg, ANY)]) + self.assertEqual(str(ctx.exception), + 'No product certificate found (arch: amd64, variant: Everything)') + + @mock.patch('pungi.phases.createrepo.get_dir_from_scm') + def test_multiple_matching(self, get_dir_from_scm): + cfg = mock.Mock() + self.compose = DummyCompose(self.topdir, { + 'product_id': cfg, + }) + get_dir_from_scm.side_effect = self.mock_get([ + 'Client-amd64-cert.pem', + 'Client-amd64-cert-duplicate.pem', + 'Everything-amd64-cert.pem', + 'Server-amd64-cert.pem', + 'Everything-x86_64-cert.pem', + 'Server-x86_64-cert.pem', + ]) + + with self.assertRaises(RuntimeError) as ctx: + get_productids_from_scm(self.compose) + + self.assertEqual(get_dir_from_scm.call_args_list, [mock.call(cfg, ANY)]) + self.assertRegexpMatches(str(ctx.exception), + 'Multiple product certificates found.+') + + if __name__ == "__main__": unittest.main()