Ejemplo n.º 1
0
    def test_load_eks_config_err(self, m_run):
        """Load EKS configuration from demo kubeconfig."""
        # Valid kubeconfig file.
        fname = "tests/support/kubeconf.yaml"
        err_resp = (K8sConfig(), True)

        # Pretend the `aws-iam-authenticator` binary does not exist.
        m_run.side_effect = FileNotFoundError
        assert k8s.load_eks_config(fname, "eks") == err_resp

        # Pretend that `aws-iam-authenticator` returned a valid but useless YAML.
        m_run.side_effect = None
        m_run.return_value = types.SimpleNamespace(
            stdout=yaml.dump({}).encode("utf8"))
        assert k8s.load_eks_config(fname, "eks") == err_resp

        # Pretend that `aws-iam-authenticator` returned an invalid YAML.
        m_run.side_effect = None
        invalid_yaml = "invalid :: - yaml".encode("utf8")
        m_run.return_value = types.SimpleNamespace(stdout=invalid_yaml)
        assert k8s.load_eks_config(fname, "eks") == err_resp

        # Pretend that `aws-iam-authenticator` ran without error but
        # returned an empty string. This typically happens if the AWS config
        # files do not exist for the selected AWS profile.
        m_run.side_effect = None
        m_run.return_value = types.SimpleNamespace(stdout=b"")
        assert k8s.load_eks_config(fname, "eks") == err_resp
Ejemplo n.º 2
0
    def test_wrong_conf(self):
        # Minikube
        fun = k8s.load_minikube_config
        resp = (K8sConfig(), True)
        assert fun("tests/support/invalid.yaml", None) == resp
        assert fun("tests/support/invalid.yaml", "invalid") == resp
        assert fun("tests/support/kubeconf.yaml", "invalid") == resp
        assert fun("tests/support/kubeconf_invalid.yaml", "minkube") == resp

        # GKE
        assert k8s.load_gke_config("tests/support/invalid.yaml", None) == resp
        assert k8s.load_gke_config("tests/support/invalid.yaml",
                                   "invalid") == resp
        assert k8s.load_gke_config("tests/support/kubeconf.yaml",
                                   "invalid") == resp
        assert k8s.load_gke_config("tests/support/kubeconf_invalid.yaml",
                                   "gke") == resp

        # EKS
        assert k8s.load_eks_config("tests/support/invalid.yaml", None) == resp
        assert k8s.load_eks_config("tests/support/invalid.yaml",
                                   "invalid") == resp
        assert k8s.load_eks_config("tests/support/kubeconf.yaml",
                                   "invalid") == resp
        assert k8s.load_eks_config("tests/support/kubeconf_invalid.yaml",
                                   "eks") == resp
Ejemplo n.º 3
0
    def test_load_eks_config_ok(self, m_run):
        """Load EKS configuration from demo kubeconfig."""
        # Mock the call to run the `aws-iam-authenticator` tool
        token = yaml.dump({"status": {"token": "EKS token"}})
        m_run.return_value = types.SimpleNamespace(stdout=token.encode("utf8"))

        # Load the K8s configuration for "eks" context.
        fname = "tests/support/kubeconf.yaml"
        ret, err = k8s.load_eks_config(fname, "eks")
        assert not err and isinstance(ret, K8sConfig)

        # The certificate will be in a temporary folder because the `Requests`
        # library insists on reading it from a file. Here we load that file and
        # manually insert its value into the returned Config structure. This
        # will make the verification step below easier to read.
        ca_cert = open(ret.ca_cert, "r").read().strip()
        ret = ret._replace(ca_cert=ca_cert)

        # Verify the expected output.
        assert ret == K8sConfig(
            url="https://5.6.7.8",
            token="EKS token",
            ca_cert="ca.cert",
            client_cert=None,
            version="",
            name="clustername-eks",
        )

        # Verify that the correct external command was called, including
        # environment variables. The "expected_*" values are directly from
        # "support/kubeconf.yaml".
        expected_cmd = [
            "aws-iam-authenticator", "token", "-i", "eks-cluster-name"
        ]
        expected_env = os.environ.copy()
        expected_env.update({"foo1": "bar1", "foo2": "bar2"})
        actual_cmd, actual_env = m_run.call_args[0][0], m_run.call_args[1][
            "env"]
        assert actual_cmd == expected_cmd
        assert actual_env == expected_env

        # EKS is not the default context in the demo kubeconf file, which means
        # this must fail.
        assert k8s.load_eks_config(fname, None) == (K8sConfig(), True)

        # Try to load a Minikube context - must fail.
        assert k8s.load_eks_config(fname, "minikube") == (K8sConfig(), True)