本文整理汇总了Python中azure.cli.core.mock.DummyCli类的典型用法代码示例。如果您正苦于以下问题:Python DummyCli类的具体用法?Python DummyCli怎么用?Python DummyCli使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DummyCli类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_supported_api_version_invalid_rt_for_profile
def test_supported_api_version_invalid_rt_for_profile(self):
cli = DummyCli()
cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile')
test_profile = {'2017-01-01-profile': {ResourceType.MGMT_STORAGE: '2020-10-10'}}
with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile):
with self.assertRaises(APIVersionException):
supported_api_version(cli, ResourceType.MGMT_COMPUTE, min_api='2020-01-01')
示例2: test_case_insensitive_enum_choices
def test_case_insensitive_enum_choices(self):
from enum import Enum
class TestEnum(Enum): # pylint: disable=too-few-public-methods
opt1 = "ALL_CAPS"
opt2 = "camelCase"
opt3 = "snake_case"
def test_handler():
pass
cli = DummyCli()
cli.loader = mock.MagicMock()
cli.loader.cli_ctx = cli
command = AzCliCommand(cli.loader, 'test command', test_handler)
command.add_argument('opt', '--opt', required=True, **enum_choice_list(TestEnum))
cmd_table = {'test command': command}
cli.commands_loader.command_table = cmd_table
parser = AzCliCommandParser(cli)
parser.load_command_table(cli.commands_loader)
args = parser.parse_args('test command --opt alL_cAps'.split())
self.assertEqual(args.opt, 'ALL_CAPS')
args = parser.parse_args('test command --opt CAMELCASE'.split())
self.assertEqual(args.opt, 'camelCase')
args = parser.parse_args('test command --opt sNake_CASE'.split())
self.assertEqual(args.opt, 'snake_case')
示例3: test_get_api_version_semver
def test_get_api_version_semver(self):
# Can get correct resource type API version if semver used
cli = DummyCli()
cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile')
test_profile = {'2017-01-01-profile': {ResourceType.MGMT_KEYVAULT: '7.0'}}
with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile):
self.assertEqual(get_api_version(cli, ResourceType.MGMT_KEYVAULT), '7.0')
示例4: test_register_simple_commands
def test_register_simple_commands(self):
def test_handler1():
pass
def test_handler2():
pass
cli = DummyCli()
cli.loader = mock.MagicMock()
cli.loader.cli_ctx = cli
command = AzCliCommand(cli.loader, 'command the-name', test_handler1)
command2 = AzCliCommand(cli.loader, 'sub-command the-second-name', test_handler2)
cmd_table = {'command the-name': command, 'sub-command the-second-name': command2}
cli.commands_loader.command_table = cmd_table
parser = AzCliCommandParser(cli)
parser.load_command_table(cli.commands_loader)
args = parser.parse_args('command the-name'.split())
self.assertIs(args.func, command)
args = parser.parse_args('sub-command the-second-name'.split())
self.assertIs(args.func, command2)
AzCliCommandParser.error = VerifyError(self,)
parser.parse_args('sub-command'.split())
self.assertTrue(AzCliCommandParser.error.called)
示例5: test_get_api_version
def test_get_api_version(self):
# Can get correct resource type API version
cli = DummyCli()
cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile')
test_profile = {'2017-01-01-profile': {ResourceType.MGMT_STORAGE: '2020-10-10'}}
with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile):
self.assertEqual(get_api_version(cli, ResourceType.MGMT_STORAGE), '2020-10-10')
示例6: test_supported_api_version_min_max_constraint_semver
def test_supported_api_version_min_max_constraint_semver(self):
cli = DummyCli()
cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile')
test_profile = {'2017-01-01-profile': {ResourceType.MGMT_KEYVAULT: '7.0'}}
with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile):
self.assertTrue(
supported_api_version(cli, ResourceType.MGMT_KEYVAULT, min_api='6.0', max_api='8.0'))
示例7: test_supported_api_version_min_max_constraint
def test_supported_api_version_min_max_constraint(self):
cli = DummyCli()
cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile')
test_profile = {'2017-01-01-profile': {ResourceType.MGMT_STORAGE: '2020-10-10'}}
with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile):
self.assertTrue(
supported_api_version(cli, ResourceType.MGMT_STORAGE, min_api='2020-01-01', max_api='2021-01-01'))
示例8: test_help_loads
def test_help_loads(self):
from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument
import knack.events as events
parser_dict = {}
cli = DummyCli()
help_ctx = cli.help_cls(cli)
try:
cli.invoke(['-h'])
except SystemExit:
pass
cmd_tbl = cli.invocation.commands_loader.command_table
cli.invocation.parser.load_command_table(cli.invocation.commands_loader)
for cmd in cmd_tbl:
try:
cmd_tbl[cmd].loader.command_name = cmd
cmd_tbl[cmd].loader.load_arguments(cmd)
except KeyError:
pass
cli.register_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, register_global_subscription_argument)
cli.register_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, register_ids_argument)
cli.raise_event(events.EVENT_INVOKER_CMD_TBL_LOADED, command_table=cmd_tbl)
cli.invocation.parser.load_command_table(cli.invocation.commands_loader)
_store_parsers(cli.invocation.parser, parser_dict)
# TODO: do we want to update this as it doesn't actually load all help.
# We do have a CLI linter which does indeed load all help.
for name, parser in parser_dict.items():
try:
help_file = GroupHelpFile(help_ctx, name, parser) if _is_group(parser) \
else CliCommandHelpFile(help_ctx, name, parser)
help_file.load(parser)
except Exception as ex:
raise HelpAuthoringException('{}, {}'.format(name, ex))
示例9: test_get_api_version_invalid_rt_2
def test_get_api_version_invalid_rt_2(self):
# None is not a valid resource type
cli = DummyCli()
cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile')
test_profile = {'2017-01-01-profile': {ResourceType.MGMT_STORAGE: '2020-10-10'}}
with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile):
with self.assertRaises(APIVersionException):
get_api_version(cli, None)
示例10: test_get_api_version_invalid_active_profile
def test_get_api_version_invalid_active_profile(self):
# The active profile is not in our profile dict
cli = DummyCli()
cli.cloud = Cloud('TestCloud', profile='not-a-real-profile')
test_profile = {'2017-01-01-profile': {ResourceType.MGMT_STORAGE: '2020-10-10'}}
with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile):
with self.assertRaises(APIVersionException):
get_api_version(cli, ResourceType.MGMT_STORAGE)
示例11: test_client_request_id_is_refreshed_correctly
def test_client_request_id_is_refreshed_correctly(self):
cli = DummyCli()
cli.refresh_request_id()
self.assertIn('x-ms-client-request-id', cli.data['headers'])
old_id = cli.data['headers']['x-ms-client-request-id']
cli.refresh_request_id()
self.assertIn('x-ms-client-request-id', cli.data['headers'])
self.assertNotEquals(old_id, cli.data['headers']['x-ms-client-request-id'])
示例12: test_when_alias_doc_is_missing
def test_when_alias_doc_is_missing(self, mock_get_active_cloud):
from azure.cli.command_modules.vm._actions import load_images_from_aliases_doc
p = mock.PropertyMock(side_effect=CloudEndpointNotSetException(''))
mock_cloud = mock.MagicMock()
type(mock_cloud.endpoints).vm_image_alias_doc = p
mock_get_active_cloud.return_value = mock_cloud
# assert
cli_ctx = DummyCli()
cli_ctx.cloud = mock_cloud
with self.assertRaises(CLIError):
load_images_from_aliases_doc(cli_ctx)
示例13: test_parser_error_spellchecker
def test_parser_error_spellchecker(self):
cli = DummyCli()
main_loader = MainCommandsLoader(cli)
cli.loader = main_loader
cli.loader.load_command_table(None)
parser = cli.parser_cls(cli)
parser.load_command_table(cli.loader)
logger_msgs = []
choice_lists = []
original_get_close_matches = difflib.get_close_matches
def mock_log_error(_, msg):
logger_msgs.append(msg)
def mock_get_close_matches(*args, **kwargs):
choice_lists.append(original_get_close_matches(*args, **kwargs))
# run multiple faulty commands and save error logs, as well as close matches
with mock.patch('logging.Logger.error', mock_log_error), \
mock.patch('difflib.get_close_matches', mock_get_close_matches):
faulty_cmd_args = [
'test module1 --opt enum_1',
'test extension1 --opt enum_1',
'test foo_bar --opt enum_3',
'test module --opt enum_3',
'test extension --opt enum_3'
]
for text in faulty_cmd_args:
with self.assertRaises(SystemExit):
parser.parse_args(text.split())
parser.parse_args('test module --opt enum_1'.split())
# assert the right type of error msg is logged for command vs argument parsing
self.assertEqual(len(logger_msgs), 5)
for msg in logger_msgs[:3]:
self.assertIn("not in the", msg)
self.assertIn("command group", msg)
for msg in logger_msgs[3:]:
self.assertIn("not a valid value for '--opt'.", msg)
# assert the right choices are matched as "close".
# If these don't hold, matching algorithm should be deemed flawed.
for choices in choice_lists[:2]:
self.assertEqual(len(choices), 1)
self.assertEqual(len(choice_lists[2]), 0)
for choices in choice_lists[3:]:
self.assertEqual(len(choices), 2)
for choice in ['enum_1', 'enum_2']:
self.assertIn(choice, choices)
示例14: mock_echo_args
def mock_echo_args(command_name, parameters):
from azure.cli.core.mock import DummyCli
try:
# TODO: continue work on this...
argv = ' '.join((command_name, parameters)).split()
cli = DummyCli()
cli.invoke(argv)
command_table = cli.invocation.commands_loader.command_table
prefunc = command_table[command_name].handler
command_table[command_name].handler = lambda args: args
cli.invoke(argv)
parsed_namespace = None # continue this too...
return parsed_namespace
finally:
command_table[command_name].handler = prefunc
示例15: test_register_command_from_extension
def test_register_command_from_extension(self):
from azure.cli.core.commands import _load_command_loader
cli = DummyCli()
main_loader = MainCommandsLoader(cli)
cli.loader = main_loader
cmd_tbl = cli.loader.load_command_table(None)
ext1 = cmd_tbl['hello noodle']
ext2 = cmd_tbl['hello world']
self.assertTrue(isinstance(ext1.command_source, ExtensionCommandSource))
self.assertFalse(ext1.command_source.overrides_command)
self.assertTrue(isinstance(ext2.command_source, ExtensionCommandSource))
self.assertTrue(ext2.command_source.overrides_command)