2016-03-07 17:38:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt testing libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase
|
|
|
|
from tests.support.mock import Mock, patch, mock_open
|
2016-03-07 17:38:04 +00:00
|
|
|
|
|
|
|
# Import Salt libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.kapacitor as kapacitor
|
2016-03-07 17:38:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-12-29 10:14:24 +00:00
|
|
|
script='testscript'):
|
2016-05-19 18:03:03 +00:00
|
|
|
'''
|
|
|
|
Run a "kapacitor.present" state after setting up mocks, and return the
|
|
|
|
state return value as well as the mocks to make assertions.
|
|
|
|
'''
|
2016-03-07 17:38:04 +00:00
|
|
|
get_mock = Mock(return_value=task)
|
2016-03-22 14:07:24 +00:00
|
|
|
|
|
|
|
if isinstance(define_result, bool):
|
|
|
|
define_result = {'success': define_result}
|
2016-03-07 17:38:04 +00:00
|
|
|
define_mock = Mock(return_value=define_result)
|
2016-03-22 14:07:24 +00:00
|
|
|
|
|
|
|
if isinstance(enable_result, bool):
|
|
|
|
enable_result = {'success': enable_result}
|
2016-03-07 17:38:04 +00:00
|
|
|
enable_mock = Mock(return_value=enable_result)
|
2016-03-22 14:07:24 +00:00
|
|
|
|
|
|
|
if isinstance(disable_result, bool):
|
|
|
|
disable_result = {'success': disable_result}
|
2016-03-09 18:16:40 +00:00
|
|
|
disable_mock = Mock(return_value=disable_result)
|
2016-03-22 14:07:24 +00:00
|
|
|
|
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
|
|
|
}):
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open(read_data=script)) as open_mock:
|
2016-03-07 17:38:04 +00:00
|
|
|
retval = kapacitor.task_present(name, tick_script, task_type=task_type,
|
|
|
|
database=database, retention_policy=retention_policy, enable=enable)
|
2016-05-19 18:03:03 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-12-29 10:14:24 +00:00
|
|
|
def _task(script='testscript', enabled=True, task_type='stream', db='testdb', rp='default'):
|
|
|
|
return {
|
|
|
|
'script': script,
|
|
|
|
'enabled': enabled,
|
|
|
|
'type': task_type,
|
|
|
|
'dbrps': [{'db': db, 'rp': rp}],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-19 15:35:30 +00:00
|
|
|
class KapacitorTestCase(TestCase, LoaderModuleMockMixin):
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
2017-02-19 15:35:30 +00:00
|
|
|
return {
|
2017-03-22 12:12:36 +00:00
|
|
|
kapacitor: {
|
|
|
|
'__opts__': {'test': False},
|
|
|
|
'__env__': 'test'
|
|
|
|
}
|
2017-02-19 15:35:30 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:38:04 +00:00
|
|
|
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
|
|
|
|
2016-12-29 10:14:24 +00:00
|
|
|
def test_task_present_existing_task_updated_script(self):
|
|
|
|
ret, get_mock, define_mock, enable_mock, _ = _present(task=_task(script='oldscript'))
|
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-12-29 10:14:24 +00:00
|
|
|
ret, get_mock, define_mock, enable_mock, _ = _present(task=_task(enabled=False))
|
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):
|
2016-12-29 10:14:24 +00:00
|
|
|
ret, get_mock, define_mock, _, disable_mock = _present(task=_task(), enable=False)
|
2016-03-09 18:16:40 +00:00
|
|
|
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'])
|