def test_cli_driver_with_no_paginate(self): driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo --no-paginate'.split()) # Because --no-paginate was used, op.call should be used instead of # op.paginate. self.assertIn(mock.call.call(mock.ANY, bucket='foo'), self.session.operation.method_calls)
def test_cli_driver_changes_args(self): emitter = HierarchicalEmitter() emitter.register("process-cli-arg.s3.list-objects", self.serialize_param) self.session.emitter = emitter driver = CLIDriver(session=self.session) driver.main("s3 list-objects --bucket foo".split()) self.assertIn(mock.call.paginate(mock.ANY, bucket="foo-altered!"), self.session.operation.method_calls)
def test_ctrl_c_is_handled(self): driver = CLIDriver(session=self.session) fake_client = mock.Mock() fake_client.list_objects.side_effect = KeyboardInterrupt fake_client.can_paginate.return_value = False driver.session.create_client = mock.Mock(return_value=fake_client) rc = driver.main('s3 list-objects --bucket foo'.split()) self.assertEqual(rc, 130)
def test_unknown_command_suggests_help(self): driver = CLIDriver(session=self.session) # We're catching SystemExit here because this is raised from the bowels # of argparser so short of patching the ArgumentParser's exit() method, # we can just catch SystemExit. with self.assertRaises(SystemExit): # Note the typo in 'list-objects' driver.main('s3 list-objecst --bucket foo --unknown-arg foo'.split()) # Tell the user what went wrong. self.assertIn("Invalid choice: 'list-objecst'", self.stderr.getvalue()) # Offer the user a suggestion. self.assertIn("maybe you meant:\n\n * list-objects", self.stderr.getvalue())
def test_expected_events_are_emitted_in_order(self): self.emitter.emit.return_value = [] self.session.emitter = self.emitter driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo'.split()) self.assert_events_fired_in_order([ # Events fired while parser is being created. 'parser-created.main', 'parser-created.s3', 'parser-created.s3-list-objects', 'process-cli-arg.s3.list-objects', ])
def test_expected_events_are_emitted_in_order(self): self.emitter.emit.return_value = [] self.session.emitter = self.emitter driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo'.split()) self.assert_events_fired_in_order([ # Events fired while parser is being created. 'building-command-table.main', 'building-top-level-params', 'top-level-args-parsed', 'building-command-table.s3', 'building-argument-table.s3.ListObjects', 'process-cli-arg.s3.list-objects', ])
def test_expected_events_are_emitted_in_order(self): driver = CLIDriver(session=self.session) self.emitter.register( 'building-command-table.s3', self.inject_command) driver.main('s3 foo'.split()) self.assert_events_fired_in_order([ 'building-command-table.main', 'building-top-level-params', 'top-level-args-parsed', 'session-initialized', 'building-command-table.s3', 'building-command-table.foo', 'building-arg-table.foo', 'before-building-argument-table-parser.s3.foo' ])
def test_expected_events_are_emitted_in_order(self): self.emitter.emit.return_value = [] self.session.emitter = self.emitter driver = CLIDriver(session=self.session) driver.main("s3 list-objects --bucket foo".split()) self.assert_events_fired_in_order( [ # Events fired while parser is being created. "building-command-table.main", "building-top-level-params", "top-level-args-parsed", "building-command-table.s3", "building-argument-table.s3.list-objects", "operation-args-parsed.s3.list-objects", "load-cli-arg.s3.list-objects", "process-cli-arg.s3.list-objects", ] )
def test_create_help_command(self): # When we generate the HTML docs, we don't actually run # commands, we just call the create_help_command methods. # We want to make sure that in this case, the corresponding # building-command-table events are fired. # The test above will prove that is true when running a command. # This test proves it is true when generating the HTML docs. self.emitter.emit.return_value = [] self.session.emitter = self.emitter driver = CLIDriver(session=self.session) main_hc = driver.create_help_command() command = main_hc.command_table['s3'] command.create_help_command() self.assert_events_fired_in_order([ # Events fired while parser is being created. 'building-command-table.main', 'building-top-level-params', 'building-command-table.s3', ])
def test_windows_style_search_path(self): driver = CLIDriver() # Because the os.environ patching happens at import time, # we have to force a reimport of the module to test our changes. six.moves.reload_module(awscli) # Our two overrides should be the last two elements in the search path. search_paths = driver.session.get_component( 'data_loader').search_paths self.assertIn('c:\\foo', search_paths) self.assertIn('c:\\bar', search_paths)
def test_expected_events_are_emitted_in_order(self): self.emitter.emit.return_value = [] self.session.emitter = self.emitter driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo'.split()) self.assert_events_fired_in_order([ # Events fired while parser is being created. 'building-command-table.main', 'building-top-level-params', 'top-level-args-parsed', 'session-initialized', 'building-command-table.s3', 'building-argument-table.s3.list-objects', 'before-building-argument-table-parser.s3.list-objects', 'operation-args-parsed.s3.list-objects', 'load-cli-arg.s3.list-objects.bucket', 'process-cli-arg.s3.list-objects', 'load-cli-arg.s3.list-objects.key', 'calling-command.s3.list-objects' ])
def test_error_unicode(self): # We need a different type for Py3 and Py2 because on Py3 six.StringIO # doesn't let us set the encoding and returns a string. if six.PY3: stderr_b = io.BytesIO() stderr = io.TextIOWrapper(stderr_b, encoding="UTF-8") else: stderr = stderr_b = six.StringIO() stderr.encoding = "UTF-8" driver = CLIDriver(session=self.session) fake_client = mock.Mock() fake_client.list_objects.side_effect = Exception(u"☃") fake_client.can_paginate.return_value = False driver.session.create_client = mock.Mock(return_value=fake_client) with mock.patch("sys.stderr", stderr): with mock.patch("locale.getpreferredencoding", lambda: "UTF-8"): rc = driver.main('s3 list-objects --bucket foo'.split()) stderr.flush() self.assertEqual(rc, 255) self.assertEqual(stderr_b.getvalue().strip(), u"☃".encode("UTF-8"))
def test_expected_events_are_emitted_in_order(self): self.emitter.emit.return_value = [] self.session.emitter = self.emitter driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo'.split()) self.assert_events_fired_in_order([ # Events fired while parser is being created. 'building-command-table.main', 'building-top-level-params', 'top-level-args-parsed', 'session-initialized', 'building-command-table.s3', 'building-argument-table.s3.list-objects', 'before-building-argument-table-parser.s3.list-objects', 'operation-args-parsed.s3.list-objects', 'load-cli-arg.s3.list-objects.bucket', 'process-cli-arg.s3.list-objects', 'load-cli-arg.s3.list-objects.marker', 'load-cli-arg.s3.list-objects.max-keys', 'calling-command.s3.list-objects' ])
def setUp(self): self.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': '', } self.environ_patch = mock.patch('os.environ', self.environ) self.environ_patch.start() emitter = HierarchicalEmitter() session = Session(EnvironmentVariables, emitter, loader=_LOADER) load_plugins({}, event_hooks=emitter) driver = CLIDriver(session=session) self.session = session self.driver = driver
def reconfigure(self, profile=None): if profile is None: return # See awscli/clidriver.py session = Session() load_plugins(session.full_config.get('plugins', {}), event_hooks=session.get_component('event_emitter')) driver = CLIDriver(session=session) driver._command_table = driver._build_command_table() driver._command_table["configure"] = ConfigureCommand( session, prompter=InteractivePrompter(self.console) ) driver.main(args=["configure", "--profile", profile])
def test_no_profile(self): driver = CLIDriver(session=self.session) driver.main("s3 list-objects --bucket foo".split()) self.assertEqual(driver.session.profile, None)
def ec2_via_awscli(args): d = CLIDriver() stat = d.main(['--output', 'json', 'ec2'] + args) sys.stdout.flush() sys.stderr.flush() return stat
def test_error_logger(self): driver = CLIDriver(session=self.session) driver.main("s3 list-objects --bucket foo --profile foo".split()) expected = {"log_level": logging.ERROR, "logger_name": "awscli"} self.assertEqual(driver.session.stream_logger_args[1], expected)
def test_error_logger(self): driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo --profile foo'.split()) expected = {'log_level': logging.ERROR, 'logger_name': 'awscli'} self.assertEqual(driver.session.stream_logger_args[1], expected)
def test_paginate_rc(self): driver = CLIDriver(session=self.session) rc = driver.main('s3 list-objects --bucket foo'.split()) self.assertEqual(rc, 0)
def test_region_is_set_for_session(self): driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo --region us-east-2'.split()) self.assertEqual(driver.session.get_config_variable('region'), 'us-east-2')
def setUp(self): self.session = FakeSession() self.driver = CLIDriver(session=self.session)
class TestCliDriver(unittest.TestCase): def setUp(self): self.session = FakeSession() self.driver = CLIDriver(session=self.session) def test_session_can_be_passed_in(self): self.assertEqual(self.driver.session, self.session) def test_paginate_rc(self): rc = self.driver.main('s3 list-objects --bucket foo'.split()) self.assertEqual(rc, 0) def test_no_profile(self): self.driver.main('s3 list-objects --bucket foo'.split()) self.assertEqual(self.driver.session.profile, None) def test_profile(self): self.driver.main('s3 list-objects --bucket foo --profile foo'.split()) self.assertEqual(self.driver.session.profile, 'foo') def test_region_is_set_for_session(self): driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo --region us-east-2'.split()) self.assertEqual(driver.session.get_config_variable('region'), 'us-east-2') def test_error_logger(self): self.driver.main('s3 list-objects --bucket foo --profile foo'.split()) expected = {'log_level': logging.ERROR, 'logger_name': 'awscli'} self.assertEqual(self.driver.session.stream_logger_args[1], expected) def test_ctrl_c_is_handled(self): fake_client = mock.Mock() fake_client.list_objects.side_effect = KeyboardInterrupt fake_client.can_paginate.return_value = False self.driver.session.create_client = mock.Mock(return_value=fake_client) rc = self.driver.main('s3 list-objects --bucket foo'.split()) self.assertEqual(rc, 130) def test_error_unicode(self): # We need a different type for Py3 and Py2 because on Py3 six.StringIO # doesn't let us set the encoding and returns a string. if six.PY3: stderr_b = io.BytesIO() stderr = io.TextIOWrapper(stderr_b, encoding="UTF-8") else: stderr = stderr_b = six.StringIO() stderr.encoding = "UTF-8" fake_client = mock.Mock() fake_client.list_objects.side_effect = Exception(u"☃") fake_client.can_paginate.return_value = False self.driver.session.create_client = mock.Mock(return_value=fake_client) with mock.patch("sys.stderr", stderr): with mock.patch("locale.getpreferredencoding", lambda: "UTF-8"): rc = self.driver.main('s3 list-objects --bucket foo'.split()) stderr.flush() self.assertEqual(rc, 255) self.assertEqual(stderr_b.getvalue().strip(), u"☃".encode("UTF-8")) def test_can_access_subcommand_table(self): table = self.driver.subcommand_table self.assertEqual(list(table), self.session.get_available_services()) def test_can_access_argument_table(self): arg_table = self.driver.arg_table expected = list(GET_DATA['cli']['options']) self.assertEqual(list(arg_table), expected)
def test_session_can_be_passed_in(self): driver = CLIDriver(session=self.session) self.assertEqual(driver.session, self.session)
def test_unknown_params_raises_error(self): driver = CLIDriver(session=self.session) rc = driver.main('s3 list-objects --bucket foo --unknown-arg foo'.split()) self.assertEqual(rc, 255) self.assertIn('Unknown options', self.stderr.getvalue())
def test_profile(self): driver = CLIDriver(session=self.session) driver.main('s3 list-objects --bucket foo --profile foo'.split()) self.assertEqual(driver.session.profile, 'foo')
def create_arg_table(self): return CLIDriver().create_help_command().arg_table