|
| 1 | +import glob |
| 2 | +import os |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from leapp import reporting |
| 7 | +from leapp.libraries.actor import checkldsoconfiguration |
| 8 | +from leapp.libraries.common.testutils import create_report_mocked |
| 9 | +from leapp.libraries.stdlib import CalledProcessError |
| 10 | +from leapp.models import InstalledRedHatSignedRPM |
| 11 | + |
| 12 | +INCLUDED_CONFIGS_GLOB_DICT_1 = {'/etc/ld.so.conf.d/*.conf': ['/etc/ld.so.conf.d/dyninst-x86_64.conf', |
| 13 | + '/etc/ld.so.conf.d/mariadb-x86_64.conf', |
| 14 | + '/etc/ld.so.conf.d/bind-export-x86_64.conf']} |
| 15 | + |
| 16 | +INCLUDED_CONFIGS_GLOB_DICT_2 = {'/etc/ld.so.conf.d/*.conf': ['/etc/ld.so.conf.d/dyninst-x86_64.conf', |
| 17 | + '/etc/ld.so.conf.d/mariadb-x86_64.conf', |
| 18 | + '/etc/ld.so.conf.d/bind-export-x86_64.conf', |
| 19 | + '/etc/ld.so.conf.d/custom1.conf', |
| 20 | + '/etc/ld.so.conf.d/custom2.conf']} |
| 21 | + |
| 22 | +INCLUDED_CONFIGS_GLOB_DICT_3 = {'/etc/ld.so.conf.d/*.conf': ['/etc/ld.so.conf.d/dyninst-x86_64.conf', |
| 23 | + '/etc/ld.so.conf.d/custom1.conf', |
| 24 | + '/etc/ld.so.conf.d/mariadb-x86_64.conf', |
| 25 | + '/etc/ld.so.conf.d/bind-export-x86_64.conf', |
| 26 | + '/etc/ld.so.conf.d/custom2.conf'], |
| 27 | + '/custom/path/*.conf': ['/custom/path/custom1.conf', |
| 28 | + '/custom/path/custom2.conf']} |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize(('included_configs_glob_dict', 'other_lines', 'custom_configs'), |
| 32 | + [ |
| 33 | + (INCLUDED_CONFIGS_GLOB_DICT_1, [], []), |
| 34 | + (INCLUDED_CONFIGS_GLOB_DICT_1, ['/custom/path.lib'], []), |
| 35 | + (INCLUDED_CONFIGS_GLOB_DICT_2, [], ['/etc/ld.so.conf.d/custom1.conf', |
| 36 | + '/etc/ld.so.conf.d/custom2.conf']), |
| 37 | + (INCLUDED_CONFIGS_GLOB_DICT_3, ['/custom/path.lib'], ['/etc/ld.so.conf.d/custom1.conf', |
| 38 | + '/etc/ld.so.conf.d/custom2.conf' |
| 39 | + '/custom/path/custom1.conf', |
| 40 | + '/custom/path/custom2.conf']), |
| 41 | + ]) |
| 42 | +def test_check_ld_so_configuration(monkeypatch, included_configs_glob_dict, other_lines, custom_configs): |
| 43 | + monkeypatch.setattr(reporting, 'create_report', create_report_mocked()) |
| 44 | + monkeypatch.setattr(glob, 'glob', lambda glob: included_configs_glob_dict[glob]) |
| 45 | + monkeypatch.setattr(checkldsoconfiguration, '_is_included_ld_so_config_custom', |
| 46 | + lambda config: config in custom_configs) |
| 47 | + monkeypatch.setattr(checkldsoconfiguration, '_parse_main_ld_so_config', |
| 48 | + lambda: (included_configs_glob_dict.keys(), other_lines)) |
| 49 | + |
| 50 | + checkldsoconfiguration.check_ld_so_configuration() |
| 51 | + |
| 52 | + report_expected = custom_configs or other_lines |
| 53 | + if not report_expected: |
| 54 | + assert reporting.create_report.called == 0 |
| 55 | + return |
| 56 | + |
| 57 | + assert reporting.create_report.called == 1 |
| 58 | + assert 'ld.so.conf' in reporting.create_report.reports[0]['title'] |
| 59 | + summary = reporting.create_report.reports[0]['summary'] |
| 60 | + |
| 61 | + all_configs = [] |
| 62 | + for configs in included_configs_glob_dict.values(): |
| 63 | + all_configs += configs |
| 64 | + |
| 65 | + if custom_configs: |
| 66 | + assert 'The following config files were marked as unsupported:' in summary |
| 67 | + |
| 68 | + for config in all_configs: |
| 69 | + assert (config in custom_configs) == (config in summary) |
| 70 | + |
| 71 | + if other_lines: |
| 72 | + assert 'The /etc/ld.so.conf file has unexpected contents' in summary |
| 73 | + |
| 74 | + for other_line in other_lines: |
| 75 | + assert other_line in summary |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize(('config_contents', 'included_config_paths', 'other_lines'), |
| 79 | + [ |
| 80 | + (['include ld.so.conf.d/*.conf\n'], |
| 81 | + ['/etc/ld.so.conf.d/*.conf'], []), |
| 82 | + (['include ld.so.conf.d/*.conf\n', '\n', '/custom/path.lib\n'], |
| 83 | + ['/etc/ld.so.conf.d/*.conf'], ['/custom/path.lib']), |
| 84 | + (['include ld.so.conf.d/*.conf\n', 'include /custom/path.conf\n'], |
| 85 | + ['/etc/ld.so.conf.d/*.conf', '/custom/path.conf'], []), |
| 86 | + ([' \n'], |
| 87 | + [], []) |
| 88 | + ]) |
| 89 | +def test_parse_main_ld_so_config(monkeypatch, config_contents, included_config_paths, other_lines): |
| 90 | + def mocked_read_file(path): |
| 91 | + assert path == checkldsoconfiguration.LD_SO_CONF_MAIN |
| 92 | + return config_contents |
| 93 | + |
| 94 | + monkeypatch.setattr(checkldsoconfiguration, '_read_file', mocked_read_file) |
| 95 | + |
| 96 | + _included_config_paths, _other_lines = checkldsoconfiguration._parse_main_ld_so_config() |
| 97 | + |
| 98 | + assert _included_config_paths == included_config_paths |
| 99 | + assert _other_lines == other_lines |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize(('config_path', 'run_result', 'package_exists', 'is_custom'), |
| 103 | + [ |
| 104 | + ('/etc/ld.so.conf.d/dyninst-x86_64.conf', 'dyninst', True, False), |
| 105 | + ('/etc/ld.so.conf.d/somelib.conf', CalledProcessError, False, True), |
| 106 | + ('/etc/custom/custom.conf', 'custom', False, True) |
| 107 | + ]) |
| 108 | +def test_is_included_ld_so_config_custom(monkeypatch, config_path, run_result, package_exists, is_custom): |
| 109 | + def mocked_run(command): |
| 110 | + assert config_path in command |
| 111 | + if run_result and not isinstance(run_result, str): |
| 112 | + raise CalledProcessError("message", ["command"], "result") |
| 113 | + return {'stdout': run_result} |
| 114 | + |
| 115 | + def mocked_has_package(model, package_name): |
| 116 | + assert model is InstalledRedHatSignedRPM |
| 117 | + assert package_name == run_result |
| 118 | + return package_exists |
| 119 | + |
| 120 | + monkeypatch.setattr(checkldsoconfiguration, 'run', mocked_run) |
| 121 | + monkeypatch.setattr(checkldsoconfiguration, 'has_package', mocked_has_package) |
| 122 | + monkeypatch.setattr(os.path, 'isfile', lambda _: True) |
| 123 | + |
| 124 | + result = checkldsoconfiguration._is_included_ld_so_config_custom(config_path) |
| 125 | + assert result == is_custom |
0 commit comments