salt/tests/unit/states/test_jboss7.py

263 lines
12 KiB
Python
Raw Normal View History

2014-11-25 10:51:43 +00:00
# -*- coding: utf-8 -*-
2017-02-19 15:35:30 +00:00
# pylint: disable=unused-argument
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt testing libs
2017-02-19 15:35:30 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
2017-02-19 15:35:30 +00:00
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
# Import Salt libs
import salt.states.jboss7 as jboss7
2014-11-04 10:16:30 +00:00
from salt.exceptions import CommandExecutionError
from salt.ext import six
2014-11-04 10:16:30 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class JBoss7StateTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
2017-02-19 15:35:30 +00:00
return {
jboss7: {
'__salt__': {
'jboss7.read_datasource': MagicMock(),
'jboss7.create_datasource': MagicMock(),
'jboss7.update_datasource': MagicMock(),
'jboss7.remove_datasource': MagicMock(),
'jboss7.read_simple_binding': MagicMock(),
'jboss7.create_simple_binding': MagicMock(),
'jboss7.update_simple_binding': MagicMock(),
}
2017-02-19 15:35:30 +00:00
}
}
2014-11-04 10:16:30 +00:00
def test_should_create_new_datasource_if_not_exists(self):
# given
2014-11-25 10:51:43 +00:00
datasource_properties = {'connection-url': 'jdbc:/old-connection-url'}
ds_status = {'created': False}
def read_func(jboss_config, name, profile):
2014-11-04 10:16:30 +00:00
if ds_status['created']:
2014-11-25 11:49:14 +00:00
return {'success': True, 'result': datasource_properties}
2014-11-04 10:16:30 +00:00
else:
2014-11-25 10:51:43 +00:00
return {'success': False, 'err_code': 'JBAS014807'}
2014-11-04 10:16:30 +00:00
2015-09-14 15:55:42 +00:00
def create_func(jboss_config, name, datasource_properties, profile):
2014-11-04 10:16:30 +00:00
ds_status['created'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(side_effect=read_func)
create_mock = MagicMock(side_effect=create_func)
update_mock = MagicMock()
with patch.dict(jboss7.__salt__, {'jboss7.read_datasource': read_mock,
'jboss7.create_datasource': create_mock,
'jboss7.update_datasource': update_mock}):
# when
2018-01-26 13:10:43 +00:00
result = jboss7.datasource_exists(name='appDS', jboss_config={},
datasource_properties=datasource_properties, profile=None)
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
# then
2018-01-26 13:10:43 +00:00
create_mock.assert_called_with(name='appDS', jboss_config={},
datasource_properties=datasource_properties, profile=None)
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
self.assertFalse(update_mock.called)
self.assertEqual(result['comment'], 'Datasource created.')
2014-11-04 10:16:30 +00:00
def test_should_update_the_datasource_if_exists(self):
2014-11-25 10:51:43 +00:00
ds_status = {'updated': False}
2014-11-04 10:16:30 +00:00
def read_func(jboss_config, name, profile):
2014-11-04 10:16:30 +00:00
if ds_status['updated']:
2014-11-25 10:51:43 +00:00
return {'success': True, 'result': {'connection-url': 'jdbc:/new-connection-url'}}
2014-11-04 10:16:30 +00:00
else:
2014-11-25 11:49:14 +00:00
return {'success': True, 'result': {'connection-url': 'jdbc:/old-connection-url'}}
2014-11-04 10:16:30 +00:00
2015-09-14 15:55:42 +00:00
def update_func(jboss_config, name, new_properties, profile):
2014-11-04 10:16:30 +00:00
ds_status['updated'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(side_effect=read_func)
create_mock = MagicMock()
update_mock = MagicMock(side_effect=update_func)
with patch.dict(jboss7.__salt__, {'jboss7.read_datasource': read_mock,
'jboss7.create_datasource': create_mock,
'jboss7.update_datasource': update_mock}):
2018-01-26 13:10:43 +00:00
result = jboss7.datasource_exists(name='appDS', jboss_config={},
datasource_properties={'connection-url': 'jdbc:/new-connection-url'},
profile=None)
2014-11-04 10:16:30 +00:00
2018-01-26 13:10:43 +00:00
update_mock.assert_called_with(name='appDS', jboss_config={},
new_properties={'connection-url': 'jdbc:/new-connection-url'},
profile=None)
2017-02-19 15:35:30 +00:00
self.assertTrue(read_mock.called)
self.assertEqual(result['comment'], 'Datasource updated.')
2014-11-04 10:16:30 +00:00
def test_should_recreate_the_datasource_if_specified(self):
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(return_value={'success': True,
'result': {'connection-url': 'jdbc:/same-connection-url'}})
create_mock = MagicMock(return_value={'success': True})
remove_mock = MagicMock(return_value={'success': True})
update_mock = MagicMock()
with patch.dict(jboss7.__salt__, {'jboss7.read_datasource': read_mock,
'jboss7.create_datasource': create_mock,
'jboss7.remove_datasource': remove_mock,
'jboss7.update_datasource': update_mock}):
2018-01-26 13:10:43 +00:00
result = jboss7.datasource_exists(name='appDS', jboss_config={},
datasource_properties={'connection-url': 'jdbc:/same-connection-url'},
recreate=True)
2017-02-19 15:35:30 +00:00
remove_mock.assert_called_with(name='appDS', jboss_config={}, profile=None)
2018-01-26 13:10:43 +00:00
create_mock.assert_called_with(name='appDS', jboss_config={},
datasource_properties={'connection-url': 'jdbc:/same-connection-url'},
profile=None)
2017-02-19 15:35:30 +00:00
self.assertEqual(result['changes']['removed'], 'appDS')
self.assertEqual(result['changes']['created'], 'appDS')
2014-11-04 10:16:30 +00:00
def test_should_inform_if_the_datasource_has_not_changed(self):
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(return_value={'success': True,
'result': {'connection-url': 'jdbc:/same-connection-url'}})
create_mock = MagicMock()
remove_mock = MagicMock()
update_mock = MagicMock(return_value={'success': True})
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
with patch.dict(jboss7.__salt__, {'jboss7.read_datasource': read_mock,
'jboss7.create_datasource': create_mock,
'jboss7.remove_datasource': remove_mock,
'jboss7.update_datasource': update_mock}):
2014-11-04 10:16:30 +00:00
2018-01-26 13:10:43 +00:00
result = jboss7.datasource_exists(name='appDS', jboss_config={},
datasource_properties={'connection-url': 'jdbc:/old-connection-url'})
2017-02-19 15:35:30 +00:00
2018-01-26 13:10:43 +00:00
update_mock.assert_called_with(name='appDS', jboss_config={},
new_properties={'connection-url': 'jdbc:/old-connection-url'}, profile=None)
2017-02-19 15:35:30 +00:00
self.assertFalse(create_mock.called)
self.assertEqual(result['comment'], 'Datasource not changed.')
2014-11-04 10:16:30 +00:00
def test_should_create_binding_if_not_exists(self):
# given
2014-11-25 10:51:43 +00:00
binding_status = {'created': False}
2014-11-25 11:49:14 +00:00
def read_func(jboss_config, binding_name, profile):
2014-11-04 10:16:30 +00:00
if binding_status['created']:
2014-11-25 11:49:14 +00:00
return {'success': True, 'result': {'value': 'DEV'}}
2014-11-04 10:16:30 +00:00
else:
2014-11-25 10:51:43 +00:00
return {'success': False, 'err_code': 'JBAS014807'}
2014-11-04 10:16:30 +00:00
2015-09-14 15:55:42 +00:00
def create_func(jboss_config, binding_name, value, profile):
2014-11-04 10:16:30 +00:00
binding_status['created'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(side_effect=read_func)
create_mock = MagicMock(side_effect=create_func)
update_mock = MagicMock()
with patch.dict(jboss7.__salt__, {'jboss7.read_simple_binding': read_mock,
'jboss7.create_simple_binding': create_mock,
'jboss7.update_simple_binding': update_mock}):
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
# when
result = jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV'}, profile=None)
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
# then
create_mock.assert_called_with(jboss_config={}, binding_name='env', value='DEV', profile=None)
self.assertEqual(update_mock.call_count, 0)
self.assertEqual(result['changes'], {'added': 'env:DEV\n'})
self.assertEqual(result['comment'], 'Bindings changed.')
2014-11-04 10:16:30 +00:00
def test_should_update_bindings_if_exists_and_different(self):
# given
2014-11-25 10:51:43 +00:00
binding_status = {'updated': False}
2014-11-25 11:49:14 +00:00
def read_func(jboss_config, binding_name, profile):
2014-11-04 10:16:30 +00:00
if binding_status['updated']:
2014-11-25 11:49:14 +00:00
return {'success': True, 'result': {'value': 'DEV2'}}
2014-11-04 10:16:30 +00:00
else:
2014-11-25 11:49:14 +00:00
return {'success': True, 'result': {'value': 'DEV'}}
2014-11-04 10:16:30 +00:00
2015-09-14 15:55:42 +00:00
def update_func(jboss_config, binding_name, value, profile):
2014-11-04 10:16:30 +00:00
binding_status['updated'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(side_effect=read_func)
create_mock = MagicMock()
update_mock = MagicMock(side_effect=update_func)
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
with patch.dict(jboss7.__salt__, {'jboss7.read_simple_binding': read_mock,
'jboss7.create_simple_binding': create_mock,
'jboss7.update_simple_binding': update_mock}):
# when
result = jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV2'}, profile=None)
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
# then
update_mock.assert_called_with(jboss_config={}, binding_name='env', value='DEV2', profile=None)
self.assertEqual(create_mock.call_count, 0)
self.assertEqual(result['changes'], {'changed': 'env:DEV->DEV2\n'})
self.assertEqual(result['comment'], 'Bindings changed.')
2014-11-04 10:16:30 +00:00
def test_should_not_update_bindings_if_same(self):
# given
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(return_value={'success': True, 'result': {'value': 'DEV2'}})
create_mock = MagicMock()
update_mock = MagicMock()
with patch.dict(jboss7.__salt__, {'jboss7.read_simple_binding': read_mock,
'jboss7.create_simple_binding': create_mock,
'jboss7.update_simple_binding': update_mock}):
# when
result = jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV2'})
# then
self.assertEqual(create_mock.call_count, 0)
self.assertEqual(update_mock.call_count, 0)
self.assertEqual(result['changes'], {})
self.assertEqual(result['comment'], 'Bindings not changed.')
2014-11-04 10:16:30 +00:00
def test_should_raise_exception_if_cannot_create_binding(self):
def read_func(jboss_config, binding_name, profile):
2014-11-25 10:51:43 +00:00
return {'success': False, 'err_code': 'JBAS014807'}
2014-11-04 10:16:30 +00:00
2015-09-14 15:55:42 +00:00
def create_func(jboss_config, binding_name, value, profile):
2014-11-25 10:51:43 +00:00
return {'success': False, 'failure-description': 'Incorrect binding name.'}
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(side_effect=read_func)
create_mock = MagicMock(side_effect=create_func)
update_mock = MagicMock()
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
with patch.dict(jboss7.__salt__, {'jboss7.read_simple_binding': read_mock,
'jboss7.create_simple_binding': create_mock,
'jboss7.update_simple_binding': update_mock}):
# when
try:
jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV2'}, profile=None)
self.fail('An exception should be thrown')
except CommandExecutionError as e:
self.assertEqual(six.text_type(e), 'Incorrect binding name.')
2014-11-04 10:16:30 +00:00
def test_should_raise_exception_if_cannot_update_binding(self):
def read_func(jboss_config, binding_name, profile):
2014-11-25 11:49:14 +00:00
return {'success': True, 'result': {'value': 'DEV'}}
2014-11-04 10:16:30 +00:00
2015-09-14 15:55:42 +00:00
def update_func(jboss_config, binding_name, value, profile):
2014-11-25 10:51:43 +00:00
return {'success': False, 'failure-description': 'Incorrect binding name.'}
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
read_mock = MagicMock(side_effect=read_func)
create_mock = MagicMock()
update_mock = MagicMock(side_effect=update_func)
with patch.dict(jboss7.__salt__, {'jboss7.read_simple_binding': read_mock,
'jboss7.create_simple_binding': create_mock,
'jboss7.update_simple_binding': update_mock}):
2014-11-04 10:16:30 +00:00
2017-02-19 15:35:30 +00:00
# when
try:
2018-01-26 13:10:43 +00:00
jboss7.bindings_exist(name='bindings', jboss_config={},
bindings={'env': '!@#!///some weird value'},
profile=None)
2017-02-19 15:35:30 +00:00
self.fail('An exception should be thrown')
except CommandExecutionError as e:
self.assertEqual(six.text_type(e), 'Incorrect binding name.')