2016-03-07 17:38:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt testing libs
|
|
|
|
from salttesting import TestCase
|
|
|
|
from salttesting.mock import Mock, patch, mock_open
|
|
|
|
|
|
|
|
# Import Salt libs
|
|
|
|
from salt.states import kapacitor
|
|
|
|
|
|
|
|
kapacitor.__opts__ = {'test': False}
|
|
|
|
kapacitor.__salt__ = {}
|
|
|
|
kapacitor.__env__ = 'test'
|
|
|
|
|
|
|
|
|
|
|
|
def _present(name='testname',
|
|
|
|
tick_script='/tmp/script.tick',
|
|
|
|
task_type='stream',
|
|
|
|
database='testdb',
|
|
|
|
retention_policy='default',
|
|
|
|
enable=True,
|
|
|
|
task=None,
|
|
|
|
define_result=True,
|
|
|
|
enable_result=True,
|
2016-03-09 18:16:40 +00:00
|
|
|
disable_result=True,
|
2016-03-07 17:38:04 +00:00
|
|
|
script='test'):
|
|
|
|
get_mock = Mock(return_value=task)
|
|
|
|
define_mock = Mock(return_value=define_result)
|
|
|
|
enable_mock = Mock(return_value=enable_result)
|
2016-03-09 18:16:40 +00:00
|
|
|
disable_mock = Mock(return_value=disable_result)
|
2016-03-07 17:38:04 +00:00
|
|
|
with patch.dict(kapacitor.__salt__, {
|
|
|
|
'kapacitor.get_task': get_mock,
|
|
|
|
'kapacitor.define_task': define_mock,
|
|
|
|
'kapacitor.enable_task': enable_mock,
|
2016-03-09 18:16:40 +00:00
|
|
|
'kapacitor.disable_task': disable_mock,
|
2016-03-07 17:38:04 +00:00
|
|
|
}):
|
|
|
|
with patch('salt.utils.fopen', mock_open(read_data=script)) as open_mock:
|
|
|
|
retval = kapacitor.task_present(name, tick_script, task_type=task_type,
|
|
|
|
database=database, retention_policy=retention_policy, enable=enable)
|
2016-03-09 18:16:40 +00:00
|
|
|
return retval, get_mock, define_mock, enable_mock, disable_mock
|
2016-03-07 17:38:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KapacitorTestCase(TestCase):
|
|
|
|
def test_task_present_new_task(self):
|
2016-03-09 18:16:40 +00:00
|
|
|
ret, get_mock, define_mock, enable_mock, _ = _present()
|
2016-03-07 17:38:04 +00:00
|
|
|
get_mock.assert_called_once_with('testname')
|
|
|
|
define_mock.assert_called_once_with('testname', '/tmp/script.tick',
|
|
|
|
database='testdb', retention_policy='default', task_type='stream')
|
|
|
|
enable_mock.assert_called_once_with('testname')
|
2016-03-09 18:16:40 +00:00
|
|
|
self.assertIn('TICKscript diff', ret['changes'])
|
2016-03-07 17:38:04 +00:00
|
|
|
self.assertIn('enabled', ret['changes'])
|
2016-03-09 18:16:40 +00:00
|
|
|
self.assertEqual(True, ret['changes']['enabled']['new'])
|
2016-03-07 17:38:04 +00:00
|
|
|
|
|
|
|
def test_task_present_existing_task(self):
|
|
|
|
old_task = {'TICKscript': 'old_task', 'Enabled': True}
|
2016-03-09 18:16:40 +00:00
|
|
|
ret, get_mock, define_mock, enable_mock, _ = _present(task=old_task)
|
2016-03-07 17:38:04 +00:00
|
|
|
get_mock.assert_called_once_with('testname')
|
|
|
|
define_mock.assert_called_once_with('testname', '/tmp/script.tick',
|
|
|
|
database='testdb', retention_policy='default', task_type='stream')
|
|
|
|
self.assertEqual(False, enable_mock.called)
|
2016-03-09 18:16:40 +00:00
|
|
|
self.assertIn('TICKscript diff', ret['changes'])
|
2016-03-07 17:38:04 +00:00
|
|
|
self.assertNotIn('enabled', ret['changes'])
|
|
|
|
|
2016-03-09 18:16:40 +00:00
|
|
|
def test_task_present_existing_task_not_enabled(self):
|
2016-03-07 17:38:04 +00:00
|
|
|
old_task = {'TICKscript': 'test', 'Enabled': False}
|
2016-03-09 18:16:40 +00:00
|
|
|
ret, get_mock, define_mock, enable_mock, _ = _present(task=old_task)
|
2016-03-07 17:38:04 +00:00
|
|
|
get_mock.assert_called_once_with('testname')
|
|
|
|
self.assertEqual(False, define_mock.called)
|
|
|
|
enable_mock.assert_called_once_with('testname')
|
|
|
|
self.assertNotIn('diff', ret['changes'])
|
|
|
|
self.assertIn('enabled', ret['changes'])
|
2016-03-09 18:16:40 +00:00
|
|
|
self.assertEqual(True, ret['changes']['enabled']['new'])
|
|
|
|
|
|
|
|
def test_task_present_disable_existing_task(self):
|
|
|
|
old_task = {'TICKscript': 'test', 'Enabled': True}
|
|
|
|
ret, get_mock, define_mock, _, disable_mock = _present(task=old_task, enable=False)
|
|
|
|
get_mock.assert_called_once_with('testname')
|
|
|
|
self.assertEqual(False, define_mock.called)
|
|
|
|
disable_mock.assert_called_once_with('testname')
|
|
|
|
self.assertNotIn('diff', ret['changes'])
|
|
|
|
self.assertIn('enabled', ret['changes'])
|
|
|
|
self.assertEqual(False, ret['changes']['enabled']['new'])
|