def test_handle_call_errors(self, stderr): """Test handle_call error messages""" arguments = [] # Run it and check immediate output # argument is empty self.assert_system_exit( handle_call, self.options, self.session, arguments, stderr=self.format_error_message( "Please specify the name of the XML-RPC method"), activate_session=None) self.activate_session_mock.assert_not_called() arguments = ['ssl_login', '--python', '--json-output'] module = { 'ast': "The ast module is required to read python syntax", 'json': "The json module is required to output JSON syntax", } for mod, msg in module.items(): with mock.patch('koji_cli.commands.%s' % mod, new=None): with self.assertRaises(SystemExit) as ex: handle_call(self.options, self.session, arguments) self.assertExitCode(ex, 2) expected = self.format_error_message(msg) self.assert_console_message(stderr, expected) self.activate_session_mock.assert_not_called()
def test_handle_call_errors(self, activate_session_mock, stderr): """Test handle_call error messages""" arguments = [] options = mock.MagicMock() # Mock out the xmlrpc server session = mock.MagicMock() # Run it and check immediate output # argument is empty expected = self.format_error_message( "Please specify the name of the XML-RPC method") self.assert_system_exit(handle_call, options, session, arguments, stderr=expected, activate_session=None) activate_session_mock.assert_not_called() arguments = ['ssl_login', '--python', '--json-output'] module = { 'ast': "The ast module is required to read python syntax", 'json': "The json module is required to output JSON syntax", } for mod, msg in module.items(): with mock.patch('koji_cli.commands.%s' % mod, new=None), \ self.assertRaises(SystemExit) as cm: handle_call(options, session, arguments) expected = self.format_error_message(msg) self.assert_console_message(stderr, expected) activate_session_mock.assert_not_called() self.assertEqual(cm.exception.code, 2)
def test_handle_call(self, stdout): """Test handle_call function""" arguments = ['ssl_login', 'cert=/etc/pki/cert', 'debug'] response = "SUCCESS" self.session.ssl_login.return_value = response handle_call(self.options, self.session, arguments) self.activate_session_mock.assert_called_with(self.session, self.options) self.session.ssl_login.assert_called_with('debug', cert='/etc/pki/cert') self.assert_console_message(stdout, "'%s'\n" % response)
def test_handle_call(self, activate_session_mock, stdout): """Test handle_call function""" arguments = ['ssl_login', 'cert=/etc/pki/cert', 'debug'] response = "SUCCESS" options = mock.MagicMock() # Mock out the xmlrpc server session = mock.MagicMock() session.ssl_login.return_value = response handle_call(options, session, arguments) activate_session_mock.assert_called_with(session, options) session.ssl_login.assert_called_with('debug', cert='/etc/pki/cert') self.assert_console_message(stdout, "'%s'\n" % response)
def test_handle_call(self, activate_session_mock, stdout): """Test handle_call function""" arguments = ['ssl_login', 'cert=/etc/pki/cert', 'debug'] response = "SUCCESS" options = mock.MagicMock() # Mock out the xmlrpc server session = mock.MagicMock() session.ssl_login.return_value = response handle_call(options, session, arguments) activate_session_mock.assert_called_with(session, options) session.ssl_login.assert_called_with('debug', cert='/etc/pki/cert') self.assert_console_message(stdout, "'%s'\n" % response)
def test_handle_call_python_syntax(self, stdout): """Test handle_call with python syntax""" response = ["SUCCESS", "FAKE-RESPONSE"] self.session.ssl_login.return_value = response[1] # Invalid python syntax arguments = ['ssl_login', 'cert=/etc/pki/cert', '--python'] with self.assertRaises(SyntaxError, msg='invalid syntax'): handle_call(self.options, self.session, arguments) arguments = ['ssl_login', '--kwargs', '{"cert":"/etc/pki/cert"}'] handle_call(self.options, self.session, arguments) self.activate_session_mock.assert_called_with(self.session, self.options) self.session.ssl_login.assert_called_with(cert='/etc/pki/cert') self.assert_console_message(stdout, "'%s'\n" % response[1])
def test_handle_call_python_syntax(self, activate_session_mock, stdout): """Test handle_call with python syntax""" arguments = [] response = ["SUCCESS", "FAKE-RESPONSE"] options = mock.MagicMock() # Mock out the xmlrpc server session = mock.MagicMock() session.ssl_login.return_value = response[1] # Invalid python syntax arguments = ['ssl_login', 'cert=/etc/pki/cert', '--python'] with self.assertRaises(SyntaxError, msg='invalid syntax'): handle_call(options, session, arguments) arguments = ['ssl_login', '--kwargs', '{"cert":"/etc/pki/cert"}'] handle_call(options, session, arguments) activate_session_mock.assert_called_with(session, options) session.ssl_login.assert_called_with(cert='/etc/pki/cert') self.assert_console_message(stdout, "'%s'\n" % response[1])
def test_handle_call_json_output(self, stdout): """Test handle_call with json output""" arguments = ['ssl_login', 'cert=/etc/pki/cert', '--json-output'] response = { 'method': 'ssl_login', 'parameters': { 'cert': '/etc/pki/cert', }, 'result': 'success' } self.session.ssl_login.return_value = response handle_call(self.options, self.session, arguments) self.activate_session_mock.assert_called_with(self.session, self.options) self.session.ssl_login.assert_called_with(cert='/etc/pki/cert') expect = json.dumps(response, indent=2, separators=(',', ': ')) self.assert_console_message(stdout, '%s\n' % expect)
def test_handle_call_errors(self, activate_session_mock, stderr): """Test handle_call error messages""" arguments = [] options = mock.MagicMock() # Mock out the xmlrpc server session = mock.MagicMock() # Run it and check immediate output # argument is empty expected = self.format_error_message( "Please specify the name of the XML-RPC method") self.assert_system_exit( handle_call, options, session, arguments, stderr=expected, activate_session=None) activate_session_mock.assert_not_called() arguments = ['ssl_login', '--python', '--json-output'] module = { 'ast': "The ast module is required to read python syntax", 'json': "The json module is required to output JSON syntax", } for mod, msg in module.items(): with mock.patch('koji_cli.commands.%s' % mod, new=None): with self.assertRaises(SystemExit) as cm: handle_call(options, session, arguments) expected = self.format_error_message(msg) self.assert_console_message(stderr, expected) activate_session_mock.assert_not_called() if isinstance(cm.exception, int): self.assertEqual(cm.exception, 2) else: self.assertEqual(cm.exception.code, 2)
def test_handle_call_json_output(self, activate_session_mock, stdout): """Test handle_call with json output""" arguments = ['ssl_login', 'cert=/etc/pki/cert', '--json-output'] options = mock.MagicMock() response = { 'method': 'ssl_login', 'parameters': { 'cert': '/etc/pki/cert', 'ca': ['/etc/pki/clientca', '/etc/pki/serverca'], }, 'result': 'success' } # Mock out the xmlrpc server session = mock.MagicMock() session.ssl_login.return_value = response handle_call(options, session, arguments) activate_session_mock.assert_called_with(session, options) session.ssl_login.assert_called_with(cert='/etc/pki/cert') expect = json.dumps(response, indent=2, separators=(',', ': ')) self.assert_console_message(stdout, '%s\n' % expect)
def test_handle_call_json_output(self, activate_session_mock, stdout): """Test handle_call with json output""" arguments = ['ssl_login', 'cert=/etc/pki/cert', '--json-output'] options = mock.MagicMock() response = { 'method': 'ssl_login', 'parameters': { 'cert': '/etc/pki/cert', 'ca': ['/etc/pki/clientca', '/etc/pki/serverca'], }, 'result': 'success' } # Mock out the xmlrpc server session = mock.MagicMock() session.ssl_login.return_value = response handle_call(options, session, arguments) activate_session_mock.assert_called_with(session, options) session.ssl_login.assert_called_with(cert='/etc/pki/cert') expect = json.dumps(response, indent=2, separators=(',', ': ')) self.assert_console_message(stdout, '%s\n' % expect)