Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jboss fix #55616

Merged
merged 7 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions salt/modules/jboss7_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def run_command(jboss_config, command, fail_on_error=True):

salt '*' jboss7_cli.run_command '{"cli_path": "integration.modules.sysmod.SysModuleTest.test_valid_docs", "controller": "10.11.12.13:9999", "cli_user": "jbossadm", "cli_password": "jbossadm"}' my_command
'''
cli_command_result = __call_cli(jboss_config, command)
cli_command_result = _call_cli(jboss_config, command)

if cli_command_result['retcode'] == 0:
cli_command_result['success'] = True
Expand Down Expand Up @@ -104,7 +104,7 @@ def run_operation(jboss_config, operation, fail_on_error=True, retries=1):

salt '*' jboss7_cli.run_operation '{"cli_path": "integration.modules.sysmod.SysModuleTest.test_valid_docs", "controller": "10.11.12.13:9999", "cli_user": "jbossadm", "cli_password": "jbossadm"}' my_operation
'''
cli_command_result = __call_cli(jboss_config, operation, retries)
cli_command_result = _call_cli(jboss_config, operation, retries)

if cli_command_result['retcode'] == 0:
if _is_cli_output(cli_command_result['stdout']):
Expand All @@ -116,8 +116,19 @@ def run_operation(jboss_config, operation, fail_on_error=True, retries=1):
if _is_cli_output(cli_command_result['stdout']):
cli_result = _parse(cli_command_result['stdout'])
cli_result['success'] = False

match = re.search(r'^(JBAS\d+):', cli_result['failure-description'])
cli_result['err_code'] = match.group(1)
# if match is None then check for wildfly error code
if match is None:
match = re.search(r'^(WFLYCTL\d+):', cli_result['failure-description'])

if match is not None:
cli_result['err_code'] = match.group(1)
else:
# Could not find err_code
log.error("Jboss 7 operation failed! Error Code could not be found!")
cli_result['err_code'] = '-1'

cli_result['stdout'] = cli_command_result['stdout']
else:
if fail_on_error:
Expand All @@ -132,7 +143,7 @@ def run_operation(jboss_config, operation, fail_on_error=True, retries=1):
return cli_result


def __call_cli(jboss_config, command, retries=1):
def _call_cli(jboss_config, command, retries=1):
command_segments = [
jboss_config['cli_path'],
'--connect',
Expand All @@ -158,11 +169,11 @@ def __call_cli(jboss_config, command, retries=1):
if cli_command_result['retcode'] == 1 and 'Unable to authenticate against controller' in cli_command_result['stderr']:
raise CommandExecutionError('Could not authenticate against controller, please check username and password for the management console. Err code: {retcode}, stdout: {stdout}, stderr: {stderr}'.format(**cli_command_result))

# It may happen that eventhough server is up it may not respond to the call
# TODO add WFLYCTL code
if cli_command_result['retcode'] == 1 and 'JBAS012144' in cli_command_result['stderr'] and retries > 0: # Cannot connect to cli
log.debug('Command failed, retrying... (%d tries left)', retries)
time.sleep(3)
return __call_cli(jboss_config, command, retries - 1)
return _call_cli(jboss_config, command, retries - 1)

return cli_command_result

Expand Down
4 changes: 2 additions & 2 deletions salt/states/jboss7.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def datasource_exists(name, jboss_config, datasource_properties, recreate=False,
read_result = __salt__['jboss7.read_datasource'](jboss_config=jboss_config, name=name, profile=profile)
ds_new_properties = read_result['result']
else:
if ds_result['err_code'] == 'JBAS014807': # ok, resource not exists:
if ds_result['err_code'] in ('JBAS014807', 'WFLYCTL0216'): # ok, resource not exists:
create_result = __salt__['jboss7.create_datasource'](jboss_config=jboss_config, name=name, datasource_properties=datasource_properties, profile=profile)
if create_result['success']:
read_result = __salt__['jboss7.read_datasource'](jboss_config=jboss_config, name=name, profile=profile)
Expand Down Expand Up @@ -246,7 +246,7 @@ def bindings_exist(name, jboss_config, bindings, profile=None):
else:
raise CommandExecutionError(update_result['failure-description'])
else:
if query_result['err_code'] == 'JBAS014807': # ok, resource not exists:
if query_result['err_code'] in ('JBAS014807', 'WFLYCTL0216'): # ok, resource not exists:
create_result = __salt__['jboss7.create_simple_binding'](binding_name=key, value=value, jboss_config=jboss_config, profile=profile)
if create_result['success']:
has_changed = True
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/modules/test_jboss7_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import salt.modules.jboss7_cli as jboss7_cli
from salt.exceptions import CommandExecutionError

from tests.support.mock import patch


class CmdMock(object):
commands = []
Expand Down Expand Up @@ -430,3 +432,17 @@ def test_escaping_operation_with_backslashes_and_quotes(self):
jboss7_cli.run_operation(self.jboss_config, operation)

self.assertEqual(self.cmd.get_last_command(), r'/opt/jboss/jboss-eap-6.0.1/bin/jboss-cli.sh --connect --controller="123.234.345.456:9999" --user="jbossadm" --password="jbossadm" --command="/subsystem=naming/binding=\"java:/sampleapp/web-module/ldap/username\":add(binding-type=simple, value=\"DOMAIN\\\\\\\\user\")"')

def test_run_operation_wflyctl_error(self):
call_cli_ret = {'retcode': 1,
'stdout': '{"failure-description" => "WFLYCTL0234523: ops"}'}
with patch('salt.modules.jboss7_cli._call_cli', return_value=call_cli_ret) as _call_cli:
ret = jboss7_cli.run_operation(None, "ls", False)
self.assertEqual(ret['err_code'], "WFLYCTL0234523")

def test_run_operation_no_code_error(self):
call_cli_ret = {'retcode': 1,
'stdout': '{"failure-description" => "ERROR234523: ops"}'}
with patch('salt.modules.jboss7_cli._call_cli', return_value=call_cli_ret) as _call_cli:
ret = jboss7_cli.run_operation(None, "ls", False)
self.assertEqual(ret['err_code'], "-1")
57 changes: 57 additions & 0 deletions tests/unit/states/test_jboss7.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,60 @@ def update_func(jboss_config, binding_name, value, profile):
self.fail('An exception should be thrown')
except CommandExecutionError as e:
self.assertEqual(six.text_type(e), 'Incorrect binding name.')

def test_datasource_exist_create_datasource_good_code(self):
jboss_config = {'cli_path': '/home/ch44d/Desktop/wildfly-18.0.0.Final/bin/jboss-cli.sh',
'controller': '127.0.0.1: 9990',
'cli_user': 'user',
'cli_password': 'user'}

datasource_properties = {'driver - name': 'h2',
'connection - url': 'jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_s2',
'jndi - name': 'java:/home/ch44d/Desktop/sqljdbc_7.4/enu/mssql-jdbc-7.4.1.jre8.jar',
'user - name': 'user',
'password': 'user',
'use - java - context': True}

read_datasource = MagicMock(return_value={'success': False,
'err_code': 'WFLYCTL0216'})

error_msg = 'Error: -1'
create_datasource = MagicMock(return_value={'success': False,
'stdout': error_msg})

with patch.dict(jboss7.__salt__, {'jboss7.read_datasource': read_datasource,
'jboss7.create_datasource': create_datasource}):
ret = jboss7.datasource_exists("SQL", jboss_config, datasource_properties)

self.assertTrue('result' in ret)
self.assertFalse(ret['result'])
self.assertTrue('comment' in ret)
self.assertTrue(error_msg in ret['comment'])

read_datasource.assert_called_once()
create_datasource.assert_called_once()

def test_datasource_exist_create_datasource_bad_code(self):
jboss_config = {'cli_path': '/home/ch44d/Desktop/wildfly-18.0.0.Final/bin/jboss-cli.sh',
'controller': '127.0.0.1: 9990',
'cli_user': 'user',
'cli_password': 'user'}

datasource_properties = {'driver - name': 'h2',
'connection - url': 'jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_s2',
'jndi - name': 'java:/home/ch44d/Desktop/sqljdbc_7.4/enu/mssql-jdbc-7.4.1.jre8.jar',
'user - name': 'user',
'password': 'user',
'use - java - context': True}

read_datasource = MagicMock(return_value={'success': False,
'err_code': 'WFLYCTL0217',
'failure-description': 'Something happened'})

with patch.dict(jboss7.__salt__, {'jboss7.read_datasource': read_datasource}):
self.assertRaises(CommandExecutionError,
jboss7.datasource_exists,
'SQL',
jboss_config,
datasource_properties)
read_datasource.assert_called_once()