# test/control_file/test_upload_changes.py # Part of ‘dput’, a Debian package upload toolkit. # # This is free software, and you are welcome to redistribute it under # certain conditions; see the end of this file for copyright # information, grant of license, and disclaimer of warranty. """ Unit tests for Debian upload changes control files. The Debian upload changes control file is defined in Policy §5.5. It is typically named with a ‘.changes’ suffix. """ import collections import contextlib import doctest import email.message import hashlib import io import os import os.path import sys import tempfile import textwrap import unittest.mock import gpg import testscenarios import testtools from dput.control_file import upload_changes import dput.crypto from dput.helper import dputhelper from ..helper import ( EXIT_STATUS_FAILURE, FakeSystemExit, FileDouble, SubprocessDouble, get_file_doubles_from_fake_file_scenarios, make_fake_file_scenarios, make_fake_locale_scenarios, make_unique_slug, patch_locale_environment_variables, patch_locale_getpreferredencoding, patch_os_stat, patch_subprocess_popen, patch_system_interfaces, set_fake_file_scenario, setup_fake_file_fixtures, setup_file_double_behaviour, ) from ..test_configfile import ( patch_runtime_config_options, set_config, ) class _FieldsMapping: """ A mapping to stand in for the `dict` of an `email.message.Message`. """ def __init__(self, *args, **kwargs): try: self._message = kwargs.pop('_message') except KeyError: raise TypeError("no ‘_message’ specified for this mapping") super().__init__(*args, **kwargs) def __len__(self, *args, **kwargs): return self._message.__len__(*args, **kwargs) def __contains__(self, *args, **kwargs): return self._message.__contains__(*args, **kwargs) def __getitem__(self, *args, **kwargs): return self._message.__getitem__(*args, **kwargs) def __setitem__(self, *args, **kwargs): self._message.__setitem__(*args, **kwargs) def __delitem__(self, *args, **kwargs): self._message.__delitem__(*args, **kwargs) def keys(self, *args, **kwargs): return self._message.keys(*args, **kwargs) def values(self, *args, **kwargs): return self._message.values(*args, **kwargs) def items(self, *args, **kwargs): return self._message.items(*args, **kwargs) def get(self, *args, **kwargs): return self._message.get(*args, **kwargs) class FakeMessage(email.message.Message): """ A fake RFC 2822 message that mocks the obsolete `rfc822.Message`. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.dict = _FieldsMapping(_message=self) def make_fake_message(fields): """ Make a fake message instance. """ message = FakeMessage() for (name, value) in fields.items(): message.add_header(name, value) return message def make_files_field_value(params_by_name): """ Make a value for “Files” field of a changes document. """ result = "\n".join( " ".join(params) for (file_name, params) in params_by_name.items()) return result def make_upload_files_params(checksums_by_file_name, sizes_by_file_name): """ Make a mapping of upload parameters for files. """ params_by_name = { file_name: [ checksums_by_file_name[file_name], str(sizes_by_file_name[file_name]), "foo", "bar", file_name] for file_name in checksums_by_file_name} return params_by_name def make_changes_document(fields, upload_params_by_name=None): """ Make a changes document from field values. :param fields: Sequence of (name, value) tuples for fields. :param upload_params_by_name: Mapping from filename to upload parameters for each file. :return: The changes document as an RFC 822 formatted text. """ document_fields = fields.copy() if upload_params_by_name is not None: files_field_text = make_files_field_value(upload_params_by_name) document_fields.update({'files': files_field_text}) document = make_fake_message(document_fields) return document def make_changes_file_scenarios(): """ Make fake Debian upload control (‘*.changes’) scenarios. """ file_path = make_changes_file_path() fake_file_empty = io.StringIO() fake_file_no_format = io.StringIO(textwrap.dedent("""\ FOO BAR Files: Lorem ipsum dolor sit amet """)) fake_file_with_signature = io.StringIO(textwrap.dedent("""\ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 FOO BAR Files: Lorem ipsum dolor sit amet -----BEGIN PGP SIGNATURE----- Version: 0.0 Comment: Proin ac massa at orci sagittis fermentum. gibberishgibberishgibberishgibberishgibberishgibberish gibberishgibberishgibberishgibberishgibberishgibberish gibberishgibberishgibberishgibberishgibberishgibberish -----END PGP SIGNATURE----- """)) fake_file_with_format = io.StringIO(textwrap.dedent("""\ Format: FOO Changed-By: Dolor sit amet Files: Lorem ipsum dolor sit amet """)) fake_file_accented_text = io.StringIO(textwrap.dedent("""\ Format: FOO Changed-By: Dølòr sít ämêt Files: Lorem ipsum dolor sit amet """)) fake_file_olditalic_text = io.StringIO(textwrap.dedent("""\ Format: FOO Changed-By: 𐌃𐌏𐌋𐌏𐌓 𐌔𐌉𐌕 𐌀𐌌𐌄𐌕 Files: Lorem ipsum dolor sit amet """)) fake_file_invalid = io.StringIO(textwrap.dedent("""\ Format: FOO Files: FOO BAR """)) fake_file_needs_utf8 = io.StringIO(textwrap.dedent("""\ # ქართული # 你好 Files: Lórem ipsüm dolôr sìt āmet """)) scenarios = [ ('no-format', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_no_format), 'expected_result': make_changes_document({ 'files': "Lorem ipsum dolor sit amet", }), }), ('with-pgp-signature', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_with_signature), 'expected_result': make_changes_document({ 'files': "Lorem ipsum dolor sit amet", }), }), ('with-format', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_with_format), 'expected_result': make_changes_document({ 'changed-by': "Dolor sit amet ", 'files': "Lorem ipsum dolor sit amet", }), }), ('non-ascii', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_accented_text), 'expected_result': make_changes_document({ 'changed-by': "Dølòr sít ämêt ", 'files': "Lorem ipsum dolor sit amet", }), }), ('higher-character-plane', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_olditalic_text), 'expected_result': make_changes_document({ 'changed-by': "𐌃𐌏𐌋𐌏𐌓 𐌔𐌉𐌕 𐌀𐌌𐌄𐌕 ", 'files': "Lorem ipsum dolor sit amet", }), }), ('error empty', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_empty), 'expected_error': KeyError, }), ('error invalid', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_invalid), 'expected_error': FakeSystemExit, }), ('encoding needs-utf8', { 'file_double': FileDouble( path=file_path, fake_file=fake_file_needs_utf8), 'expected_result': make_changes_document({ 'files': "Lórem ipsüm dolôr sìt āmet", }), }), ] for (scenario_name, scenario) in scenarios: scenario['changes_file_scenario_name'] = scenario_name return scenarios def set_fake_upload_file_paths(testcase): """ Set the fake upload file paths. """ testcase.fake_upload_file_paths = [ os.path.join( os.path.dirname(testcase.changes_file_double.path), os.path.basename(tempfile.mktemp())) for __ in range(10)] required_suffixes = [".dsc", ".tar.xz"] suffixes = required_suffixes + getattr( testcase, 'additional_file_suffixes', []) file_path_base = testcase.fake_upload_file_paths.pop() for suffix in suffixes: file_path = file_path_base + suffix testcase.fake_upload_file_paths.insert(0, file_path) def set_file_checksums(testcase): """ Set the fake file checksums for the test case. """ testcase.fake_checksum_by_file = { os.path.basename(file_path): make_unique_slug(testcase) for file_path in testcase.fake_upload_file_paths} def set_file_sizes(testcase): """ Set the fake file sizes for the test case. """ testcase.fake_size_by_file = { os.path.basename(file_path): testcase.getUniqueInteger() for file_path in testcase.fake_upload_file_paths} def set_file_doubles(testcase): """ Set the file doubles for the test case. """ for file_path in testcase.fake_upload_file_paths: file_double = FileDouble(file_path) file_double.set_os_stat_scenario('okay') file_double.stat_result = file_double.stat_result._replace( st_size=testcase.fake_size_by_file[ os.path.basename(file_path)], ) file_double.register_for_testcase(testcase) def setup_upload_file_fixtures(testcase): """ Set fixtures for fake files to upload for the test case. """ set_fake_upload_file_paths(testcase) set_file_checksums(testcase) set_file_sizes(testcase) set_file_doubles(testcase) def make_changes_file_path(file_dir_path=None): """ Make a filesystem path for the changes file. """ if file_dir_path is None: file_dir_path = tempfile.mktemp() file_name = os.path.basename( "{base}.changes".format(base=tempfile.mktemp())) file_path = os.path.join(file_dir_path, file_name) return file_path def setup_changes_file_fixtures(testcase): """ Set up fixtures for changes file doubles. """ file_path = make_changes_file_path() scenarios = make_fake_file_scenarios(file_path) testcase.changes_file_scenarios = scenarios file_doubles = get_file_doubles_from_fake_file_scenarios( scenarios.values()) setup_file_double_behaviour(testcase, file_doubles) def set_changes_file_scenario(testcase, name): """ Set the changes file scenario for this test case. """ scenario = dict(testcase.changes_file_scenarios)[name] testcase.changes_file_scenario = scenario testcase.changes_file_double = scenario['file_double'] testcase.changes_file_double.register_for_testcase(testcase) class parse_changes_TestCase( testscenarios.WithScenarios, testtools.TestCase): """ Base for test cases for `parse_changes` function. """ scenarios = NotImplemented def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) self.test_infile = io.StringIO() class parse_changes_SuccessTestCase(parse_changes_TestCase): """ Success test cases for `parse_changes` function. """ scenarios = [ (name, scenario) for (name, scenario) in make_changes_file_scenarios() if not name.startswith('error')] def test_gives_expected_result_for_infile(self): """ Should give the expected result for specified input file. """ result = upload_changes.parse_changes(self.file_double.fake_file) normalised_result_set = { (key.lower(), value.strip()) for (key, value) in result.items()} self.assertEqual( set(self.expected_result.items()), normalised_result_set) class parse_changes_ErrorTestCase(parse_changes_TestCase): """ Error test cases for `parse_changes` function. """ scenarios = [ (name, scenario) for (name, scenario) in make_changes_file_scenarios() if name.startswith('error')] def test_raises_expected_exception_for_infile(self): """ Should raise the expected exception for specified input file. """ with testtools.ExpectedException(self.expected_error): upload_changes.parse_changes(self.file_double.fake_file) def patch_parse_changes(testcase): """ Patch the `parse_changes` function for the test case. """ func_patcher = unittest.mock.patch.object( upload_changes, "parse_changes", autospec=True) func_patcher.start() testcase.addCleanup(func_patcher.stop) class checksum_test_TestCase( testscenarios.WithScenarios, testtools.TestCase): """ Base for test cases for `checksum_test` function. """ scenarios = NotImplemented def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) setup_fake_file_fixtures(self) set_fake_file_scenario(self, self.fake_file_scenario_name) file_content = self.file_double.fake_file.getvalue().encode('utf-8') self.file_double.fake_file = io.BytesIO(file_content) self.set_test_args() def set_test_args(self): """ Set the arguments for the test call to the function. """ self.test_args = dict( filename=self.file_double.path, hash_name=self.hash_name, ) class checksum_test_SuccessTestCase(checksum_test_TestCase): """ Success test cases for `checksum_test` function. """ hash_scenarios = [ ('md5', { 'hash_name': "md5", 'test_hash_func': hashlib.md5, }), ('sha1', { 'hash_name': "sha1", 'test_hash_func': hashlib.sha1, }), ] fake_file_scenarios = [ (name, scenario) for (name, scenario) in make_fake_file_scenarios().items() if not name.startswith('error')] scenarios = testscenarios.multiply_scenarios( hash_scenarios, fake_file_scenarios) def test_returns_expected_result_for_input(self): """ Should return expected result for specified inputs. """ expected_hash = self.test_hash_func( self.file_double.fake_file.getvalue()) expected_result = expected_hash.hexdigest() result = upload_changes.checksum_test(**self.test_args) self.assertEqual(expected_result, result) class checksum_test_ErrorTestCase(checksum_test_TestCase): """ Error test cases for `checksum_test` function. """ hash_scenarios = [ ('md5', { 'hash_name': "md5", }), ] fake_file_scenarios = [ (name, scenario) for (name, scenario) in make_fake_file_scenarios().items() if name.startswith('error')] scenarios = testscenarios.multiply_scenarios( hash_scenarios, fake_file_scenarios) def test_calls_sys_exit_if_error_reading_file(self): """ Should call `sys.exit` if unable to read the file. """ set_fake_file_scenario(self, 'error-read-denied') with testtools.ExpectedException(FakeSystemExit): upload_changes.checksum_test(**self.test_args) expected_output = textwrap.dedent("""\ Can't open {path} """).format(path=self.file_double.path) self.assertIn(expected_output, sys.stdout.getvalue()) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) class check_upload_variant_TestCase( testscenarios.WithScenarios, testtools.TestCase): """ Test cases for `check_upload_variant` function. """ scenarios = [ ('simple', { 'fields': { 'architecture': "foo bar baz", }, 'expected_result': True, }), ('arch-missing', { 'fields': { 'spam': "Lorem ipsum dolor sit amet", }, 'expected_result': False, }), ('source-only', { 'fields': { 'architecture': "source", }, 'expected_result': False, }), ('source-and-others', { 'fields': { 'architecture': "foo source bar", }, 'expected_result': False, }), ] def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) self.set_changes_document(self.fields) self.set_test_args() def set_changes_document(self, fields): """ Set the package changes document based on specified fields. """ self.test_changes_document = make_changes_document(fields) def set_test_args(self): """ Set the arguments for the test call to the function. """ self.test_args = { 'changes': self.test_changes_document, 'debug': False, } def test_returns_expected_result_for_changes_document(self): """ Should return expected result for specified changes document. """ result = upload_changes.check_upload_variant(**self.test_args) self.assertEqual(self.expected_result, result) def test_emits_debug_message_showing_architecture(self): """ Should emit a debug message for the specified architecture. """ if 'architecture' not in self.fields: self.skipTest("Architecture field not in this scenario") self.test_args['debug'] = True upload_changes.check_upload_variant(**self.test_args) expected_output = textwrap.dedent("""\ D: Architecture: {arch} """).format(arch=self.fields['architecture']) self.assertIn(expected_output, sys.stdout.getvalue()) def test_emits_debug_message_for_binary_upload(self): """ Should emit a debug message for the specified architecture. """ triggers_binaryonly = bool(self.expected_result) if not triggers_binaryonly: self.skipTest("Scenario does not trigger binary-only upload") self.test_args['debug'] = True upload_changes.check_upload_variant(**self.test_args) expected_output = textwrap.dedent("""\ D: Doing a binary upload only. """) self.assertIn(expected_output, sys.stdout.getvalue()) class verify_signature_BaseTestCase( testscenarios.WithScenarios, testtools.TestCase): """ Base for test cases for `verify_signature` function. """ locale_getpreferredencoding_return_value = "UTF-8" scenarios = NotImplemented default_args = dict( host="foo", check_only=False, allow_unsigned_uploads=False, binary_upload=False, debug=None, ) def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) patch_locale_getpreferredencoding(self) set_config( self, getattr(self, 'config_scenario_name', 'exist-simple')) patch_runtime_config_options(self) self.set_changes_file_scenario('okay') self.set_dsc_file_scenario('okay') self.set_test_args() setup_fake_file_fixtures(self) set_fake_file_scenario(self, 'exist-minimal') self.patch_check_file_signature() def patch_check_file_signature(self): """ Patch the `check_file_signature` function for this test case. """ func_patcher = unittest.mock.patch.object( dput.crypto, "check_file_signature", autospec=True) func_patcher.start() self.addCleanup(func_patcher.stop) def set_changes_file_scenario(self, name): """ Set the changes file scenario for this test case. """ file_double = FileDouble(tempfile.mktemp()) file_double.set_open_scenario(name) file_double.register_for_testcase(self) self.changes_file_double = file_double def set_dsc_file_scenario(self, name): """ Set the source control file scenario for this test case. """ file_double = FileDouble(tempfile.mktemp()) file_double.set_open_scenario(name) file_double.register_for_testcase(self) self.dsc_file_double = file_double def set_test_args(self): """ Set test args for this test case. """ extra_args = getattr(self, 'extra_args', {}) self.test_args = self.default_args.copy() self.test_args['config'] = self.runtime_config_parser self.test_args['changes_file_path'] = self.changes_file_double.path self.test_args['dsc_file_path'] = self.dsc_file_double.path self.test_args.update(extra_args) class verify_signature_DebugMessageTestCase(verify_signature_BaseTestCase): """ Test cases for `verify_signature` debug messages. """ scenarios = [ ('default', {}), ] def test_emits_debug_message_showing_files(self): """ Should emit a debug message for the specified files. """ self.test_args['debug'] = True upload_changes.verify_signature(**self.test_args) expected_output = textwrap.dedent("""\ D: upload control file: {changes_path} D: source control file: {dsc_path} """).format( changes_path=self.test_args['changes_file_path'], dsc_path=self.test_args['dsc_file_path']) self.assertIn(expected_output, sys.stdout.getvalue()) class verify_signature_ChecksTestCase(verify_signature_BaseTestCase): """ Test cases for `verify_signature` checks. """ file_args_scenarios = [ ('default', { 'expected_checks': ['changes', 'dsc'], }), ('source unsigned', { 'extra_args': { 'binary_upload': False, 'allow_unsigned_uploads': True, }, 'expected_checks': [], }), ('source check-only', { 'extra_args': { 'binary_upload': False, 'check_only': True, }, 'expected_checks': ['changes', 'dsc'], }), ('source allow_unsigned_uploads', { 'extra_args': { 'binary_upload': False, }, 'config_scenario_name': 'exist-default-not-unsigned', 'expected_checks': ['changes', 'dsc'], }), ('binary unsigned', { 'extra_args': { 'binary_upload': True, 'allow_unsigned_uploads': True, }, 'expected_checks': [], }), ('binary check-only', { 'extra_args': { 'binary_upload': True, 'check_only': True, }, 'expected_checks': ['changes'], }), ('binary allow_unsigned_uploads', { 'extra_args': { 'binary_upload': True, }, 'config_scenario_name': 'exist-default-not-unsigned', 'expected_checks': ['changes'], }), ] locale_scenarios = make_fake_locale_scenarios() scenarios = testscenarios.multiply_scenarios( file_args_scenarios, locale_scenarios) def test_checks_changes_signature_only_when_expected(self): """ Should only check the changes document signature if expected. """ upload_changes.verify_signature(**self.test_args) registry = FileDouble.get_registry_for_testcase(self) file_double = registry[self.changes_file_double.path] fake_file = file_double.fake_file_opened_with_mode_and_encoding( encoding='UTF-8') check_call = unittest.mock.call(fake_file) if 'changes' in self.expected_checks: self.assertIn( check_call, dput.crypto.check_file_signature.call_args_list) else: self.assertNotIn( check_call, dput.crypto.check_file_signature.call_args_list) def test_calls_sys_exit_when_check_file_signature_raises_gpg_error(self): """ Should `sys.exit` when `check_file_signature` raises GPG error. """ if not self.expected_checks: self.skipTest("No signature checks requested") dput.crypto.check_file_signature.side_effect = gpg.errors.GPGMEError(0) with testtools.ExpectedException(FakeSystemExit): upload_changes.verify_signature(**self.test_args) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) def test_raises_same_error_when_check_file_signature_raises_error(self): """ Should re-raise `check_file_signature` error when not a GPG error. """ if not self.expected_checks: self.skipTest("No signature checks requested") test_error = RuntimeError("whoops") dput.crypto.check_file_signature.side_effect = test_error with testtools.ExpectedException(type(test_error)): upload_changes.verify_signature(**self.test_args) def test_checks_dsc_signature_only_when_expected(self): """ Should only check the ‘dsc’ document signature if expected. """ upload_changes.verify_signature(**self.test_args) registry = FileDouble.get_registry_for_testcase(self) file_double = registry[self.dsc_file_double.path] fake_file = file_double.fake_file_opened_with_mode_and_encoding( encoding='UTF-8') check_call = unittest.mock.call(fake_file) if 'dsc' in self.expected_checks: self.assertIn( check_call, dput.crypto.check_file_signature.call_args_list) else: self.assertNotIn( check_call, dput.crypto.check_file_signature.call_args_list) SourceCheckResult = collections.namedtuple( 'SourceCheckResult', ['include_orig', 'include_tar']) class source_check_BaseTestCase( testscenarios.WithScenarios, testtools.TestCase): """ Base for test cases for `source_check` function. """ def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) self.make_changes_document() self.set_test_args() def make_test_version_string(self): """ Make the test version string from component parts. """ version_string = self.test_upstream_version if hasattr(self, 'test_epoch'): version_string = "{epoch}:{version}".format( epoch=self.test_epoch, version=version_string) if hasattr(self, 'test_release'): version_string = "{version}-{release}".format( version=version_string, release=self.test_release) return version_string def make_changes_document(self): """ Make the fake document for upload changes control file. """ fields = {} if ( hasattr(self, 'test_upstream_version') and not hasattr(self, 'test_version') ): self.test_version = self.make_test_version_string() if hasattr(self, 'test_version'): fields.update({'version': self.test_version}) self.changes_document = make_changes_document(fields) def set_test_args(self): self.test_args = { 'changes': self.changes_document, 'debug': False, } class source_check_SuccessTestCase(source_check_BaseTestCase): """ Test cases for `source_check` function, success result. """ default_expected_result = SourceCheckResult( include_orig=False, include_tar=False) scenarios = [ ('no-version', { 'expected_result': default_expected_result, }), ('no-epoch native-version', { 'test_upstream_version': "1.2", 'expected_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('epoch native-version', { 'test_epoch': "3", 'test_upstream_version': "1.2", 'expected_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('no-epoch debian-release', { 'test_upstream_version': "1.2", 'test_release': "5", 'expected_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('no-epoch version-with-hyphen debian-release', { 'test_upstream_version': "1.2-3", 'test_release': "5", 'expected_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('epoch debian-release', { 'test_epoch': "3", 'test_upstream_version': "1.2", 'test_release': "5", 'expected_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('no-epoch new-upstream-version', { 'test_upstream_version': "1.2", 'test_release': "1", 'expected_result': SourceCheckResult( include_orig=True, include_tar=False), }), ('no-epoch new-upstream-version-with-hyphen', { 'test_upstream_version': "1.2-3", 'test_release': "1", 'expected_result': SourceCheckResult( include_orig=True, include_tar=False), }), ('epoch new_upstream-version', { 'test_epoch': "3", 'test_upstream_version': "1.2", 'test_release': "1", 'expected_result': SourceCheckResult( include_orig=True, include_tar=False), }), ('no-epoch nmu', { 'test_upstream_version': "1.2", 'test_release': "4.5", 'expected_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('epoch nmu', { 'test_epoch': "3", 'test_upstream_version': "1.2", 'test_release': "4.5", 'expected_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('no-epoch nmu before-first-release', { 'test_upstream_version': "1.2", 'test_release': "0.1", 'expected_result': SourceCheckResult( include_orig=True, include_tar=False), }), ('epoch nmu before-first-release', { 'test_epoch': "3", 'test_upstream_version': "1.2", 'test_release': "0.1", 'expected_result': SourceCheckResult( include_orig=True, include_tar=False), }), ('no-epoch nmu after-first-release', { 'test_upstream_version': "1.2", 'test_release': "1.1", 'expected_result': SourceCheckResult( include_orig=True, include_tar=False), }), ('epoch nmu after-first-release', { 'test_epoch': "3", 'test_upstream_version': "1.2", 'test_release': "1.1", 'expected_result': SourceCheckResult( include_orig=True, include_tar=False), }), ] def test_returns_expected_result_for_changes_document(self): """ Should return expected result for specified changes document. """ result = upload_changes.source_check(**self.test_args) self.assertEqual(self.expected_result, result) def test_emits_version_string_debug_message_only_if_version(self): """ Should emit message for version only if has version. """ self.test_args['debug'] = True version = getattr(self, 'test_version', None) message_lead = "D: Package Version:" expected_output = textwrap.dedent("""\ {lead} {version} """).format( lead=message_lead, version=version) upload_changes.source_check(**self.test_args) if hasattr(self, 'test_version'): self.assertIn(expected_output, sys.stdout.getvalue()) else: self.assertNotIn(message_lead, sys.stdout.getvalue()) def test_emits_epoch_debug_message_only_if_epoch(self): """ Should emit message for epoch only if has an epoch. """ self.test_args['debug'] = True upload_changes.source_check(**self.test_args) expected_output = textwrap.dedent("""\ D: Epoch found """) upload_changes.source_check(**self.test_args) if (hasattr(self, 'test_epoch') and hasattr(self, 'test_release')): self.assertIn(expected_output, sys.stdout.getvalue()) else: self.assertNotIn(expected_output, sys.stdout.getvalue()) def test_emits_upstream_version_debug_message_only_if_nonnative(self): """ Should emit message for upstream version only if non-native. """ self.test_args['debug'] = True upstream_version = getattr(self, 'test_upstream_version', None) message_lead = "D: Upstream Version:" expected_output = textwrap.dedent("""\ {lead} {version} """).format( lead=message_lead, version=upstream_version) upload_changes.source_check(**self.test_args) if hasattr(self, 'test_release'): self.assertIn(expected_output, sys.stdout.getvalue()) else: self.assertNotIn(message_lead, sys.stdout.getvalue()) def test_emits_debian_release_debug_message_only_if_nonnative(self): """ Should emit message for Debian release only if non-native. """ self.test_args['debug'] = True debian_release = getattr(self, 'test_release', None) message_lead = "D: Debian Version:" expected_output = textwrap.dedent("""\ {lead} {version} """).format( lead=message_lead, version=debian_release) upload_changes.source_check(**self.test_args) if hasattr(self, 'test_release'): self.assertIn(expected_output, sys.stdout.getvalue()) else: self.assertNotIn(message_lead, sys.stdout.getvalue()) class source_check_ErrorTestCase(source_check_BaseTestCase): """ Test cases for `source_check` function, error result. """ scenarios = [ ('version-characters-invalid', { 'test_version': "inv@l!d", 'expected_error': ( dputhelper.UploadChangesInvalidVersionDputError), }), ('version-epoch-characters-invalid', { 'test_version': "1%3:1.2.3-1", 'expected_error': ( dputhelper.UploadChangesInvalidVersionDputError), }), ('version-debian-release-characters-invalid', { 'test_version': "3:1.2.3-1@5", 'expected_error': ( dputhelper.UploadChangesInvalidVersionDputError), }), ('version-too-many-epochs', { 'test_version': "3:7:1.2.3-1", 'expected_error': ( dputhelper.UploadChangesInvalidVersionDputError), }), ] def test_raises_expected_exception_for_infile(self): """ Should raise the expected exception for specified input file. """ with testtools.ExpectedException(self.expected_error): upload_changes.source_check(**self.test_args) class verify_files_BaseTestCase(testtools.TestCase): """ Base for test cases for `verify_files` function. """ function_to_test = staticmethod(upload_changes.verify_files) default_args = dict( host="foo", check_only=None, check_version=None, allow_unsigned_uploads=None, debug=None, ) def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) self.file_double_by_path = {} set_config(self, 'exist-simple') patch_runtime_config_options(self) self.set_test_args() setup_changes_file_fixtures(self) set_changes_file_scenario(self, 'exist-minimal') self.test_args.update(dict( changes_file_directory=os.path.dirname( self.changes_file_double.path), changes_file_name=os.path.basename( self.changes_file_double.path), )) patch_os_stat(self) setup_upload_file_fixtures(self) self.set_expected_files_to_upload() self.patch_checksum_test() self.patch_parse_changes() self.patch_check_upload_variant() self.set_expected_binary_upload() self.set_expected_source_control_file_path() self.patch_version_check() self.patch_verify_signature() self.patch_source_check() def set_expected_files_to_upload(self): """ Set the expected `files_to_upload` result for this test case. """ self.expected_files_to_upload = { path for path in self.fake_upload_file_paths} self.expected_files_to_upload.add(self.changes_file_double.path) def patch_checksum_test(self): """ Patch `checksum_test` function for this test case. """ func_patcher = unittest.mock.patch.object( upload_changes, "checksum_test", autospec=True) mock_func = func_patcher.start() self.addCleanup(func_patcher.stop) def get_checksum_for_file(path, hash_name): return self.fake_checksum_by_file[os.path.basename(path)] mock_func.side_effect = get_checksum_for_file def set_changes_document(self): """ Set the changes document for this test case. """ self.changes_document = make_changes_document( fields={}, upload_params_by_name=self.upload_params_by_name) self.test_distribution = getattr(self, 'test_distribution', "lorem") if self.test_distribution is not None: self.changes_document.add_header( 'distribution', self.test_distribution) self.runtime_config_parser.set( self.test_args['host'], 'allowed_distributions', self.test_distribution) upload_changes.parse_changes.return_value = self.changes_document def set_upload_params(self): """ Set the upload parameters for this test case. """ self.upload_params_by_name = make_upload_files_params( self.fake_checksum_by_file, self.fake_size_by_file) def patch_parse_changes(self): """ Patch `parse_changes` function for this test case. """ func_patcher = unittest.mock.patch.object( upload_changes, "parse_changes", autospec=True) func_patcher.start() self.addCleanup(func_patcher.stop) self.set_upload_params() self.set_changes_document() def patch_check_upload_variant(self): """ Patch `check_upload_variant` function for this test case. """ if not hasattr(self, 'check_upload_variant_return_value'): self.check_upload_variant_return_value = True func_patcher = unittest.mock.patch.object( upload_changes, "check_upload_variant", autospec=True, return_value=self.check_upload_variant_return_value) func_patcher.start() self.addCleanup(func_patcher.stop) def patch_version_check(self): """ Patch `version_check` function for this test case. """ func_patcher = unittest.mock.patch.object( upload_changes, "version_check", autospec=True) func_patcher.start() self.addCleanup(func_patcher.stop) def patch_verify_signature(self): """ Patch `verify_signature` function for this test case. """ func_patcher = unittest.mock.patch.object( upload_changes, "verify_signature", autospec=True) func_patcher.start() self.addCleanup(func_patcher.stop) def patch_source_check(self): """ Patch `source_check` function for this test case. """ func_patcher = unittest.mock.patch.object( upload_changes, "source_check", autospec=True) mock_func = func_patcher.start() self.addCleanup(func_patcher.stop) source_check_result = getattr( self, 'source_check_result', SourceCheckResult( include_orig=False, include_tar=False)) mock_func.return_value = source_check_result def set_test_args(self): """ Set test args for this test case. """ extra_args = getattr(self, 'extra_args', {}) self.test_args = self.default_args.copy() self.test_args['config'] = self.runtime_config_parser self.test_args.update(extra_args) def set_expected_binary_upload(self): """ Set expected value for `binary_upload` flag. """ self.expected_binary_upload = self.check_upload_variant_return_value def set_expected_source_control_file_path(self): """ Set expected value for source control file path. """ file_name = next( os.path.basename(file_path) for file_path in self.fake_upload_file_paths if file_path.endswith(".dsc")) if not self.expected_binary_upload: self.expected_source_control_file_path = os.path.join( os.path.dirname(self.changes_file_double.path), file_name) else: self.expected_source_control_file_path = "" class verify_files_TestCase( testscenarios.WithScenarios, verify_files_BaseTestCase): """ Test cases for `verify_files` function. """ filenames_scenarios = [ ('default', {}), ('include foo.tar.gz', { 'additional_file_suffixes': [".tar.gz"], 'source_check_result': SourceCheckResult( include_orig=False, include_tar=True), }), ('include foo.orig.tar.gz', { 'additional_file_suffixes': [".orig.tar.gz"], 'source_check_result': SourceCheckResult( include_orig=True, include_tar=False), }), ('unexpected foo.tar.gz', { 'additional_file_suffixes': [".tar.gz"], 'expected_rejection_message': ( "Package includes a .tar.gz file although"), }), ('unexpected foo.orig.tar.gz', { 'additional_file_suffixes': [".orig.tar.gz"], 'expected_rejection_message': ( "Package includes an .orig.tar.gz file although"), }), ] check_upload_variant_scenarios = [ ('source-and-binary', {}), ('binary-only', { 'check_upload_variant_return_value': False, }), ] distribution_scenarios = [ ('distribution-default', {}), ('no-distribution', { 'test_distribution': None, }), ] hash_name_scenarios = [ ('hash-name-default', {}), ('hash-name-default-sha512', { 'config_extras': { 'default': { 'hash': "sha512", }, 'host': { 'hash': None, }, }, }), ('hash-name-host-sha512', { 'config_extras': { 'default': { 'hash': "b0gUs", }, 'host': { 'hash': "sha512", }, }, }), ] scenarios = testscenarios.multiply_scenarios( filenames_scenarios, check_upload_variant_scenarios, distribution_scenarios, hash_name_scenarios, ) def test_emits_changes_file_path_debug_message(self): """ Should emit debug message for changes file path. """ self.test_args['debug'] = True self.function_to_test(**self.test_args) expected_output = textwrap.dedent("""\ D: Parsing Debian upload changes control file {path} """).format(path=self.changes_file_double.path) self.assertIn(expected_output, sys.stdout.getvalue()) def test_calls_sys_exit_if_input_read_denied(self): """ Should call `sys.exit` if input file read access is denied. """ set_changes_file_scenario(self, 'error-read-denied') with testtools.ExpectedException(FakeSystemExit): self.function_to_test(**self.test_args) expected_output = textwrap.dedent("""\ Can't open {path} """).format(path=self.changes_file_double.path) self.assertIn(expected_output, sys.stdout.getvalue()) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) def test_calls_parse_changes_with_changes_files(self): """ Should call `parse_changes` with changes file. """ self.function_to_test(**self.test_args) file_double = self.changes_file_double with open(file_double.path, encoding='UTF-8') as expected_file: upload_changes.parse_changes.assert_called_with(expected_file) def test_calls_check_upload_variant_with_changes_document(self): """ Should call `check_upload_variant` with changes document. """ self.function_to_test(**self.test_args) upload_changes.check_upload_variant.assert_called_with( self.changes_document, unittest.mock.ANY) def test_emits_upload_dsc_file_debug_message(self): """ Should emit debug message for ‘*.dsc’ file. """ if getattr(self, 'check_upload_variant_return_value', True): self.skipTest("Binary package upload for this scenario") self.test_args['debug'] = True self.function_to_test(**self.test_args) dsc_file_path = next( os.path.basename(file_path) for file_path in self.fake_upload_file_paths if file_path.endswith(".dsc")) expected_output = textwrap.dedent("""\ D: dsc-File: {path} """).format(path=dsc_file_path) self.assertThat( sys.stdout.getvalue(), testtools.matchers.Contains(expected_output)) def test_calls_sys_exit_when_source_upload_omits_dsc_file(self): """ Should call `sys.exit` when source upload omits ‘*.dsc’ file. """ if getattr(self, 'check_upload_variant_return_value', True): self.skipTest("Binary package upload for this scenario") self.fake_checksum_by_file = { file_path: checksum for (file_path, checksum) in self.fake_checksum_by_file.items() if not file_path.endswith(".dsc") } self.set_upload_params() self.set_changes_document() with testtools.ExpectedException(FakeSystemExit): self.function_to_test(**self.test_args) expected_output = textwrap.dedent("""\ Error: no dsc file found in sourceful upload """) self.assertThat( sys.stderr.getvalue(), testtools.matchers.Contains(expected_output)) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) def test_calls_version_check_when_specified_in_config(self): """ Should call `version_check` when specified in config. """ self.runtime_config_parser.set( self.test_args['host'], 'check_version', "true") self.function_to_test(**self.test_args) upload_changes.version_check.assert_called_with( files_directory=os.path.dirname(self.changes_file_double.path), changes=self.changes_document, debug=self.test_args['debug']) def test_calls_version_check_when_specified_in_args(self): """ Should call `version_check` when specified in arguments. """ self.test_args['check_version'] = True self.function_to_test(**self.test_args) upload_changes.version_check.assert_called_with( files_directory=os.path.dirname(self.changes_file_double.path), changes=self.changes_document, debug=self.test_args['debug']) def test_calls_sys_exit_when_host_section_not_in_config(self): """ Should call `sys.exit` when specified host not in config. """ self.runtime_config_parser.remove_section(self.test_args['host']) with testtools.ExpectedException(FakeSystemExit): self.function_to_test(**self.test_args) expected_output = textwrap.dedent("""\ Error in config file: No section: ... """) self.assertThat( sys.stderr.getvalue(), testtools.matchers.DocTestMatches( expected_output, doctest.ELLIPSIS)) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) def test_calls_verify_signature_with_expected_args(self): """ Should call `verify_signature` with expected args. """ self.function_to_test(**self.test_args) upload_changes.verify_signature.assert_called_with( self.test_args['host'], self.changes_file_double.path, self.expected_source_control_file_path, self.runtime_config_parser, self.test_args['check_only'], self.test_args['allow_unsigned_uploads'], unittest.mock.ANY, self.test_args['debug']) def test_calls_source_check_with_changes_document(self): """ Should call `source_check` with changes document. """ self.function_to_test(**self.test_args) upload_changes.source_check.assert_called_with( self.changes_document, self.test_args['debug']) def test_emits_upload_file_path_debug_message(self): """ Should emit debug message for each upload file path. """ self.test_args['debug'] = True self.function_to_test(**self.test_args) for file_path in self.fake_upload_file_paths: expected_output = textwrap.dedent("""\ D: File to upload: {path} """).format(path=file_path) self.expectThat( sys.stdout.getvalue(), testtools.matchers.Contains(expected_output)) def test_calls_checksum_test_with_upload_files(self): """ Should call `checksum_test` with each upload file path. """ self.function_to_test(**self.test_args) expected_calls = [ unittest.mock.call(file_path, unittest.mock.ANY) for file_path in self.fake_upload_file_paths] upload_changes.checksum_test.assert_has_calls( expected_calls, any_order=True) def test_calls_checksum_test_with_configured_hash_name(self): """ Should call `checksum_test` with expected hash name. """ self.function_to_test(**self.test_args) expected_hash_name = self.runtime_config_parser.get( self.test_host, 'hash') expected_calls = [ unittest.mock.call(unittest.mock.ANY, expected_hash_name) for file_path in self.fake_upload_file_paths] upload_changes.checksum_test.assert_has_calls( expected_calls, any_order=True) def set_bogus_file_checksums(self): """ Set bogus file checksums that will not match. """ self.fake_checksum_by_file = { file_name: self.getUniqueString() for file_name in self.fake_checksum_by_file} def test_emits_checksum_okay_debug_message(self): """ Should emit debug message checksum okay for each file. """ self.test_args['debug'] = True self.function_to_test(**self.test_args) for file_path in self.fake_upload_file_paths: expected_output = textwrap.dedent("""\ D: Checksum for {path} is fine """).format(path=file_path) self.expectThat( sys.stdout.getvalue(), testtools.matchers.Contains(expected_output)) def test_emits_checksum_mismatch_debug_message(self): """ Should emit debug message when a checksum does not match. """ self.test_args['debug'] = True self.set_bogus_file_checksums() with testtools.ExpectedException(FakeSystemExit): self.function_to_test(**self.test_args) expected_output = textwrap.dedent("""\ ... D: Checksum from .changes: ... D: Generated Checksum: ... ... """) self.assertThat( sys.stdout.getvalue(), testtools.matchers.DocTestMatches( expected_output, doctest.ELLIPSIS)) def test_calls_sys_exit_when_checksum_mismatch(self): """ Should call `sys.exit` when a checksum does not match. """ specified_checksum_by_file = self.fake_checksum_by_file self.set_bogus_file_checksums() with testtools.ExpectedException(FakeSystemExit): self.function_to_test(**self.test_args) expected_output_for_files = [ textwrap.dedent("""\ Checksum doesn't match for {file_name} """).format( file_name=os.path.join( os.path.dirname(self.changes_file_double.path), file_name), specified_hash=specified_hash, computed_hash=self.fake_checksum_by_file[file_name]) for (file_name, specified_hash) in specified_checksum_by_file.items()] self.assertThat( sys.stdout.getvalue(), testtools.matchers.MatchesAny(*[ testtools.matchers.Contains(expected_output) for expected_output in expected_output_for_files])) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) def test_calls_os_stat_with_upload_files(self): """ Should call `os.stat` with each upload file path. """ self.function_to_test(**self.test_args) expected_calls = [ unittest.mock.call(file_path) for file_path in self.fake_upload_file_paths] os.stat.assert_has_calls(expected_calls, any_order=True) def set_bogus_file_sizes(self): """ Set bogus file sizes that will not match. """ file_double_registry = FileDouble.get_registry_for_testcase(self) for file_name in self.fake_size_by_file: bogus_size = self.getUniqueInteger() self.fake_size_by_file[file_name] = bogus_size file_path = os.path.join( os.path.dirname(self.changes_file_double.path), file_name) file_double = file_double_registry[file_path] file_double.stat_result = file_double.stat_result._replace( st_size=bogus_size) def test_emits_size_mismatch_debug_message(self): """ Should emit debug message when a size does not match. """ self.test_args['debug'] = True self.set_bogus_file_sizes() self.function_to_test(**self.test_args) expected_output = textwrap.dedent("""\ ... D: size from .changes: ... D: calculated size: ... ... """) self.assertThat( sys.stdout.getvalue(), testtools.matchers.DocTestMatches( expected_output, doctest.ELLIPSIS)) def test_emits_size_mismatch_message_for_each_file(self): """ Should emit error message for each file with size mismatch. """ self.set_bogus_file_sizes() self.function_to_test(**self.test_args) for file_path in self.fake_upload_file_paths: expected_output = textwrap.dedent("""\ size doesn't match for {path} """).format(path=file_path) self.expectThat( sys.stdout.getvalue(), testtools.matchers.Contains(expected_output)) def test_emits_rejection_warning_when_unexpected_tarball(self): """ Should emit warning of rejection when unexpected tarball. """ if not hasattr(self, 'expected_rejection_message'): self.skipTest("No rejection message expected") self.function_to_test(**self.test_args) sys.stderr.write("calls: {calls!r}\n".format( calls=sys.stdout.write.mock_calls)) self.assertThat( sys.stdout.getvalue(), testtools.matchers.Contains(self.expected_rejection_message)) def test_raises_error_when_distribution_mismatch(self): """ Should raise error when distribution mismatch against allowed. """ if not getattr(self, 'test_distribution', None): self.skipTest("No distribution set for this test case") self.runtime_config_parser.set( self.test_args['host'], 'allowed_distributions', "dolor sit amet") with testtools.ExpectedException(dputhelper.DputUploadFatalError): self.function_to_test(**self.test_args) def test_emits_changes_file_upload_debug_message(self): """ Should emit debug message for upload of changes file. """ self.test_args['debug'] = True self.function_to_test(**self.test_args) expected_output = textwrap.dedent("""\ D: File to upload: {path} """).format(path=self.changes_file_double.path) self.assertIn(expected_output, sys.stdout.getvalue()) def test_returns_expected_files_to_upload_collection(self): """ Should return expected `files_to_upload` collection value. """ result = self.function_to_test(**self.test_args) expected_result = self.expected_files_to_upload self.assertEqual(expected_result, set(result)) class verify_files_UnusualPreferredEncodingTestCase( testscenarios.WithScenarios, verify_files_BaseTestCase): """ Test cases for `verify_files` function when preferred encoding is unusual. """ locale_getpreferredencoding_return_value = "ANSI_X3.4-1968" scenarios = [ ('default', {}), ] def setUp(self): """ Set up test fixtures. """ patch_locale_getpreferredencoding(self) super().setUp() set_changes_file_scenario(self, 'exist-needs-utf8') self.test_args.update(dict( changes_file_directory=os.path.dirname( self.changes_file_double.path), changes_file_name=os.path.basename( self.changes_file_double.path), )) def test_calls_parse_changes_with_changes_files(self): """ Should call `parse_changes` with changes file. """ self.function_to_test(**self.test_args) file_double = self.changes_file_double with open(file_double.path, encoding='UTF-8') as expected_file: upload_changes.parse_changes.assert_called_with(expected_file) class guess_upload_host_TestCase( testscenarios.WithScenarios, testtools.TestCase): """ Test cases for `guess_upload_host` function. """ changes_file_scenarios = [ ('no-distribution', { 'fake_file': io.StringIO(textwrap.dedent("""\ Files: Lorem ipsum dolor sit amet """)), }), ('distribution-spam', { 'fake_file': io.StringIO(textwrap.dedent("""\ Distribution: spam Files: Lorem ipsum dolor sit amet """)), }), ('distribution-beans', { 'fake_file': io.StringIO(textwrap.dedent("""\ Distribution: beans Files: Lorem ipsum dolor sit amet """)), }), ('needs-utf8 no-distribution', { 'fake_file': io.StringIO(textwrap.dedent("""\ Files: Lórem ipsüm dolôr sìt āmet """)), }), ] scenarios = [ ('distribution-found-of-one', { 'changes_file_scenario_name': "distribution-spam", 'test_distribution': "spam", 'config_scenario_name': "exist-distribution-one", 'expected_host': "foo", }), ('distribution-notfound-of-one', { 'changes_file_scenario_name': "distribution-beans", 'test_distribution': "beans", 'config_scenario_name': "exist-distribution-one", 'config_default_default_host_main': None, 'expected_host': "ftp-master", }), ('distribution-first-of-three', { 'changes_file_scenario_name': "distribution-spam", 'test_distribution': "spam", 'config_scenario_name': "exist-distribution-three", 'expected_host': "foo", }), ('distribution-last-of-three', { 'changes_file_scenario_name': "distribution-beans", 'test_distribution': "beans", 'config_scenario_name': "exist-distribution-three", 'expected_host': "foo", }), ('no-configured-distribution', { 'changes_file_scenario_name': "distribution-beans", 'config_scenario_name': "exist-distribution-none", 'config_default_default_host_main': None, 'expected_host': "ftp-master", }), ('no-distribution', { 'changes_file_scenario_name': "no-distribution", 'config_scenario_name': "exist-simple", 'config_default_default_host_main': None, 'expected_host': "ftp-master", }), ('no-distribution nonstandard-default-section', { 'config_scenario_name': ( "exist-simple-nonstandard-default-section"), 'config_extras': { 'default': { 'default_host_main': "pharetra", }, }, 'config_default_default_host_main': None, 'expected_host': "pharetra", }), ('default-distribution', { 'config_scenario_name': "exist-default-distribution-only", 'config_default_default_host_main': "consecteur", 'expected_host': "consecteur", }), ('unusual-preferred-encoding no-distribution', { 'locale_preferredencoding_return_value': "ANSI_X3.4-1968", 'changes_file_scenario_name': "needs-utf8 no-distribution", 'config_scenario_name': "exist-simple", 'config_default_default_host_main': None, 'expected_host': "ftp-master", }), ] def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) if hasattr(self, 'locale_preferredencoding_return_value'): patch_locale_getpreferredencoding( self, encoding=self.locale_preferredencoding_return_value) set_config( self, getattr(self, 'config_scenario_name', 'exist-minimal')) patch_runtime_config_options(self) self.setup_changes_file_fixtures() set_changes_file_scenario( self, getattr(self, 'changes_file_scenario_name', 'no-distribution')) self.set_test_args() def set_test_args(self): """ Set the arguments for the test call to the function. """ self.test_args = dict( changes_file_directory=os.path.dirname( self.changes_file_double.path), changes_file_name=os.path.basename( self.changes_file_double.path), config=self.runtime_config_parser, debug=False, ) def setup_changes_file_fixtures(self): """ Set up fixtures for fake changes file. """ file_path = make_changes_file_path() scenarios = [s for (__, s) in self.changes_file_scenarios] for scenario in scenarios: scenario['file_double'] = FileDouble( file_path, scenario['fake_file']) setup_file_double_behaviour( self, get_file_doubles_from_fake_file_scenarios(scenarios)) def test_calls_sys_exit_if_read_denied(self): """ Should call `sys.exit` if read permission denied. """ self.changes_file_double.set_os_access_scenario('denied') self.changes_file_double.set_open_scenario('read_denied') with testtools.ExpectedException(FakeSystemExit): upload_changes.guess_upload_host(**self.test_args) expected_output = textwrap.dedent("""\ Can't open {path} """).format(path=self.changes_file_double.path) self.assertIn(expected_output, sys.stdout.getvalue()) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) def test_returns_expected_host(self): """ Should return expected host value. """ result = upload_changes.guess_upload_host(**self.test_args) self.assertEqual(self.expected_host, result) def test_emits_debug_message_for_host(self): """ Should emit a debug message for the discovered host. """ self.test_args['debug'] = True config_parser = self.runtime_config_parser if not ( config_parser.has_section(self.expected_host) and config_parser.get(self.expected_host, 'distributions')): self.skipTest("No distributions specified") upload_changes.guess_upload_host(**self.test_args) expected_output = textwrap.dedent("""\ D: guessing host {host} based on distribution {dist} """).format( host=self.expected_host, dist=self.test_distribution) self.assertIn(expected_output, sys.stdout.getvalue()) class version_check_TestCase( testscenarios.WithScenarios, testtools.TestCase): """ Base for test cases for `version_check` function. """ package_file_scenarios = [ ('package-arch {arch}'.format(arch=arch), { 'file_double': FileDouble(), 'field_output': textwrap.dedent("""\ Architecture: {arch} """).format(arch=arch), }) for arch in ["all", "foo", "bar", "baz"]] version_scenarios = [ ('version none', { 'upload_version': "lorem", }), ('version equal', { 'upload_version': "lorem", 'installed_version': "lorem", }), ('version unequal', { 'upload_version': "lorem", 'installed_version': "ipsum", }), ] scenarios = NotImplemented def setUp(self): """ Set up test fixtures. """ super().setUp() patch_system_interfaces(self) self.patch_dpkg_program_file() if not hasattr(self, 'test_architectures'): self.test_architectures = [] setup_changes_file_fixtures(self) self.set_changes_file_scenario('no-format') self.set_upload_files() self.set_changes_document() self.set_test_args() patch_subprocess_popen(self) self.set_dpkg_print_architecture_scenario('simple') self.set_dpkg_field_scenario('simple') self.set_dpkg_status_scenario('simple') def set_changes_file_scenario(self, name): """ Set the package changes document based on scenario name. """ scenarios = make_changes_file_scenarios() scenario = dict(scenarios)[name] self.changes_file_double = scenario['file_double'] def set_upload_files(self): """ Set the files marked for upload in this scenario. """ package_file_suffix = ".deb" file_suffix_by_arch = { arch: "_{arch}{suffix}".format( arch=arch, suffix=package_file_suffix) for arch in self.test_architectures} self.additional_file_suffixes = list(file_suffix_by_arch.values()) setup_upload_file_fixtures(self) registry = FileDouble.get_registry_for_testcase(self) for path in self.fake_upload_file_paths: for (arch, suffix) in file_suffix_by_arch.items(): if path.endswith(suffix): file_double = registry[path] package_file_scenario = dict(self.package_file_scenarios)[ "package-arch {arch}".format(arch=arch)] package_file_scenario['file_double'] = file_double def set_changes_document(self): """ Set the changes document for this test case. """ upload_params_by_name = make_upload_files_params( self.fake_checksum_by_file, self.fake_size_by_file) self.changes_document = make_changes_document( fields={ 'architecture': " ".join(self.test_architectures), }, upload_params_by_name=upload_params_by_name) def set_test_args(self): """ Set the arguments for the test call to the function. """ self.test_args = dict( files_directory=os.path.dirname(self.changes_file_double.path), changes=self.changes_document, debug=False, ) def patch_dpkg_program_file(self): """ Patch the Dpkg program file for this test case. """ file_path = "/usr/bin/dpkg" file_double = FileDouble(file_path) file_double.register_for_testcase(self) self.dpkg_program_file_double = file_double def set_dpkg_print_architecture_scenario(self, name): """ Set the scenario for ‘dpkg --print-architecture’ behaviour. """ argv = ( os.path.basename(self.dpkg_program_file_double.path), "--print-architecture") double = SubprocessDouble( self.dpkg_program_file_double.path, argv=argv) double.register_for_testcase(self) double.set_subprocess_popen_scenario('success') double.set_stdout_content(self.host_architecture) self.dpkg_print_architecture_subprocess_double = double def set_dpkg_field_scenario(self, name): """ Set the scenario for ‘dpkg --field’ behaviour. """ for arch in self.test_architectures: package_file_scenario = dict(self.package_file_scenarios)[ 'package-arch {arch}'.format(arch=arch)] upload_file_path = package_file_scenario['file_double'].path argv = ( os.path.basename(self.dpkg_program_file_double.path), "--field", upload_file_path) double = SubprocessDouble( self.dpkg_program_file_double.path, argv=argv) double.register_for_testcase(self) double.set_subprocess_popen_scenario('success') package_file_scenario['package_name'] = make_unique_slug(self) package_file_scenario['package_version'] = self.upload_version field_output = package_file_scenario['field_output'] field_output += textwrap.dedent("""\ Package: {name} Version: {version} """).format( name=package_file_scenario['package_name'], version=package_file_scenario['package_version']) double.set_stdout_content(field_output) def set_dpkg_status_scenario(self, name): """ Set the scenario for ‘dpkg -s’ behaviour. """ for arch in self.test_architectures: package_file_scenario = dict(self.package_file_scenarios)[ 'package-arch {arch}'.format(arch=arch)] argv = ( os.path.basename(self.dpkg_program_file_double.path), "-s", package_file_scenario['package_name']) double = SubprocessDouble( self.dpkg_program_file_double.path, argv=argv) double.register_for_testcase(self) double.set_subprocess_popen_scenario('success') version_field = "" if hasattr(self, 'installed_version'): version_field = "Version: {version}\n".format( version=self.installed_version) status_output = textwrap.dedent("""\ {version}""").format(version=version_field) double.set_stdout_content(status_output) def get_subprocess_doubles_matching_argv_prefix(self, argv_prefix): """ Get subprocess doubles for this test case matching the argv. """ subprocess_registry = SubprocessDouble.get_registry_for_testcase(self) subprocess_doubles = [ double for double in subprocess_registry.values() if double.argv[:len(argv_prefix)] == argv_prefix ] return subprocess_doubles class version_check_ConsoleEncodingTestCase(version_check_TestCase): """ Test cases for `version_check` with various console text encodings. """ locale_scenarios = make_fake_locale_scenarios() host_architecture_scenarios = [ ('host-arch foo', { 'host_architecture': "foo", }), ] scenarios = testscenarios.multiply_scenarios( locale_scenarios, host_architecture_scenarios, version_check_TestCase.version_scenarios) def setUp(self): """ Set up fixtures for this test case. """ super().setUp() patch_locale_environment_variables(self) def test_emits_debug_message_showing_architecture(self): """ Should emit a debug message for the specified architecture. """ test_architecture = self.getUniqueString() subprocess_double = self.dpkg_print_architecture_subprocess_double subprocess_double.set_stdout_content( "{arch}\n".format(arch=test_architecture)) self.test_args['debug'] = True with contextlib.suppress(FakeSystemExit): upload_changes.version_check(**self.test_args) expected_output = textwrap.dedent("""\ D: detected architecture: '{arch}' """).format(arch=test_architecture) self.assertIn(expected_output, sys.stdout.getvalue()) class version_check_ArchitectureMatchTestCase(version_check_TestCase): """ Test cases for `version_check` when host architecture matches. """ host_architecture_scenarios = [ ('host-arch foo', { 'host_architecture': "foo", }), ] package_architecture_scenarios = [ ('one binary', { 'test_architectures': ["foo"], }), ('three binaries', { 'test_architectures': ["foo", "bar", "baz"], }), ('all-arch binary', { 'test_architectures': ["all"], }), ] scenarios = testscenarios.multiply_scenarios( host_architecture_scenarios, package_architecture_scenarios, version_check_TestCase.version_scenarios) def test_emits_debug_message_showing_architecture(self): """ Should emit a debug message for the specified architecture. """ test_architecture = self.getUniqueString() subprocess_double = self.dpkg_print_architecture_subprocess_double subprocess_double.set_stdout_content( "{arch}\n".format(arch=test_architecture)) self.test_args['debug'] = True with contextlib.suppress(FakeSystemExit): upload_changes.version_check(**self.test_args) expected_output = textwrap.dedent("""\ D: detected architecture: '{arch}' """).format(arch=test_architecture) self.assertIn(expected_output, sys.stdout.getvalue()) def test_omits_stderr_output_message_when_not_debug(self): """ Should omit any debug messages for `stderr` output. """ doubles = self.get_subprocess_doubles_matching_argv_prefix( ("dpkg", "--print-architecture")) if self.test_architectures: doubles.extend( self.get_subprocess_doubles_matching_argv_prefix( ("dpkg", "--field"))) doubles.extend( self.get_subprocess_doubles_matching_argv_prefix( ("dpkg", "-s"))) for double in doubles: double.set_stderr_content(self.getUniqueString()) self.test_args['debug'] = False with contextlib.suppress(FakeSystemExit): upload_changes.version_check(**self.test_args) message_snippet = " stderr output:" self.assertNotIn(message_snippet, sys.stdout.getvalue()) def test_emits_debug_message_for_architecture_stderr_output(self): """ Should emit debug message for Dpkg architecture `stderr`. """ subprocess_double = self.dpkg_print_architecture_subprocess_double test_output = self.getUniqueString() subprocess_double.set_stderr_content(test_output) self.test_args['debug'] = True with contextlib.suppress(FakeSystemExit): upload_changes.version_check(**self.test_args) expected_output = textwrap.dedent("""\ D: dpkg-architecture stderr output: {output!r} """).format(output=test_output) self.assertIn(expected_output, sys.stdout.getvalue()) def test_emits_debug_message_for_field_stderr_output(self): """ Should emit debug message for Dpkg fields `stderr`. """ test_output = self.getUniqueString() for double in self.get_subprocess_doubles_matching_argv_prefix( ("dpkg", "--field")): double.set_stderr_content(test_output) self.test_args['debug'] = True with contextlib.suppress(FakeSystemExit): upload_changes.version_check(**self.test_args) expected_output = textwrap.dedent("""\ D: dpkg stderr output: {output!r} """).format(output=test_output) self.assertIn(expected_output, sys.stdout.getvalue()) def test_emits_debug_message_for_status_stderr_output(self): """ Should emit debug message for Dpkg status `stderr`. """ test_output = self.getUniqueString() for double in self.get_subprocess_doubles_matching_argv_prefix( ("dpkg", "-s")): double.set_stderr_content(test_output) self.test_args['debug'] = True with contextlib.suppress(FakeSystemExit): upload_changes.version_check(**self.test_args) expected_output = textwrap.dedent("""\ D: dpkg stderr output: {output!r} """).format(output=test_output) self.assertIn(expected_output, sys.stdout.getvalue()) def test_emits_expected_debug_message_for_installed_version(self): """ Should emit debug message for package installed version. """ self.test_args['debug'] = True message_lead = "D: Installed-Version:" if hasattr(self, 'installed_version'): upload_changes.version_check(**self.test_args) expected_output = "{lead} {version}".format( lead=message_lead, version=self.installed_version) self.assertIn(expected_output, sys.stdout.getvalue()) else: with testtools.ExpectedException(FakeSystemExit): upload_changes.version_check(**self.test_args) self.assertNotIn(message_lead, sys.stdout.getvalue()) def test_emits_expected_debug_message_for_upload_version(self): """ Should emit debug message for package upload version. """ self.test_args['debug'] = True message_lead = "D: Check-Version:" if hasattr(self, 'installed_version'): upload_changes.version_check(**self.test_args) expected_output = "{lead} {version}".format( lead=message_lead, version=self.upload_version) self.assertIn(expected_output, sys.stdout.getvalue()) else: with testtools.ExpectedException(FakeSystemExit): upload_changes.version_check(**self.test_args) self.assertNotIn(message_lead, sys.stdout.getvalue()) class version_check_ArchitectureMismatchTestCase(version_check_TestCase): """ Test cases for `version_check` when no match to host architecture. """ host_architecture_scenarios = [ ('host-arch spam', { 'host_architecture': "spam", }), ] package_architecture_scenarios = [ ('one binary', { 'test_architectures': ["foo"], }), ] scenarios = testscenarios.multiply_scenarios( host_architecture_scenarios, package_architecture_scenarios, version_check_TestCase.version_scenarios) def test_emits_debug_message_stating_arch_mismatch(self): """ Should emit a debug message stating the architecture mismatch. """ self.test_args['debug'] = True upload_changes.version_check(**self.test_args) file_scenarios = dict(self.package_file_scenarios) for arch in self.test_architectures: file_scenario_name = "package-arch {arch}".format(arch=arch) file_scenario = file_scenarios[file_scenario_name] file_double = file_scenario['file_double'] expected_output = textwrap.dedent("""\ D: not install-checking {path} due to arch mismatch """).format(path=file_double.path) self.expectThat( sys.stdout.getvalue(), testtools.matchers.Contains(expected_output)) class version_check_PackageNotInstalledTestCase(version_check_TestCase): """ Test cases for `version_check` when package is not installed. """ host_architecture_scenarios = [ ('host-arch foo', { 'host_architecture': "foo", }), ] package_architecture_scenarios = [ ('one binary', { 'test_architectures': ["foo"], }), ] version_scenarios = [ ('version none', { 'upload_version': "lorem", }), ] scenarios = testscenarios.multiply_scenarios( host_architecture_scenarios, package_architecture_scenarios, version_scenarios) def test_emits_message_stating_package_not_installed(self): """ Should emit message stating package is not installed. """ with testtools.ExpectedException(FakeSystemExit): upload_changes.version_check(**self.test_args) expected_output = textwrap.dedent("""\ Uninstalled Package. Test it before uploading it. """) self.assertIn(expected_output, sys.stdout.getvalue()) def test_calls_sys_exit_with_error_status(self): """ Should call `sys.exit` with exit status indicating error. """ with testtools.ExpectedException(FakeSystemExit): upload_changes.version_check(**self.test_args) sys.exit.assert_called_with(EXIT_STATUS_FAILURE) # Copyright © 2015–2026 Ben Finney # # This is free software: you may copy, modify, and/or distribute this work # under the terms of the GNU General Public License as published by the # Free Software Foundation; version 3 of that license or any later version. # No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details. # Local variables: # coding: utf-8 # mode: python # End: # vim: fileencoding=utf-8 filetype=python :