Skip to content

Commit

Permalink
Merge pull request #54657 from garethgreenaway/2019_2_1_port_49250
Browse files Browse the repository at this point in the history
[master] Porting #49250 to master
  • Loading branch information
dwoz authored Dec 27, 2019
2 parents 2307861 + 14cf3b1 commit 3548367
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 17 deletions.
52 changes: 38 additions & 14 deletions salt/states/jboss7.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ def deployed(name, jboss_config, salt_source=None):
(optional) File on salt master (e.g. salt://application-web-0.39.war). If absent, no files will be retrieved and the artifact in target_file will be used for the deployment.
undeploy:
(optional) Regular expression to match against existing deployments. When present, if there is a deployment that matches the regular expression, it will be undeployed before the new artifact is deployed.
undeploy_force:
(optional) If True, the artifact will be undeployed although it has not changed.
Examples:
Expand Down Expand Up @@ -335,7 +337,7 @@ def deployed(name, jboss_config, salt_source=None):
if not validate_success:
return _error(ret, validate_comment)

resolved_source, get_artifact_comment = __get_artifact(salt_source)
resolved_source, get_artifact_comment, changed = __get_artifact(salt_source)
log.debug('resolved_source=%s', resolved_source)
log.debug('get_artifact_comment=%s', get_artifact_comment)

Expand All @@ -347,24 +349,42 @@ def deployed(name, jboss_config, salt_source=None):
if not find_success:
return _error(ret, find_comment)

require_deployment = True

log.debug('deployment=%s', deployment)
if deployment is not None:
__salt__['jboss7.undeploy'](jboss_config, deployment)
ret['changes']['undeployed'] = deployment

deploy_result = __salt__['jboss7.deploy'](jboss_config=jboss_config, source_file=resolved_source)
log.debug('deploy_result=%s', deploy_result)
if deploy_result['success']:
comment = __append_comment(new_comment='Deployment completed.', current_comment=comment)
ret['comment'] = comment
ret['changes']['deployed'] = resolved_source
else:
comment = __append_comment(new_comment='''Deployment failed\nreturn code={retcode}\nstdout='{stdout}'\nstderr='{stderr}'''.format(**deploy_result), current_comment=comment)
return _error(ret, comment)
if 'undeploy_force' in salt_source:
if salt_source['undeploy_force']:
ret['changes']['undeployed'] = __undeploy(jboss_config, deployment)
else:
if changed:
ret['changes']['undeployed'] = __undeploy(jboss_config, deployment)
else:
require_deployment = False
comment = __append_comment(new_comment='The artifact {} was already deployed'.format(deployment), current_comment=comment)
else:
ret['changes']['undeployed'] = __undeploy(jboss_config, deployment)

if require_deployment:
deploy_result = __salt__['jboss7.deploy'](jboss_config=jboss_config, source_file=resolved_source)
log.debug('deploy_result=%s', str(deploy_result))
if deploy_result['success']:
comment = __append_comment(new_comment='Deployment completed.', current_comment=comment)
ret['changes']['deployed'] = resolved_source
else:
comment = __append_comment(new_comment='''Deployment failed\nreturn code={retcode}\nstdout='{stdout}'\nstderr='{stderr}'''.format(**deploy_result), current_comment=comment)
_error(ret, comment)

ret['comment'] = comment

return ret


def __undeploy(jboss_config, deployment):
__salt__['jboss7.undeploy'](jboss_config, deployment)
return deployment


def __validate_arguments(jboss_config, salt_source):
result, comment = __check_dict_contains(jboss_config, 'jboss_config', ['cli_path', 'controller'])
if salt_source is None:
Expand Down Expand Up @@ -397,6 +417,7 @@ def __find_deployment(jboss_config, salt_source=None):
def __get_artifact(salt_source):
resolved_source = None
comment = None
changed = False

if salt_source is None:
log.debug('salt_source == None')
Expand Down Expand Up @@ -446,6 +467,9 @@ def __get_artifact(salt_source):
else:
comment = manage_result['comment']

if manage_result['changes']:
changed = True

except Exception as e:
log.debug(traceback.format_exc())
comment = 'Unable to manage file: {0}'.format(e)
Expand All @@ -454,7 +478,7 @@ def __get_artifact(salt_source):
resolved_source = salt_source['target_file']
comment = ''

return resolved_source, comment
return resolved_source, comment, changed


def reloaded(name, jboss_config, timeout=60, interval=5):
Expand Down
146 changes: 143 additions & 3 deletions tests/unit/states/test_jboss7.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,152 @@ def setup_loader_modules(self):
'jboss7.read_simple_binding': MagicMock(),
'jboss7.create_simple_binding': MagicMock(),
'jboss7.update_simple_binding': MagicMock(),
}
'jboss7.undeploy': MagicMock(),
'jboss7.deploy': MagicMock,
'file.get_managed': MagicMock,
'file.manage_file': MagicMock,
'jboss7.list_deployments': MagicMock,
},
'__env__': 'base'
}
}

def test_should_not_redeploy_unchanged(self):
# given
parameters = {'target_file': 'some_artifact', 'undeploy_force': False, 'undeploy': 'some_artifact',
'source': 'some_artifact_on_master'}
jboss_conf = {'cli_path': 'somewhere', 'controller': 'some_controller'}

def list_deployments(jboss_config):
return ['some_artifact']

def file_get_managed(name, template, source, source_hash, source_hash_name, user, group, mode, attrs, saltenv,
context, defaults, skip_verify, kwargs):
return 'sfn', 'hash', ''

def file_manage_file(name, sfn, ret, source, source_sum, user, group, mode, attrs, saltenv, backup, makedirs,
template, show_diff, contents, dir_mode):
return {'result': True, 'changes': False}

jboss7_undeploy_mock = MagicMock()
jboss7_deploy_mock = MagicMock()
file_get_managed = MagicMock(side_effect=file_get_managed)
file_manage_file = MagicMock(side_effect=file_manage_file)
list_deployments_mock = MagicMock(side_effect=list_deployments)
with patch.dict(jboss7.__salt__, {'jboss7.undeploy': jboss7_undeploy_mock,
'jboss7.deploy': jboss7_deploy_mock,
'file.get_managed': file_get_managed,
'file.manage_file': file_manage_file,
'jboss7.list_deployments': list_deployments_mock}):
# when
result = jboss7.deployed(name="unchanged", jboss_config=jboss_conf, salt_source=parameters)

# then
self.assertFalse(jboss7_undeploy_mock.called)
self.assertFalse(jboss7_deploy_mock.called)

def test_should_redeploy_changed(self):
# given
parameters = {'target_file': 'some_artifact', 'undeploy_force': False, 'undeploy': 'some_artifact',
'source': 'some_artifact_on_master'}
jboss_conf = {'cli_path': 'somewhere', 'controller': 'some_controller'}

def list_deployments(jboss_config):
return ['some_artifact']

def file_get_managed(name, template, source, source_hash, source_hash_name, user, group, mode, attrs, saltenv,
context, defaults, skip_verify, kwargs):
return 'sfn', 'hash', ''

def file_manage_file(name, sfn, ret, source, source_sum, user, group, mode, attrs, saltenv, backup, makedirs,
template, show_diff, contents, dir_mode):
return {'result': True, 'changes': True}

jboss7_undeploy_mock = MagicMock()
jboss7_deploy_mock = MagicMock()
file_get_managed = MagicMock(side_effect=file_get_managed)
file_manage_file = MagicMock(side_effect=file_manage_file)
list_deployments_mock = MagicMock(side_effect=list_deployments)
with patch.dict(jboss7.__salt__, {'jboss7.undeploy': jboss7_undeploy_mock,
'jboss7.deploy': jboss7_deploy_mock,
'file.get_managed': file_get_managed,
'file.manage_file': file_manage_file,
'jboss7.list_deployments': list_deployments_mock}):
# when
result = jboss7.deployed(name="unchanged", jboss_config=jboss_conf, salt_source=parameters)

# then
self.assertTrue(jboss7_undeploy_mock.called)
self.assertTrue(jboss7_deploy_mock.called)

def test_should_deploy_different_artifact(self):
# given
parameters = {'target_file': 'some_artifact', 'undeploy_force': False, 'undeploy': 'some_artifact',
'source': 'some_artifact_on_master'}
jboss_conf = {'cli_path': 'somewhere', 'controller': 'some_controller'}

def list_deployments(jboss_config):
return ['some_other_artifact']

def file_get_managed(name, template, source, source_hash, source_hash_name, user, group, mode, attrs, saltenv,
context, defaults, skip_verify, kwargs):
return 'sfn', 'hash', ''

def file_manage_file(name, sfn, ret, source, source_sum, user, group, mode, attrs, saltenv, backup, makedirs,
template, show_diff, contents, dir_mode):
return {'result': True, 'changes': False}

jboss7_undeploy_mock = MagicMock()
jboss7_deploy_mock = MagicMock()
file_get_managed = MagicMock(side_effect=file_get_managed)
file_manage_file = MagicMock(side_effect=file_manage_file)
list_deployments_mock = MagicMock(side_effect=list_deployments)
with patch.dict(jboss7.__salt__, {'jboss7.undeploy': jboss7_undeploy_mock,
'jboss7.deploy': jboss7_deploy_mock,
'file.get_managed': file_get_managed,
'file.manage_file': file_manage_file,
'jboss7.list_deployments': list_deployments_mock}):
# when
result = jboss7.deployed(name="unchanged", jboss_config=jboss_conf, salt_source=parameters)

# then
self.assertFalse(jboss7_undeploy_mock.called)
self.assertTrue(jboss7_deploy_mock.called)

def test_should_redploy_undeploy_force(self):
# given
parameters = {'target_file': 'some_artifact', 'undeploy_force': True, 'undeploy': 'some_artifact',
'source': 'some_artifact_on_master'}
jboss_conf = {'cli_path': 'somewhere', 'controller': 'some_controller'}

def list_deployments(jboss_config):
return ['some_artifact']

def file_get_managed(name, template, source, source_hash, source_hash_name, user, group, mode, attrs, saltenv,
context, defaults, skip_verify, kwargs):
return 'sfn', 'hash', ''

def file_manage_file(name, sfn, ret, source, source_sum, user, group, mode, attrs, saltenv, backup, makedirs,
template, show_diff, contents, dir_mode):
return {'result': True, 'changes': False}

jboss7_undeploy_mock = MagicMock()
jboss7_deploy_mock = MagicMock()
file_get_managed = MagicMock(side_effect=file_get_managed)
file_manage_file = MagicMock(side_effect=file_manage_file)
list_deployments_mock = MagicMock(side_effect=list_deployments)
with patch.dict(jboss7.__salt__, {'jboss7.undeploy': jboss7_undeploy_mock,
'jboss7.deploy': jboss7_deploy_mock,
'file.get_managed': file_get_managed,
'file.manage_file': file_manage_file,
'jboss7.list_deployments': list_deployments_mock}):
# when
result = jboss7.deployed(name="unchanged", jboss_config=jboss_conf, salt_source=parameters)

# then
self.assertTrue(jboss7_undeploy_mock.called)
self.assertTrue(jboss7_deploy_mock.called)

def test_should_create_new_datasource_if_not_exists(self):
# given
datasource_properties = {'connection-url': 'jdbc:/old-connection-url'}
Expand Down Expand Up @@ -104,7 +246,6 @@ def test_should_recreate_the_datasource_if_specified(self):
'jboss7.create_datasource': create_mock,
'jboss7.remove_datasource': remove_mock,
'jboss7.update_datasource': update_mock}):

result = jboss7.datasource_exists(name='appDS', jboss_config={},
datasource_properties={'connection-url': 'jdbc:/same-connection-url'},
recreate=True)
Expand All @@ -127,7 +268,6 @@ def test_should_inform_if_the_datasource_has_not_changed(self):
'jboss7.create_datasource': create_mock,
'jboss7.remove_datasource': remove_mock,
'jboss7.update_datasource': update_mock}):

result = jboss7.datasource_exists(name='appDS', jboss_config={},
datasource_properties={'connection-url': 'jdbc:/old-connection-url'})

Expand Down

0 comments on commit 3548367

Please sign in to comment.