def test_errors_when_unsupported_global_parameter_in_alias(self): # Unsupported global parameters are: --profile and --debug alias_value = ('myservice myoperation --profile value') command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table, extra_params=['profile']) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) with self.assertRaises(InvalidAliasException): alias_cmd([], FakeParsedArgs(command=self.alias_name))
def test_alias_with_only_service_command(self): alias_value = 'myservice' command_table = self.create_command_table([alias_value]) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) alias_cmd([], FakeParsedArgs(command=self.alias_name)) command_table['myservice'].assert_called_with( [], FakeParsedArgs(command='myservice'))
def test_errors_when_shell_cannot_parse_alias(self): # Ending with a escape character that does not escape anything # is invalid and cannot be properly parsed. alias_value = ('myservice myoperation \\') command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) with self.assertRaises(InvalidAliasException): alias_cmd([], FakeParsedArgs(command=self.alias_name))
def test_properly_preserves_quoted_values(self): alias_value = ('myservice myoperation --my-parameter \' \n$""\'') command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) alias_cmd([], FakeParsedArgs(command=self.alias_name)) command_table['myservice'].assert_called_with( ['myoperation', '--my-parameter', ' \n$""'], FakeParsedArgs(command='myservice'))
def test_properly_parses_aliases_broken_by_multiple_lines(self): alias_value = ('myservice myoperation \\' '\n--my-parameter val') command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) alias_cmd([], FakeParsedArgs(command=self.alias_name)) command_table['myservice'].assert_called_with( ['myoperation', '--my-parameter', 'val'], FakeParsedArgs(command='myservice'))
def test_alias_with_operation_and_parameters(self): alias_value = 'myservice myoperation --my-parameter val' command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) alias_cmd([], FakeParsedArgs(command=self.alias_name)) command_table['myservice'].assert_called_with( ['myoperation', '--my-parameter', 'val'], FakeParsedArgs(command='myservice'))
def test_alias_then_help_command(self): alias_value = 'myservice myoperation' command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) parsed_globals = FakeParsedArgs(command=self.alias_name) alias_cmd(['help'], parsed_globals) command_table['myservice'].assert_called_with( ['myoperation', 'help'], FakeParsedArgs(command='myservice'))
def test_errors_when_service_command_is_invalid(self): alias_value = 'non-existent-service myoperation' command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) with self.assertRaises(SystemExit): # Even though we catch the system exit, a message will always # be forced to screen because it happened at system exit. # The patch is to ensure it does not get displayed by nosetests. with mock.patch('sys.stderr'): alias_cmd([], FakeParsedArgs(command=self.alias_name))
def test_sets_global_parameters_when_differs_from_defaults(self): alias_value = 'myservice myoperation --global-with-default non-default' command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table) parser.add_argument('--global-with-default', default='default') alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) alias_cmd([], FakeParsedArgs(command=self.alias_name)) command_table['myservice'].assert_called_with( ['myoperation'], FakeParsedArgs(command='myservice', global_with_default='non-default'))
def test_alias_with_operation_and_global_parameters(self): alias_value = 'myservice myoperation --global-param val' command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table, extra_params=['global-param']) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) alias_cmd([], FakeParsedArgs(command=self.alias_name)) command_table['myservice'].assert_called_with(['myoperation'], FakeParsedArgs( command='myservice', global_param='val'))
def test_errors_when_no_service_command(self): alias_value = '--global-param=val' command_table = self.create_command_table(['myservice']) parser = self.create_parser( command_table, extra_params=['global-param']) alias_cmd = ServiceAliasCommand( self.alias_name, alias_value, self.session, command_table, parser) with self.assertRaises(SystemExit): # Even though we catch the system exit, a message will always # be forced to screen because it happened at system exit. # The patch is to ensure it does not get displayed. with mock.patch('sys.stderr'): alias_cmd([], FakeParsedArgs(command=self.alias_name))
def tests_alias_with_shadow_proxy_command(self): alias_value = 'some-service' self.alias_name = alias_value shadow_proxy_command = mock.Mock(spec=CLICommand) shadow_proxy_command.name = alias_value command_table = {} parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser, shadow_proxy_command) command_table[self.alias_name] = alias_cmd alias_cmd([], FakeParsedArgs(command=self.alias_name)) shadow_proxy_command.assert_called_with( [], FakeParsedArgs(command=alias_value))
def test_alias_with_shadow_proxy_command_no_match(self): alias_value = 'some-other-command' self.alias_name = 'some-service' shadow_proxy_command = mock.Mock(spec=CLICommand) shadow_proxy_command.name = 'some-service' command_table = self.create_command_table([alias_value]) parser = self.create_parser(command_table) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser, shadow_proxy_command) command_table[self.alias_name] = alias_cmd alias_cmd([], FakeParsedArgs(command=self.alias_name)) command_table[alias_value].assert_called_with( [], FakeParsedArgs(command=alias_value)) # Even though it was provided, it should not be called because # the alias value did not match the shadow command name. self.assertFalse(shadow_proxy_command.called)
def test_global_parameters_can_be_emitted_and_modified(self): alias_value = 'myservice myoperation --global-param val' command_table = self.create_command_table(['myservice']) parser = self.create_parser(command_table, extra_params=['global-param']) alias_cmd = ServiceAliasCommand(self.alias_name, alias_value, self.session, command_table, parser) def replace_global_param_value_with_foo(event_name, **kwargs): parsed_args = kwargs['parsed_args'] parsed_args.global_param = 'foo' self.session.emit.side_effect = replace_global_param_value_with_foo alias_cmd([], FakeParsedArgs(command=self.alias_name)) self.session.emit.assert_called_with('top-level-args-parsed', parsed_args=mock.ANY, session=self.session) command_table['myservice'].assert_called_with(['myoperation'], FakeParsedArgs( command='myservice', global_param='foo'))