class TestTopicTagDB(unittest.TestCase): def setUp(self): self.topic_tag_db = TopicTagDB() self.file_creator = FileCreator() def tearDown(self): self.file_creator.remove_all()
class TestStreamingOutput(BaseAWSCommandParamsTest): def setUp(self): super(TestStreamingOutput, self).setUp() self.files = FileCreator() def tearDown(self): super(TestStreamingOutput, self).tearDown() self.files.remove_all() def test_get_media_streaming_output(self): cmdline = ( 'kinesis-video-media get-media --stream-name test-stream ' '--start-selector StartSelectorType=EARLIEST %s' ) self.parsed_response = { 'ContentType': 'video/webm', 'Payload': six.BytesIO(b'testbody') } outpath = self.files.full_path('outfile') params = { 'StartSelector': {'StartSelectorType': 'EARLIEST'}, 'StreamName': 'test-stream' } self.assert_params_for_cmd(cmdline % outpath, params) with open(outpath, 'rb') as outfile: self.assertEqual(outfile.read(), b'testbody')
class TestStreamingOutput(BaseAWSCommandParamsTest): def setUp(self): super(TestStreamingOutput, self).setUp() self.files = FileCreator() def tearDown(self): super(TestStreamingOutput, self).tearDown() self.files.remove_all() def test_get_media_streaming_output(self): cmdline = ('kinesis-video-media get-media --stream-name test-stream ' '--start-selector StartSelectorType=EARLIEST %s') self.parsed_response = { 'ContentType': 'video/webm', 'Payload': six.BytesIO(b'testbody') } outpath = self.files.full_path('outfile') params = { 'StartSelector': { 'StartSelectorType': 'EARLIEST' }, 'StreamName': 'test-stream' } self.assert_params_for_cmd(cmdline % outpath, params) with open(outpath, 'rb') as outfile: self.assertEqual(outfile.read(), b'testbody')
class TestIsSpecialFile(unittest.TestCase): def setUp(self): self.files = FileCreator() self.filename = 'foo' def tearDown(self): self.files.remove_all() def test_is_character_device(self): file_path = os.path.join(self.files.rootdir, self.filename) self.files.create_file(self.filename, contents='') with mock.patch('stat.S_ISCHR') as mock_class: mock_class.return_value = True self.assertTrue(is_special_file(file_path)) def test_is_block_device(self): file_path = os.path.join(self.files.rootdir, self.filename) self.files.create_file(self.filename, contents='') with mock.patch('stat.S_ISBLK') as mock_class: mock_class.return_value = True self.assertTrue(is_special_file(file_path)) def test_is_fifo(self): file_path = os.path.join(self.files.rootdir, self.filename) mode = 0o600 | stat.S_IFIFO os.mknod(file_path, mode) self.assertTrue(is_special_file(file_path)) def test_is_socket(self): file_path = os.path.join(self.files.rootdir, self.filename) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(file_path) self.assertTrue(is_special_file(file_path))
class TestHelpCommandBase(unittest.TestCase): def setUp(self): self.session = mock.Mock() self.file_creator = FileCreator() def tearDown(self): self.file_creator.remove_all()
class TestAPIVersions(BaseAWSCommandParamsTest): def setUp(self): super(TestAPIVersions, self).setUp() self.files = FileCreator() # We just pick ec2 because it is a service that actually has # multiple api versions. self.service_name = 'ec2' self.api_version = '2014-10-01' config_contents = ('[default]\n' 'api_versions =\n' ' %s = %s\n' % (self.service_name, self.api_version)) self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'myconfig', config_contents) self.driver = create_clidriver() def tearDown(self): super(TestAPIVersions, self).tearDown() self.files.remove_all() def test_command_send_correct_api_version(self): cmdline = 'ec2 describe-instances' self.run_cmd(cmdline) # Make sure that the correct api version is used for the client # by checking the version that was sent in the request. self.assertEqual(self.last_params['Version'], self.api_version) def test_command_interface_reflects_api_version(self): # Take an arbitrary command such as describe-nat-gateways that is not # in the 2014-10-01 EC2 API version and make sure its CLI command # interface is not available as well. cmdline = 'ec2 describe-nat-gateways' _, stderr, _ = self.run_cmd(cmdline, expected_rc=2) self.assertIn("Invalid choice: 'describe-nat-gateways'", stderr)
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(ValueError): 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 TestAPIVersions(BaseAWSCommandParamsTest): def setUp(self): super(TestAPIVersions, self).setUp() self.files = FileCreator() # We just pick ec2 because it is a service that actually has # multiple api versions. self.service_name = 'ec2' self.api_version = '2014-10-01' config_contents = ( '[default]\n' 'api_versions =\n' ' %s = %s\n' % (self.service_name, self.api_version) ) self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'myconfig', config_contents) self.driver = create_clidriver() def tearDown(self): super(TestAPIVersions, self).tearDown() self.files.remove_all() def test_command_send_correct_api_version(self): cmdline = 'ec2 describe-instances' self.run_cmd(cmdline) # Make sure that the correct api version is used for the client # by checking the version that was sent in the request. self.assertEqual(self.last_params['Version'], self.api_version) def test_command_interface_reflects_api_version(self): # Take an arbitrary command such as describe-nat-gateways that is not # in the 2014-10-01 EC2 API version and make sure its CLI command # interface is not available as well. cmdline = 'ec2 describe-nat-gateways' _, stderr, _ = self.run_cmd(cmdline, expected_rc=2) self.assertIn("Invalid choice: 'describe-nat-gateways'", stderr)
class TestSyncCommand(BaseAWSCommandParamsTest): prefix = 's3 sync ' def setUp(self): super(TestSyncCommand, self).setUp() self.files = FileCreator() def tearDown(self): super(TestSyncCommand, self).tearDown() self.files.remove_all() def test_website_redirect_ignore_paramfile(self): full_path = self.files.create_file('foo.txt', 'mycontent') cmdline = '%s %s s3://bucket/key.txt --website-redirect %s' % \ (self.prefix, self.files.rootdir, 'http://someserver') self.parsed_responses = [{ "CommonPrefixes": [], "Contents": [] }, { 'ETag': '"c8afdb36c52cf4727836669019e69222"' }] self.run_cmd(cmdline, expected_rc=0) # The only operations we should have called are ListObjects/PutObject. self.assertEqual(len(self.operations_called), 2, self.operations_called) self.assertEqual(self.operations_called[0][0].name, 'ListObjects') self.assertEqual(self.operations_called[1][0].name, 'PutObject') # Make sure that the specified web address is used as opposed to the # contents of the web address when uploading the object self.assertEqual( self.operations_called[1][1]['website_redirect_location'], 'http://someserver')
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.assertEquals(filename, resolve_given_outfile_path(filename)) def test_works_with_relative_filename(self): filename = "../valid" self.assertEquals(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(ValueError): 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 TestArgsResolution(BaseAWSCommandParamsTest): def setUp(self): super(TestArgsResolution, self).setUp() self.files = FileCreator() config_contents = ( '[profile bar]\n' 'region = us-west-2\n' ) self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'myconfig', config_contents) self.driver = create_clidriver() def tearDown(self): super(TestArgsResolution, self).tearDown() self.files.remove_all() def test_profile_resolution_order(self): self.environ['AWS_PROFILE'] = 'foo' self.parsed_responses = [{"Reservations": []}] self.run_cmd('--profile bar ec2 describe-instances', expected_rc=0) self.assertEqual(self.driver.session.profile, 'bar') def test_can_get_version_with_non_existent_profile(self): self.environ['AWS_PROFILE'] = 'foo' # ProfileNotFound exception shouldn't be raised self.run_cmd('--version', expected_rc=0)
def test_make_zip(self): test_file_creator = FileCreator() test_file_creator.append_file( 'index.js', 'exports handler = (event, context, callback) => {callback(null, event);}' ) dirname = test_file_creator.rootdir expected_files = {'index.js'} random_name = ''.join( random.choice(string.ascii_letters) for _ in range(10)) outfile = os.path.join(tempfile.gettempdir(), random_name) zipfile_name = None try: zipfile_name = make_zip(outfile, dirname) test_zip_file = zipfile.ZipFile(zipfile_name, "r") with closing(test_zip_file) as zf: files_in_zip = set() for info in zf.infolist(): files_in_zip.add(info.filename) self.assertEquals(files_in_zip, expected_files) finally: if zipfile_name: os.remove(zipfile_name) test_file_creator.remove_all()
class BaseSSOTest(BaseAWSCommandParamsTest): def setUp(self): super(BaseSSOTest, self).setUp() self.files = FileCreator() self.start_url = 'https://mysigin.com' self.sso_region = 'us-west-2' self.account = '012345678912' self.role_name = 'SSORole' self.config_file = self.files.full_path('config') self.environ['AWS_CONFIG_FILE'] = self.config_file self.set_config_file_content() self.access_token = 'foo.token.string' def tearDown(self): super(BaseSSOTest, self).tearDown() self.files.remove_all() def set_config_file_content(self, content=None): if content is None: content = ('[default]\n' 'sso_start_url=%s\n' 'sso_region=%s\n' 'sso_role_name=%s\n' 'sso_account_id=%s\n' % (self.start_url, self.sso_region, self.role_name, self.account)) self.files.create_file(self.config_file, content) # We need to recreate the driver (which includes its session) in order # for the config changes to be pulled in by the session. self.driver = create_clidriver()
def test_make_zip(self): test_file_creator = FileCreator() test_file_creator.append_file('index.js', 'exports handler = (event, context, callback) => {callback(null, event);}') dirname = test_file_creator.rootdir expected_files = set(['index.js']) random_name = ''.join(random.choice(string.ascii_letters) for _ in range(10)) outfile = os.path.join(tempfile.gettempdir(), random_name) zipfile_name = None try: zipfile_name = make_zip(outfile, dirname) test_zip_file = zipfile.ZipFile(zipfile_name, "r") with closing(test_zip_file) as zf: files_in_zip = set() for info in zf.infolist(): files_in_zip.add(info.filename) self.assertEquals(files_in_zip, expected_files) finally: if zipfile_name: os.remove(zipfile_name) test_file_creator.remove_all()
class TestGetGameSessionLogCommand(unittest.TestCase): def setUp(self): self.create_client_patch = mock.patch( 'botocore.session.Session.create_client') self.mock_create_client = self.create_client_patch.start() self.session = get_session() self.client = mock.Mock() self.mock_create_client.return_value = self.client self.cmd = GetGameSessionLogCommand(self.session) self.contents = b'mycontents' self.file_creator = FileCreator() self.urlopen_patch = mock.patch( 'awscli.customizations.gamelift.getlog.urlopen') self.urlopen_mock = self.urlopen_patch.start() self.urlopen_mock.return_value = six.BytesIO(self.contents) def tearDown(self): self.create_client_patch.stop() self.file_creator.remove_all() self.urlopen_patch.stop() def test_get_game_session_log(self): session_id = 'mysessionid' save_as = os.path.join(self.file_creator.rootdir, 'mylog') args = ['--game-session-id', session_id, '--save-as', save_as] global_args = Namespace() global_args.region = 'us-west-2' global_args.endpoint_url = None global_args.verify_ssl = None presigned_url = 'mypresignedurl' self.client.get_game_session_log_url.return_value = { 'PreSignedUrl': presigned_url } # Call the command self.cmd(args, global_args) # Ensure the client was created properly self.mock_create_client.assert_called_once_with( 'gamelift', region_name='us-west-2', endpoint_url=None, verify=None) # Ensure the client was called correctly self.client.get_game_session_log_url.assert_called_once_with( GameSessionId=session_id) # Ensure the presigned url was used self.urlopen_mock.assert_called_once_with(presigned_url) # Ensure the contents were saved to the file with open(save_as, 'rb') as f: self.assertEqual(f.read(), self.contents)
class BaseCLIInputArgumentTest(BaseAWSCommandParamsTest): def setUp(self): super(BaseCLIInputArgumentTest, self).setUp() self.files = FileCreator() def tearDown(self): super(BaseCLIInputArgumentTest, self).tearDown() self.files.remove_all()
class TestThrowsWarning(unittest.TestCase): def setUp(self): self.files = FileCreator() self.root = self.files.rootdir self.session = FakeSession() self.service = self.session.get_service('s3') self.endpoint = self.service.get_endpoint('us-east-1') def tearDown(self): self.files.remove_all() def test_no_warning(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) self.files.create_file("foo.txt", contents="foo") full_path = os.path.join(self.root, "foo.txt") return_val = file_gen.triggers_warning(full_path) self.assertFalse(return_val) self.assertTrue(file_gen.result_queue.empty()) def test_no_exists(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) filename = os.path.join(self.root, 'file') return_val = file_gen.triggers_warning(filename) self.assertTrue(return_val) warning_message = file_gen.result_queue.get() self.assertEqual( warning_message.message, ("warning: Skipping file %s. File does not exist." % filename)) def test_no_read_access(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) self.files.create_file("foo.txt", contents="foo") full_path = os.path.join(self.root, "foo.txt") open_function = 'awscli.customizations.s3.filegenerator._open' with mock.patch(open_function) as mock_class: mock_class.side_effect = OSError() return_val = file_gen.triggers_warning(full_path) self.assertTrue(return_val) warning_message = file_gen.result_queue.get() self.assertEqual(warning_message.message, ("warning: Skipping file %s. File/Directory is " "not readable." % full_path)) @unittest.skipIf(platform.system() not in ['Darwin', 'Linux'], 'Special files only supported on mac/linux') def test_is_special_file_warning(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) file_path = os.path.join(self.files.rootdir, 'foo') # Use socket for special file. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(file_path) return_val = file_gen.triggers_warning(file_path) self.assertTrue(return_val) warning_message = file_gen.result_queue.get() self.assertEqual(warning_message.message, ("warning: Skipping file %s. File is character " "special device, block special device, FIFO, or " "socket." % file_path))
class BaseHistoryCommandParamsTest(BaseAWSCommandParamsTest): def setUp(self): history_recorder = self._make_clean_history_recorder() super(BaseHistoryCommandParamsTest, self).setUp() self.history_recorder = history_recorder self.files = FileCreator() config_contents = ('[default]\n' 'cli_history = enabled') self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'config', config_contents) self.environ['AWS_CLI_HISTORY_FILE'] = self.files.create_file( 'history.db', '') self.driver = create_clidriver() # The run_cmd patches stdout with a StringIO object (similar to what # nose does). Therefore it will run into issues when # get_binary_stdout is called because it returns sys.stdout.buffer # for Py3 and StringIO does not have a buffer self.binary_stdout_patch = mock.patch('awscli.utils.get_binary_stdout') mock_get_binary_stdout = self.binary_stdout_patch.start() self.binary_stdout = BytesIO() mock_get_binary_stdout.return_value = self.binary_stdout def _make_clean_history_recorder(self): # This is to ensure that for each new test run the CLI is using # a brand new HistoryRecorder as this is global so previous test # runs could have injected handlers onto it as all of the tests # are ran in the same process. history_recorder = HistoryRecorder() # The HISTORY_RECORDER is instantiated on module import before we # doing any patching which means we cannot simply patch # botocore.get_global_history_recorder as the objects are already # instantiated as so we have to individually patch each one of these... self._apply_history_recorder_patch('awscli.clidriver', history_recorder) self._apply_history_recorder_patch('awscli.customizations.history', history_recorder) return history_recorder def _apply_history_recorder_patch(self, module, history_recorder): patch_history_recorder = mock.patch(module + '.HISTORY_RECORDER', history_recorder) patch_history_recorder.start() self.addCleanup(patch_history_recorder.stop) def _cleanup_db_connections(self): # Reaching into private data to close out the database connection. # Windows won't let us delete the tempdir until these connections are # closed in the tearDown step and we have no other way of forcing # them to close. handlers = self.history_recorder._handlers for handler in handlers: handler._writer.close() def tearDown(self): super(BaseHistoryCommandParamsTest, self).tearDown() self._cleanup_db_connections() self.files.remove_all() self.binary_stdout_patch.stop()
class TestShow(unittest.TestCase): def setUp(self): self.files = FileCreator() self.environ = os.environ.copy() self.environ['AWS_CONFIG_FILE'] = self.files.create_file( 'config', ('[default]\n' 'cli_history = enabled')) self.environ['AWS_DEFAULT_PROFILE'] = 'default' self.environ['AWS_DEFAULT_REGION'] = 'us-west-2' self.environ['AWS_CLI_HISTORY_FILE'] = os.path.join( self.files.rootdir, 'history.db') def tearDown(self): self.files.remove_all() def remove_color(self, output): return re.compile(r'\x1b[^m]*m').sub('', output) def assert_contains_in_order(self, lines, contents): current_pos = 0 prev_line = None for line in lines: self.assertIn(line, contents) new_pos = contents.find(line) if new_pos < current_pos: self.fail('Line: "%s" should have came after line: "%s"' % (line, prev_line)) prev_line = line current_pos = new_pos def test_show(self): # Make a call that does not require credentials just in case the # user was using the config file to provide credentials. cmd = 'sts assume-role-with-saml ' cmd += '--role-arn arn:aws:iam::...:invalid ' cmd += '--principal-arn arn:aws:iam::...:invalid ' cmd += '--saml-assertion fake-assertion' aws(cmd, env_vars=self.environ) # Now run the show command and make sure the general output is all # there. result = aws('history show', env_vars=self.environ) uncolored_content = self.remove_color(result.stdout) self.assert_contains_in_order([ 'AWS CLI command entered', 'with AWS CLI version: aws-cli/', "with arguments: ['sts', 'assume-role-with-saml',", '[0] API call made', 'to service: sts', 'using operation: AssumeRoleWithSAML', 'with parameters: {', ' "PrincipalArn": "arn:aws:iam::...:invalid",', ' "RoleArn": "arn:aws:iam::...:invalid",', ' "SAMLAssertion": "fake-assertion"', '[0] HTTP request sent', 'to URL: https://sts.amazonaws.com/', 'with method: POST', 'with body: Action=AssumeRoleWithSAML&Version=2011-06-15', '[0] HTTP response received', 'with status code: 400', 'with body: <?xml version="1.0" ?>', '[0] HTTP response parsed', 'parsed to: {', ' "Error": {', 'AWS CLI command exited', 'with return code: 255' ], uncolored_content)
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.assertEquals("abc123", fp.read()) self.assertEquals(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.assertEquals(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.assertEquals("", 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 TestThrowsWarning(unittest.TestCase): def setUp(self): self.files = FileCreator() self.root = self.files.rootdir self.session = FakeSession() self.service = self.session.get_service('s3') self.endpoint = self.service.get_endpoint('us-east-1') def tearDown(self): self.files.remove_all() def test_no_warning(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) self.files.create_file("foo.txt", contents="foo") full_path = os.path.join(self.root, "foo.txt") return_val = file_gen.triggers_warning(full_path) self.assertFalse(return_val) self.assertTrue(file_gen.result_queue.empty()) def test_no_exists(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) filename = os.path.join(self.root, 'file') return_val = file_gen.triggers_warning(filename) self.assertTrue(return_val) warning_message = file_gen.result_queue.get() self.assertEqual(warning_message.message, ("warning: Skipping file %s. File does not exist." % filename)) def test_no_read_access(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) self.files.create_file("foo.txt", contents="foo") full_path = os.path.join(self.root, "foo.txt") open_function = 'awscli.customizations.s3.filegenerator._open' with mock.patch(open_function) as mock_class: mock_class.side_effect = OSError() return_val = file_gen.triggers_warning(full_path) self.assertTrue(return_val) warning_message = file_gen.result_queue.get() self.assertEqual(warning_message.message, ("warning: Skipping file %s. File/Directory is " "not readable." % full_path)) @unittest.skipIf(platform.system() not in ['Darwin', 'Linux'], 'Special files only supported on mac/linux') def test_is_special_file_warning(self): file_gen = FileGenerator(self.service, self.endpoint, '', False) file_path = os.path.join(self.files.rootdir, 'foo') # Use socket for special file. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(file_path) return_val = file_gen.triggers_warning(file_path) self.assertTrue(return_val) warning_message = file_gen.result_queue.get() self.assertEqual(warning_message.message, ("warning: Skipping file %s. File is character " "special device, block special device, FIFO, or " "socket." % file_path))
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') 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') 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', 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') arg.add_to_params({}, outfile) arg.save_query({ 'ResponseMetadata': { 'HTTPStatusCode': 200 }, 'baz': 'abc123' }) with open(outfile) as fp: self.assertEquals('abc123', fp.read()) self.assertEquals(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') self.assertEquals(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') arg.add_to_params({}, outfile) arg.save_query({'ResponseMetadata': {'HTTPStatusCode': 200}}) with open(outfile) as fp: self.assertEquals('', fp.read())
class TestAliasCommandInjector(unittest.TestCase): def setUp(self): self.files = FileCreator() self.alias_file = self.files.create_file("alias", "[toplevel]\n") self.alias_loader = AliasLoader(self.alias_file) self.session = mock.Mock(spec=Session) self.alias_cmd_injector = AliasCommandInjector(self.session, self.alias_loader) self.command_table = {} self.parser = MainArgParser( command_table=self.command_table, version_string="version", description="description", argument_table={} ) def tearDown(self): self.files.remove_all() def test_service_alias_command(self): with open(self.alias_file, "a+") as f: f.write("my-alias = my-alias-value\n") self.alias_cmd_injector.inject_aliases(self.command_table, self.parser) self.assertIn("my-alias", self.command_table) self.assertIsInstance(self.command_table["my-alias"], ServiceAliasCommand) def test_external_alias_command(self): with open(self.alias_file, "a+") as f: f.write("my-alias = !my-alias-value\n") self.alias_cmd_injector.inject_aliases(self.command_table, self.parser) self.assertIn("my-alias", self.command_table) self.assertIsInstance(self.command_table["my-alias"], ExternalAliasCommand) def test_clobbers_builtins(self): builtin_cmd = mock.Mock(spec=CLICommand) self.command_table["builtin"] = builtin_cmd with open(self.alias_file, "a+") as f: f.write("builtin = my-alias-value\n") self.alias_cmd_injector.inject_aliases(self.command_table, self.parser) self.assertIn("builtin", self.command_table) self.assertIsInstance(self.command_table["builtin"], ServiceAliasCommand) def test_shadow_proxy_command(self): builtin_cmd = mock.Mock(spec=CLICommand) builtin_cmd.name = "builtin" self.command_table["builtin"] = builtin_cmd with open(self.alias_file, "a+") as f: f.write("builtin = builtin\n") self.alias_cmd_injector.inject_aliases(self.command_table, self.parser) self.command_table["builtin"]([], FakeParsedArgs(command="builtin")) # The builtin command should be passed to the alias # command when added to the table. builtin_cmd.assert_called_with([], FakeParsedArgs(command="builtin"))
class TestIgnoreFilesLocally(unittest.TestCase): """ This class tests the ability to ignore particular files. This includes skipping symlink when desired. """ def setUp(self): self.session = FakeSession() self.service = self.session.get_service('s3') self.endpoint = self.service.get_endpoint('us-east-1') self.files = FileCreator() def tearDown(self): self.files.remove_all() def test_warning(self): path = os.path.join(self.files.rootdir, 'badsymlink') os.symlink('non-existent-file', path) filegenerator = FileGenerator(self.service, self.endpoint, '', True) self.assertTrue(filegenerator.should_ignore_file(path)) def test_skip_symlink(self): filename = 'foo.txt' self.files.create_file(os.path.join(self.files.rootdir, filename), contents='foo.txt contents') sym_path = os.path.join(self.files.rootdir, 'symlink') os.symlink(filename, sym_path) filegenerator = FileGenerator(self.service, self.endpoint, '', False) self.assertTrue(filegenerator.should_ignore_file(sym_path)) def test_no_skip_symlink(self): filename = 'foo.txt' path = self.files.create_file(os.path.join(self.files.rootdir, filename), contents='foo.txt contents') sym_path = os.path.join(self.files.rootdir, 'symlink') os.symlink(path, sym_path) filegenerator = FileGenerator(self.service, self.endpoint, '', True) self.assertFalse(filegenerator.should_ignore_file(sym_path)) self.assertFalse(filegenerator.should_ignore_file(path)) def test_no_skip_symlink_dir(self): filename = 'dir' path = os.path.join(self.files.rootdir, 'dir/') os.mkdir(path) sym_path = os.path.join(self.files.rootdir, 'symlink') os.symlink(path, sym_path) filegenerator = FileGenerator(self.service, self.endpoint, '', True) self.assertFalse(filegenerator.should_ignore_file(sym_path)) self.assertFalse(filegenerator.should_ignore_file(path))
class TestPlugins(BaseCLIDriverTest): def setUp(self): super(TestPlugins, self).setUp() self.files = FileCreator() self.plugins_site_packages = os.path.join(self.files.rootdir, 'site-packages') self.plugin_module_name = 'add_awscli_cmd_plugin' self.plugin_filename = os.path.join(os.path.dirname(__file__), self.plugin_module_name) + '.py' self.setup_plugin_site_packages() def setup_plugin_site_packages(self): os.makedirs(self.plugins_site_packages) shutil.copy(self.plugin_filename, self.plugins_site_packages) def tearDown(self): super(TestPlugins, self).tearDown() self.files.remove_all() def assert_plugin_loaded(self, clidriver): self.assertIn('plugin-test-cmd', clidriver.subcommand_table) def assert_plugin_not_loaded(self, clidriver): self.assertNotIn('plugin-test-cmd', clidriver.subcommand_table) def create_config(self, config_contents): config_file = self.files.create_file('config', config_contents) self.environ['AWS_CONFIG_FILE'] = config_file 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 test_plugins_are_not_loaded_when_path_specified(self): self.create_config('[plugins]\n' 'myplugin = %s\n' % self.plugin_module_name) clidriver = create_clidriver() self.assert_plugin_not_loaded(clidriver) 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)
class TestCreateFunction(BaseAWSCommandParamsTest): prefix = 'lambda create-function' def setUp(self): super(TestCreateFunction, self).setUp() # Make a temporary file self.files = FileCreator() self.contents_of_file = 'myzipcontents' self.temp_file = self.files.create_file('foo', self.contents_of_file) def tearDown(self): super(TestCreateFunction, self).tearDown() self.files.remove_all() def test_create_function(self): cmdline = self.prefix cmdline += ' --function-name myfunction --runtime myruntime' cmdline += ' --role myrole --handler myhandler --zip-file myzip' result = { 'FunctionName': 'myfunction', 'Runtime': 'myruntime', 'Role': 'myrole', 'Handler': 'myhandler', 'Code': { 'ZipFile': 'myzip' } } self.assert_params_for_cmd(cmdline, result) def test_create_function_with_file(self): cmdline = self.prefix cmdline += ' --function-name myfunction --runtime myruntime' cmdline += ' --role myrole --handler myhandler' cmdline += ' --zip-file file://%s' % self.temp_file result = { 'FunctionName': 'myfunction', 'Runtime': 'myruntime', 'Role': 'myrole', 'Handler': 'myhandler', 'Code': { 'ZipFile': self.contents_of_file } } self.assert_params_for_cmd(cmdline, result) def test_create_function_code_argument_cause_error(self): cmdline = self.prefix cmdline += ' --function-name myfunction --runtime myruntime' cmdline += ' --role myrole --handler myhandler --zip-file myzip' cmdline += ' --code mycode' stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=255) self.assertIn('Unknown options: --code', stderr)
class TestIgnoreFilesLocally(unittest.TestCase): """ This class tests the ability to ignore particular files. This includes skipping symlink when desired. """ def setUp(self): self.session = FakeSession() self.service = self.session.get_service('s3') self.endpoint = self.service.get_endpoint('us-east-1') self.files = FileCreator() def tearDown(self): self.files.remove_all() def test_bad_symlink(self): path = os.path.join(self.files.rootdir, 'badsymlink') os.symlink('non-existent-file', path) filegenerator = FileGenerator(self.service, self.endpoint, '', True) self.assertFalse(filegenerator.should_ignore_file(path)) def test_skip_symlink(self): filename = 'foo.txt' self.files.create_file(os.path.join(self.files.rootdir, filename), contents='foo.txt contents') sym_path = os.path.join(self.files.rootdir, 'symlink') os.symlink(filename, sym_path) filegenerator = FileGenerator(self.service, self.endpoint, '', False) self.assertTrue(filegenerator.should_ignore_file(sym_path)) def test_no_skip_symlink(self): filename = 'foo.txt' path = self.files.create_file(os.path.join(self.files.rootdir, filename), contents='foo.txt contents') sym_path = os.path.join(self.files.rootdir, 'symlink') os.symlink(path, sym_path) filegenerator = FileGenerator(self.service, self.endpoint, '', True) self.assertFalse(filegenerator.should_ignore_file(sym_path)) self.assertFalse(filegenerator.should_ignore_file(path)) def test_no_skip_symlink_dir(self): filename = 'dir' path = os.path.join(self.files.rootdir, 'dir/') os.mkdir(path) sym_path = os.path.join(self.files.rootdir, 'symlink') os.symlink(path, sym_path) filegenerator = FileGenerator(self.service, self.endpoint, '', True) self.assertFalse(filegenerator.should_ignore_file(sym_path)) self.assertFalse(filegenerator.should_ignore_file(path))
class BaseRekognitionTest(BaseAWSCommandParamsTest): def setUp(self): super(BaseRekognitionTest, self).setUp() self.files = FileCreator() self.temp_file = self.files.create_file('foo', 'mycontents') with open(self.temp_file, 'rb') as f: self.temp_file_bytes = f.read() def tearDown(self): super(BaseRekognitionTest, self).tearDown() self.files.remove_all()
class BaseRekognitionTest(BaseAWSCommandParamsTest): def setUp(self): super(BaseRekognitionTest, self).setUp() self.files = FileCreator() self.temp_file = self.files.create_file( 'foo', 'mycontents') with open(self.temp_file, 'rb') as f: self.temp_file_bytes = f.read() def tearDown(self): super(BaseRekognitionTest, self).tearDown() self.files.remove_all()
class TestDeployCommand(BaseAWSCommandParamsTest): def setUp(self): super(TestDeployCommand, self).setUp() self.files = FileCreator() self.parsed_responses = [ # First it checks to see if a stack with that name exists. So # we fake a response indicating that the stack exists and is in # an OK state. { 'Stacks': { 'StackName': 'Stack', 'StackStatus': 'UPDATE_COMPLETE' } }, # Now it creates a changeset, so we fake a response with an ID. { 'Id': 'FakeChangeSetId' }, # This fakes a failed response from the waiter because the # changeset was empty. { 'StackName': 'Stack', 'Status': 'FAILED', 'StatusReason': ('The submitted information didn\'t contain changes. ' 'Submit different information to create a change set.'), 'ExecutionStatus': 'UNAVAILABLE' }, ] # The template is inspected before we make any of the calls so it # needs to have valid JSON content. path = self.files.create_file('template.json', '{}') self.command = ('cloudformation deploy --template-file %s ' '--stack-name Stack') % path def tearDown(self): self.files.remove_all() super(TestDeployCommand, self).tearDown() def test_does_return_zero_exit_code_on_empty_changeset_by_default(self): self.run_cmd(self.command, expected_rc=0) def test_does_return_zero_exit_code_on_empty_changeset(self): self.command += ' --no-fail-on-empty-changeset' self.run_cmd(self.command, expected_rc=0) def test_does_return_non_zero_exit_code_on_empty_changeset(self): self.command += ' --fail-on-empty-changeset' self.run_cmd(self.command, expected_rc=255)
class TestGetGameSessionLogCommand(unittest.TestCase): def setUp(self): self.create_client_patch = mock.patch("botocore.session.Session.create_client") self.mock_create_client = self.create_client_patch.start() self.session = get_session() self.client = mock.Mock() self.mock_create_client.return_value = self.client self.cmd = GetGameSessionLogCommand(self.session) self.contents = b"mycontents" self.file_creator = FileCreator() self.urlopen_patch = mock.patch("awscli.customizations.gamelift.getlog.urlopen") self.urlopen_mock = self.urlopen_patch.start() self.urlopen_mock.return_value = six.BytesIO(self.contents) def tearDown(self): self.create_client_patch.stop() self.file_creator.remove_all() self.urlopen_patch.stop() def test_get_game_session_log(self): session_id = "mysessionid" save_as = os.path.join(self.file_creator.rootdir, "mylog") args = ["--game-session-id", session_id, "--save-as", save_as] global_args = Namespace() global_args.region = "us-west-2" global_args.endpoint_url = None global_args.verify_ssl = None presigned_url = "mypresignedurl" self.client.get_game_session_log_url.return_value = {"PreSignedUrl": presigned_url} # Call the command self.cmd(args, global_args) # Ensure the client was created properly self.mock_create_client.assert_called_once_with( "gamelift", region_name="us-west-2", endpoint_url=None, verify=None ) # Ensure the client was called correctly self.client.get_game_session_log_url.assert_called_once_with(GameSessionId=session_id) # Ensure the presigned url was used self.urlopen_mock.assert_called_once_with(presigned_url) # Ensure the contents were saved to the file with open(save_as, "rb") as f: self.assertEqual(f.read(), self.contents)
class TestCreateFunction(BaseAWSCommandParamsTest): prefix = 'lambda create-function' def setUp(self): super(TestCreateFunction, self).setUp() # Make a temporary file self.files = FileCreator() self.contents_of_file = 'myzipcontents' self.temp_file = self.files.create_file( 'foo', self.contents_of_file) def tearDown(self): super(TestCreateFunction, self).tearDown() self.files.remove_all() def test_create_function(self): cmdline = self.prefix cmdline += ' --function-name myfunction --runtime myruntime' cmdline += ' --role myrole --handler myhandler --zip-file myzip' result = { 'FunctionName': 'myfunction', 'Runtime': 'myruntime', 'Role': 'myrole', 'Handler': 'myhandler', 'Code': {'ZipFile': 'myzip'} } self.assert_params_for_cmd(cmdline, result) def test_create_function_with_file(self): cmdline = self.prefix cmdline += ' --function-name myfunction --runtime myruntime' cmdline += ' --role myrole --handler myhandler' cmdline += ' --zip-file file://%s' % self.temp_file result = { 'FunctionName': 'myfunction', 'Runtime': 'myruntime', 'Role': 'myrole', 'Handler': 'myhandler', 'Code': {'ZipFile': self.contents_of_file} } self.assert_params_for_cmd(cmdline, result) def test_create_function_code_argument_cause_error(self): cmdline = self.prefix cmdline += ' --function-name myfunction --runtime myruntime' cmdline += ' --role myrole --handler myhandler --zip-file myzip' cmdline += ' --code mycode' stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=255) self.assertIn('Unknown options: --code', stderr)
class BaseLambdaTests(BaseAWSCommandParamsTest): def setUp(self): super(BaseLambdaTests, self).setUp() self.files = FileCreator() self.temp_file = self.files.create_file("foo", "mycontents") self.zip_file = os.path.join(self.files.rootdir, "foo.zip") with closing(zipfile.ZipFile(self.zip_file, "w")) as f: f.write(self.temp_file) with open(self.zip_file, "rb") as f: self.zip_file_contents = f.read() def tearDown(self): super(BaseLambdaTests, self).tearDown() self.files.remove_all()
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') 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') 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', 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') arg.add_to_params({}, outfile) arg.save_query({'ResponseMetadata': {'HTTPStatusCode': 200}, 'baz': 'abc123'}) with open(outfile) as fp: self.assertEquals('abc123', fp.read()) self.assertEquals(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') self.assertEquals(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') arg.add_to_params({}, outfile) arg.save_query({'ResponseMetadata': {'HTTPStatusCode': 200}}) with open(outfile) as fp: self.assertEquals('', fp.read())
class BaseLambdaTests(BaseAWSCommandParamsTest): def setUp(self): super(BaseLambdaTests, self).setUp() self.files = FileCreator() self.temp_file = self.files.create_file('foo', 'mycontents') self.zip_file = os.path.join(self.files.rootdir, 'foo.zip') with closing(zipfile.ZipFile(self.zip_file, 'w')) as f: f.write(self.temp_file) with open(self.zip_file, 'rb') as f: self.zip_file_contents = f.read() def tearDown(self): super(BaseLambdaTests, self).tearDown() self.files.remove_all()
class TestCLITimestampParser(BaseCLIWireResponseTest): def setUp(self): super(TestCLITimestampParser, self).setUp() self.files = FileCreator() self.wire_response = json.dumps({ 'builds': [{ 'startTime': 0, }] }).encode('utf-8') self.command = ['codebuild', 'batch-get-builds', '--ids', 'foo'] self.patch_send(content=self.wire_response) def tearDown(self): super(TestCLITimestampParser, self).tearDown() self.files.remove_all() 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_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_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)
class TestZipDirectory(unittest.TestCase): def setUp(self): self.file_creator = FileCreator() self.zip_file = self.file_creator.create_file('build.zip', '') self._dir_root = 'mybuild' def tearDown(self): self.file_creator.remove_all() @property def dir_root(self): return self.file_creator.full_path(self._dir_root) def add_to_directory(self, filename): self.file_creator.create_file( os.path.join(self._dir_root, filename), 'Some contents') def assert_contents_of_zip_file(self, filenames): zip_file_object = zipfile.ZipFile( self.zip_file, 'r', zipfile.ZIP_DEFLATED) with contextlib.closing(zip_file_object) as zf: ref_zipfiles = [] zipfile_contents = zf.namelist() for ref_zipfile in zipfile_contents: if os.sep == '\\': # Internally namelist() represent directories with # forward slashes so we need to account for that if # the separator is a backslash depending on the operating # system. ref_zipfile = ref_zipfile.replace('/', '\\') ref_zipfiles.append(ref_zipfile) self.assertEqual(sorted(ref_zipfiles), filenames) def test_single_file(self): self.add_to_directory('foo') zip_directory(self.zip_file, self.dir_root) self.assert_contents_of_zip_file(['foo']) def test_multiple_files(self): self.add_to_directory('foo') self.add_to_directory('bar') zip_directory(self.zip_file, self.dir_root) self.assert_contents_of_zip_file(['bar', 'foo']) def test_nested_file(self): filename = os.path.join('mydir', 'foo') self.add_to_directory(filename) zip_directory(self.zip_file, self.dir_root) self.assert_contents_of_zip_file([filename])
class TestCLITimestampParser(BaseCLIWireResponseTest): def setUp(self): super(TestCLITimestampParser, self).setUp() self.files = FileCreator() self.wire_response = json.dumps({ 'builds': [{ 'startTime': 0, }] }).encode('utf-8') self.command = ['codebuild', 'batch-get-builds', '--ids', 'foo'] self.patch_send(content=self.wire_response) def tearDown(self): super(TestCLITimestampParser, self).tearDown() self.files.remove_all() 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_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 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)
class BaseS3TransferCommandTest(BaseAWSCommandParamsTest): def setUp(self): super(BaseS3TransferCommandTest, self).setUp() self.files = FileCreator() def tearDown(self): super(BaseS3TransferCommandTest, self).tearDown() self.files.remove_all() def assert_operations_called(self, expected_operations_with_params): actual_operations_with_params = [ (operation_called[0].name, operation_called[1]) for operation_called in self.operations_called ] self.assertEqual(actual_operations_with_params, expected_operations_with_params)
class BaseS3TransferCommandTest(BaseAWSCommandParamsTest): def setUp(self): super(BaseS3TransferCommandTest, self).setUp() self.files = FileCreator() def tearDown(self): super(BaseS3TransferCommandTest, self).tearDown() self.files.remove_all() def assert_operations_called(self, expected_operations_with_params): actual_operations_with_params = [ (operation_called[0].name, operation_called[1]) for operation_called in self.operations_called ] self.assertEqual( actual_operations_with_params, expected_operations_with_params)
class TestAddModel(BaseAWSCommandParamsTest): prefix = 'configure add-model' def setUp(self): super(TestAddModel, self).setUp() self.files = FileCreator() self.customer_data_root = self.files.rootdir self.data_loader = self.driver.session.get_component('data_loader') self.data_loader.CUSTOMER_DATA_PATH = self.customer_data_root self.service_definition = { "version": "2.0", "metadata": { "apiVersion": '2015-12-02', "endpointPrefix": 'myservice', }, "operations": {}, "shapes": {} } def tearDown(self): super(TestAddModel, self).tearDown() self.files.remove_all() def test_add_model(self): cmdline = self.prefix + ' --service-model %s' % json.dumps( self.service_definition, separators=(',', ':')) self.run_cmd(cmdline) # Ensure that the model exists in the correct location. self.assertTrue( os.path.exists(os.path.join( self.customer_data_root, 'myservice', '2015-12-02', 'service-2.json'))) def test_add_model_with_service_name(self): cmdline = self.prefix + ' --service-model %s' % json.dumps( self.service_definition, separators=(',', ':')) cmdline += ' --service-name override-name' self.run_cmd(cmdline) # Ensure that the model exists in the correct location. self.assertTrue( os.path.exists(os.path.join( self.customer_data_root, 'override-name', '2015-12-02', 'service-2.json')))
class TestGetGameSessionLog(BaseAWSCommandParamsTest): prefix = 'gamelift get-game-session-log' def setUp(self): super(TestGetGameSessionLog, self).setUp() self.files = FileCreator() self.filename = os.path.join(self.files.rootdir, 'myfile') self.urlopen_patch = mock.patch( 'awscli.customizations.gamelift.getlog.urlopen') self.contents = b'My Contents' self.urlopen_mock = self.urlopen_patch.start() self.urlopen_mock.return_value = six.BytesIO(self.contents) def tearDown(self): super(TestGetGameSessionLog, self).tearDown() self.files.remove_all() self.urlopen_patch.stop() def test_get_game_session_log(self): cmdline = self.prefix cmdline += ' --game-session-id mysession' cmdline += ' --save-as %s' % self.filename self.parsed_responses = [{'PreSignedUrl': 'myurl'}] stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=0) self.assertEqual(len(self.operations_called), 1) self.assertEqual( self.operations_called[0][0].name, 'GetGameSessionLogUrl') self.assertEqual( self.operations_called[0][1], {'GameSessionId': 'mysession'} ) # Ensure the contents were saved to the file self.assertTrue(os.path.exists(self.filename)) with open(self.filename, 'rb') as f: self.assertEqual(f.read(), self.contents) # Ensure the output is as expected self.assertIn( 'Successfully downloaded log archive for game session ' 'mysession to %s' % self.filename, stdout )
class TestAddModel(BaseAWSCommandParamsTest): prefix = 'configure add-model' def setUp(self): super(TestAddModel, self).setUp() self.files = FileCreator() self.customer_data_root = self.files.rootdir self.data_loader = self.driver.session.get_component('data_loader') self.data_loader.CUSTOMER_DATA_PATH = self.customer_data_root self.service_definition = { "version": "2.0", "metadata": { "apiVersion": '2015-12-02', "endpointPrefix": 'myservice', }, "operations": {}, "shapes": {} } def tearDown(self): super(TestAddModel, self).tearDown() self.files.remove_all() def test_add_model(self): cmdline = self.prefix + ' --service-model %s' % json.dumps( self.service_definition, separators=(',', ':')) self.run_cmd(cmdline) # Ensure that the model exists in the correct location. self.assertTrue( os.path.exists( os.path.join(self.customer_data_root, 'myservice', '2015-12-02', 'service-2.json'))) def test_add_model_with_service_name(self): cmdline = self.prefix + ' --service-model %s' % json.dumps( self.service_definition, separators=(',', ':')) cmdline += ' --service-name override-name' self.run_cmd(cmdline) # Ensure that the model exists in the correct location. self.assertTrue( os.path.exists( os.path.join(self.customer_data_root, 'override-name', '2015-12-02', 'service-2.json')))
class TestParamFile(unittest.TestCase): def setUp(self): self.files = FileCreator() def tearDown(self): self.files.remove_all() def get_paramfile(self, path): return get_paramfile(path, LOCAL_PREFIX_MAP.copy()) def test_text_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'file://' + filename data = self.get_paramfile(prefixed_filename) self.assertEqual(data, contents) self.assertIsInstance(data, six.string_types) def test_binary_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'fileb://' + filename data = self.get_paramfile(prefixed_filename) self.assertEqual(data, b'This is a test') self.assertIsInstance(data, six.binary_type) @skip_if_windows('Binary content error only occurs ' 'on non-Windows platforms.') def test_cannot_load_text_file(self): contents = b'\xbfX\xac\xbe' filename = self.files.create_file('foo', contents, mode='wb') prefixed_filename = 'file://' + filename with self.assertRaises(ResourceLoadingError): self.get_paramfile(prefixed_filename) def test_file_does_not_exist_raises_error(self): with self.assertRaises(ResourceLoadingError): self.get_paramfile('file://file/does/not/existsasdf.txt') def test_no_match_uris_returns_none(self): self.assertIsNone(self.get_paramfile('foobar://somewhere.bar')) def test_non_string_type_returns_none(self): self.assertIsNone(self.get_paramfile(100))
class TestAliases(BaseAWSHelpOutputTest): 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) 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 test_alias_not_in_main_help(self): self.add_alias('my-alias', 'ec2 describe-regions') self.driver.main(['help']) self.assert_not_contains('my-alias')
class TestRmCommand(BaseAWSCommandParamsTest): prefix = 's3 rm' def setUp(self): super(TestRmCommand, self).setUp() self.files = FileCreator() def tearDown(self): super(TestRmCommand, self).tearDown() self.files.remove_all() def test_operations_used_in_upload(self): cmdline = '%s s3://bucket/key.txt' % self.prefix self.run_cmd(cmdline, expected_rc=0) # The only operation we should have called is DeleteObject. self.assertEqual( len(self.operations_called), 1, self.operations_called) self.assertEqual(self.operations_called[0][0].name, 'DeleteObject')
class TestRmCommand(BaseAWSCommandParamsTest): prefix = 's3 rm' def setUp(self): super(TestRmCommand, self).setUp() self.files = FileCreator() def tearDown(self): super(TestRmCommand, self).tearDown() self.files.remove_all() def test_operations_used_in_upload(self): cmdline = '%s s3://bucket/key.txt' % self.prefix self.run_cmd(cmdline, expected_rc=0) # The only operation we should have called is DeleteObject. self.assertEqual(len(self.operations_called), 1, self.operations_called) self.assertEqual(self.operations_called[0][0].name, 'DeleteObject')
class BaseTestCLIFollowParamURL(BaseAWSCommandParamsTest): def setUp(self): super(BaseTestCLIFollowParamURL, self).setUp() self.files = FileCreator() self.prefix = 'lambda get-function --function-name' def tearDown(self): super(BaseTestCLIFollowParamURL, self).tearDown() self.files.remove_all() def assert_param_expansion_is_correct(self, provided_param, expected_param): cmd = '%s %s' % (self.prefix, provided_param) # We do not care about the return code here. All that is of interest # is what happened to the arguments before they were passed to botocore # which we get from the params={} key. For binary types we will fail in # python 3 with an rc of 255 and get an rc of 0 in python 2 where it # can't tell the difference, so we pass ANY here to ignore the rc. self.assert_params_for_cmd(cmd, params={'FunctionName': expected_param}, expected_rc=ANY)
class TestIsReadable(unittest.TestCase): def setUp(self): self.files = FileCreator() self.filename = 'foo' self.full_path = os.path.join(self.files.rootdir, self.filename) def tearDown(self): self.files.remove_all() def test_unreadable_file(self): self.files.create_file(self.filename, contents="foo") open_function = 'awscli.customizations.s3.filegenerator._open' with mock.patch(open_function) as mock_class: mock_class.side_effect = OSError() self.assertFalse(is_readable(self.full_path)) def test_unreadable_directory(self): os.mkdir(self.full_path) with mock.patch('os.listdir') as mock_class: mock_class.side_effect = OSError() self.assertFalse(is_readable(self.full_path))
class TestParamFile(unittest.TestCase): def setUp(self): self.files = FileCreator() def tearDown(self): self.files.remove_all() def test_text_file(self): contents = "This is a test" filename = self.files.create_file("foo", contents) prefixed_filename = "file://" + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, contents) self.assertIsInstance(data, six.string_types) def test_binary_file(self): contents = "This is a test" filename = self.files.create_file("foo", contents) prefixed_filename = "fileb://" + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, b"This is a test") self.assertIsInstance(data, six.binary_type) @skip_if_windows("Binary content error only occurs " "on non-Windows platforms.") def test_cannot_load_text_file(self): contents = b"\xbfX\xac\xbe" filename = self.files.create_file("foo", contents, mode="wb") prefixed_filename = "file://" + filename with self.assertRaises(ResourceLoadingError): get_paramfile(prefixed_filename) def test_file_does_not_exist_raises_error(self): with self.assertRaises(ResourceLoadingError): get_paramfile("file://file/does/not/existsasdf.txt") def test_no_match_uris_returns_none(self): self.assertIsNone(get_paramfile("foobar://somewhere.bar")) def test_non_string_type_returns_none(self): self.assertIsNone(get_paramfile(100))
class TestParamFile(unittest.TestCase): def setUp(self): self.files = FileCreator() def tearDown(self): self.files.remove_all() def test_text_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'file://' + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, contents) self.assertIsInstance(data, six.string_types) def test_binary_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'fileb://' + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, b'This is a test') self.assertIsInstance(data, six.binary_type)
class TestMvCommand(BaseAWSCommandParamsTest): prefix = 's3 mv ' def setUp(self): super(TestMvCommand, self).setUp() self.files = FileCreator() def tearDown(self): super(TestMvCommand, self).tearDown() self.files.remove_all() def test_cant_mv_object_onto_itself(self): cmdline = '%s s3://bucket/key s3://bucket/key' % self.prefix stderr = self.run_cmd(cmdline, expected_rc=255)[1] self.assertIn('Cannot mv a file onto itself', stderr) def test_cant_mv_object_with_implied_name(self): # The "key" key name is implied in the dst argument. cmdline = '%s s3://bucket/key s3://bucket/' % self.prefix stderr = self.run_cmd(cmdline, expected_rc=255)[1] self.assertIn('Cannot mv a file onto itself', stderr)
class TestTopicDocumentEventHandlerBase(unittest.TestCase): def setUp(self): self.session = mock.Mock() self.file_creator = FileCreator() self.tags_dict = {} # Make a temporary json index to base information on self.json_index = self.file_creator.create_file('index.json', '') with open(self.json_index, 'w') as f: json.dump(self.tags_dict, f, indent=4, sort_keys=True) self.index_patch = mock.patch('awscli.topictags.TopicTagDB.index_file', self.json_index) self.dir_patch = mock.patch('awscli.topictags.TopicTagDB.topic_dir', self.file_creator.rootdir) self.index_patch.start() self.dir_patch.start() def tearDown(self): self.dir_patch.stop() self.index_patch.stop() self.file_creator.remove_all()