def test_esm_apps_disabled(monkeypatch, null_logger):
    mock_responses = MockResponses()
    mock_responses.ua_status_file = "tests/assets/ubuntu-advantage-status-disabled.json"
    apply_mock_responses(monkeypatch, mock_responses)
    sysinfo = LocalSysInfo(null_logger)

    assert sysinfo.esm_apps_enabled is False
def test_is_snap_false(monkeypatch, null_logger):
    mock_responses = MockResponses()
    apply_mock_responses(monkeypatch, mock_responses)

    sysinfo = LocalSysInfo(null_logger)
    assert not sysinfo.is_snap
    assert sysinfo.snap_user_common is None
def test_get_codename_from_file(monkeypatch, null_logger):
    mock_responses = MockResponses()
    mock_responses.get_distro_information_raises = True
    apply_mock_responses(monkeypatch, mock_responses)

    sysinfo = LocalSysInfo(null_logger)
    assert sysinfo.codename == "trusty"
def test_package_count(monkeypatch, null_logger):
    mock_responses = MockResponses()
    apply_mock_responses(monkeypatch, mock_responses)

    sysinfo = LocalSysInfo(null_logger)
    # This property is lazy-loaded
    assert sysinfo.package_count == 14
def test_is_snap_true(monkeypatch, null_logger):
    mock_responses = MockResponses()
    mock_responses.environ_snap_user_common = "/home/test/snap"
    apply_mock_responses(monkeypatch, mock_responses)

    sysinfo = LocalSysInfo(null_logger)
    assert sysinfo.is_snap
    assert sysinfo.snap_user_common == "/home/test/snap"
Beispiel #6
0
def get_sysinfo(opt, logger):
    local_sysinfo = LocalSysInfo(logger)
    target_sysinfo = TargetSysInfo(opt, local_sysinfo)

    debug.log_config_options(opt, logger)
    debug.log_local_system_info(local_sysinfo, opt.manifest_mode, logger)
    debug.log_target_system_info(target_sysinfo, logger)

    return local_sysinfo, target_sysinfo
def test_esm_apps_malformed_json(monkeypatch, null_logger):
    mock_responses = MockResponses()
    mock_responses.ua_status_file = (
        "tests/assets/ubuntu-advantage-status-malformed-json.json")
    apply_mock_responses(monkeypatch, mock_responses)
    sysinfo = LocalSysInfo(null_logger)

    assert sysinfo.esm_apps_enabled is False
    assert sysinfo.esm_infra_enabled is False
def test_package_count_error(monkeypatch, null_logger):
    mock_responses = MockResponses()
    mock_responses.installed_pkgs = lambda logger: raise_mock_exception()
    apply_mock_responses(monkeypatch, mock_responses)

    with pytest.raises(PkgCountError):
        sysinfo = LocalSysInfo(null_logger)
        # This property is lazy-loaded
        sysinfo.package_count
def test_get_codename_lsb_module_other(monkeypatch, null_logger):
    mock_responses = MockResponses()
    mock_responses.get_distro_information = {"ID": "something_else"}
    apply_mock_responses(monkeypatch, mock_responses)

    with pytest.raises(DistribIDError) as di:
        sysinfo = LocalSysInfo(null_logger)
        # This property is lazy-loaded
        sysinfo.codename

    assert "something_else" in str(di)
Beispiel #10
0
def main():
    global LOGGER

    args = parse_args()

    # Configure debug logging as early as possible
    LOGGER = set_output_verbosity(args)

    local_sysinfo = LocalSysInfo(LOGGER)

    try:
        opt = Options(args)
    except (ArgumentError, ValueError) as err:
        error_exit("Invalid option or argument: %s" % err,
                   const.CLI_ERROR_RETURN_CODE)

    error_exit_code = (const.NAGIOS_UNKNOWN_RETURN_CODE
                       if opt.nagios_mode else const.ERROR_RETURN_CODE)

    try:
        try:
            target_sysinfo = TargetSysInfo(opt, local_sysinfo)

            log_config_options(opt)
            log_local_system_info(local_sysinfo, opt.manifest_mode)
            log_target_system_info(target_sysinfo)
        except (FileNotFoundError, PermissionError) as err:
            error_exit("Failed to determine the correct Ubuntu codename: %s" %
                       err)
        except DistribIDError as di:
            error_exit(
                "Invalid linux distribution detected, CVEScan must be run on Ubuntu: %s"
                % di)
        except PkgCountError as pke:
            error_exit("Failed to determine the local package count: %s" % pke)

        output_formatter = load_output_formatter(opt)

        download_cache = USTDownloadCache(LOGGER)
        uct_data = load_uct_data(opt, download_cache, target_sysinfo)
        cve_scanner = CVEScanner(LOGGER)
        scan_results = cve_scanner.scan(target_sysinfo.codename, uct_data,
                                        target_sysinfo.installed_pkgs)
        (results,
         return_code) = output_formatter.format_output(scan_results,
                                                       target_sysinfo)
    except Exception as ex:
        error_exit(
            "An unexpected error occurred while running CVEScan: %s" % ex,
            error_exit_code,
        )

    LOGGER.info(results)
    sys.exit(return_code)
def test_get_codename_from_not_ubuntu(monkeypatch, null_logger):
    mock_responses = MockResponses()
    mock_responses.get_distro_information_raises = True
    mock_responses.lsb_release_file = "tests/assets/lsb-release-not-ubuntu"
    apply_mock_responses(monkeypatch, mock_responses)

    with pytest.raises(DistribIDError) as di:
        sysinfo = LocalSysInfo(null_logger)
        # This property is lazy-loaded
        sysinfo.codename

    assert "not-ubuntu" in str(di)
def test_ua_permission_denied(monkeypatch, null_logger):
    def raise_(x):
        raise x

    mock_responses = MockResponses()
    mock_responses.ua_status_file = "tests/assets/ubuntu-advantage-status-enabled.json"
    apply_mock_responses(monkeypatch, mock_responses)
    monkeypatch.setattr(
        LocalSysInfo,
        "_get_raw_ua_status",
        lambda *args, **kwargs: raise_(PermissionError()),
    )
    sysinfo = LocalSysInfo(null_logger)

    assert sysinfo.esm_apps_enabled is False
    assert sysinfo.esm_infra_enabled is False
def test_installed_pkgs_list(monkeypatch, null_logger):
    mock_responses = MockResponses()
    apply_mock_responses(monkeypatch, mock_responses)

    sysinfo = LocalSysInfo(null_logger)
    assert sysinfo.installed_pkgs == DEFAULT_INSTALLED_PKGS
def test_get_codename_lsb_module(monkeypatch, null_logger):
    mock_responses = MockResponses()
    apply_mock_responses(monkeypatch, mock_responses)

    sysinfo = LocalSysInfo(null_logger)
    assert sysinfo.codename == "trusty"