Path: blob/develop/tests/unit/customizations/test_cliinputjson.py
1567 views
# Copyright 2014 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.12import os13import shutil14import tempfile1516from awscli.testutils import mock, unittest17from awscli.argprocess import ParamError18from awscli.customizations.cliinputjson import CliInputJSONArgument192021class TestCliInputJSONArgument(unittest.TestCase):22def setUp(self):23self.session = mock.Mock()24self.argument = CliInputJSONArgument(self.session)2526# Create the various forms the data could come in. The two main forms27# are as a string and or as a path to a file.28self.input_json = '{"A": "foo", "B": "bar"}'2930# Make a temporary file31self.temp_dir = tempfile.mkdtemp()32self.temp_file = os.path.join(self.temp_dir, 'foo.json')33with open(self.temp_file, 'w') as f:34f.write(self.input_json)3536def tearDown(self):37shutil.rmtree(self.temp_dir)3839def test_register_argument_action(self):40register_args = self.session.register.call_args_list41self.assertEqual(register_args[0][0][0], 'calling-command.*')42self.assertEqual(register_args[0][0][1],43self.argument.add_to_call_parameters)4445def test_add_to_call_parameters_no_file(self):46parsed_args = mock.Mock()47# Make the value a JSON string48parsed_args.cli_input_json = self.input_json49call_parameters = {}50self.argument.add_to_call_parameters(51service_operation=None, call_parameters=call_parameters,52parsed_args=parsed_args, parsed_globals=None53)54self.assertEqual(call_parameters, {'A': 'foo', 'B': 'bar'})5556def test_add_to_call_parameters_with_file(self):57parsed_args = mock.Mock()58# Make the value a file with JSON located inside.59parsed_args.cli_input_json = 'file://' + self.temp_file60call_parameters = {}61self.argument.add_to_call_parameters(62service_operation=None, call_parameters=call_parameters,63parsed_args=parsed_args, parsed_globals=None64)65self.assertEqual(call_parameters, {'A': 'foo', 'B': 'bar'})6667def test_add_to_call_parameters_bad_json(self):68parsed_args = mock.Mock()69# Create a bad JSON input70parsed_args.cli_input_json = self.input_json + ','71call_parameters = {}72with self.assertRaises(ParamError):73self.argument.add_to_call_parameters(74service_operation=None, call_parameters=call_parameters,75parsed_args=parsed_args, parsed_globals=None76)7778def test_add_to_call_parameters_no_clobber(self):79parsed_args = mock.Mock()80parsed_args.cli_input_json = self.input_json81# The value for ``A`` should not be clobbered by the input JSON82call_parameters = {'A': 'baz'}83self.argument.add_to_call_parameters(84service_operation=None, call_parameters=call_parameters,85parsed_args=parsed_args, parsed_globals=None86)87self.assertEqual(call_parameters, {'A': 'baz', 'B': 'bar'})8889def test_no_add_to_call_parameters(self):90parsed_args = mock.Mock()91parsed_args.cli_input_json = None92call_parameters = {'A': 'baz'}93self.argument.add_to_call_parameters(94service_operation=None, call_parameters=call_parameters,95parsed_args=parsed_args, parsed_globals=None96)97# Nothing should have been added to the call parameters because98# ``cli_input_json`` is not in the ``parsed_args``99self.assertEqual(call_parameters, {'A': 'baz'})100101102