def cmd(self, command, checks=None, allowed_exceptions=None, debug=False): #pylint: disable=no-self-use allowed_exceptions = allowed_exceptions or [] if not isinstance(allowed_exceptions, list): allowed_exceptions = [allowed_exceptions] if self._debug or debug: print('\n\tRUNNING: {}'.format(command)) command_list = shlex.split(command) output = StringIO() try: cli_main(command_list, file=output) except Exception as ex: # pylint: disable=broad-except ex_msg = str(ex) if not next((x for x in allowed_exceptions if x in ex_msg), None): raise ex self._track_executed_commands(command_list) result = output.getvalue().strip() output.close() if self._debug or debug: print('\tRESULT: {}\n'.format(result)) if checks: checks = [checks] if not isinstance(checks, list) else checks for check in checks: check.compare(result) result = result or '{}' try: return json.loads(result) except Exception: # pylint: disable=broad-except return result
def test_file_download_recursively_without_pattern(self): cmd = "storage file download-batch -s {} -d {} --account-name {} --account-key {}".format( self._test_source_share, self._test_folder, self._file_service.account_name, self._file_service.account_key ) cli_main(cmd.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 41
def test_blob_download_recursively_with_pattern_2(self): cmd = 'storage blob download-batch -s https://{}/{} -d {} --pattern {} --account-key {}'\ .format(self._blob_service.primary_endpoint, self._test_source_container, self._test_folder, 'apple/*', self._blob_service.account_key) cli_main(cmd.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 10
def test_blob_download_recursively_without_pattern(self): cmd = 'storage blob download-batch -s {} -d {} --account-name {} --account-key {}'\ .format(self._test_source_container, self._test_folder, self._blob_service.account_name, self._blob_service.account_key) cli_main(cmd.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 41
def test_blob_download_recursively_with_pattern_3(self): cmd = 'storage blob download-batch -s https://{}/{} -d {} --pattern {} --account-key {}'\ .format(self._blob_service.primary_endpoint, self._test_source_container, self._test_folder, '*/file_0', self._blob_service.account_key) cli_main(cmd.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 4
def test_file_download_recursively_without_pattern(self): cmd = 'storage file download-batch -s {} -d {} --account-name {} --account-key {}'\ .format(self._test_source_share, self._test_folder, self._file_service.account_name, self._file_service.account_key) cli_main(cmd.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 41
def test_download_files_recursively_without_pattern(self): command = 'storage blob download-batch -s {} -d {} --account-name {} --account-key {}'\ .format(self._test_source_container, self._test_folder, self._blob_service.account_name, self._blob_service.account_key) cli_main(command.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 31
def test_file_download_recursively_with_pattern_3(self): cmd = 'storage file download-batch -s https://{}/{} -d {} --pattern {} --account-key {}'\ .format(self._file_service.primary_endpoint, self._test_source_share, self._test_folder, '*/file_0', self._file_service.account_key) cli_main(cmd.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 4
def test_download_files_recursively_with_pattern_3(self): #pylint: disable=line-too-long command = 'storage blob download-batch -s https://{}/{} -d {} --pattern {} --account-key {}'\ .format(self._blob_service.primary_endpoint, self._test_source_container, self._test_folder, '*/file_0', self._blob_service.account_key) cli_main(command.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 3
def _test_blob_upload_multiple_files_patterns(self, pattern, expected_count): url = 'http://{}/{}'.format(self._blob_service.primary_endpoint, self._test_container_name) command = 'storage blob upload-batch -s {} -d {} --account-key {} --pattern {}' \ .format(self._resource_folder, url, self._test_account_key, pattern) cli_main(command.split()) blobs = [b.name for b in self._blob_service.list_blobs(self._test_container_name)] assert len(blobs) == expected_count
def test_file_upload_multiple_files_no_pattern(self): url = 'https://{}/{}'.format(self._file_service.primary_endpoint, self._test_share_name) cmd = 'storage file upload-batch -s {} -d {} --account-key {}'\ .format(self._resource_folder, url, self._test_account_key) cli_main(cmd.split()) files = list(glob_files_remotely(self._file_service, self._test_share_name, pattern=None)) assert len(files) == 41
def _test_file_upload_multiple_files_patterns(self, pattern, expected_count): url = 'http://{}/{}'.format(self._file_service.primary_endpoint, self._test_share_name) command = 'storage file upload-batch -s {} -d {} --account-key {} --pattern {}' \ .format(self._resource_folder, url, self._test_account_key, pattern) cli_main(command.split()) files = list(glob_files_remotely(self._file_service, self._test_share_name, pattern=None)) assert len(files) == expected_count
def test_file_download_recursively_with_pattern_3(self): cmd = "storage file download-batch -s https://{}/{} -d {} --pattern {} --account-key {}".format( self._file_service.primary_endpoint, self._test_source_share, self._test_folder, "*/file_0", self._file_service.account_key, ) cli_main(cmd.split()) assert sum(len(f) for r, d, f in os.walk(self._test_folder)) == 4
def test_blob_upload_multiple_files_no_pattern(self): url = 'http://{}/{}'.format(self._blob_service.primary_endpoint, self._test_container_name) command = 'storage blob upload-batch -s {} -d {} --account-key {}'\ .format(self._resource_folder, url, self._test_account_key) cli_main(command.split()) blobs = [ b.name for b in self._blob_service.list_blobs(self._test_container_name) ] assert len(blobs) == 41
def test_file_upload_multiple_files_no_pattern(self): url = 'https://{}/{}'.format(self._file_service.primary_endpoint, self._test_share_name) cmd = 'storage file upload-batch -s {} -d {} --account-key {}'\ .format(self._resource_folder, url, self._test_account_key) cli_main(cmd.split()) files = list( glob_files_remotely(self._file_service, self._test_share_name, pattern=None)) assert len(files) == 31
def test_blob_upload_multiple_files_dry_run(self): url = 'http://{}/{}'.format(self._blob_service.primary_endpoint, self._test_container_name) command = 'storage blob upload-batch -s {} -d {} --account-key {} --dryrun' \ .format(self._resource_folder, url, self._test_account_key) from six import StringIO buf = StringIO() cli_main(command.split(), file=buf) blobs = [b.name for b in self._blob_service.list_blobs(self._test_container_name)] assert len(blobs) == 0 self._keep_test_context = True
def setUpClass(cls): StorageIntegrationTestBase.setUpClass() # set up sample container cls._test_source_container = cls.generate_new_container_name() assert cls._blob_service.create_container(cls._test_source_container) cli_main('storage blob upload-batch -s {} -d {} --connection-string {}' .format(cls._resource_folder, cls._test_source_container, cls._test_connection_string) .split()) test_blobs = [b.name for b in cls._blob_service.list_blobs(cls._test_source_container)] assert len(test_blobs) == 41
def _test_file_upload_multiple_files_patterns(self, pattern, expected_count): url = 'http://{}/{}'.format(self._file_service.primary_endpoint, self._test_share_name) command = 'storage file upload-batch -s {} -d {} --account-key {} --pattern {}' \ .format(self._resource_folder, url, self._test_account_key, pattern) cli_main(command.split()) files = list( glob_files_remotely(self._file_service, self._test_share_name, pattern=None)) assert len(files) == expected_count
def setUpClass(cls): StorageIntegrationTestBase.setUpClass() # set up sample container cls._test_source_container = cls.generate_new_container_name() assert cls._blob_service.create_container(cls._test_source_container) cli_main( 'storage blob upload-batch -s {} -d {} --connection-string {}'. format(cls._resource_folder, cls._test_source_container, cls._test_connection_string).split()) test_blobs = [ b.name for b in cls._blob_service.list_blobs(cls._test_source_container) ] assert len(test_blobs) == 41
def _in_process_execute(self, command): # from azure.cli import as cli_main from azure.cli.main import main as cli_main from six import StringIO from vcr.errors import CannotOverwriteExistingCassetteException if command.startswith('az '): command = command[3:] output_buffer = StringIO() try: # issue: stderr cannot be redirect in this form, as a result some failure information # is lost when command fails. self.exit_code = cli_main(shlex.split(command), file=output_buffer) or 0 self.output = output_buffer.getvalue() except CannotOverwriteExistingCassetteException as ex: raise AssertionError(ex) except CliExecutionError as ex: if ex.exception: raise ex.exception else: raise ex except Exception as ex: # pylint: disable=broad-except self.exit_code = 1 self.output = output_buffer.getvalue() self.process_error = ex finally: output_buffer.close()
def setUpClass(cls): StorageIntegrationTestBase.setUpClass() # set up sample file share cls._test_source_share = cls.generate_new_share_name() assert cls._file_service.create_share(cls._test_source_share) cli_main( "storage file upload-batch -s {} -d {} --connection-string {}".format( cls._resource_folder, cls._test_source_share, cls._test_connection_string ).split() ) from ..util import glob_files_remotely test_files = [f for f in glob_files_remotely(cls._file_service, cls._test_source_share, pattern=None)] assert len(test_files) == 41
def test_blob_upload_multiple_files_dry_run(self): url = 'http://{}/{}'.format(self._blob_service.primary_endpoint, self._test_container_name) command = 'storage blob upload-batch -s {} -d {} --account-key {} --dryrun' \ .format(self._resource_folder, url, self._test_account_key) from six import StringIO buf = StringIO() cli_main(command.split(), file=buf) blobs = [ b.name for b in self._blob_service.list_blobs(self._test_container_name) ] assert len(blobs) == 0 self._keep_test_context = True
def setUpClass(cls): StorageIntegrationTestBase.setUpClass() # set up sample file share cls._test_source_share = cls.generate_new_share_name() assert cls._file_service.create_share(cls._test_source_share) cli_main( 'storage file upload-batch -s {} -d {} --connection-string {}'. format(cls._resource_folder, cls._test_source_share, cls._test_connection_string).split()) from ..util import glob_files_remotely test_files = [ f for f in glob_files_remotely( cls._file_service, cls._test_source_share, pattern=None) ] assert len(test_files) == 41
def cmd(self, command, checks=None, allowed_exceptions=None, debug=False): # pylint: disable=no-self-use from six import StringIO allowed_exceptions = allowed_exceptions or [] if not isinstance(allowed_exceptions, list): allowed_exceptions = [allowed_exceptions] if self._debug or debug: print('\n\tRUNNING: {}'.format(command)) command_list = shlex.split(command) output = StringIO() applog = StringIO() try: cli_main(command_list, output=output, logging_stream=applog) except Exception as ex: # pylint: disable=broad-except ex_msg = str(ex) logger.error('Command "%s" => %s. Output: %s. Logging: %s', command, ex_msg, output.getvalue(), applog.getvalue()) if not next((x for x in allowed_exceptions if x in ex_msg), None): raise ex logger.info('Command "%s" => %s.', command, 'success') self._track_executed_commands(command_list) result = output.getvalue().strip() output.close() if self._debug or debug: print('\tRESULT: {}\n'.format(result)) if checks: checks = [checks] if not isinstance(checks, list) else checks for check in checks: check.compare(result) if '-o' in command_list and 'tsv' in command_list: return result else: try: result = result or '{}' return json.loads(result) except Exception: # pylint: disable=broad-except return result
def test_file_download_batch_help(self): with self.assertRaises(SystemExit): cli_main("storage file download-batch -h".split())
def test_file_download_batch_help(self): with self.assertRaises(SystemExit): cli_main('storage file download-batch -h'.split())
def execute(cmd): cmd_list = shlex.split(cmd) with capture() as out: cli_main(cmd_list) return out[0]
def test_blob_upload_help(self): with self.assertRaises(SystemExit): cli_main('storage blob upload-batch -h'.split())
def test_warn_on_old_commands(self): from azure.cli.main import main as cli_main cmd = 'appservice web list'.split(' ') cli_main(cmd) err = "WARNING: This command is deprecating and will be removed in future releases. Use 'az webapp list' instead." self.assertTrue(err in sys.stderr.getvalue())