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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
# 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 os
from awscli.customizations.arguments import (
NestedBlobArgumentHoister,
OverrideRequiredArgsArgument,
QueryOutFileArgument,
StatefulArgument,
is_parsed_result_successful,
resolve_given_outfile_path,
)
from awscli.customizations.exceptions import ParamValidationError
from awscli.testutils import FileCreator, mock, skip_if_windows, unittest
class TestOverrideRequiredArgsArgument(unittest.TestCase):
def setUp(self):
self.session = mock.Mock()
self.argument = OverrideRequiredArgsArgument(self.session)
# Set up a sample argument_table
self.argument_table = {}
self.mock_arg = mock.Mock()
self.mock_arg.required = True
self.argument_table['mock-arg'] = self.mock_arg
def test_register_argument_action(self):
register_args = self.session.register.call_args
self.assertEqual(
register_args[0][0], 'before-building-argument-table-parser'
)
self.assertEqual(
register_args[0][1], self.argument.override_required_args
)
def test_override_required_args_if_in_cmdline(self):
args = ['--no-required-args']
self.argument.override_required_args(self.argument_table, args)
self.assertFalse(self.mock_arg.required)
def test_no_override_required_args_if_not_in_cmdline(self):
args = []
self.argument.override_required_args(self.argument_table, args)
self.assertTrue(self.mock_arg.required)
class TestStatefulArgument(unittest.TestCase):
def test_persists_value_when_added_to_params(self):
self.session = mock.Mock()
arg = StatefulArgument('test')
arg.add_to_params({}, 'foo')
self.assertEqual('foo', arg.value)
class TestArgumentHelpers(unittest.TestCase):
def setUp(self):
self.files = FileCreator()
def tearDown(self):
self.files.remove_all()
def test_only_validates_filename_when_set(self):
resolve_given_outfile_path(None)
def test_works_with_valid_filename(self):
filename = self.files.create_file('valid', '')
self.assertEqual(filename, resolve_given_outfile_path(filename))
def test_works_with_relative_filename(self):
filename = '../valid'
self.assertEqual(filename, resolve_given_outfile_path(filename))
def test_raises_when_cannot_write_to_file(self):
filename = os.sep.join(['_path', 'not', '_exist_', 'file.xyz'])
with self.assertRaises(ParamValidationError):
resolve_given_outfile_path(filename)
def test_checks_if_valid_result(self):
result = {'ResponseMetadata': {'HTTPStatusCode': 200}}
self.assertTrue(is_parsed_result_successful(result))
def test_checks_if_invalid_result(self):
result = {'ResponseMetadata': {'HTTPStatusCode': 300}}
self.assertFalse(is_parsed_result_successful(result))
class TestQueryFileArgument(unittest.TestCase):
def setUp(self):
self.files = FileCreator()
def tearDown(self):
self.files.remove_all()
def test_proxies_to_super_ctor(self):
session = mock.Mock()
arg = QueryOutFileArgument(session, 'foo', 'bar.baz', 'event', 0o600)
self.assertEqual('foo', arg.name)
self.assertEqual('bar.baz', arg.query)
def test_adds_default_help_text(self):
session = mock.Mock()
arg = QueryOutFileArgument(session, 'foo', 'bar.baz', 'event', 0o600)
self.assertEqual(
(
'Saves the command output contents of bar.baz '
'to the given filename'
),
arg.documentation,
)
def test_does_not_add_help_text_if_set(self):
session = mock.Mock()
arg = QueryOutFileArgument(
session, 'foo', 'bar.baz', 'event', 0o600, help_text='abc'
)
self.assertEqual('abc', arg.documentation)
def test_saves_query_to_file(self):
outfile = self.files.create_file('not-empty-test', '')
session = mock.Mock()
arg = QueryOutFileArgument(session, 'foo', 'baz', 'event', 0o600)
arg.add_to_params({}, outfile)
arg.save_query(
{'ResponseMetadata': {'HTTPStatusCode': 200}, 'baz': 'abc123'}
)
with open(outfile) as fp:
self.assertEqual('abc123', fp.read())
self.assertEqual(1, session.register.call_count)
session.register.assert_called_with('event', arg.save_query)
def test_does_not_save_when_not_set(self):
session = mock.Mock()
QueryOutFileArgument(session, 'foo', 'baz', 'event', 0o600)
self.assertEqual(0, session.register.call_count)
def test_saves_query_to_file_as_empty_string_when_none_result(self):
outfile = self.files.create_file('none-test', '')
session = mock.Mock()
arg = QueryOutFileArgument(session, 'foo', 'baz', 'event', 0o600)
arg.add_to_params({}, outfile)
arg.save_query({'ResponseMetadata': {'HTTPStatusCode': 200}})
with open(outfile) as fp:
self.assertEqual('', fp.read())
@skip_if_windows("Test not valid on windows.")
def test_permissions_on_created_file(self):
outfile = self.files.create_file('not-empty-test', '')
session = mock.Mock()
arg = QueryOutFileArgument(session, 'foo', 'baz', 'event', 0o600)
arg.add_to_params({}, outfile)
arg.save_query(
{'ResponseMetadata': {'HTTPStatusCode': 200}, 'baz': 'abc123'}
)
with open(outfile) as fp:
fp.read()
self.assertEqual(os.stat(outfile).st_mode & 0xFFF, 0o600)
class TestNestedBlobArgumentHoister(unittest.TestCase):
def setUp(self):
self.blob_hoister = NestedBlobArgumentHoister(
'complexArgX', 'valueY', 'argY', 'argYDoc', '.argYDocAddendum'
)
self.arg_table = {
'complexArgX': mock.Mock(
required=True,
documentation='complexArgXDoc',
argument_model=mock.Mock(
members={
'valueY': mock.Mock(
type_name='blob',
)
}
),
)
}
def test_apply_to_arg_table(self):
self.blob_hoister(None, self.arg_table)
self.assertFalse(self.arg_table['complexArgX'].required)
self.assertEqual(
self.arg_table['complexArgX'].documentation,
'complexArgXDoc.argYDocAddendum',
)
argY = self.arg_table['argY']
self.assertFalse(argY.required)
self.assertEqual(argY.documentation, 'argYDoc')
self.assertEqual(argY.argument_model.type_name, 'blob')
def test_populates_underlying_complex_arg(self):
self.blob_hoister(None, self.arg_table)
argY = self.arg_table['argY']
# parameters bag doesn't
# already contain 'ComplexArgX'
parameters = {
'any': 'other',
}
argY.add_to_params(parameters, 'test-value')
self.assertEqual('other', parameters['any'])
self.assertEqual('test-value', parameters['ComplexArgX']['valueY'])
def test_preserves_member_values_in_underlying_complex_arg(self):
self.blob_hoister(None, self.arg_table)
argY = self.arg_table['argY']
# parameters bag already contains 'ComplexArgX'
# but that does not contain 'valueY'
parameters = {
'any': 'other',
'ComplexArgX': {
'another': 'one',
},
}
argY.add_to_params(parameters, 'test-value')
self.assertEqual('other', parameters['any'])
self.assertEqual('one', parameters['ComplexArgX']['another'])
self.assertEqual('test-value', parameters['ComplexArgX']['valueY'])
def test_overrides_target_member_in_underlying_complex_arg(self):
self.blob_hoister(None, self.arg_table)
argY = self.arg_table['argY']
# parameters bag already contains 'ComplexArgX'
# and that already contains 'valueY'
parameters = {
'any': 'other',
'ComplexArgX': {
'another': 'one',
'valueY': 'doomed',
},
}
argY.add_to_params(parameters, 'test-value')
self.assertEqual('other', parameters['any'])
self.assertEqual('one', parameters['ComplexArgX']['another'])
self.assertEqual('test-value', parameters['ComplexArgX']['valueY'])
def test_not_apply_to_mismatch_arg_type(self):
nonmatching_arg_table = {
'complexArgX': mock.Mock(
required=True,
documentation='complexArgXDoc',
argument_model=mock.Mock(
members={
'valueY': mock.Mock(
type_name='string',
)
}
),
)
}
self.blob_hoister(None, nonmatching_arg_table)
self.assertTrue(nonmatching_arg_table['complexArgX'].required)
self.assertEqual(
nonmatching_arg_table['complexArgX'].documentation,
'complexArgXDoc',
)
self.assertFalse('argY' in self.arg_table)
|