Path: blob/develop/tests/unit/customizations/test_generatecliskeleton.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.12from botocore.model import DenormalizedStructureBuilder1314from awscli.testutils import mock, unittest15from awscli.customizations.generatecliskeleton import \16GenerateCliSkeletonArgument17from awscli.compat import StringIO181920class TestGenerateCliSkeleton(unittest.TestCase):21def setUp(self):22self.session = mock.Mock()23# Create a mock service operation object24self.service_operation = mock.Mock()2526# Make an arbitrary input model shape.27self.input_shape = {28'A': {29'type': 'structure',30'members': {31'B': {'type': 'string'},32}33}34}35shape = DenormalizedStructureBuilder().with_members(36self.input_shape).build_model()37self.operation_model = mock.Mock(input_shape=shape)38self.argument = GenerateCliSkeletonArgument(self.session, self.operation_model)3940# This is what the json should should look like after being41# generated to standard output.42self.ref_json_output = \43'{\n "A": {\n "B": ""\n }\n}\n'4445def test_register_argument_action(self):46register_args = self.session.register.call_args_list47self.assertEqual(register_args[0][0][0], 'calling-command.*')48self.assertEqual(register_args[0][0][1],49self.argument.generate_json_skeleton)5051def test_no_override_required_args_when_output(self):52argument_table = {}53mock_arg = mock.Mock()54mock_arg.required = True55argument_table['required-arg'] = mock_arg56args = ['--generate-cli-skeleton', 'output']57self.argument.override_required_args(argument_table, args)58self.assertTrue(argument_table['required-arg'].required)5960def test_override_required_args_when_input(self):61argument_table = {}62mock_arg = mock.Mock()63mock_arg.required = True64argument_table['required-arg'] = mock_arg65args = ['--generate-cli-skeleton']66self.argument.override_required_args(argument_table, args)67self.assertFalse(argument_table['required-arg'].required)6869def test_override_required_args_when_output_present_but_not_value(self):70argument_table = {}71mock_arg = mock.Mock()72mock_arg.required = True73argument_table['required-arg'] = mock_arg74args = ['--generate-cli-skeleton', '--some-other-param', 'output']75self.argument.override_required_args(argument_table, args)76self.assertFalse(argument_table['required-arg'].required)7778def test_generate_json_skeleton(self):79parsed_args = mock.Mock()80parsed_args.generate_cli_skeleton = 'input'81with mock.patch('sys.stdout', StringIO()) as mock_stdout:82rc = self.argument.generate_json_skeleton(83service_operation=self.service_operation, call_parameters=None,84parsed_args=parsed_args, parsed_globals=None85)86# Ensure the contents printed to standard output are correct.87self.assertEqual(self.ref_json_output, mock_stdout.getvalue())88# Ensure it is the correct return code of zero.89self.assertEqual(rc, 0)9091def test_no_generate_json_skeleton(self):92parsed_args = mock.Mock()93parsed_args.generate_cli_skeleton = None94with mock.patch('sys.stdout', StringIO()) as mock_stdout:95rc = self.argument.generate_json_skeleton(96service_operation=self.service_operation, call_parameters=None,97parsed_args=parsed_args, parsed_globals=None98)99# Ensure nothing is printed to standard output100self.assertEqual('', mock_stdout.getvalue())101# Ensure nothing is returned because it was never called.102self.assertEqual(rc, None)103104105def test_generate_json_skeleton_no_input_shape(self):106parsed_args = mock.Mock()107parsed_args.generate_cli_skeleton = 'input'108# Set the input shape to ``None``.109self.argument = GenerateCliSkeletonArgument(110self.session, mock.Mock(input_shape=None))111with mock.patch('sys.stdout', StringIO()) as mock_stdout:112rc = self.argument.generate_json_skeleton(113service_operation=self.service_operation, call_parameters=None,114parsed_args=parsed_args, parsed_globals=None115)116# Ensure the contents printed to standard output are correct,117# which should be an empty dictionary.118self.assertEqual('{}\n', mock_stdout.getvalue())119# Ensure it is the correct return code of zero.120self.assertEqual(rc, 0)121122def test_generate_json_skeleton_with_timestamp(self):123parsed_args = mock.Mock()124parsed_args.generate_cli_skeleton = 'input'125input_shape = {126'A': {127'type': 'structure',128'members': {129'B': {'type': 'timestamp'},130}131}132}133shape = DenormalizedStructureBuilder().with_members(134input_shape).build_model()135operation_model = mock.Mock(input_shape=shape)136argument = GenerateCliSkeletonArgument(137self.session, operation_model)138with mock.patch('sys.stdout', StringIO()) as mock_stdout:139rc = argument.generate_json_skeleton(140call_parameters=None, parsed_args=parsed_args,141parsed_globals=None142)143self.assertEqual(144'{\n'145' "A": {\n'146' "B": "1970-01-01T00:00:00"\n'147' }\n'148'}\n', mock_stdout.getvalue())149self.assertEqual(rc, 0)150151152