def test_event_emission_for_top_level_params(self): driver = create_clidriver() # --unknown-foo is an known arg, so we expect a 255 rc. rc = driver.main('ec2 describe-instances --unknown-arg foo'.split()) self.assertEqual(rc, 255) self.assertIn('Unknown options: --unknown-arg', self.stderr.getvalue()) # The argument table is memoized in the CLIDriver object. So # when we call main() above, it will get created and cached # and the argument table won't get created again (and therefore # the building-top-level-params event will not get generated again). # So, for this test we need to create a new driver object. driver = create_clidriver() driver.session.register( 'building-top-level-params', self.inject_new_param) driver.session.register( 'top-level-args-parsed', lambda parsed_args, **kwargs: args_seen.append(parsed_args)) args_seen = [] # Now we should get an rc of 0 as the arg is expected # (though nothing actually does anything with the arg). self.patch_make_request() rc = driver.main('ec2 describe-instances --unknown-arg foo'.split()) self.assertEqual(rc, 0) self.assertEqual(len(args_seen), 1) self.assertEqual(args_seen[0].unknown_arg, 'foo')
def test_aws_with_endpoint_url(self): driver = create_clidriver() driver.main( 'ec2 describe-instances --endpoint-url https://foobar.com/'.split( )) host = self.last_request_headers()['Host'] self.assertEqual(host, 'foobar.com')
def test_can_generate_skeletons_for_all_service_comands(): environ = { 'AWS_DATA_PATH': os.environ['AWS_DATA_PATH'], 'AWS_DEFAULT_REGION': 'us-east-1', 'AWS_ACCESS_KEY_ID': 'access_key', 'AWS_SECRET_ACCESS_KEY': 'secret_key', 'AWS_CONFIG_FILE': '', 'AWS_SHARED_CREDENTIALS_FILE': '', } with mock.patch('os.environ', environ): driver = create_clidriver() help_command = driver.create_help_command() for command_name, command_obj in help_command.command_table.items(): if command_name in PREVIEW_SERVICES: # Skip over any preview services for now. continue sub_help = command_obj.create_help_command() # This avoids command objects like ``PreviewModeCommand`` that # do not exhibit any visible functionality (i.e. provides a command # for the CLI). if hasattr(sub_help, 'command_table'): for sub_name, sub_command in sub_help.command_table.items(): op_help = sub_command.create_help_command() arg_table = op_help.arg_table if 'generate-cli-skeleton' in arg_table: yield _test_gen_skeleton, command_name, sub_name,
def update_token(headers): '''update_token uses HTTP basic authentication to attempt to authenticate given a 401 response. We take as input previous headers, and update them. Parameters ========== response: the http request response to parse for the challenge. ''' try: from awscli.clidriver import create_clidriver except: bot.exit('Please install pip install sregistry[aws]') driver = create_clidriver() aws = driver.session.create_client('ecr') tokens = aws.get_authorization_token() token = tokens['authorizationData'][0]['authorizationToken'] try: token = {"Authorization": "Basic %s" % token} headers.update(token) except Exception: bot.error("Error getting token.") sys.exit(1) return headers
def test_file_param_does_not_exist(self): driver = create_clidriver() rc = driver.main("ec2 describe-instances " "--filters file://does/not/exist.json".split()) self.assertEqual(rc, 255) self.assertIn( "Error parsing parameter '--filters': " "file does not exist: does/not/exist.json", self.stderr.getvalue() )
def _run_cmd(cmd, expected_rc=0): logging.debug("Calling cmd: %s", cmd) # Drivers do not seem to be reusable since the formatters seem to not clear # themselves between runs. This is fine in practice since a driver is only # called once but for tests it means we need to create a new driver for # each test, which is far more heavyweight than it needs to be. Might be # worth seeing if we can make drivers reusable to speed these up generated # tests. driver = create_clidriver() if not isinstance(cmd, list): cmdlist = cmd.split() else: cmdlist = cmd with capture_output() as captured: try: rc = driver.main(cmdlist) except SystemExit as e: # We need to catch SystemExit so that we # can get a proper rc and still present the # stdout/stderr to the test runner so we can # figure out what went wrong. rc = e.code stderr = captured.stderr.getvalue() stdout = captured.stdout.getvalue() assert_equal( rc, expected_rc, "Unexpected rc (expected: %s, actual: %s) for command: %s\n" "stdout:\n%sstderr:\n%s" % ( expected_rc, rc, cmd, stdout, stderr)) return stdout, stderr, rc
def _initiate_session(self, target_instance): log_bold("Starting session in " + target_instance) try: driver = create_clidriver() driver.main(["ssm", "start-session", "--target", target_instance]) except: raise UnrecoverableException("Failed to start session")
def test_no_shadowed_builtins(): """Verify no command params are shadowed or prefixed by the built in param. The CLI parses all command line options into a single namespace. This means that option names must be unique and cannot conflict with the top level params. For example, there's a top level param ``--version``. If an operation for a service also provides a ``--version`` option, it can never be called because we'll assume the user meant the top level ``--version`` param. Beyond just direct shadowing, a param which prefixes a builtin is also effectively shadowed because argparse will expand prefixes of arguments. So `--end` would expand to `--endpoint-url` for instance. In order to ensure this doesn't happen, this test will go through every command table and ensure we're not shadowing any builtins. Also, rather than being a test generator, we're going to just aggregate all the failures in one pass and surface them as a single test failure. """ driver = create_clidriver() help_command = driver.create_help_command() top_level_params = set(driver.create_help_command().arg_table) for command_name, command_obj in help_command.command_table.items(): sub_help = command_obj.create_help_command() if hasattr(sub_help, 'command_table'): yield (_assert_does_not_shadow, command_name, sub_help.command_table, top_level_params)
def test_custom_arg_paramfile(self, mock_handler): mock_paramfile = mock.Mock(autospec=True) mock_paramfile.return_value = None mock_handler.return_value = mock_paramfile driver = create_clidriver() driver.session.register( 'building-argument-table', self.inject_new_param) self.patch_make_request() rc = driver.main( 'ec2 describe-instances --unknown-arg file:///foo'.split()) self.assertEqual(rc, 0) # Make sure uri_param was called mock_paramfile.assert_any_call( event_name='load-cli-arg.ec2.describe-instances.unknown-arg', operation_name='describe-instances', param=mock.ANY, service_name='ec2', value='file:///foo', ) # Make sure it was called with our passed-in URI self.assertEqual( 'file:///foo', mock_paramfile.call_args_list[-1][1]['value'])
def run_aws(ctx, args, **_): # type: (click.Context, Tuple[str, ...], Any) -> None """Execute awscli commands using the version bundled with Runway. This command gives access to the awscli when it might not otherwise be installed (e.g. when using a binary release of Runway). IMPORTANT: When using options shared with Runway "--" must be placed before the awscli command. """ # Ensure runway awscli v1 is not instantiated during `runway kbenv` executions, # which may in turn invoke system awscli v2 via kubeconfigs causing a # "'Namespace' object has no attribute 'cli_binary_format'" error from awscli.clidriver import ( # pylint: disable=import-outside-toplevel create_clidriver, ) if not ctx.obj.debug: # suppress awscli debug logs for name, logger in logging.getLogger( "awscli").manager.loggerDict.items(): if name.startswith("awscli.") and isinstance( logger, logging.Logger): logger.setLevel(logging.ERROR) with SafeHaven(environ={"LC_CTYPE": "en_US.UTF"}): ctx.exit(create_clidriver().main(list(args)))
def awscli_function(source_path, dest_path='', function='ls', profile='default', recursive='no', file=''): """ Method to perform S3 operations AWSCLI package :param source_path: The Path of the Source S3 file (e.g). s3://s3.amazon.com/test/ :param dest_path='': The Path of the Destination S3 file (e.g). s3://s3.amazon.com/test/ :param function='ls': The function to be performed, ls,cp and mv :param profile='default': The S3 credentials profile to be used for the processing :param recursive='no': Incase to repeatdy move/copy files then set it as yes :param file='': The wildcard search string for a file to be processed """ import os from awscli.clidriver import create_clidriver driver = create_clidriver() if recursive == 'yes': rec = '--recursive' else: rec = '' if file <> '': if function == 'ls': flnm = '' dest_path = '' else: flnm = "--exclude '*' --include '" + file+"*'" else: flnm='' prfl = '--profile=' + profile code = 's3 ' + function + ' ' + source_path + ' ' + dest_path + ' ' + flnm + ' ' + rec + ' ' + prfl driver.main(str(code).split()) return True
def test_empty_params_gracefully_handled(self): driver = create_clidriver() # Simulates the equivalent in bash: --identifies "" cmd = 'ses get-identity-dkim-attributes --identities'.split() cmd.append('') rc = driver.main(cmd) self.assertEqual(rc, 0)
def test_custom_arg_paramfile(self, mock_handler): mock_paramfile = mock.Mock(autospec=True) mock_paramfile.return_value = None mock_handler.return_value = mock_paramfile driver = create_clidriver() driver.session.register('building-argument-table', self.inject_new_param) self.patch_make_request() rc = driver.main( 'ec2 describe-instances --unknown-arg file:///foo'.split()) self.assertEqual(rc, 0) # Make sure uri_param was called mock_paramfile.assert_any_call( event_name='load-cli-arg.ec2.describe-instances.unknown-arg', operation_name='describe-instances', param=mock.ANY, service_name='ec2', value='file:///foo', ) # Make sure it was called with our passed-in URI self.assertEqual('file:///foo', mock_paramfile.call_args_list[-1][1]['value'])
def _run_cmd(cmd, expected_rc=0): logging.debug("Calling cmd: %s", cmd) # Drivers do not seem to be reusable since the formatters seem to not clear # themselves between runs. This is fine in practice since a driver is only # called once but for tests it means we need to create a new driver for # each test, which is far more heavyweight than it needs to be. Might be # worth seeing if we can make drivers reusable to speed these up generated # tests. driver = create_clidriver() if not isinstance(cmd, list): cmdlist = cmd.split() else: cmdlist = cmd with capture_output() as captured: try: rc = driver.main(cmdlist) except SystemExit as e: # We need to catch SystemExit so that we # can get a proper rc and still present the # stdout/stderr to the test runner so we can # figure out what went wrong. rc = e.code stderr = captured.stderr.getvalue() stdout = captured.stdout.getvalue() assert_equal( rc, expected_rc, "Unexpected rc (expected: %s, actual: %s) for command: %s\n" "stdout:\n%sstderr:\n%s" % (expected_rc, rc, cmd, stdout, stderr)) return stdout, stderr, rc
def test_file_param_does_not_exist(self): driver = create_clidriver() rc = driver.main('ec2 describe-instances ' '--filters file://does/not/exist.json'.split()) self.assertEqual(rc, 255) self.assertIn("Bad value for argument '--filters': " "file does not exist: does/not/exist.json", self.stderr.getvalue())
def test_default(self): self.driver = create_clidriver() expected_time = 0 stdout, _, _ = self.run_cmd(self.command) json_response = json.loads(stdout) start_time = json_response["builds"][0]["startTime"] self.assertEqual(expected_time, start_time)
def test_plugins_loaded_from_specified_path(self): self.create_config( '[plugins]\n' 'cli_legacy_plugin_path = %s\n' 'myplugin = %s\n' % (self.plugins_site_packages, self.plugin_module_name)) clidriver = create_clidriver() self.assert_plugin_loaded(clidriver)
def _generate_index(filename): # This will eventually be moved into some utility function. index_generator = generator.IndexGenerator( [indexer.create_model_indexer(filename)], ) driver = clidriver.create_clidriver() driver.session.register('building-command-table.main', _ec2_only_command_table) index_generator.generate_index(driver)
def clear_bucket(bucket_name: str) -> int: logging.info('cleaning up S3 bucket: {}'.format(bucket_name)) args = [ '--endpoint', os.environ['AWS_ENDPOINT_S3'], 's3', 'rm', 's3://{}'.format(bucket_name), '--recursive' ] status = clidriver.create_clidriver().main(args) return status
def _generate_command_tests(): driver = create_clidriver() help_command = driver.create_help_command() top_level_params = set(driver.create_help_command().arg_table) for command_name, command_obj in help_command.command_table.items(): sub_help = command_obj.create_help_command() if hasattr(sub_help, 'command_table'): yield command_name, sub_help.command_table, top_level_params
def test_file_param_does_not_exist(self): driver = create_clidriver() rc = driver.main('ec2 describe-instances ' '--filters file://does/not/exist.json'.split()) self.assertEqual(rc, 255) self.assertIn("Error parsing parameter '--filters': " "file does not exist: does/not/exist.json", self.stderr.getvalue())
def test_aws_with_region(self): driver = create_clidriver() driver.main('ec2 describe-instances --region us-east-1'.split()) host = self.last_request_headers()['Host'] self.assertEqual(host, 'ec2.us-east-1.amazonaws.com') driver.main('ec2 describe-instances --region us-west-2'.split()) host = self.last_request_headers()['Host'] self.assertEqual(host, 'ec2.us-west-2.amazonaws.com')
def test_http_file_param_does_not_exist(self): driver = create_clidriver() rc = driver.main('ec2 describe-instances ' '--filters http://does/not/exist.json'.split()) self.assertEqual(rc, 255) self.assertIn("Bad value for argument '--filters': " "Unable to retrieve http://does/not/exist.json: " "received non 200 status code of 404", self.stderr.getvalue())
def _do_run(self, cmdline): driver = create_clidriver() self._session_stubber.register(driver.session) rc = driver.main(cmdline) self._session_stubber.assert_no_remaining_responses() runner_result = CLIRunnerResult(rc) runner_result.aws_requests = copy.copy( self._session_stubber.received_aws_requests) return runner_result
def test_default(self): self.driver = create_clidriver() expected_time = datetime.datetime.fromtimestamp(0).replace( tzinfo=tzlocal()).isoformat() stdout, _, _ = self.run_cmd(self.command) json_response = json.loads(stdout) start_time = json_response["builds"][0]["startTime"] self.assertEqual(expected_time, start_time)
def awsclis3(args: List[str]) -> Tuple[int, str]: pre_args = [ # '--debug', '--endpoint', endpoint_aws_s3() ] driver = clidriver.create_clidriver() status = driver.main(pre_args + args) return status
def setUpClass(cls): cls.db_connection = db.DatabaseConnection(":memory:") index_generator = generator.IndexGenerator([ ModelIndexer(cls.db_connection), APICallIndexer(cls.db_connection) ], ) driver = clidriver.create_clidriver() driver.session.register('building-command-table.main', _ddb_only_command_table) index_generator.generate_index(driver)
def test_custom_arg_no_paramfile(self): driver = create_clidriver() driver.session.register( 'building-argument-table', self.inject_new_param_no_paramfile) self.patch_make_request() rc = driver.main( 'ec2 describe-instances --unknown-arg file:///foo'.split()) self.assertEqual(rc, 0)
def test_file_param_does_not_exist(self): driver = create_clidriver() rc = driver.main('ec2 describe-instances ' '--filters file://does/not/exist.json'.split()) self.assertEqual(rc, 255) error_msg = self.stderr.getvalue() self.assertIn("Error parsing parameter '--filters': " "Unable to load paramfile file://does/not/exist.json", error_msg) self.assertIn("No such file or directory", error_msg)
def test_looks_in_all_specified_paths(self): nonexistent_dir = os.path.join(self.files.rootdir, 'no-exist') plugin_path = os.pathsep.join( [nonexistent_dir, self.plugins_site_packages]) self.create_config('[plugins]\n' 'cli_legacy_plugin_path = %s\n' 'myplugin = %s\n' % (plugin_path, self.plugin_module_name)) clidriver = create_clidriver() self.assert_plugin_loaded(clidriver)
def test_none(self): self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'none', '[default]\ncli_timestamp_format = wire\n') self.driver = create_clidriver() expected_time = 0 stdout, _, _ = self.run_cmd(self.command) json_response = json.loads(stdout) start_time = json_response["builds"][0]["startTime"] self.assertEqual(expected_time, start_time)
def copy_webui_code(event): is_processed = True try: logger.info('started processing s3 data event: ' + str(event)) driver = create_clidriver() destinationBucketPermission = event['ResourceProperties'][ 'DestinationBucketPermission'] copy_command = 's3 cp s3://{source_path} s3://{destination_path} --recursive --acl {destinationBucketPermission}' source_bucket = event['ResourceProperties']['SourceBucket'] destination_bucket = event['ResourceProperties']['DestinationBucket'] if not source_bucket.endswith('/'): source_bucket = source_bucket + '/' if not destination_bucket.endswith('/'): destination_bucket = destination_bucket + '/' source_path = source_bucket + event['ResourceProperties']['SourceKey'] destination_path = destination_bucket + event['ResourceProperties'][ 'DestinationKey'] logger.info('started copy s3 data: source path:%s destination_path:%s', source_path, destination_path) formatted_copy_command = copy_command.format( source_path=source_path, destination_path=destination_path, destinationBucketPermission=destinationBucketPermission) logger.info('formatted copy command:%s', formatted_copy_command) driver.main(formatted_copy_command.split()) #Fix for index.html content-type index_copy_command = 's3 cp s3://{source_path}/index.html s3://{destination_path} --acl {destinationBucketPermission} --metadata-directive REPLACE --content-type text/html' formatted_index_copy_command = index_copy_command.format( source_path=source_path, destination_path=destination_path, destinationBucketPermission=destinationBucketPermission) logger.info('formatted index copy command:%s', formatted_index_copy_command) driver.main(formatted_index_copy_command.split()) bot_name = event['ResourceProperties']['BotName'] pool_id = event['ResourceProperties']['CognitoPoolId'] static_host_bucket = event['ResourceProperties']['DestinationBucket'] logger.info('upload statichost_config:%s %s %s %s %s', bot_name, region_name, pool_id, static_host_bucket, destinationBucketPermission) is_processed = upload_statichost_config(bot_name, region_name, pool_id, static_host_bucket, destinationBucketPermission) is_processed = upload_lex_ui_load_config(bot_name, region_name, pool_id, static_host_bucket, destinationBucketPermission) is_processed = upload_lex_mobile_config(bot_name, region_name, pool_id, static_host_bucket, destinationBucketPermission) logger.info('completed s3 data processing ') except Exception as e: logger.error('Failed to copy static web hosting source code' + str(e)) is_processed = False return is_processed
def setUp(self): super(TestPreviewMode, self).setUp() self.stderr = six.StringIO() self.stderr_patch = mock.patch('sys.stderr', self.stderr) self.stderr_patch.start() self.driver = create_clidriver() self.full_config = {} # Implementation detail, but we want to patch out the # session config, as that's the only way to control # preview services. self.driver.session._config = self.full_config
def test_none(self): self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'none', '[default]\ncli_timestamp_format = none\n') self.driver = create_clidriver() expected_time = 0 stdout, _, _ = self.run_cmd(self.command) json_response = json.loads(stdout) start_time = json_response["builds"][0]["startTime"] self.assertEqual(expected_time, start_time)
def test_integration_fips_using_cli_driver(self, dt_mock, stdout_mock): dt_mock.utcnow.return_value = datetime(2010, 10, 8) driver = create_clidriver() rc = driver.main('codecommit credential-helper get'.split()) output = stdout_mock.getvalue().strip() self.assertEquals( ('username=foo\n' 'password=20101008T000000Z' '500037cb3514b3fe01ebcda7c80973f5b4c0d8199a7a6563b85fd6edf272d460'), output) self.assertEquals(0, rc)
def test_integration_vpc_using_cli_driver(self, dt_mock, stdout_mock): dt_mock.utcnow.return_value = datetime(2010, 10, 8) driver = create_clidriver() rc = driver.main('codecommit credential-helper get'.split()) output = stdout_mock.getvalue().strip() self.assertEquals( ('username=foo\n' 'password=20101008T000000Z' '9ed987cc6336c3de2d9f06b9236c7a9fd76b660b080db15983290e636dbfbd6b'), output) self.assertEquals(0, rc)
def test_iso(self): self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'iso', '[default]\ncli_timestamp_format = iso8601\n') self.driver = create_clidriver() expected_time = datetime.datetime.fromtimestamp(0).replace( tzinfo=tzlocal()).isoformat() stdout, _, _ = self.run_cmd(self.command) json_response = json.loads(stdout) start_time = json_response["builds"][0]["startTime"] self.assertEqual(expected_time, start_time)
def test_integration_using_cli_driver(self, dt_mock, stdout_mock): dt_mock.utcnow.return_value = datetime(2010, 10, 8) driver = create_clidriver() rc = driver.main('codecommit credential-helper get'.split()) output = stdout_mock.getvalue().strip() self.assertEquals( ('username=foo\n' 'password=20101008T000000Z' '7dc259e2d505af354a1219b9bcd784bd384dc706efa0d9aefc571f214be4c89c'), output) self.assertEquals(0, rc)
def test_override_calling_command_error(self): self.driver = create_clidriver() # Make a function that will return an error. The handler will cause # an error to be returned and later raised. def override_with_error(**kwargs): return ValueError() self.driver.session.register('calling-command', override_with_error) # An exception should be thrown as a result of the handler, which # will result in 255 rc. rc = self.driver.main('ec2 describe-instances'.split()) self.assertEqual(rc, 255)
def test_override_calling_command(self): self.driver = create_clidriver() # Make a function that will return an override such that its value # is used over whatever is returned by the invoker which is usually # zero. def override_with_rc(**kwargs): return 20 self.driver.session.register('calling-command', override_with_rc) rc = self.driver.main('ec2 describe-instances'.split()) # Check that the overriden rc is as expected. self.assertEqual(rc, 20)
def assert_params_for_cmd(self, cmd, params, expected_rc=0): logging.debug("Calling cmd: %s", cmd) driver = create_clidriver() driver.session.register("before-call", self.before_call) if not isinstance(cmd, list): cmdlist = cmd.split() else: cmdlist = cmd rc = driver.main(cmdlist) self.assertEqual( rc, expected_rc, "Unexpected rc (expected: %s, actual: %s) for command: %s" % (expected_rc, rc, cmd) ) self.assertDictEqual(params, self.last_params) return rc
def test_aws_configure_in_error_message_no_credentials(self): driver = create_clidriver() def raise_exception(*args, **kwargs): raise NoCredentialsError() driver.session.register( 'building-command-table', lambda command_table, **kwargs: \ command_table.__setitem__('ec2', raise_exception)) with mock.patch('sys.stderr') as f: driver.main('ec2 describe-instances'.split()) self.assertEqual( f.write.call_args_list[0][0][0], 'Unable to locate credentials. ' 'You can configure credentials by running "aws configure".')
def test_can_generate_skeletons_for_all_service_comands(): driver = create_clidriver() help_command = driver.create_help_command() for command_name, command_obj in help_command.command_table.items(): sub_help = command_obj.create_help_command() # This avoids command objects like ``PreviewModeCommand`` that # do not exhibit any visible functionality (i.e. provides a command # for the CLI). if hasattr(sub_help, 'command_table'): for sub_name, sub_command in sub_help.command_table.items(): op_help = sub_command.create_help_command() arg_table = op_help.arg_table if 'generate-cli-skeleton' in arg_table: yield _test_gen_skeleton, command_name, sub_name
def test_custom_command_paramfile(self): with mock.patch('awscli.handlers.uri_param', return_value=None) as uri_param_mock: driver = create_clidriver() driver.session.register( 'building-command-table', self.inject_command) self.patch_make_request() rc = driver.main( 'ec2 foo --bar file:///foo'.split()) self.assertEqual(rc, 0) uri_param_mock.assert_called()
def test_no_shadowed_builtins(): """Verify no command params are shadowed or prefixed by the built in param. The CLI parses all command line options into a single namespace. This means that option names must be unique and cannot conflict with the top level params. For example, there's a top level param ``--version``. If an operation for a service also provides a ``--version`` option, it can never be called because we'll assume the user meant the top level ``--version`` param. Beyond just direct shadowing, a param which prefixes a builtin is also effectively shadowed because argparse will expand prefixes of arguments. So `--end` would expand to `--endpoint-url` for instance. In order to ensure this doesn't happen, this test will go through every command table and ensure we're not shadowing any builtins. Also, rather than being a test generator, we're going to just aggregate all the failures in one pass and surface them as a single test failure. """ driver = create_clidriver() help_command = driver.create_help_command() top_level_params = set(driver.create_help_command().arg_table) errors = [] for command_name, command_obj in help_command.command_table.items(): sub_help = command_obj.create_help_command() if hasattr(sub_help, 'command_table'): for sub_name, sub_command in sub_help.command_table.items(): op_help = sub_command.create_help_command() arg_table = op_help.arg_table for arg_name in arg_table: if any(p.startswith(arg_name) for p in top_level_params): # Then we are shadowing or prefixing a top level # argument. errors.append( 'Shadowing/Prefixing a top level option: ' '%s.%s.%s' % (command_name, sub_name, arg_name)) if errors: raise AssertionError('\n' + '\n'.join(errors))
def test_custom_arg_paramfile(self): with mock.patch('awscli.handlers.uri_param', return_value=None) as uri_param_mock: driver = create_clidriver() driver.session.register( 'building-argument-table', self.inject_new_param) self.patch_make_request() rc = driver.main( 'ec2 describe-instances --unknown-arg file:///foo'.split()) self.assertEqual(rc, 0) # Make sure uri_param was called uri_param_mock.assert_called() # Make sure it was called with our passed-in URI self.assertEqual('file:///foo', uri_param_mock.call_args_list[-1][1]['value'])
def setUp(self): self.clidriver = create_clidriver() self.session = self.clidriver.session self.argument_table = {} self.service_model = self.session.get_service_model('config') self.operation_model = self.service_model.operation_model( 'PutConfigurationRecorder') configuration_recorder_model = self.operation_model.\ input_shape.members['ConfigurationRecorder'] self.old_configuration_recorder_argument = CLIArgument( name='configuration-recorder', argument_model=configuration_recorder_model, operation_model=self.operation_model, is_required=True, event_emitter=self.session.get_component('event_emitter'), serialized_name='ConfigurationRecorder' ) self.argument_table['configuration-recorder'] = \ self.old_configuration_recorder_argument
def test_custom_command_schema(self): driver = create_clidriver() driver.session.register( 'building-command-table', self.inject_command_schema) self.patch_make_request() # Test single shorthand item rc = driver.main( 'ec2 foo --bar Name=test,Count=4'.split()) self.assertEqual(rc, 0) # Test shorthand list of items with optional values rc = driver.main( 'ec2 foo --bar Name=test,Count=4 Name=another'.split()) self.assertEqual(rc, 0) # Test missing require shorthand item rc = driver.main( 'ec2 foo --bar Count=4'.split()) self.assertEqual(rc, 255) # Test extra unknown shorthand item rc = driver.main( 'ec2 foo --bar Name=test,Unknown='.split()) self.assertEqual(rc, 255) # Test long form JSON rc = driver.main( 'ec2 foo --bar {"Name":"test","Count":4}'.split()) self.assertEqual(rc, 0) # Test malformed long form JSON rc = driver.main( 'ec2 foo --bar {"Name":"test",Count:4}'.split()) self.assertEqual(rc, 255)
def test_custom_command_paramfile(self): with mock.patch('awscli.handlers.uri_param', return_value=None) as uri_param_mock: driver = create_clidriver() driver.session.register( 'building-command-table', self.inject_command) self.patch_make_request() rc = driver.main( 'ec2 foo --bar file:///foo'.split()) self.assertEqual(rc, 0) uri_param_mock.assert_any_call( event_name='load-cli-arg.custom.foo.bar', operation_name='foo', param=mock.ANY, service_name='custom', value='file:///foo', )
def run_cmd(self, cmd, expected_rc=0): logging.debug("Calling cmd: %s", cmd) self.patch_make_request() driver = create_clidriver() driver.session.register('before-call', self.before_call) if not isinstance(cmd, list): cmdlist = cmd.split() else: cmdlist = cmd captured_stderr = six.StringIO() captured_stdout = six.StringIO() with mock.patch('sys.stderr', captured_stderr): with mock.patch('sys.stdout', captured_stdout): rc = driver.main(cmdlist) stderr = captured_stderr.getvalue() stdout = captured_stdout.getvalue() self.assertEqual( rc, expected_rc, "Unexpected rc (expected: %s, actual: %s) for command: %s" % ( expected_rc, rc, cmd)) return stdout, stderr, rc