Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/test_generatecliskeleton.py
1567 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
from botocore.model import DenormalizedStructureBuilder
14
15
from awscli.testutils import mock, unittest
16
from awscli.customizations.generatecliskeleton import \
17
GenerateCliSkeletonArgument
18
from awscli.compat import StringIO
19
20
21
class TestGenerateCliSkeleton(unittest.TestCase):
22
def setUp(self):
23
self.session = mock.Mock()
24
# Create a mock service operation object
25
self.service_operation = mock.Mock()
26
27
# Make an arbitrary input model shape.
28
self.input_shape = {
29
'A': {
30
'type': 'structure',
31
'members': {
32
'B': {'type': 'string'},
33
}
34
}
35
}
36
shape = DenormalizedStructureBuilder().with_members(
37
self.input_shape).build_model()
38
self.operation_model = mock.Mock(input_shape=shape)
39
self.argument = GenerateCliSkeletonArgument(self.session, self.operation_model)
40
41
# This is what the json should should look like after being
42
# generated to standard output.
43
self.ref_json_output = \
44
'{\n "A": {\n "B": ""\n }\n}\n'
45
46
def test_register_argument_action(self):
47
register_args = self.session.register.call_args_list
48
self.assertEqual(register_args[0][0][0], 'calling-command.*')
49
self.assertEqual(register_args[0][0][1],
50
self.argument.generate_json_skeleton)
51
52
def test_no_override_required_args_when_output(self):
53
argument_table = {}
54
mock_arg = mock.Mock()
55
mock_arg.required = True
56
argument_table['required-arg'] = mock_arg
57
args = ['--generate-cli-skeleton', 'output']
58
self.argument.override_required_args(argument_table, args)
59
self.assertTrue(argument_table['required-arg'].required)
60
61
def test_override_required_args_when_input(self):
62
argument_table = {}
63
mock_arg = mock.Mock()
64
mock_arg.required = True
65
argument_table['required-arg'] = mock_arg
66
args = ['--generate-cli-skeleton']
67
self.argument.override_required_args(argument_table, args)
68
self.assertFalse(argument_table['required-arg'].required)
69
70
def test_override_required_args_when_output_present_but_not_value(self):
71
argument_table = {}
72
mock_arg = mock.Mock()
73
mock_arg.required = True
74
argument_table['required-arg'] = mock_arg
75
args = ['--generate-cli-skeleton', '--some-other-param', 'output']
76
self.argument.override_required_args(argument_table, args)
77
self.assertFalse(argument_table['required-arg'].required)
78
79
def test_generate_json_skeleton(self):
80
parsed_args = mock.Mock()
81
parsed_args.generate_cli_skeleton = 'input'
82
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
83
rc = self.argument.generate_json_skeleton(
84
service_operation=self.service_operation, call_parameters=None,
85
parsed_args=parsed_args, parsed_globals=None
86
)
87
# Ensure the contents printed to standard output are correct.
88
self.assertEqual(self.ref_json_output, mock_stdout.getvalue())
89
# Ensure it is the correct return code of zero.
90
self.assertEqual(rc, 0)
91
92
def test_no_generate_json_skeleton(self):
93
parsed_args = mock.Mock()
94
parsed_args.generate_cli_skeleton = None
95
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
96
rc = self.argument.generate_json_skeleton(
97
service_operation=self.service_operation, call_parameters=None,
98
parsed_args=parsed_args, parsed_globals=None
99
)
100
# Ensure nothing is printed to standard output
101
self.assertEqual('', mock_stdout.getvalue())
102
# Ensure nothing is returned because it was never called.
103
self.assertEqual(rc, None)
104
105
106
def test_generate_json_skeleton_no_input_shape(self):
107
parsed_args = mock.Mock()
108
parsed_args.generate_cli_skeleton = 'input'
109
# Set the input shape to ``None``.
110
self.argument = GenerateCliSkeletonArgument(
111
self.session, mock.Mock(input_shape=None))
112
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
113
rc = self.argument.generate_json_skeleton(
114
service_operation=self.service_operation, call_parameters=None,
115
parsed_args=parsed_args, parsed_globals=None
116
)
117
# Ensure the contents printed to standard output are correct,
118
# which should be an empty dictionary.
119
self.assertEqual('{}\n', mock_stdout.getvalue())
120
# Ensure it is the correct return code of zero.
121
self.assertEqual(rc, 0)
122
123
def test_generate_json_skeleton_with_timestamp(self):
124
parsed_args = mock.Mock()
125
parsed_args.generate_cli_skeleton = 'input'
126
input_shape = {
127
'A': {
128
'type': 'structure',
129
'members': {
130
'B': {'type': 'timestamp'},
131
}
132
}
133
}
134
shape = DenormalizedStructureBuilder().with_members(
135
input_shape).build_model()
136
operation_model = mock.Mock(input_shape=shape)
137
argument = GenerateCliSkeletonArgument(
138
self.session, operation_model)
139
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
140
rc = argument.generate_json_skeleton(
141
call_parameters=None, parsed_args=parsed_args,
142
parsed_globals=None
143
)
144
self.assertEqual(
145
'{\n'
146
' "A": {\n'
147
' "B": "1970-01-01T00:00:00"\n'
148
' }\n'
149
'}\n', mock_stdout.getvalue())
150
self.assertEqual(rc, 0)
151
152