def test_filter_out_pkgs_in_blacklisted_repos(monkeypatch): monkeypatch.setattr(api, 'show_message', show_message_mocked()) monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setattr(library, 'get_repositories_blacklisted', get_repos_blacklisted_mocked(set(['blacklisted']))) monkeypatch.setenv('LEAPP_VERBOSE', '1') to_install = { 'pkg01': 'repo01', 'pkg02': 'repo02', 'skipped01': 'blacklisted', 'skipped02': 'blacklisted' } filter_out_pkgs_in_blacklisted_repos(to_install) msgs = [ '2 packages will not be installed due to blacklisted repositories:', '- skipped01', '- skipped02' ] assert api.show_message.called == 1 assert api.show_message.msg == '\n'.join(msgs) assert reporting.report_generic.called == 1 assert reporting.report_generic.report_fields['summary'] == '\n'.join(msgs) assert reporting.report_generic.report_fields[ 'title'] == 'Packages will not be installed' assert to_install == {'pkg01': 'repo01', 'pkg02': 'repo02'}
def test_migration(monkeypatch): for packages, services, migrate in [ (['ntp'], ['ntpd'], ['ntpd']), (['ntp', 'ntpdate'], ['ntpd'], ['ntpd']), (['ntpdate'], ['ntpdate'], ['ntpdate']), (['ntp', 'ntpdate'], ['ntpdate'], ['ntpdate']), (['ntp', 'ntpdate'], ['ntpd', 'ntpdate'], ['ntpd', 'ntpdate']), (['ntp', 'ntpdate', 'ntp-perl'], ['ntpd', 'ntpdate'], ['ntpd', 'ntpdate']), (['ntp', 'ntpdate'], ['ntpd', 'ntpdate', 'ntp-wait'], ['ntpd', 'ntpdate']), (['ntp', 'ntpdate', 'ntp-perl'], ['ntpd', 'ntpdate', 'ntp-wait'], ['ntpd', 'ntpdate', 'ntp-wait']), ]: monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setattr(library, 'check_service', lambda service: service[:-8] in services) monkeypatch.setattr(library, 'is_file', lambda _: True) monkeypatch.setattr(library, 'get_tgz64', lambda _: '') decision = library.check_ntp(set(packages)) assert reporting.report_generic.called == 1 assert 'configuration will be migrated' in reporting.report_generic.report_fields[ 'title'] for service in ['ntpd', 'ntpdate']: migrated = re.search( r'\b{}\b'.format(service), reporting.report_generic.report_fields['title']) is not None assert migrated == (service in migrate) assert decision.migrate_services == migrate
def test_map_repositories(monkeypatch): monkeypatch.setattr(api, 'show_message', show_message_mocked()) monkeypatch.setattr(library, 'REPOSITORIES_MAPPING', {'repo': 'mapped'}) monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setenv('LEAPP_VERBOSE', '1') to_install = { 'pkg01': 'repo', 'pkg02': 'repo', 'skipped01': 'not_mapped', 'skipped02': 'not_mapped' } map_repositories(to_install) msgs = [ '2 packages will not be installed due to not mapped repositories:', '- skipped01', '- skipped02' ] assert api.show_message.called == 1 assert api.show_message.msg == '\n'.join(msgs) assert reporting.report_generic.called == 1 assert reporting.report_generic.report_fields[ 'title'] == 'Packages will not be installed' assert reporting.report_generic.report_fields['summary'] == '\n'.join(msgs) assert to_install == {'pkg01': 'mapped', 'pkg02': 'mapped'}
def test_scan_invalid_file_csv(monkeypatch): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) with pytest.raises(StopActorExecution): scan_repositories('files/tests/sample04.csv') assert reporting.report_generic.called == 1 assert 'inhibitor' in reporting.report_generic.report_fields['flags'] assert 'Repositories map file is invalid' in reporting.report_generic.report_fields[ 'title']
def test_enough_space_available(monkeypatch): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) get_avail_bytes_on_boot = fake_get_avail_bytes_on_boot( MIN_AVAIL_BYTES_FOR_BOOT) check_avail_space_on_boot(get_avail_bytes_on_boot) assert reporting.report_generic.called == 0
def test_skip_check(monkeypatch): monkeypatch.setattr(os, "getenv", lambda _unused: True) monkeypatch.setattr(reporting, "report_generic", report_generic_mocked()) with pytest.raises(StopActorExecution): library.skip_check() assert reporting.report_generic.called == 1 assert 'Skipped signed packages check' in reporting.report_generic.report_fields[ 'title']
def test_nomigration(monkeypatch): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setattr(library, 'check_service', lambda _: False) monkeypatch.setattr(library, 'is_file', lambda _: False) monkeypatch.setattr(library, 'get_tgz64', lambda _: '') library.check_ntp(set(['chrony', 'linuxptp', 'xterm'])) assert reporting.report_generic.called == 0
def test_skip_check(monkeypatch): monkeypatch.setattr(os, "getenv", lambda _unused: True) monkeypatch.setattr(reporting, "report_generic", report_generic_mocked()) assert library.skip_check() assert reporting.report_generic.called == 1 assert 'Skipped OS release check' in reporting.report_generic.report_fields[ 'title'] assert reporting.report_generic.report_fields['severity'] == 'high' assert 'flags' not in reporting.report_generic.report_fields
def test_pes_data_not_found(monkeypatch): def file_not_exists(_filepath): return False monkeypatch.setattr(os.path, 'isfile', file_not_exists) monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) with pytest.raises(StopActorExecution): get_events('/etc/leapp/pes-data.json') assert reporting.report_generic.called == 1 assert 'inhibitor' in reporting.report_generic.report_fields['flags']
def test_installed_nodefconf_noudp(monkeypatch): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setattr(library, 'is_sysconfig_default', lambda: False) monkeypatch.setattr(library, 'is_udp_disabled', lambda: True) library.check_memcached(True) assert reporting.report_generic.called == 1 assert reporting.report_generic.report_fields[ 'title'] == 'memcached has already disabled UDP port'
def test_not_supported_release(monkeypatch): def os_release_mocked(*models): yield create_os_release('unsupported', SUPPORTED_VERSION['rhel'][0]) monkeypatch.setattr(api, "consume", os_release_mocked) monkeypatch.setattr(reporting, "report_generic", report_generic_mocked()) library.check_os_version(SUPPORTED_VERSION) assert reporting.report_generic.called == 1 assert 'Unsupported OS' in reporting.report_generic.report_fields['title'] assert 'flags' in reporting.report_generic.report_fields assert 'inhibitor' in reporting.report_generic.report_fields['flags']
def test_inhibit_upgrade(monkeypatch): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) # Test 4.2 MiB available on /boot bytes_available = 4.2 * 2**20 inhibit_upgrade(bytes_available) assert reporting.report_generic.called == 1 assert 'inhibitor' in reporting.report_generic.report_fields['flags'] mib_needed = (MIN_AVAIL_BYTES_FOR_BOOT - bytes_available) / 2**20 assert "needs additional {0} MiB".format( mib_needed) in reporting.report_generic.report_fields['summary']
def test_repository_mapping_file_not_found(monkeypatch): def file_not_exists(_filepath): return False monkeypatch.setattr('os.path.isfile', file_not_exists) monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) with pytest.raises(StopActorExecution): scan_repositories('/etc/leapp/files/repomap.csv') assert reporting.report_generic.called == 1 assert 'inhibitor' in reporting.report_generic.report_fields['flags'] assert 'Repositories map file not found' in reporting.report_generic.report_fields[ 'title']
def test_installed_defconf(monkeypatch): for udp_disabled in (False, True): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setattr(library, 'is_sysconfig_default', lambda: True) monkeypatch.setattr(library, 'is_udp_disabled', lambda: udp_disabled) library.check_memcached(True) assert reporting.report_generic.called == 1 assert reporting.report_generic.report_fields[ 'title'] == 'memcached service is using default configuration'
def test_not_enough_space_available(monkeypatch): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) # Test 0 bytes available /boot get_avail_bytes_on_boot = fake_get_avail_bytes_on_boot(0) check_avail_space_on_boot(get_avail_bytes_on_boot) # Test 0.1 MiB less then required in /boot get_avail_bytes_on_boot = fake_get_avail_bytes_on_boot( MIN_AVAIL_BYTES_FOR_BOOT - 0.1 * 2**20) check_avail_space_on_boot(get_avail_bytes_on_boot) assert reporting.report_generic.called == 2
def test_uninstalled(monkeypatch): for sysconfig_default in (False, True): for udp_disabled in (False, True): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setattr(library, 'is_sysconfig_default', lambda: sysconfig_default) monkeypatch.setattr(library, 'is_udp_disabled', lambda: udp_disabled) library.check_memcached(False) assert reporting.report_generic.called == 0
def test_get_events(monkeypatch): monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) with pytest.raises(StopActorExecution): get_events('files/tests/sample02.json') assert reporting.report_generic.called == 1 assert 'inhibitor' in reporting.report_generic.report_fields['flags'] reporting.report_generic.called = 0 reporting.report_generic.model_instances = [] with pytest.raises(StopActorExecution): get_events('files/tests/sample03.json') assert reporting.report_generic.called == 1 assert 'inhibitor' in reporting.report_generic.report_fields['flags']
def test_report_skipped_packages_no_verbose_mode(monkeypatch): monkeypatch.setattr(api, 'produce', produce_mocked()) monkeypatch.setattr(api, 'show_message', show_message_mocked()) monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setenv('LEAPP_VERBOSE', '0') report_skipped_packages('packages will not be installed:', ['skipped01', 'skipped02']) message = '2 packages will not be installed:\n- skipped01\n- skipped02' assert api.show_message.called == 0 assert reporting.report_generic.called == 1 assert reporting.report_generic.report_fields[ 'title'] == 'Packages will not be installed' assert reporting.report_generic.report_fields['summary'] == message
def test_supported_release(monkeypatch): def os_mocked_first_release(*models): yield create_os_release('rhel', SUPPORTED_VERSION['rhel'][0]) def os_mocked_second_release(*models): yield create_os_release('rhel', SUPPORTED_VERSION['rhel'][1]) monkeypatch.setattr(api, "consume", os_mocked_first_release) monkeypatch.setattr(reporting, "report_generic", report_generic_mocked()) library.check_os_version(SUPPORTED_VERSION) monkeypatch.setattr(api, "consume", os_mocked_second_release) library.check_os_version(SUPPORTED_VERSION) assert reporting.report_generic.called == 0
def test_actor_execution_without_unsigned_data(monkeypatch): def consume_unsigned_message_mocked(*models): installed_rpm = [] yield InstalledUnsignedRPM(items=installed_rpm) monkeypatch.setattr(api, "consume", consume_unsigned_message_mocked) monkeypatch.setattr(api, "produce", produce_mocked()) monkeypatch.setattr(api, "show_message", lambda x: True) monkeypatch.setattr(reporting, "report_with_remediation", report_generic_mocked()) packages = library.get_unsigned_packages() assert not packages library.generate_report(packages) assert reporting.report_with_remediation.called == 0
def test_actor_execution_with_unsigned_data(monkeypatch): def consume_unsigned_message_mocked(*models): installed_rpm = [ RPM(name='sample02', version='0.1', release='1.sm01', epoch='1', packager=RH_PACKAGER, arch='noarch', pgpsig='SOME_OTHER_SIG_X'), RPM(name='sample04', version='0.1', release='1.sm01', epoch='1', packager=RH_PACKAGER, arch='noarch', pgpsig='SOME_OTHER_SIG_X'), RPM(name='sample06', version='0.1', release='1.sm01', epoch='1', packager=RH_PACKAGER, arch='noarch', pgpsig='SOME_OTHER_SIG_X'), RPM(name='sample08', version='0.1', release='1.sm01', epoch='1', packager=RH_PACKAGER, arch='noarch', pgpsig='SOME_OTHER_SIG_X') ] yield InstalledUnsignedRPM(items=installed_rpm) monkeypatch.setattr(api, "consume", consume_unsigned_message_mocked) monkeypatch.setattr(api, "produce", produce_mocked()) monkeypatch.setattr(api, "show_message", lambda x: True) monkeypatch.setattr(reporting, "report_with_remediation", report_generic_mocked()) packages = library.get_unsigned_packages() assert len(packages) == 4 library.generate_report(packages) assert reporting.report_with_remediation.called == 1 assert 'Packages not signed by Red Hat found' in reporting.report_with_remediation.report_fields[ 'title'] assert 'yum remove sample' in reporting.report_with_remediation.report_fields[ 'remediation']
def test_migration(monkeypatch): for ntp_services, chrony_services, ignored_lines in [ ([], [], 0), (['ntpd'], ['chronyd'], 0), (['ntpdate'], ['chronyd'], 1), (['ntp-wait'], ['chrony-wait'], 0), (['ntpd', 'ntpdate', 'ntp-wait'], ['chronyd', 'chronyd', 'chrony-wait'], 1), ]: monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) monkeypatch.setattr(library, 'extract_tgz64', extract_tgz64_mocked()) monkeypatch.setattr(library, 'enable_service', enable_service_mocked()) monkeypatch.setattr(library, 'write_file', write_file_mocked()) monkeypatch.setattr(library, 'ntp2chrony', ntp2chrony_mocked(ignored_lines)) library.migrate_ntp(ntp_services, 'abcdef') if ntp_services: assert reporting.report_generic.called == 1 if ignored_lines > 0: assert 'configuration partially migrated to chrony' in \ reporting.report_generic.report_fields['title'] else: assert 'configuration migrated to chrony' in \ reporting.report_generic.report_fields['title'] assert library.extract_tgz64.called == 1 assert library.extract_tgz64.s == 'abcdef' assert library.enable_service.called == len(chrony_services) assert library.enable_service.names == chrony_services assert library.write_file.called == (0 if 'ntpd' in ntp_services else 1) if library.write_file.called: assert library.write_file.name == '/etc/ntp.conf.nosources' assert 'without ntp configuration' in library.write_file.content assert library.ntp2chrony.called == 1 assert library.ntp2chrony.args == ( '/', '/etc/ntp.conf' if 'ntpd' in ntp_services else '/etc/ntp.conf.nosources', '/etc/ntp/step-tickers' if 'ntpdate' in ntp_services else '') else: assert reporting.report_generic.called == 0 assert library.extract_tgz64.called == 0 assert library.enable_service.called == 0 assert library.write_file.called == 0 assert library.ntp2chrony.called == 0
def test_invalid_versions(monkeypatch): def os_release_mocked(*models): yield create_os_release('rhel', '7.6') monkeypatch.setattr(api, "consume", os_release_mocked) monkeypatch.setattr(reporting, "report_generic", report_generic_mocked()) with pytest.raises(StopActorExecution): library.check_os_version('string') with pytest.raises(StopActorExecution): library.check_os_version(None) library.check_os_version({}) assert reporting.report_generic.called == 1 with pytest.raises(StopActorExecutionError): library.check_os_version({'rhel': None})
def test_get_os_release_info(monkeypatch): monkeypatch.setattr('leapp.libraries.stdlib.api.produce', produce_mocked()) monkeypatch.setattr(reporting, 'report_generic', report_generic_mocked()) expected = OSReleaseFacts( release_id='rhel', name='Red Hat Enterprise Linux Server', pretty_name='Red Hat Enterprise Linux', version='7.6 (Maipo)', version_id='7.6', variant='Server', variant_id='server') assert expected == get_os_release_info('tests/files/os-release') assert not get_os_release_info('tests/files/unexistent-file') assert reporting.report_generic.called == 1 assert 'inhibitor' in reporting.report_generic.report_fields['flags']
def test_invalid_fstab_info(monkeypatch): class logger_mocked(object): def __init__(self): self.errmsg = None def error(self, msg): self.errmsg = msg def __call__(self): return self monkeypatch.setattr(reporting, "report_with_remediation", report_generic_mocked()) monkeypatch.setattr(api, 'current_logger', logger_mocked()) library._get_fstab_info('tests/files/invalid_fstab') assert reporting.report_with_remediation.called == 1 assert reporting.report_with_remediation.report_fields['severity'] == 'high' assert 'Problems with parsing data in /etc/fstab' in reporting.report_with_remediation.report_fields['title'] assert 'inhibitor' in reporting.report_with_remediation.report_fields['flags'] assert "The fstab configuration file seems to be invalid" in api.current_logger.errmsg
def test_no_skip_check(monkeypatch): monkeypatch.setattr(os, "getenv", lambda _unused: False) monkeypatch.setattr(reporting, "report_generic", report_generic_mocked()) assert not library.skip_check() assert reporting.report_generic.called == 0