salt/tests/unit/states/jboss7_test.py

236 lines
10 KiB
Python
Raw Normal View History

2014-11-25 10:51:43 +00:00
# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import
# Import Salt testing libs
from salttesting.unit import skipIf, TestCase
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt libs
2014-11-04 10:16:30 +00:00
from salt.states import jboss7
from salt.exceptions import CommandExecutionError
# Import 3rd-party libs
import salt.ext.six as six
2014-11-04 10:16:30 +00:00
try:
# will pass if executed along with other tests
__salt__
except NameError:
from salt.ext.six.moves import builtins as __builtin__
2014-11-04 10:16:30 +00:00
# if executed separately we need to export __salt__ dictionary ourselves
__builtin__.__salt__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
2014-11-04 10:16:30 +00:00
class JBoss7StateTestCase(TestCase):
org_module_functions = {}
2014-11-25 10:51:43 +00:00
2014-11-04 10:16:30 +00:00
def __save_module_functions(self):
for name, val in six.iteritems(jboss7.__dict__):
2014-11-04 10:16:30 +00:00
if callable(val):
self.org_module_functions[name] = val
def __restore_module_functions(self):
for name, val in six.iteritems(self.org_module_functions):
2014-11-04 10:16:30 +00:00
jboss7.__dict__[name] = val
def setUp(self):
self.__save_module_functions()
__salt__['jboss7.read_datasource'] = MagicMock()
__salt__['jboss7.create_datasource'] = MagicMock()
2014-11-25 10:51:43 +00:00
__salt__['jboss7.update_datasource'] = MagicMock()
__salt__['jboss7.remove_datasource'] = MagicMock()
__salt__['jboss7.remove_datasource'] = MagicMock()
__salt__['jboss7.read_simple_binding'] = MagicMock()
__salt__['jboss7.create_simple_binding'] = MagicMock()
__salt__['jboss7.update_simple_binding'] = MagicMock()
2014-11-04 10:16:30 +00:00
def tearDown(self):
self.__restore_module_functions()
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}
2014-11-04 10:16:30 +00:00
def read_func(jboss_config, name):
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
def create_func(jboss_config, name, datasource_properties):
ds_status['created'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
__salt__['jboss7.read_datasource'] = MagicMock(side_effect=read_func)
__salt__['jboss7.create_datasource'] = MagicMock(side_effect=create_func)
# when
result = jboss7.datasource_exists(name='appDS', jboss_config={}, datasource_properties=datasource_properties)
# then
__salt__['jboss7.create_datasource'].assert_called_with(name='appDS', jboss_config={}, datasource_properties=datasource_properties)
self.assertFalse(__salt__['jboss7.update_datasource'].called)
self.assertEqual(result['comment'], 'Datasource created.')
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):
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
def update_func(jboss_config, name, new_properties):
ds_status['updated'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
__salt__['jboss7.read_datasource'] = MagicMock(side_effect=read_func)
__salt__['jboss7.update_datasource'] = MagicMock(side_effect=update_func)
2014-11-25 10:51:43 +00:00
result = jboss7.datasource_exists(name='appDS', jboss_config={}, datasource_properties={'connection-url': 'jdbc:/new-connection-url'})
2014-11-04 10:16:30 +00:00
2014-11-25 10:51:43 +00:00
__salt__['jboss7.update_datasource'].assert_called_with(name='appDS', jboss_config={}, new_properties={'connection-url': 'jdbc:/new-connection-url'})
2014-11-04 10:16:30 +00:00
self.assertFalse(__salt__['jboss7.create_datasource'].called)
self.assertEqual(result['comment'], 'Datasource updated.')
def test_should_recreate_the_datasource_if_specified(self):
__salt__['jboss7.remove_datasource'].return_value = {
'success': True
}
__salt__['jboss7.read_datasource'].return_value = {
'success': True,
2014-11-25 10:51:43 +00:00
'result': {'connection-url': 'jdbc:/same-connection-url'}
2014-11-04 10:16:30 +00:00
}
__salt__['jboss7.create_datasource'].return_value = {
'success': True
}
2014-11-25 10:51:43 +00:00
result = jboss7.datasource_exists(name='appDS', jboss_config={}, datasource_properties={'connection-url': 'jdbc:/same-connection-url'}, recreate=True)
2014-11-04 10:16:30 +00:00
__salt__['jboss7.remove_datasource'].assert_called_with(name='appDS', jboss_config={})
2014-11-25 10:51:43 +00:00
__salt__['jboss7.create_datasource'].assert_called_with(name='appDS', jboss_config={}, datasource_properties={'connection-url': 'jdbc:/same-connection-url'})
2014-11-04 10:16:30 +00:00
self.assertEqual(result['changes']['removed'], 'appDS')
self.assertEqual(result['changes']['created'], 'appDS')
def test_should_inform_if_the_datasource_has_not_changed(self):
__salt__['jboss7.read_datasource'].return_value = {
'success': True,
2014-11-25 10:51:43 +00:00
'result': {'connection-url': 'jdbc:/old-connection-url'}
2014-11-04 10:16:30 +00:00
}
2014-11-25 10:51:43 +00:00
__salt__['jboss7.update_datasource'].return_value = {
2014-11-04 10:16:30 +00:00
'success': True
}
2014-11-25 10:51:43 +00:00
result = jboss7.datasource_exists(name='appDS', jboss_config={}, datasource_properties={'connection-url': 'jdbc:/old-connection-url'})
2014-11-04 10:16:30 +00:00
2014-11-25 10:51:43 +00:00
__salt__['jboss7.update_datasource'].assert_called_with(name='appDS', jboss_config={}, new_properties={'connection-url': 'jdbc:/old-connection-url'})
2014-11-04 10:16:30 +00:00
self.assertFalse(__salt__['jboss7.create_datasource'].called)
self.assertEqual(result['comment'], 'Datasource not changed.')
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
2014-11-04 10:16:30 +00:00
def read_func(jboss_config, binding_name):
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
def create_func(jboss_config, binding_name, value):
binding_status['created'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
__salt__['jboss7.read_simple_binding'] = MagicMock(side_effect=read_func)
__salt__['jboss7.create_simple_binding'] = MagicMock(side_effect=create_func)
# when
2014-11-25 10:51:43 +00:00
result = jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV'})
2014-11-04 10:16:30 +00:00
# then
2014-11-25 10:51:43 +00:00
__salt__['jboss7.create_simple_binding'].assert_called_with(jboss_config={}, binding_name='env', value='DEV')
2014-11-04 10:16:30 +00:00
self.assertEqual(__salt__['jboss7.update_simple_binding'].call_count, 0)
2014-11-25 10:51:43 +00:00
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
2014-11-04 10:16:30 +00:00
def read_func(jboss_config, binding_name):
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
def update_func(jboss_config, binding_name, value):
binding_status['updated'] = True
2014-11-25 10:51:43 +00:00
return {'success': True}
2014-11-04 10:16:30 +00:00
__salt__['jboss7.read_simple_binding'] = MagicMock(side_effect=read_func)
__salt__['jboss7.update_simple_binding'] = MagicMock(side_effect=update_func)
# when
2014-11-25 10:51:43 +00:00
result = jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV2'})
2014-11-04 10:16:30 +00:00
# then
2014-11-25 10:51:43 +00:00
__salt__['jboss7.update_simple_binding'].assert_called_with(jboss_config={}, binding_name='env', value='DEV2')
2014-11-04 10:16:30 +00:00
self.assertEqual(__salt__['jboss7.create_simple_binding'].call_count, 0)
2014-11-25 10:51:43 +00:00
self.assertEqual(result['changes'], {'changed': 'env:DEV->DEV2\n'})
2014-11-04 10:16:30 +00:00
self.assertEqual(result['comment'], 'Bindings changed.')
def test_should_not_update_bindings_if_same(self):
# given
2014-11-25 11:49:14 +00:00
__salt__['jboss7.read_simple_binding'].return_value = {'success': True, 'result': {'value': 'DEV2'}}
2014-11-04 10:16:30 +00:00
# when
2014-11-25 10:51:43 +00:00
result = jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV2'})
2014-11-04 10:16:30 +00:00
# then
self.assertEqual(__salt__['jboss7.create_simple_binding'].call_count, 0)
self.assertEqual(__salt__['jboss7.update_simple_binding'].call_count, 0)
self.assertEqual(result['changes'], {})
self.assertEqual(result['comment'], 'Bindings not changed.')
def test_should_raise_exception_if_cannot_create_binding(self):
def read_func(jboss_config, binding_name):
2014-11-25 10:51:43 +00:00
return {'success': False, 'err_code': 'JBAS014807'}
2014-11-04 10:16:30 +00:00
def create_func(jboss_config, binding_name, value):
2014-11-25 10:51:43 +00:00
return {'success': False, 'failure-description': 'Incorrect binding name.'}
2014-11-04 10:16:30 +00:00
__salt__['jboss7.read_simple_binding'] = MagicMock(side_effect=read_func)
__salt__['jboss7.create_simple_binding'] = MagicMock(side_effect=create_func)
# when
try:
2014-11-25 10:51:43 +00:00
jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': 'DEV2'})
2014-11-04 10:16:30 +00:00
self.fail('An exception should be thrown')
except CommandExecutionError as e:
2014-11-25 10:51:43 +00:00
self.assertEqual(str(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):
2014-11-25 11:49:14 +00:00
return {'success': True, 'result': {'value': 'DEV'}}
2014-11-04 10:16:30 +00:00
def update_func(jboss_config, binding_name, value):
2014-11-25 10:51:43 +00:00
return {'success': False, 'failure-description': 'Incorrect binding name.'}
2014-11-04 10:16:30 +00:00
__salt__['jboss7.read_simple_binding'] = MagicMock(side_effect=read_func)
__salt__['jboss7.update_simple_binding'] = MagicMock(side_effect=update_func)
# when
try:
2014-11-25 10:51:43 +00:00
jboss7.bindings_exist(name='bindings', jboss_config={}, bindings={'env': '!@#!///some weird value'})
2014-11-04 10:16:30 +00:00
self.fail('An exception should be thrown')
except CommandExecutionError as e:
self.assertEqual(str(e), 'Incorrect binding name.')