Path: blob/develop/tests/unit/customizations/eks/test_update_kubeconfig.py
2621 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])169170self._correct_user_entry = OrderedDict([171("name", describe_cluster_response()["cluster"]["arn"]),172("user", OrderedDict([173("exec", OrderedDict([174("apiVersion", API_VERSION),175("args",176[177"--region",178"region",179"eks",180"get-token",181"--cluster-name",182"ExampleCluster",183"--output",184"json",185]),186("command", "aws")187]))188]))189])190191self._correct_user_entry_outpost_cluster = OrderedDict([192("name", describe_cluster_response()["cluster"]["arn"]),193("user", OrderedDict([194("exec", OrderedDict([195("apiVersion", API_VERSION),196("args",197[198"--region",199"region",200"eks",201"get-token",202"--cluster-id",203"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",204"--output",205"json",206]),207("command", "aws")208]))209]))210])211212self._mock_client = mock.Mock()213self._mock_client.describe_cluster.return_value =\214describe_cluster_response()215216self._session = mock.Mock(spec=botocore.session.Session)217self._session.create_client.return_value = self._mock_client218self._session.profile = None219220self._client = EKSClient(self._session, parsed_args=Namespace(cluster_name="ExampleCluster", role_arn=None, proxy_url=None))221222def test_get_cluster_description(self):223self.assertEqual(self._client.cluster_description,224describe_cluster_response()["cluster"])225self._mock_client.describe_cluster.assert_called_once_with(226name="ExampleCluster"227)228self._session.create_client.assert_called_once_with("eks")229230def test_get_cluster_description_no_status(self):231self._mock_client.describe_cluster.return_value = \232describe_cluster_no_status_response()233with self.assertRaises(EKSClusterError):234self._client.cluster_description235self._mock_client.describe_cluster.assert_called_once_with(236name="ExampleCluster"237)238self._session.create_client.assert_called_once_with("eks")239240def test_get_cluster_entry(self):241self.assertEqual(self._client.get_cluster_entry(),242self._correct_cluster_entry)243self._mock_client.describe_cluster.assert_called_once_with(244name="ExampleCluster"245)246self._session.create_client.assert_called_once_with("eks")247248def test_get_cluster_entry_with_proxy_url_passed(self):249proxy_url = "https://myproxy.com"250correct_cluster_entry_with_proxy_url = OrderedDict([251("cluster", OrderedDict([252("certificate-authority-data", describe_cluster_response()\253["cluster"]["certificateAuthority"]["data"]),254("server", describe_cluster_response()["cluster"]["endpoint"]),255("proxy-url", proxy_url)256])),257("name", describe_cluster_response()["cluster"]["arn"])258])259client = EKSClient(self._session, parsed_args=Namespace(cluster_name="ProxiedCluster",260role_arn=None,261proxy_url=proxy_url))262self.assertEqual(client.get_cluster_entry(),263correct_cluster_entry_with_proxy_url)264self._mock_client.describe_cluster.assert_called_once_with(265name="ProxiedCluster"266)267self._session.create_client.assert_called_once_with("eks")268269def test_get_user_entry(self):270self.assertEqual(self._client.get_user_entry(),271self._correct_user_entry)272self._mock_client.describe_cluster.assert_called_once_with(273name="ExampleCluster"274)275self._session.create_client.assert_called_once_with("eks")276277def test_get_user_entry_outpost_cluster(self):278self._mock_client.describe_cluster.return_value =\279describe_cluster_response_outpost_cluster()280self.assertEqual(self._client.get_user_entry(),281self._correct_user_entry_outpost_cluster)282self._mock_client.describe_cluster.assert_called_once_with(283name="ExampleCluster"284)285self._session.create_client.assert_called_once_with("eks")286287def test_get_both(self):288self.assertEqual(self._client.get_cluster_entry(),289self._correct_cluster_entry)290self.assertEqual(self._client.get_user_entry(),291self._correct_user_entry)292self._mock_client.describe_cluster.assert_called_once_with(293name="ExampleCluster"294)295self._session.create_client.assert_called_once_with("eks")296297def test_cluster_creating(self):298self._mock_client.describe_cluster.return_value =\299describe_cluster_creating_response()300with self.assertRaises(EKSClusterError):301self._client.cluster_description302self._mock_client.describe_cluster.assert_called_once_with(303name="ExampleCluster"304)305self._session.create_client.assert_called_once_with("eks")306307def test_cluster_deleting(self):308self._mock_client.describe_cluster.return_value =\309describe_cluster_deleting_response()310with self.assertRaises(EKSClusterError):311self._client.cluster_description312self._mock_client.describe_cluster.assert_called_once_with(313name="ExampleCluster"314)315self._session.create_client.assert_called_once_with("eks")316317def test_profile(self):318self._session.profile = "profile"319self._correct_user_entry["user"]["exec"]["env"] = [320OrderedDict([321("name", "AWS_PROFILE"),322("value", "profile")323])324]325self.assertEqual(self._client.get_user_entry(),326self._correct_user_entry)327self._mock_client.describe_cluster.assert_called_once_with(328name="ExampleCluster"329)330self._session.create_client.assert_called_once_with("eks")331332def test_create_user_with_alias(self):333self._correct_user_entry["name"] = "alias"334self.assertEqual(self._client.get_user_entry(user_alias="alias"),335self._correct_user_entry)336self._mock_client.describe_cluster.assert_called_once_with(337name="ExampleCluster"338)339self._session.create_client.assert_called_once_with("eks")340341def test_create_user_without_alias(self):342self.assertEqual(self._client.get_user_entry(),343self._correct_user_entry)344self._mock_client.describe_cluster.assert_called_once_with(345name="ExampleCluster"346)347self._session.create_client.assert_called_once_with("eks")348349350