def test_arg_override_first_corrupted(self): paths = glob.glob(get_testdata("invalid_*")) + [ get_testdata("valid_bad_context"), get_testdata("valid_no_user") ] env_variable = generate_env_variable(paths) self.assert_chosen_path(env_variable, get_testdata("output_combined"), EXAMPLE_ARN, get_testdata("output_combined"))
def test_first_corrupted(self): paths = glob.glob(get_testdata("invalid_*")) + [ get_testdata("valid_bad_context"), get_testdata("valid_no_user") ] env_variable = generate_env_variable(paths) selector = KubeconfigSelector(env_variable, None, self._validator, self._loader) self.assertRaises(kubeconfig.KubeconfigCorruptedError, selector.choose_kubeconfig, EXAMPLE_ARN)
def test_parse_env_variable(self): paths = [ "", "", get_testdata("valid_bad_cluster"), get_testdata("valid_bad_cluster2"), "", get_testdata("valid_existing"), "" ] env_variable = generate_env_variable(paths) selector = KubeconfigSelector(env_variable, None, self._validator, self._loader) self.assertEqual(selector._paths, [path for path in paths if len(path) > 0])
def test_arg_override(self): paths = [get_testdata("valid_simple"), get_testdata("valid_existing")] + glob.glob( get_testdata("invalid_*")) + [ get_testdata("valid_bad_context"), get_testdata("valid_no_user"), get_testdata("output_single"), get_testdata("output_single_with_role") ] env_variable = generate_env_variable(paths) self.assert_chosen_path(env_variable, get_testdata("output_combined"), EXAMPLE_ARN, get_testdata("output_combined"))
def test_invalid(self): invalid_cases = glob.glob(get_testdata("invalid_*")) for case in invalid_cases: with open(case, 'r') as stream: content_dict = ordered_yaml_load(stream) config = Kubeconfig(None, content_dict) self.assertRaises(KubeconfigCorruptedError, self._validator.validate_config, config)
def assert_cmd_dry(self, passed_config, env_variable_configs, default_config=os.path.join(".kube", "config"), role=None): """ Run update-kubeconfig using dry-run, assert_cmd_dry runs directly referencing the testdata directory, since dry_run won't write to file The KUBECONFIG environment variable will be set to contain the configs listed in env_variable_configs (regardless of whether they exist). The default path will be set to default_config Returns the captured output :param passed_config: A filename to be passed to --kubeconfig :type passed_config: string :param env_variable_configs: A list of filenames to put in KUBECONFIG :type env_variable_configs: list or None :param default config: A config to be the default path :type default_config: string :returns: The captured output :rtype: CapturedOutput """ env_variable = self.build_temp_environment_variable( env_variable_configs ) args = ["--name", "ExampleCluster", "--dry-run"] if passed_config is not None: args += ["--kubeconfig", get_testdata(passed_config)] if role is not None: args += ["--role-arn", "RoleArn"] with capture_output() as captured: with mock.patch.dict(os.environ, {'KUBECONFIG': env_variable}): with mock.patch( "awscli.customizations.eks.update_kubeconfig.DEFAULT_PATH", get_testdata(default_config)): self.command(args, None) self.mock_create_client.assert_called_once_with('eks') self.client \ .describe_cluster.assert_called_once_with(name='ExampleCluster') return captured
def assert_cmd_dry(self, passed_config, env_variable_configs, default_config=os.path.join(".kube", "config"), role=None): """ Run update-kubeconfig using dry-run, assert_cmd_dry runs directly referencing the testdata directory, since dry_run won't write to file The KUBECONFIG environment variable will be set to contain the configs listed in env_variable_configs (regardless of whether they exist). The default path will be set to default_config Returns the captured output :param passed_config: A filename to be passed to --kubeconfig :type passed_config: string :param env_variable_configs: A list of filenames to put in KUBECONFIG :type env_variable_configs: list or None :param default config: A config to be the default path :type default_config: string :returns: The captured output :rtype: CapturedOutput """ env_variable = self.build_temp_environment_variable( env_variable_configs) args = ["--name", "ExampleCluster", "--dry-run"] if passed_config is not None: args += ["--kubeconfig", get_testdata(passed_config)] if role is not None: args += ["--role-arn", "RoleArn"] with capture_output() as captured: with mock.patch.dict(os.environ, {'KUBECONFIG': env_variable}): with mock.patch( "awscli.customizations.eks.update_kubeconfig.DEFAULT_PATH", get_testdata(default_config)): self.command(args, None) self.mock_create_client.assert_called_once_with('eks') self.client \ .describe_cluster.assert_called_once_with(name='ExampleCluster') return captured
def assert_output(self, captured, file): """ Compares the captured output with the testdata named file For approximate equality. """ with open(get_testdata(file)) as f: self.assertMultiLineEqual( sanitize_output(captured.stdout.getvalue()), f.read().strip())
def test_arg_override(self): paths = [ get_testdata("valid_simple"), get_testdata("valid_existing") ] + glob.glob(get_testdata("invalid_*")) + [ get_testdata("valid_bad_context"), get_testdata("valid_no_user"), get_testdata("output_single"), get_testdata("output_single_with_role") ] env_variable = generate_env_variable(paths) self.assert_chosen_path(env_variable, get_testdata("output_combined"), EXAMPLE_ARN, get_testdata("output_combined"))
def assert_output(self, captured, file): """ Compares the captured output with the testdata named file For approximate equality. """ with open(get_testdata(file)) as f: self.assertMultiLineEqual( sanitize_output(captured.stdout.getvalue()), f.read().strip() )
def test_valid(self): valid_cases = glob.glob(get_testdata( "valid_*" )) for case in valid_cases: with open(case, 'r') as stream: content_dict = ordered_yaml_load(stream) if content_dict is not None: config = Kubeconfig(None, content_dict) try: self._validator.validate_config(config) except KubeconfigError as e: self.fail("Valid file {0} raised {1}.".format(case, e))
def _clone_config(self, config): """ Copies the testdata named config into the temp directory, Returns the new path :param config: The name of the testdata to copy :type config: str """ old_path = os.path.abspath(get_testdata(config)) new_path = os.path.join(self._temp_directory, config) shutil.copy2(old_path, new_path) return new_path
def initialize_tempfiles(self, files): """ Initializes a directory of tempfiles containing copies of each testdata file listed in files. Returns the absolute path of the containing directory. :param files: A list of filenames found in testdata :type files: list """ self._temp_directory = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self._temp_directory) if files is not None: for file in files: shutil.copy2(get_testdata(file), self._get_temp_config(file)) return self._temp_directory
def test_choose_env_only(self): paths = [get_testdata("valid_simple"), get_testdata("valid_existing")] + glob.glob( get_testdata("invalid_*")) + [ get_testdata("valid_bad_context"), get_testdata("valid_no_user") ] env_variable = generate_env_variable(paths) self.assert_chosen_path(env_variable, None, EXAMPLE_ARN, get_testdata("valid_simple"))
def assert_config_state(self, config_name, correct_output_name): """ Asserts that the temp config named config_name has the same content as the testdata named correct_output_name. Should be called after initialize_tempfiles. :param config_name: The filename (not the path) of the tempfile to compare :type config_name: str :param correct_output_name: The filename (not the path) of the testdata to compare :type correct_output_name: str """ with open(self._get_temp_config(config_name)) as file1: with open(get_testdata(correct_output_name)) as file2: self.assertMultiLineEqual(file1.read().strip(), file2.read().strip())
def test_choose_env_only(self): paths = [ get_testdata("valid_simple"), get_testdata("valid_existing") ] + glob.glob(get_testdata("invalid_*")) + [ get_testdata("valid_bad_context"), get_testdata("valid_no_user") ] env_variable = generate_env_variable(paths) self.assert_chosen_path(env_variable, None, EXAMPLE_ARN, get_testdata("valid_simple"))