Exemple #1
0
def test_check_all_binary_dependencies_saved_versions(mocker):
    fake_namespace = 'fake'
    fake_version = LooseVersion('0.0.0')
    check_dependency_mock = mocker.patch(
        'util.dependencies_checker.check_dependency')
    check_dependency_mock.return_value = True, fake_version
    fake_config_path = '/usr/ogorek/nctl_config'
    fake_config = mocker.patch('util.dependencies_checker.Config')
    fake_config.return_value.config_path = fake_config_path

    saved_versions = {
        dependency_name: fake_version
        for dependency_name in get_dependency_map().keys()
    }

    load_dependency_versions_mock = mocker.patch(
        'util.dependencies_checker.load_dependency_versions',
        return_value=saved_versions)
    save_dependency_versions_mock = mocker.patch(
        'util.dependencies_checker.save_dependency_versions',
        return_value=None)

    check_all_binary_dependencies(namespace=fake_namespace)

    assert load_dependency_versions_mock.call_count == 1, 'Saved dependency versions were not loaded.'
    assert save_dependency_versions_mock.call_count == 0, 'Saved dependencies versions were overwritten.'
    assert check_dependency_mock.call_count == len(
        get_dependency_map()), 'Not all dependencies were checked.'
    expected_check_dependency_calls = [
        call(
            dependency_name=dependency_name,
            dependency_spec=dependency_spec,
            namespace=fake_namespace,
            saved_versions=saved_versions)
        for dependency_name, dependency_spec in get_dependency_map().items()
    ]
    check_dependency_mock.assert_has_calls(
        expected_check_dependency_calls, any_order=True)
Exemple #2
0
def test_check_all_binary_dependencies(mocker):
    check_dependency_mock = mocker.patch(
        'util.dependencies_checker.check_dependency')
    check_dependency_mock.return_value = True, LooseVersion('0.0.0')
    fake_config_path = '/usr/ogorek/nctl_config'
    fake_config = mocker.patch('util.dependencies_checker.Config')
    fake_config.return_value.config_path = fake_config_path

    load_dependency_versions_mock = mocker.patch(
        'util.dependencies_checker.load_dependency_versions',
        return_value=None)
    save_dependency_versions_mock = mocker.patch(
        'util.dependencies_checker.save_dependency_versions',
        return_value=None)

    check_all_binary_dependencies(namespace='fake')

    assert load_dependency_versions_mock.call_count == 1, 'Saved dependency versions were not loaded.'
    assert save_dependency_versions_mock.call_count == 1, 'Dependency versions were not saved.'
    assert check_dependency_mock.call_count == len(
        get_dependency_map()), 'Not all dependencies were checked.'
Exemple #3
0
def verify(state: State):
    try:
        with spinner(text=Texts.CHECKING_OS_MSG):
            check_os()
        click.echo(Texts.OS_SUPPORTED_MSG)
    except InvalidOsError as exception:
        handle_error(logger,
                     str(exception),
                     str(exception),
                     add_verbosity_msg=True)
        exit(1)

    dependencies = get_dependency_map()
    kubectl_dependency_name = 'kubectl'
    kubectl_dependency_spec = dependencies[kubectl_dependency_name]

    with spinner(text=Texts.VERIFYING_DEPENDENCY_MSG.format(
            dependency_name=kubectl_dependency_name)):
        valid, installed_version = check_dependency(
            dependency_name=kubectl_dependency_name,
            dependency_spec=kubectl_dependency_spec)

    supported_versions_sign = '>='
    logger.info(
        Texts.VERSION_CHECKING_MSG.format(
            dependency_name=kubectl_dependency_name,
            installed_version=installed_version,
            supported_versions_sign=supported_versions_sign,
            expected_version=kubectl_dependency_spec.expected_version))

    if valid:
        click.echo(
            Texts.DEPENDENCY_VERIFICATION_SUCCESS_MSG.format(
                dependency_name=kubectl_dependency_name))
    else:
        handle_error(
            logger,
            Texts.KUBECTL_INVALID_VERSION_ERROR_MSG.format(
                installed_version=installed_version,
                supported_versions_sign=supported_versions_sign,
                expected_version=  # noqa
                kubectl_dependency_spec.expected_version),
            Texts.KUBECTL_INVALID_VERSION_ERROR_MSG,
            add_verbosity_msg=state.verbosity == 0)
        exit(1)

    del dependencies[kubectl_dependency_name]

    try:
        with spinner(text=Texts.CHECKING_CONNECTION_TO_CLUSTER_MSG):
            check_connection_to_cluster()
        with spinner(text=Texts.CHECKING_PORT_FORWARDING_FROM_CLUSTER_MSG):
            check_port_forwarding()
    except KubectlConnectionError as e:
        handle_error(logger,
                     str(e),
                     str(e),
                     add_verbosity_msg=state.verbosity == 0)
        exit(1)
    except FileNotFoundError:
        handle_error(logger,
                     Texts.KUBECTL_NOT_INSTALLED_ERROR_MSG,
                     Texts.KUBECTL_NOT_INSTALLED_ERROR_MSG,
                     add_verbosity_msg=state.verbosity == 0)
        exit(1)

    try:
        namespace = 'kube-system' if is_current_user_administrator(
        ) else get_kubectl_current_context_namespace()
    except Exception:
        handle_error(logger,
                     Texts.GET_K8S_NAMESPACE_ERROR_MSG,
                     Texts.GET_K8S_NAMESPACE_ERROR_MSG,
                     add_verbosity_msg=state.verbosity == 0)
        exit(1)

    dependency_versions = {}
    for dependency_name, dependency_spec in dependencies.items():
        try:
            supported_versions_sign = '==' if dependency_spec.match_exact_version else '>='
            with spinner(text=Texts.VERIFYING_DEPENDENCY_MSG.format(
                    dependency_name=dependency_name)):
                valid, installed_version = check_dependency(
                    dependency_name=dependency_name,
                    dependency_spec=dependency_spec,
                    namespace=namespace)
            dependency_versions[dependency_name] = installed_version
            logger.info(
                Texts.VERSION_CHECKING_MSG.format(
                    dependency_name=dependency_name,
                    installed_version=installed_version,
                    supported_versions_sign=supported_versions_sign,
                    expected_version=dependency_spec.expected_version))
            if valid:
                click.echo(
                    Texts.DEPENDENCY_VERIFICATION_SUCCESS_MSG.format(
                        dependency_name=dependency_name))
            else:
                click.echo(
                    Texts.INVALID_VERSION_WARNING_MSG.format(
                        dependency_name=dependency_name,
                        installed_version=installed_version,
                        supported_versions_sign=supported_versions_sign,
                        expected_version=dependency_spec.expected_version))
        except FileNotFoundError:
            handle_error(logger,
                         Texts.DEPENDENCY_NOT_INSTALLED_ERROR_MSG.format(
                             dependency_name=dependency_name),
                         Texts.DEPENDENCY_NOT_INSTALLED_ERROR_MSG.format(
                             dependency_name=dependency_name),
                         add_verbosity_msg="client" not in dependency_name)
            exit(1)
        except (RuntimeError, ValueError, TypeError):
            handle_error(logger,
                         Texts.DEPENDENCY_VERSION_CHECK_ERROR_MSG.format(
                             dependency_name=dependency_name),
                         Texts.DEPENDENCY_VERSION_CHECK_ERROR_MSG.format(
                             dependency_name=dependency_name),
                         add_verbosity_msg=state.verbosity == 0)
            exit(1)
        except Exception:
            handle_error(logger,
                         Texts.DEPENDENCY_VERIFICATION_OTHER_ERROR_MSG.format(
                             dependency_name=dependency_name),
                         Texts.DEPENDENCY_VERIFICATION_OTHER_ERROR_MSG.format(
                             dependency_name=dependency_name),
                         add_verbosity_msg=state.verbosity == 0)
            exit(1)
    else:
        # This block is entered if all dependencies were validated successfully
        # Save dependency versions in a file
        save_dependency_versions(dependency_versions)