forked from aws/aws-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cliinputjson.py
More file actions
101 lines (89 loc) · 4.03 KB
/
test_cliinputjson.py
File metadata and controls
101 lines (89 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import mock
import os
import shutil
import tempfile
from awscli.testutils import unittest
from awscli.argprocess import ParamError
from awscli.customizations.cliinputjson import CliInputJSONArgument
class TestCliInputJSONArgument(unittest.TestCase):
def setUp(self):
self.session = mock.Mock()
self.argument = CliInputJSONArgument(self.session)
# Create the various forms the data could come in. The two main forms
# are as a string and or as a path to a file.
self.input_json = '{"A": "foo", "B": "bar"}'
# Make a temporary file
self.temp_dir = tempfile.mkdtemp()
self.temp_file = os.path.join(self.temp_dir, 'foo.json')
with open(self.temp_file, 'w') as f:
f.write(self.input_json)
def tearDown(self):
shutil.rmtree(self.temp_dir)
def test_register_argument_action(self):
register_args = self.session.register.call_args_list
self.assertEqual(register_args[0][0][0], 'calling-command.*')
self.assertEqual(register_args[0][0][1],
self.argument.add_to_call_parameters)
def test_add_to_call_parameters_no_file(self):
parsed_args = mock.Mock()
# Make the value a JSON string
parsed_args.cli_input_json = self.input_json
call_parameters = {}
self.argument.add_to_call_parameters(
service_operation=None, call_parameters=call_parameters,
parsed_args=parsed_args, parsed_globals=None
)
self.assertEqual(call_parameters, {'A': 'foo', 'B': 'bar'})
def test_add_to_call_parameters_with_file(self):
parsed_args = mock.Mock()
# Make the value a file with JSON located inside.
parsed_args.cli_input_json = 'file://' + self.temp_file
call_parameters = {}
self.argument.add_to_call_parameters(
service_operation=None, call_parameters=call_parameters,
parsed_args=parsed_args, parsed_globals=None
)
self.assertEqual(call_parameters, {'A': 'foo', 'B': 'bar'})
def test_add_to_call_parameters_bad_json(self):
parsed_args = mock.Mock()
# Create a bad JSON input
parsed_args.cli_input_json = self.input_json + ','
call_parameters = {}
with self.assertRaises(ParamError):
self.argument.add_to_call_parameters(
service_operation=None, call_parameters=call_parameters,
parsed_args=parsed_args, parsed_globals=None
)
def test_add_to_call_parameters_no_clobber(self):
parsed_args = mock.Mock()
parsed_args.cli_input_json = self.input_json
# The value for ``A`` should not be clobbered by the input JSON
call_parameters = {'A': 'baz'}
self.argument.add_to_call_parameters(
service_operation=None, call_parameters=call_parameters,
parsed_args=parsed_args, parsed_globals=None
)
self.assertEqual(call_parameters, {'A': 'baz', 'B': 'bar'})
def test_no_add_to_call_parameters(self):
parsed_args = mock.Mock()
parsed_args.cli_input_json = None
call_parameters = {'A': 'baz'}
self.argument.add_to_call_parameters(
service_operation=None, call_parameters=call_parameters,
parsed_args=parsed_args, parsed_globals=None
)
# Nothing should have been added to the call parameters because
# ``cli_input_json`` is not in the ``parsed_args``
self.assertEqual(call_parameters, {'A': 'baz'})