Path: blob/develop/tests/unit/customizations/eks/test_update_kubeconfig.py
1569 views
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import glob14import os15import shutil16import sys17import tempfile18from argparse import Namespace1920import botocore21from botocore.compat import OrderedDict2223import awscli.customizations.eks.kubeconfig as kubeconfig24from awscli.customizations.eks.exceptions import EKSClusterError, EKSError25from awscli.customizations.eks.ordered_yaml import ordered_yaml_load26from awscli.customizations.eks.update_kubeconfig import (API_VERSION,27EKSClient,28KubeconfigSelector)29from awscli.customizations.utils import uni_print30from awscli.testutils import mock, unittest31from tests.functional.eks.test_util import (32describe_cluster_creating_response, describe_cluster_deleting_response,33describe_cluster_no_status_response, describe_cluster_response,34describe_cluster_response_outpost_cluster, get_testdata)353637def generate_env_variable(files):38"""39Generate a string which is an environment variable40containing the absolute paths for each file in files4142:param files: The names of the files to put in the environment variable43:type files: list44"""45output = ""46for file in files:47if len(output) == 0:48output = file49else:50output += os.path.pathsep + file51return output525354EXAMPLE_ARN = "arn:aws:eks:region:111222333444:cluster/ExampleCluster"5556class TestKubeconfigSelector(unittest.TestCase):57def setUp(self):58self._validator = kubeconfig.KubeconfigValidator()59self._loader = kubeconfig.KubeconfigLoader(self._validator)6061def assert_chosen_path(self,62env_variable,63path_in,64cluster_name,65chosen_path):66selector = KubeconfigSelector(env_variable, path_in,67self._validator,68self._loader)69self.assertEqual(selector.choose_kubeconfig(cluster_name).path,70chosen_path)7172def test_parse_env_variable(self):73paths = [74"",75"",76get_testdata("valid_bad_cluster"),77get_testdata("valid_bad_cluster2"),78"",79get_testdata("valid_existing"),80""81]8283env_variable = generate_env_variable(paths)8485selector = KubeconfigSelector(env_variable, None, self._validator,86self._loader)87self.assertEqual(selector._paths, [path for path in paths88if len(path) > 0])8990def test_choose_env_only(self):91paths = [92get_testdata("valid_simple"),93get_testdata("valid_existing")94] + glob.glob(get_testdata("invalid_*")) + [95get_testdata("valid_bad_context"),96get_testdata("valid_no_user")97]98env_variable = generate_env_variable(paths)99self.assert_chosen_path(env_variable,100None,101EXAMPLE_ARN,102get_testdata("valid_simple"))103104def test_choose_existing(self):105paths = [106get_testdata("valid_simple"),107get_testdata("valid_existing")108] + glob.glob(get_testdata("invalid_*")) + [109get_testdata("valid_bad_context"),110get_testdata("valid_no_user"),111get_testdata("output_single"),112get_testdata("output_single_with_role")113]114env_variable = generate_env_variable(paths)115self.assert_chosen_path(env_variable,116None,117EXAMPLE_ARN,118get_testdata("output_single"))119120def test_arg_override(self):121paths = [122get_testdata("valid_simple"),123get_testdata("valid_existing")124] + glob.glob(get_testdata("invalid_*")) + [125get_testdata("valid_bad_context"),126get_testdata("valid_no_user"),127get_testdata("output_single"),128get_testdata("output_single_with_role")129]130env_variable = generate_env_variable(paths)131self.assert_chosen_path(env_variable,132get_testdata("output_combined"),133EXAMPLE_ARN,134get_testdata("output_combined"))135136def test_first_corrupted(self):137paths = glob.glob(get_testdata("invalid_*")) + [138get_testdata("valid_bad_context"),139get_testdata("valid_no_user")140]141env_variable = generate_env_variable(paths)142selector = KubeconfigSelector(env_variable, None, self._validator,143self._loader)144self.assertRaises(kubeconfig.KubeconfigCorruptedError,145selector.choose_kubeconfig,146EXAMPLE_ARN)147148def test_arg_override_first_corrupted(self):149paths = glob.glob(get_testdata("invalid_*")) + [150get_testdata("valid_bad_context"),151get_testdata("valid_no_user")152]153env_variable = generate_env_variable(paths)154self.assert_chosen_path(env_variable,155get_testdata("output_combined"),156EXAMPLE_ARN,157get_testdata("output_combined"))158159class TestEKSClient(unittest.TestCase):160def setUp(self):161self._correct_cluster_entry = OrderedDict([162("cluster", OrderedDict([163("certificate-authority-data", describe_cluster_response()\164["cluster"]["certificateAuthority"]["data"]),165("server", describe_cluster_response()["cluster"]["endpoint"])166])),167("name", describe_cluster_response()["cluster"]["arn"])168])169self._correct_user_entry = OrderedDict([170("name", describe_cluster_response()["cluster"]["arn"]),171("user", OrderedDict([172("exec", OrderedDict([173("apiVersion", API_VERSION),174("args",175[176"--region",177"region",178"eks",179"get-token",180"--cluster-name",181"ExampleCluster",182"--output",183"json",184]),185("command", "aws")186]))187]))188])189190self._correct_user_entry_outpost_cluster = OrderedDict([191("name", describe_cluster_response()["cluster"]["arn"]),192("user", OrderedDict([193("exec", OrderedDict([194("apiVersion", API_VERSION),195("args",196[197"--region",198"region",199"eks",200"get-token",201"--cluster-id",202"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",203"--output",204"json",205]),206("command", "aws")207]))208]))209])210211self._mock_client = mock.Mock()212self._mock_client.describe_cluster.return_value =\213describe_cluster_response()214215self._session = mock.Mock(spec=botocore.session.Session)216self._session.create_client.return_value = self._mock_client217self._session.profile = None218219self._client = EKSClient(self._session, parsed_args=Namespace(cluster_name="ExampleCluster", role_arn=None))220221def test_get_cluster_description(self):222self.assertEqual(self._client.cluster_description,223describe_cluster_response()["cluster"])224self._mock_client.describe_cluster.assert_called_once_with(225name="ExampleCluster"226)227self._session.create_client.assert_called_once_with("eks")228229def test_get_cluster_description_no_status(self):230self._mock_client.describe_cluster.return_value = \231describe_cluster_no_status_response()232with self.assertRaises(EKSClusterError):233self._client.cluster_description234self._mock_client.describe_cluster.assert_called_once_with(235name="ExampleCluster"236)237self._session.create_client.assert_called_once_with("eks")238239def test_get_cluster_entry(self):240self.assertEqual(self._client.get_cluster_entry(),241self._correct_cluster_entry)242self._mock_client.describe_cluster.assert_called_once_with(243name="ExampleCluster"244)245self._session.create_client.assert_called_once_with("eks")246247def test_get_user_entry(self):248self.assertEqual(self._client.get_user_entry(),249self._correct_user_entry)250self._mock_client.describe_cluster.assert_called_once_with(251name="ExampleCluster"252)253self._session.create_client.assert_called_once_with("eks")254255def test_get_user_entry_outpost_cluster(self):256self._mock_client.describe_cluster.return_value =\257describe_cluster_response_outpost_cluster()258self.assertEqual(self._client.get_user_entry(),259self._correct_user_entry_outpost_cluster)260self._mock_client.describe_cluster.assert_called_once_with(261name="ExampleCluster"262)263self._session.create_client.assert_called_once_with("eks")264265def test_get_both(self):266self.assertEqual(self._client.get_cluster_entry(),267self._correct_cluster_entry)268self.assertEqual(self._client.get_user_entry(),269self._correct_user_entry)270self._mock_client.describe_cluster.assert_called_once_with(271name="ExampleCluster"272)273self._session.create_client.assert_called_once_with("eks")274275def test_cluster_creating(self):276self._mock_client.describe_cluster.return_value =\277describe_cluster_creating_response()278with self.assertRaises(EKSClusterError):279self._client.cluster_description280self._mock_client.describe_cluster.assert_called_once_with(281name="ExampleCluster"282)283self._session.create_client.assert_called_once_with("eks")284285def test_cluster_deleting(self):286self._mock_client.describe_cluster.return_value =\287describe_cluster_deleting_response()288with self.assertRaises(EKSClusterError):289self._client.cluster_description290self._mock_client.describe_cluster.assert_called_once_with(291name="ExampleCluster"292)293self._session.create_client.assert_called_once_with("eks")294295def test_profile(self):296self._session.profile = "profile"297self._correct_user_entry["user"]["exec"]["env"] = [298OrderedDict([299("name", "AWS_PROFILE"),300("value", "profile")301])302]303self.assertEqual(self._client.get_user_entry(),304self._correct_user_entry)305self._mock_client.describe_cluster.assert_called_once_with(306name="ExampleCluster"307)308self._session.create_client.assert_called_once_with("eks")309310def test_create_user_with_alias(self):311self._correct_user_entry["name"] = "alias"312self.assertEqual(self._client.get_user_entry(user_alias="alias"),313self._correct_user_entry)314self._mock_client.describe_cluster.assert_called_once_with(315name="ExampleCluster"316)317self._session.create_client.assert_called_once_with("eks")318319def test_create_user_without_alias(self):320self.assertEqual(self._client.get_user_entry(),321self._correct_user_entry)322self._mock_client.describe_cluster.assert_called_once_with(323name="ExampleCluster"324)325self._session.create_client.assert_called_once_with("eks")326327328