Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/test_cliinputjson.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
import os
14
import shutil
15
import tempfile
16
17
from awscli.testutils import mock, unittest
18
from awscli.argprocess import ParamError
19
from awscli.customizations.cliinputjson import CliInputJSONArgument
20
21
22
class TestCliInputJSONArgument(unittest.TestCase):
23
def setUp(self):
24
self.session = mock.Mock()
25
self.argument = CliInputJSONArgument(self.session)
26
27
# Create the various forms the data could come in. The two main forms
28
# are as a string and or as a path to a file.
29
self.input_json = '{"A": "foo", "B": "bar"}'
30
31
# Make a temporary file
32
self.temp_dir = tempfile.mkdtemp()
33
self.temp_file = os.path.join(self.temp_dir, 'foo.json')
34
with open(self.temp_file, 'w') as f:
35
f.write(self.input_json)
36
37
def tearDown(self):
38
shutil.rmtree(self.temp_dir)
39
40
def test_register_argument_action(self):
41
register_args = self.session.register.call_args_list
42
self.assertEqual(register_args[0][0][0], 'calling-command.*')
43
self.assertEqual(register_args[0][0][1],
44
self.argument.add_to_call_parameters)
45
46
def test_add_to_call_parameters_no_file(self):
47
parsed_args = mock.Mock()
48
# Make the value a JSON string
49
parsed_args.cli_input_json = self.input_json
50
call_parameters = {}
51
self.argument.add_to_call_parameters(
52
service_operation=None, call_parameters=call_parameters,
53
parsed_args=parsed_args, parsed_globals=None
54
)
55
self.assertEqual(call_parameters, {'A': 'foo', 'B': 'bar'})
56
57
def test_add_to_call_parameters_with_file(self):
58
parsed_args = mock.Mock()
59
# Make the value a file with JSON located inside.
60
parsed_args.cli_input_json = 'file://' + self.temp_file
61
call_parameters = {}
62
self.argument.add_to_call_parameters(
63
service_operation=None, call_parameters=call_parameters,
64
parsed_args=parsed_args, parsed_globals=None
65
)
66
self.assertEqual(call_parameters, {'A': 'foo', 'B': 'bar'})
67
68
def test_add_to_call_parameters_bad_json(self):
69
parsed_args = mock.Mock()
70
# Create a bad JSON input
71
parsed_args.cli_input_json = self.input_json + ','
72
call_parameters = {}
73
with self.assertRaises(ParamError):
74
self.argument.add_to_call_parameters(
75
service_operation=None, call_parameters=call_parameters,
76
parsed_args=parsed_args, parsed_globals=None
77
)
78
79
def test_add_to_call_parameters_no_clobber(self):
80
parsed_args = mock.Mock()
81
parsed_args.cli_input_json = self.input_json
82
# The value for ``A`` should not be clobbered by the input JSON
83
call_parameters = {'A': 'baz'}
84
self.argument.add_to_call_parameters(
85
service_operation=None, call_parameters=call_parameters,
86
parsed_args=parsed_args, parsed_globals=None
87
)
88
self.assertEqual(call_parameters, {'A': 'baz', 'B': 'bar'})
89
90
def test_no_add_to_call_parameters(self):
91
parsed_args = mock.Mock()
92
parsed_args.cli_input_json = None
93
call_parameters = {'A': 'baz'}
94
self.argument.add_to_call_parameters(
95
service_operation=None, call_parameters=call_parameters,
96
parsed_args=parsed_args, parsed_globals=None
97
)
98
# Nothing should have been added to the call parameters because
99
# ``cli_input_json`` is not in the ``parsed_args``
100
self.assertEqual(call_parameters, {'A': 'baz'})
101
102