2015-05-08 10:51:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
|
|
'''
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-03-22 16:42:17 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import (
|
2015-05-08 10:51:17 +00:00
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON,
|
|
|
|
MagicMock,
|
|
|
|
patch)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.influxdb08_user as influxdb08_user
|
2015-05-08 10:51:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-03-22 16:42:17 +00:00
|
|
|
class InfluxdbUserTestCase(TestCase, LoaderModuleMockMixin):
|
2015-05-08 10:51:17 +00:00
|
|
|
'''
|
2016-05-31 07:48:41 +00:00
|
|
|
Test cases for salt.states.influxdb08_user
|
2015-05-08 10:51:17 +00:00
|
|
|
'''
|
2017-03-22 16:42:17 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {influxdb08_user: {}}
|
|
|
|
|
2015-05-08 10:51:17 +00:00
|
|
|
# 'present' function tests: 1
|
|
|
|
|
|
|
|
def test_present(self):
|
|
|
|
'''
|
|
|
|
Test to ensure that the cluster admin or database user is present.
|
|
|
|
'''
|
|
|
|
name = 'salt'
|
|
|
|
passwd = 'salt'
|
|
|
|
|
|
|
|
ret = {'name': name,
|
|
|
|
'result': False,
|
|
|
|
'comment': '',
|
|
|
|
'changes': {}}
|
|
|
|
|
|
|
|
mock = MagicMock(side_effect=[False, False, False, True])
|
|
|
|
mock_t = MagicMock(side_effect=[True, False])
|
|
|
|
mock_f = MagicMock(return_value=False)
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.dict(influxdb08_user.__salt__,
|
|
|
|
{'influxdb08.db_exists': mock_f,
|
|
|
|
'influxdb08.user_exists': mock,
|
|
|
|
'influxdb08.user_create': mock_t}):
|
2015-05-08 10:51:17 +00:00
|
|
|
comt = ('Database mydb does not exist')
|
|
|
|
ret.update({'comment': comt})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.present(name, passwd,
|
2015-05-08 10:51:17 +00:00
|
|
|
database='mydb'), ret)
|
|
|
|
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.dict(influxdb08_user.__opts__, {'test': True}):
|
2015-05-08 10:51:17 +00:00
|
|
|
comt = ('User {0} is not present and needs to be created'
|
|
|
|
.format(name))
|
|
|
|
ret.update({'comment': comt, 'result': None})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.present(name, passwd), ret)
|
2015-05-08 10:51:17 +00:00
|
|
|
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.dict(influxdb08_user.__opts__, {'test': False}):
|
2015-05-08 10:51:17 +00:00
|
|
|
comt = ('User {0} has been created'.format(name))
|
|
|
|
ret.update({'comment': comt, 'result': True,
|
|
|
|
'changes': {'salt': 'Present'}})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.present(name, passwd), ret)
|
2015-05-08 10:51:17 +00:00
|
|
|
|
|
|
|
comt = ('Failed to create user {0}'.format(name))
|
|
|
|
ret.update({'comment': comt, 'result': False, 'changes': {}})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.present(name, passwd), ret)
|
2015-05-08 10:51:17 +00:00
|
|
|
|
|
|
|
comt = ('User {0} is already present'.format(name))
|
|
|
|
ret.update({'comment': comt, 'result': True})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.present(name, passwd), ret)
|
2015-05-08 10:51:17 +00:00
|
|
|
|
|
|
|
# 'absent' function tests: 1
|
|
|
|
|
|
|
|
def test_absent(self):
|
|
|
|
'''
|
|
|
|
Test to ensure that the named cluster admin or database user is absent.
|
|
|
|
'''
|
|
|
|
name = 'salt'
|
|
|
|
|
|
|
|
ret = {'name': name,
|
|
|
|
'result': None,
|
|
|
|
'comment': '',
|
|
|
|
'changes': {}}
|
|
|
|
|
|
|
|
mock = MagicMock(side_effect=[True, True, True, False])
|
|
|
|
mock_t = MagicMock(side_effect=[True, False])
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.dict(influxdb08_user.__salt__,
|
|
|
|
{'influxdb08.user_exists': mock,
|
|
|
|
'influxdb08.user_remove': mock_t}):
|
|
|
|
with patch.dict(influxdb08_user.__opts__, {'test': True}):
|
2015-05-08 10:51:17 +00:00
|
|
|
comt = ('User {0} is present and needs to be removed'
|
|
|
|
.format(name))
|
|
|
|
ret.update({'comment': comt})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.absent(name), ret)
|
2015-05-08 10:51:17 +00:00
|
|
|
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.dict(influxdb08_user.__opts__, {'test': False}):
|
2015-05-08 10:51:17 +00:00
|
|
|
comt = ('User {0} has been removed'.format(name))
|
|
|
|
ret.update({'comment': comt, 'result': True,
|
|
|
|
'changes': {'salt': 'Absent'}})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.absent(name), ret)
|
2015-05-08 10:51:17 +00:00
|
|
|
|
|
|
|
comt = ('Failed to remove user {0}'.format(name))
|
|
|
|
ret.update({'comment': comt, 'result': False, 'changes': {}})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.absent(name), ret)
|
2015-05-08 10:51:17 +00:00
|
|
|
|
|
|
|
comt = ('User {0} is not present, so it cannot be removed'
|
|
|
|
.format(name))
|
|
|
|
ret.update({'comment': comt, 'result': True})
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertDictEqual(influxdb08_user.absent(name), ret)
|