From 2ed47962c01994ee72602e38f0e509b1444b05ee Mon Sep 17 00:00:00 2001 From: cmcmarrow Date: Wed, 11 Dec 2019 16:11:58 -0700 Subject: [PATCH 1/5] jboss fix --- salt/modules/jboss7_cli.py | 7 +++++++ salt/states/jboss7.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/salt/modules/jboss7_cli.py b/salt/modules/jboss7_cli.py index a59c88029817..29f9a34ad796 100644 --- a/salt/modules/jboss7_cli.py +++ b/salt/modules/jboss7_cli.py @@ -116,8 +116,14 @@ 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']) + # if match is None then check for wildfly error code + if match is None: + match = re.search(r'^(WFLYCTL\d+):', cli_result['failure-description']) + cli_result['err_code'] = match.group(1) + cli_result['stdout'] = cli_command_result['stdout'] else: if fail_on_error: @@ -159,6 +165,7 @@ def __call_cli(jboss_config, command, retries=1): 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 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) diff --git a/salt/states/jboss7.py b/salt/states/jboss7.py index 891a504591a4..517ad4c3358c 100644 --- a/salt/states/jboss7.py +++ b/salt/states/jboss7.py @@ -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) From 45a83a848943611189d47fc1ee013a0acec5a92e Mon Sep 17 00:00:00 2001 From: cmcmarrow Date: Wed, 11 Dec 2019 16:25:01 -0700 Subject: [PATCH 2/5] more fixs --- salt/modules/jboss7_cli.py | 2 +- salt/states/jboss7.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/salt/modules/jboss7_cli.py b/salt/modules/jboss7_cli.py index 29f9a34ad796..03e287331eb2 100644 --- a/salt/modules/jboss7_cli.py +++ b/salt/modules/jboss7_cli.py @@ -165,7 +165,7 @@ def __call_cli(jboss_config, command, retries=1): 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 + # 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) diff --git a/salt/states/jboss7.py b/salt/states/jboss7.py index 517ad4c3358c..8c36ea3e7a73 100644 --- a/salt/states/jboss7.py +++ b/salt/states/jboss7.py @@ -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'] in {'JBAS014807', 'WFLYCTL0216'}: # 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) @@ -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 From 70036f48e4f2fcb096e637467793d334555e14e9 Mon Sep 17 00:00:00 2001 From: cmcmarrow Date: Tue, 17 Dec 2019 12:56:01 -0700 Subject: [PATCH 3/5] add test --- salt/modules/jboss7_cli.py | 8 ++++---- tests/unit/modules/test_jboss7_cli.py | 12 ++++++++++++ tests/unit/states/test_jboss7.py | 4 ++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/salt/modules/jboss7_cli.py b/salt/modules/jboss7_cli.py index 03e287331eb2..fca813dce751 100644 --- a/salt/modules/jboss7_cli.py +++ b/salt/modules/jboss7_cli.py @@ -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 @@ -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']): @@ -138,7 +138,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', @@ -169,7 +169,7 @@ def __call_cli(jboss_config, command, retries=1): 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 diff --git a/tests/unit/modules/test_jboss7_cli.py b/tests/unit/modules/test_jboss7_cli.py index 30ea5fd62e40..e826da0e4773 100644 --- a/tests/unit/modules/test_jboss7_cli.py +++ b/tests/unit/modules/test_jboss7_cli.py @@ -13,6 +13,15 @@ import salt.modules.jboss7_cli as jboss7_cli from salt.exceptions import CommandExecutionError +from tests.support.mock import ( + Mock, + MagicMock, + patch, + mock_open, + NO_MOCK, + NO_MOCK_REASON +) + class CmdMock(object): commands = [] @@ -430,3 +439,6 @@ 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_support(self): + x = jboss7_cli._call_cli \ No newline at end of file diff --git a/tests/unit/states/test_jboss7.py b/tests/unit/states/test_jboss7.py index 8f1913633f07..799c5ddd3f77 100644 --- a/tests/unit/states/test_jboss7.py +++ b/tests/unit/states/test_jboss7.py @@ -260,3 +260,7 @@ 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_(self): + pass + # TODO From 234af0da75a1b10f341492c9b11f5f6bc57c7ba6 Mon Sep 17 00:00:00 2001 From: cmcmarrow Date: Mon, 6 Jan 2020 12:13:03 -0700 Subject: [PATCH 4/5] add module testwq --- salt/modules/jboss7_cli.py | 8 ++++++-- tests/unit/modules/test_jboss7_cli.py | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/salt/modules/jboss7_cli.py b/salt/modules/jboss7_cli.py index fca813dce751..4295c0ff27f4 100644 --- a/salt/modules/jboss7_cli.py +++ b/salt/modules/jboss7_cli.py @@ -122,7 +122,12 @@ def run_operation(jboss_config, operation, fail_on_error=True, retries=1): if match is None: match = re.search(r'^(WFLYCTL\d+):', cli_result['failure-description']) - cli_result['err_code'] = match.group(1) + 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: @@ -164,7 +169,6 @@ 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) diff --git a/tests/unit/modules/test_jboss7_cli.py b/tests/unit/modules/test_jboss7_cli.py index e826da0e4773..9e2ac5b370e2 100644 --- a/tests/unit/modules/test_jboss7_cli.py +++ b/tests/unit/modules/test_jboss7_cli.py @@ -440,5 +440,16 @@ def test_escaping_operation_with_backslashes_and_quotes(self): 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_support(self): - x = jboss7_cli._call_cli \ No newline at end of file + 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") From bdd1dc9d29e5c0f80feb964707970518225cc979 Mon Sep 17 00:00:00 2001 From: cmcmarrow Date: Mon, 6 Jan 2020 14:19:19 -0700 Subject: [PATCH 5/5] add state test --- tests/unit/modules/test_jboss7_cli.py | 9 +--- tests/unit/states/test_jboss7.py | 59 +++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/tests/unit/modules/test_jboss7_cli.py b/tests/unit/modules/test_jboss7_cli.py index 9e2ac5b370e2..5af2b3d85bd6 100644 --- a/tests/unit/modules/test_jboss7_cli.py +++ b/tests/unit/modules/test_jboss7_cli.py @@ -13,14 +13,7 @@ import salt.modules.jboss7_cli as jboss7_cli from salt.exceptions import CommandExecutionError -from tests.support.mock import ( - Mock, - MagicMock, - patch, - mock_open, - NO_MOCK, - NO_MOCK_REASON -) +from tests.support.mock import patch class CmdMock(object): diff --git a/tests/unit/states/test_jboss7.py b/tests/unit/states/test_jboss7.py index 799c5ddd3f77..4c2ddc030447 100644 --- a/tests/unit/states/test_jboss7.py +++ b/tests/unit/states/test_jboss7.py @@ -261,6 +261,59 @@ def update_func(jboss_config, binding_name, value, profile): except CommandExecutionError as e: self.assertEqual(six.text_type(e), 'Incorrect binding name.') - def test_(self): - pass - # TODO + 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()