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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
# Copyright 2016 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.alias import AliasLoader, register_alias_commands
from awscli.customizations.commands import BasicCommand
from awscli.testutils import (
BaseAWSCommandParamsTest,
FileCreator,
skip_if_windows,
)
# We need to test that aliases work with custom BasicCommand,
# but the actual custom commands in the CLI can be complex and
# don't want the alias tests to fail if they are updated with new API
# calls they invoke. This custom command is a simple wrapper to the
# aws resourcegroupstaggingapi get-resources command.
class CustomTestCommand(BasicCommand):
NAME = 'resources'
ARG_TABLE = [
{'name': 'resource-types', 'action': 'store', 'required': False},
{'name': 'tags', 'action': 'store', 'required': False},
]
def _run_main(self, parsed_args, parsed_globals):
client = self._session.create_client('resourcegroupstaggingapi')
kwargs = {}
if parsed_args.resource_types is not None:
kwargs['ResourceTypeFilters'] = [parsed_args.resource_types]
if parsed_args.tags is not None:
key, value = parsed_args.tags.split('=')
kwargs['TagFilters'] = [{'Key': key, 'Values': [value]}]
client.get_resources(**kwargs)
class TestAliases(BaseAWSCommandParamsTest):
def setUp(self):
super(TestAliases, self).setUp()
self.files = FileCreator()
self.alias_file = self.files.create_file('alias', '[toplevel]\n')
self.driver.alias_loader = AliasLoader(self.alias_file)
self.event_emitter = self.driver.session.get_component('event_emitter')
# Alias injection is part of the built-in handler chain which defaults
# to the real ~/.aws/cli/alias file. We need to unregister the default
# injector so we can swap in our own version that points to a
# test file.
self.event_emitter.unregister(
'building-command-table', unique_id='cli-alias-injector'
)
register_alias_commands(
self.event_emitter, alias_filename=self.alias_file
)
def tearDown(self):
super(TestAliases, self).tearDown()
self.files.remove_all()
def add_alias(self, alias_name, alias_value):
with open(self.alias_file, 'a+') as f:
f.write('%s = %s\n' % (alias_name, alias_value))
def assert_single_operation_called(
self, cmdline, service_name, operation_name, params
):
self.assert_params_for_cmd(cmdline, params)
operations_called = self.operations_called
self.assertEqual(len(operations_called), 1)
single_op = operations_called[0]
self.assertEqual(
single_op[0].service_model.service_name,
service_name,
)
self.assertEqual(single_op[0].name, operation_name)
def test_subcommand_alias(self):
self.add_alias('my-alias', 'ec2 describe-regions')
cmdline = 'my-alias'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={},
)
def test_subcommand_alias_with_additonal_params(self):
self.add_alias(
'my-alias', 'ec2 describe-regions --region-names us-east-1'
)
cmdline = 'my-alias'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={'RegionNames': ['us-east-1']},
)
def test_subcommand_alias_then_additonal_params(self):
self.add_alias('my-alias', 'ec2')
cmdline = 'my-alias describe-regions --region-names us-east-1'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={'RegionNames': ['us-east-1']},
)
def test_subcommand_alias_with_global_params(self):
self.add_alias(
'my-alias',
'ec2 describe-regions --query Regions[].RegionName --output text',
)
self.parsed_responses = [
{
'Regions': [
{
'Endpoint': 'ec2.us-east-1.amazonaws.com',
'RegionName': 'us-east-1',
}
]
}
]
cmdline = 'my-alias'
stdout, _, _ = self.assert_params_for_cmd(cmdline, {})
self.assertEqual(len(self.operations_called), 1)
self.assertEqual(
self.operations_called[0][0].service_model.service_name, 'ec2'
)
self.assertEqual(self.operations_called[0][0].name, 'DescribeRegions')
self.assertEqual(stdout.strip(), 'us-east-1')
def test_subcommand_alias_then_global_params(self):
self.add_alias('my-alias', 'ec2 describe-regions')
self.parsed_responses = [
{
'Regions': [
{
'Endpoint': 'ec2.us-east-1.amazonaws.com',
'RegionName': 'us-east-1',
}
]
}
]
cmdline = 'my-alias '
cmdline += '--query=Regions[].RegionName '
cmdline += '--output=text'
stdout, _, _ = self.assert_params_for_cmd(cmdline, {})
self.assertEqual(len(self.operations_called), 1)
self.assertEqual(
self.operations_called[0][0].service_model.service_name, 'ec2'
)
self.assertEqual(self.operations_called[0][0].name, 'DescribeRegions')
self.assertEqual(stdout.strip(), 'us-east-1')
def test_global_params_then_subcommand_alias(self):
self.add_alias('my-alias', 'ec2 describe-regions')
self.parsed_responses = [
{
'Regions': [
{
'Endpoint': 'ec2.us-east-1.amazonaws.com',
'RegionName': 'us-east-1',
}
]
}
]
cmdline = '--query=Regions[].RegionName '
cmdline += '--output=text '
cmdline += 'my-alias'
stdout, _, _ = self.assert_params_for_cmd(cmdline, {})
self.assertEqual(len(self.operations_called), 1)
self.assertEqual(
self.operations_called[0][0].service_model.service_name, 'ec2'
)
self.assertEqual(self.operations_called[0][0].name, 'DescribeRegions')
self.assertEqual(stdout.strip(), 'us-east-1')
def test_alias_overrides_builtin_command(self):
self.add_alias('ec2', 's3api')
cmdline = 'ec2 list-buckets'
self.assert_params_for_cmd(cmdline, {})
self.assertEqual(len(self.operations_called), 1)
self.assertEqual(self.operations_called[0][0].name, 'ListBuckets')
def test_alias_proxies_to_shadowed_command(self):
self.add_alias('ec2', 'ec2')
cmdline = 'ec2 describe-regions'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={},
)
def test_alias_chaining(self):
self.add_alias('base-alias', 'ec2 describe-regions')
self.add_alias('wrapper-alias', 'base-alias --region-names us-east-1')
cmdline = 'wrapper-alias'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={'RegionNames': ['us-east-1']},
)
def test_alias_chaining_with_globals(self):
self.add_alias('base-alias', 'ec2 describe-regions')
self.add_alias(
'wrapper-alias',
'base-alias --query Regions[].RegionName --output text',
)
cmdline = 'wrapper-alias'
self.parsed_responses = [
{
'Regions': [
{
'Endpoint': 'ec2.us-east-1.amazonaws.com',
'RegionName': 'us-east-1',
}
]
}
]
stdout, _, _ = self.assert_params_for_cmd(cmdline, {})
self.assertEqual(len(self.operations_called), 1)
self.assertEqual(
self.operations_called[0][0].service_model.service_name, 'ec2'
)
self.assertEqual(self.operations_called[0][0].name, 'DescribeRegions')
self.assertEqual(stdout.strip(), 'us-east-1')
def test_external_alias(self):
# The external alias is tested by using mkdir; a command that
# is universal for the various OS's we support
directory_to_make = os.path.join(self.files.rootdir, 'newdir')
self.add_alias('mkdir', '!mkdir %s' % directory_to_make)
self.run_cmd('mkdir')
self.assertTrue(os.path.isdir(directory_to_make))
def test_external_alias_then_additonal_args(self):
# The external alias is tested by using mkdir; a command that
# is universal for the various OS's we support
directory_to_make = os.path.join(self.files.rootdir, 'newdir')
self.add_alias('mkdir', '!mkdir')
self.run_cmd('mkdir %s' % directory_to_make)
self.assertTrue(os.path.isdir(directory_to_make))
def test_external_alias_with_quoted_arguments(self):
directory_to_make = os.path.join(self.files.rootdir, 'new dir')
self.add_alias('mkdir', '!mkdir')
self.run_cmd(['mkdir', directory_to_make])
self.assertTrue(os.path.isdir(directory_to_make))
@skip_if_windows('Windows does not support BASH functions')
def test_external_alias_with_wrapper_bash_function(self):
# The external alias is tested by using mkdir; a command that
# is universal for the various OS's we support
directory_to_make = os.path.join(self.files.rootdir, 'newdir')
self.add_alias('mkdir', '!f() { mkdir "${1}"; }; f')
self.run_cmd('mkdir %s' % directory_to_make)
self.assertTrue(os.path.isdir(directory_to_make))
def test_operation_level_alias_with_additonal_params(self):
with open(self.alias_file, 'a+') as f:
f.write('[command ec2]\n')
f.write('regions = describe-regions --region-names us-west-2\n')
cmdline = 'ec2 regions'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={'RegionNames': ['us-west-2']},
)
def test_can_shadow_subcommand_alias(self):
with open(self.alias_file, 'a+') as f:
f.write('[command ec2]\n')
f.write(
'describe-regions = describe-regions --region-names us-west-2\n'
)
cmdline = 'ec2 describe-regions'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={'RegionNames': ['us-west-2']},
)
def test_multi_nested_command_alias(self):
with open(self.alias_file, 'a+') as f:
f.write('[command ec2 wait]\n')
f.write('vpc-ready = vpc-available\n')
cmdline = 'ec2 wait vpc-ready'
self.parsed_response = {
'Vpcs': [{'State': 'available'}],
}
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeVpcs',
params={},
)
def test_can_extend_subcommand_internal_aliases(self):
with open(self.alias_file, 'a+') as f:
f.write('[command ec2]\n')
f.write(
'regions = describe-regions ' '--query Regions[].RegionName\n'
)
cmdline = 'ec2 regions --region-names us-west-2'
self.assert_single_operation_called(
cmdline,
service_name='ec2',
operation_name='DescribeRegions',
params={'RegionNames': ['us-west-2']},
)
def test_operation_level_external_alias(self):
directory_to_make = os.path.join(self.files.rootdir, 'newdir')
with open(self.alias_file, 'a+') as f:
f.write('[command ec2]\n')
f.write('mkdir = !mkdir\n')
self.run_cmd('ec2 mkdir %s' % directory_to_make)
self.assertTrue(os.path.isdir(directory_to_make))
def test_can_create_bag_of_options_alias(self):
with open(self.alias_file, 'a+') as f:
f.write('[command cloudformation list-stacks]\n')
f.write(
'created = --stack-status-filter CREATE_COMPLETE '
'--query StackSummaries[].[StackName,StackStatus] '
'--output text\n'
)
cmdline = 'cloudformation list-stacks created'
self.assert_single_operation_called(
cmdline,
service_name='cloudformation',
operation_name='ListStacks',
params={'StackStatusFilter': ['CREATE_COMPLETE']},
)
def test_can_support_multiple_alias_expansions(self):
with open(self.alias_file, 'a+') as f:
f.write(
'cfn = cloudformation\n'
'[command cloudformation]\n'
'stacks = list-stacks\n'
'[command cloudformation list-stacks]\n'
'created = --stack-status-filter CREATE_COMPLETE '
'--query StackSummaries[].[StackName,StackStatus] '
'--output text\n'
)
cmdline = 'cfn stacks created'
self.assert_single_operation_called(
cmdline,
service_name='cloudformation',
operation_name='ListStacks',
params={'StackStatusFilter': ['CREATE_COMPLETE']},
)
def test_can_merge_explicit_and_alias_local_params(self):
section = '[command resourcegroupstaggingapi get-resources]\n'
alias = (
'mysvc = --tag-filters Key=foo,Values=bar '
'Key=bar,Values=baz --query ResourceTagMappingList[].[ResourceARN]'
' --output text\n'
)
with open(self.alias_file, 'a+') as f:
f.write(section)
f.write(alias)
# If we add additional operatio-specific params such as
# "--resource-type-filters ecs", then we should merge
# those values with the params specified in the alias definition.
cmdline = (
'resourcegroupstaggingapi get-resources mysvc '
'--resource-type-filters ecs'
)
self.assert_single_operation_called(
cmdline,
service_name='resourcegroupstaggingapi',
operation_name='GetResources',
params={
'TagFilters': [
{'Key': 'foo', 'Values': ['bar']},
{'Key': 'bar', 'Values': ['baz']},
],
'ResourceTypeFilters': ['ecs'],
},
)
def test_can_handle_bag_of_options_with_required_args(self):
with open(self.alias_file, 'a+') as f:
f.write('[command iam create-user]\n')
f.write('test-user = --user-name test-user\n')
# The current behavior without aliases is that `--user-name`
# is a required parameter. So without changing anything
# if you ran "aws iam create-user test-user", the parser
# would complain that the `--user-name` arg is required.
# However, the `--user-name` is specified, it's just in the
# `test-user` alias. So we have to ensure that we're able
# to delegate to the alias in the hopes that it can provide
# the required value. FWIW we had to do something similar
# in the `--generate-cli-skeleton` flow.
self.assert_single_operation_called(
'iam create-user test-user',
service_name='iam',
operation_name='CreateUser',
params={'UserName': 'test-user'},
)
def test_can_handle_bag_of_options_with_custom_command(self):
self.event_emitter.register(
'building-command-table.resourcegroupstaggingapi',
CustomTestCommand.add_command,
)
with open(self.alias_file, 'a+') as f:
f.write('[command resourcegroupstaggingapi resources]\n')
f.write('mycluster = --resource-types ecs:cluster\n')
cmdline = 'resourcegroupstaggingapi resources mycluster'
self.assert_single_operation_called(
cmdline,
service_name='resourcegroupstaggingapi',
operation_name='GetResources',
params={'ResourceTypeFilters': ['ecs:cluster']},
)
def test_can_handle_custom_bag_of_options_and_overrides(self):
self.event_emitter.register(
'building-command-table.resourcegroupstaggingapi',
CustomTestCommand.add_command,
)
with open(self.alias_file, 'a+') as f:
f.write('[command resourcegroupstaggingapi resources]\n')
f.write('mycluster = --resource-types ecs:cluster\n')
cmdline = 'resourcegroupstaggingapi resources mycluster --tags foo=bar'
# We should merge the cmdline params from the 'mycluster' alias along
# with the explicitly provided --tags option from cmdline args.
self.assert_single_operation_called(
cmdline,
service_name='resourcegroupstaggingapi',
operation_name='GetResources',
params={
'ResourceTypeFilters': ['ecs:cluster'],
'TagFilters': [{'Key': 'foo', 'Values': ['bar']}],
},
)
def test_can_handle_multiple_bag_of_options(self):
with open(self.alias_file, 'a+') as f:
f.write('[command ecs describe-tasks]\n')
f.write('mycluster = --cluster mycluster\n')
f.write('mytasks = --tasks foo\n')
cmdline = 'ecs describe-tasks mycluster mytasks'
self.assert_single_operation_called(
cmdline,
service_name='ecs',
operation_name='DescribeTasks',
params={'cluster': 'mycluster', 'tasks': ['foo']},
)
def test_can_handlle_multiple_bags_of_options_with_overrides(self):
with open(self.alias_file, 'a+') as f:
f.write('[command ecs describe-tasks]\n')
f.write('mycluster = --cluster mycluster\n')
f.write('mytasks = --tasks foo\n')
cmdline = 'ecs describe-tasks mycluster mytasks --include TAGS'
self.assert_single_operation_called(
cmdline,
service_name='ecs',
operation_name='DescribeTasks',
params={
'cluster': 'mycluster',
'tasks': ['foo'],
'include': ['TAGS'],
},
)
def test_can_invoke_custom_test_command(self):
# Sanity check to ensure the custom test command we're using
# works properly so we don't waste time debugging a test because
# the test command implementation is wrong.
self.event_emitter.register(
'building-command-table.resourcegroupstaggingapi',
CustomTestCommand.add_command,
)
cmdline = 'resourcegroupstaggingapi resources --resource-types ecs --tags foo=bar'
self.assert_single_operation_called(
cmdline,
service_name='resourcegroupstaggingapi',
operation_name='GetResources',
params={
'ResourceTypeFilters': ['ecs'],
'TagFilters': [{'Key': 'foo', 'Values': ['bar']}],
},
)
|