Exemple #1
0
def test_check_dependency_parse():
    test_dependency_name = 'test-dep'
    test_version = LooseVersion('0.0.1')
    test_version_output = 'version:"0.0.1"'
    version_command_mock = MagicMock()
    version_command_mock.return_value = test_version_output, 0, test_version_output
    test_dependency = DependencySpec(expected_version=test_version,
                                     version_command=version_command_mock,
                                     version_command_args=[],
                                     version_field='version:',
                                     match_exact_version=False)

    assert check_dependency(test_dependency_name,
                            test_dependency) == (True, test_version)
Exemple #2
0
def test_check_dependency():
    test_dependency_name = 'test-dep'
    test_version = '0.0.1'
    version_command_mock = MagicMock()
    version_command_mock.return_value = test_version, 0, test_version
    test_dependency = DependencySpec(expected_version=test_version,
                                     version_command=version_command_mock,
                                     version_command_args=[],
                                     version_field=None,
                                     match_exact_version=False)

    assert check_dependency(test_dependency_name,
                            test_dependency) == (True,
                                                 LooseVersion(test_version))
Exemple #3
0
def test_check_dependency_namespace():
    test_dependency_name = 'test-dep'
    test_namespace = 'test-namespace'
    test_version = '0.0.1'
    version_command_mock = MagicMock()
    version_command_mock.return_value = test_version, 0, test_version
    test_dependency = DependencySpec(
        expected_version=test_version,
        version_command=version_command_mock,
        version_command_args=[NAMESPACE_PLACEHOLDER],
        version_field=None,
        match_exact_version=False)

    valid_dep, installed_version = check_dependency(
        test_dependency_name, test_dependency, namespace=test_namespace)
    assert (valid_dep, installed_version) == (True, LooseVersion(test_version))
    version_command_mock.assert_called_with([test_namespace])
Exemple #4
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)