def test_add_extension_with_name_valid_checksum(self): extension_name = MY_EXT_NAME computed_extension_sha256 = _compute_file_hash(MY_EXT_SOURCE) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, computed_extension_sha256)): add_extension(extension_name=extension_name) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_NAME], MY_EXT_NAME)
def test_add_extension_with_name_invalid_checksum(self): extension_name = MY_EXT_NAME bad_sha256 = 'thishashisclearlywrong' with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, bad_sha256)): with self.assertRaises(CLIError) as err: add_extension(extension_name=extension_name) self.assertTrue('The checksum of the extension does not match the expected value.' in str(err.exception))
def test_update_extension_no_updates(self): add_extension(source=MY_EXT_SOURCE) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.3+dev') with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', side_effect=NoExtensionCandidatesError()): with self.assertRaises(CLIError) as err: update_extension(MY_EXT_NAME) self.assertTrue("No updates available for '{}'.".format(MY_EXT_NAME) in str(err.exception))
def test_add_list_show_remove_extension(self): add_extension(MY_EXT_SOURCE) actual = list_extensions() self.assertEqual(len(actual), 1) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_NAME], MY_EXT_NAME) remove_extension(MY_EXT_NAME) num_exts = len(list_extensions()) self.assertEqual(num_exts, 0)
def test_add_extension_with_name_source_not_whl(self): extension_name = 'myextension' with mock.patch( 'azure.cli.command_modules.extension.custom.resolve_from_index', return_value=('{}.notwhl'.format(extension_name), None)): with self.assertRaises(ValueError) as err: add_extension(extension_name=extension_name) self.assertTrue( 'Unknown extension type. Only Python wheels are supported.' in str(err.exception))
def test_update_extension(self): add_extension(source=MY_EXT_SOURCE) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.3+dev') newer_extension = _get_test_data_file('myfirstcliextension-0.0.4+dev-py2.py3-none-any.whl') computed_extension_sha256 = _compute_file_hash(newer_extension) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(newer_extension, computed_extension_sha256)): update_extension(MY_EXT_NAME) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.4+dev')
def test_add_list_show_remove_extension_with_dashes(self): add_extension(MY_SECOND_EXT_SOURCE_DASHES) actual = list_extensions() self.assertEqual(len(actual), 1) ext = show_extension(MY_SECOND_EXT_NAME_DASHES) self.assertEqual(ext[OUT_KEY_NAME], MY_SECOND_EXT_NAME_DASHES) self.assertIn(OUT_KEY_NAME, ext[OUT_KEY_METADATA], "Unable to get full metadata") self.assertEqual(ext[OUT_KEY_METADATA][OUT_KEY_NAME], MY_SECOND_EXT_NAME_DASHES) remove_extension(MY_SECOND_EXT_NAME_DASHES) num_exts = len(list_extensions()) self.assertEqual(num_exts, 0)
def test_add_extension_with_name_but_it_already_exists(self): # Add extension without name first add_extension(source=MY_EXT_SOURCE) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_NAME], MY_EXT_NAME) # Now add using name computed_extension_sha256 = _compute_file_hash(MY_EXT_SOURCE) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, computed_extension_sha256)): with self.assertRaises(CLIError) as err: add_extension(extension_name=MY_EXT_NAME) self.assertTrue('The extension {} already exists.'.format(MY_EXT_NAME) in str(err.exception))
def test_add_extension_verify_no_pip_proxy(self): extension_name = MY_EXT_NAME computed_extension_sha256 = _compute_file_hash(MY_EXT_SOURCE) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, computed_extension_sha256)), \ mock.patch('azure.cli.command_modules.extension.custom.shutil'), \ mock.patch('azure.cli.command_modules.extension.custom.check_output') as check_output: add_extension(extension_name=extension_name) args = check_output.call_args pip_cmd = args[0][0] if '--proxy' in pip_cmd: raise AssertionError("proxy parameter in check_output args although no proxy specified")
def test_update_extension_exception_in_update_and_rolled_back(self): add_extension(source=MY_EXT_SOURCE) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.3+dev') newer_extension = _get_test_data_file('myfirstcliextension-0.0.4+dev-py2.py3-none-any.whl') bad_sha256 = 'thishashisclearlywrong' with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(newer_extension, bad_sha256)): with self.assertRaises(CLIError) as err: update_extension(MY_EXT_NAME) self.assertTrue('Failed to update. Rolled {} back to {}.'.format(ext['name'], ext[OUT_KEY_VERSION]) in str(err.exception)) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.3+dev')
def test_add_extension_with_pip_proxy(self): extension_name = MY_EXT_NAME proxy_param = '--proxy' proxy_endpoint = "https://*****:*****@proxy.microsoft.com" computed_extension_sha256 = _compute_file_hash(MY_EXT_SOURCE) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, computed_extension_sha256)), \ mock.patch('azure.cli.command_modules.extension.custom.shutil'), \ mock.patch('azure.cli.command_modules.extension.custom.check_output') as check_output: add_extension(extension_name=extension_name, pip_proxy=proxy_endpoint) args = check_output.call_args pip_cmd = args[0][0] proxy_index = pip_cmd.index(proxy_param) assert pip_cmd[proxy_index + 1] == proxy_endpoint
def test_add_extension_with_name_but_it_already_exists(self): # Add extension without name first add_extension(source=MY_EXT_SOURCE) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_NAME], MY_EXT_NAME) # Now add using name computed_extension_sha256 = _compute_file_hash(MY_EXT_SOURCE) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, computed_extension_sha256)): with mock.patch('azure.cli.command_modules.extension.custom.logger') as mock_logger: add_extension(extension_name=MY_EXT_NAME) call_args = mock_logger.warning.call_args self.assertEqual("The extension '%s' already exists.", call_args[0][0]) self.assertEqual(MY_EXT_NAME, call_args[0][1]) self.assertEqual(mock_logger.warning.call_count, 1)
def test_add_list_show_remove_extension_extra_index_url(self): """ Tests extension addition while specifying --extra-index-url parameter. :return: """ extra_index_urls = ['https://testpypi.python.org/simple', 'https://pypi.python.org/simple'] add_extension(source=MY_EXT_SOURCE, pip_extra_index_urls=extra_index_urls) actual = list_extensions() self.assertEqual(len(actual), 1) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_NAME], MY_EXT_NAME) remove_extension(MY_EXT_NAME) num_exts = len(list_extensions()) self.assertEqual(num_exts, 0)
def test_update_extension_extra_index_url(self): """ Tests extension update while specifying --extra-index-url parameter. :return: """ extra_index_urls = ['https://testpypi.python.org/simple', 'https://pypi.python.org/simple'] add_extension(source=MY_EXT_SOURCE, pip_extra_index_urls=extra_index_urls) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.3+dev') newer_extension = _get_test_data_file('myfirstcliextension-0.0.4+dev-py2.py3-none-any.whl') computed_extension_sha256 = _compute_file_hash(newer_extension) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(newer_extension, computed_extension_sha256)): update_extension(MY_EXT_NAME, pip_extra_index_urls=extra_index_urls) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.4+dev')
def test_update_extension_verify_no_pip_proxy(self): add_extension(source=MY_EXT_SOURCE) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.3+dev') newer_extension = _get_test_data_file('myfirstcliextension-0.0.4+dev-py2.py3-none-any.whl') computed_extension_sha256 = _compute_file_hash(newer_extension) with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, computed_extension_sha256)), \ mock.patch('azure.cli.command_modules.extension.custom.shutil'), \ mock.patch('azure.cli.command_modules.extension.custom.is_valid_sha256sum', return_value=(True, computed_extension_sha256)), \ mock.patch('azure.cli.command_modules.extension.custom.extension_exists', return_value=None), \ mock.patch('azure.cli.command_modules.extension.custom.check_output') as check_output: update_extension(MY_EXT_NAME) args = check_output.call_args pip_cmd = args[0][0] if '--proxy' in pip_cmd: raise AssertionError("proxy parameter in check_output args although no proxy specified")
def test_update_extension_with_pip_proxy(self): add_extension(source=MY_EXT_SOURCE) ext = show_extension(MY_EXT_NAME) self.assertEqual(ext[OUT_KEY_VERSION], '0.0.3+dev') newer_extension = _get_test_data_file('myfirstcliextension-0.0.4+dev-py2.py3-none-any.whl') computed_extension_sha256 = _compute_file_hash(newer_extension) proxy_param = '--proxy' proxy_endpoint = "https://*****:*****@proxy.microsoft.com" with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=(MY_EXT_SOURCE, computed_extension_sha256)), \ mock.patch('azure.cli.command_modules.extension.custom.shutil'), \ mock.patch('azure.cli.command_modules.extension.custom.is_valid_sha256sum', return_value=(True, computed_extension_sha256)), \ mock.patch('azure.cli.command_modules.extension.custom.extension_exists', return_value=None), \ mock.patch('azure.cli.command_modules.extension.custom.check_output') as check_output: update_extension(MY_EXT_NAME, pip_proxy=proxy_endpoint) args = check_output.call_args pip_cmd = args[0][0] proxy_index = pip_cmd.index(proxy_param) assert pip_cmd[proxy_index + 1] == proxy_endpoint
def test_add_extension_twice(self): add_extension(MY_EXT_SOURCE) num_exts = len(list_extensions()) self.assertEqual(num_exts, 1) with self.assertRaises(CLIError): add_extension(MY_EXT_SOURCE)
def test_add_extension_valid_whl_name_filenotfound(self): with self.assertRaises(CLIError): add_extension( _get_test_data_file('mywheel-0.0.3+dev-py2.py3-none-any.whl')) actual = list_extensions() self.assertEqual(len(actual), 0)
def test_add_extension_with_name_source_not_whl(self): extension_name = 'myextension' with mock.patch('azure.cli.command_modules.extension.custom.resolve_from_index', return_value=('{}.notwhl'.format(extension_name), None)): with self.assertRaises(ValueError) as err: add_extension(extension_name=extension_name) self.assertTrue('Unknown extension type. Only Python wheels are supported.' in str(err.exception))
def test_add_extension_invalid_whl_name(self): with self.assertRaises(CLIError): add_extension(os.path.join('invalid', 'ext', 'path', 'file.whl')) actual = list_extensions() self.assertEqual(len(actual), 0)
def test_add_extension_invalid(self): with self.assertRaises(ValueError): add_extension(MY_BAD_EXT_SOURCE) actual = list_extensions() self.assertEqual(len(actual), 0)
def test_add_extension_valid_whl_name_filenotfound(self): with self.assertRaises(CLIError): add_extension(_get_test_data_file('mywheel-0.0.3+dev-py2.py3-none-any.whl')) actual = list_extensions() self.assertEqual(len(actual), 0)