Merge pull request #25096 from jfindlay/postgres_group_test

Postgres group test
This commit is contained in:
Justin Anderson 2015-07-01 12:48:25 -06:00
commit 21709aa483
2 changed files with 135 additions and 30 deletions

View File

@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import skipIf, TestCase
from salttesting.mock import (
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch
)
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt Libs
from salt.states import postgres_group
postgres_group.__opts__ = {}
postgres_group.__salt__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
class PostgresGroupTestCase(TestCase):
'''
Test cases for salt.states.postgres_group
'''
# 'present' function tests: 1
def test_present(self):
'''
Test to ensure that the named group is present
with the specified privileges.
'''
name = 'frank'
ret = {'name': name,
'changes': {},
'result': False,
'comment': ''}
mock_t = MagicMock(return_value=True)
mock = MagicMock(return_value=None)
with patch.dict(postgres_group.__salt__,
{'postgres.role_get': mock,
'postgres.group_create': mock_t}):
with patch.dict(postgres_group.__opts__, {'test': True}):
comt = ('Group {0} is set to be created'.format(name))
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(postgres_group.present(name), ret)
with patch.dict(postgres_group.__opts__, {'test': False}):
comt = ('The group {0} has been created'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(postgres_group.present(name), ret)
# 'absent' function tests: 1
def test_absent(self):
'''
Test to ensure that the named group is absent.
'''
name = 'frank'
ret = {'name': name,
'changes': {},
'result': False,
'comment': ''}
mock_t = MagicMock(return_value=True)
mock = MagicMock(side_effect=[True, True, False])
with patch.dict(postgres_group.__salt__,
{'postgres.user_exists': mock,
'postgres.group_remove': mock_t}):
with patch.dict(postgres_group.__opts__, {'test': True}):
comt = ('Group {0} is set to be removed'.format(name))
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(postgres_group.absent(name), ret)
with patch.dict(postgres_group.__opts__, {'test': False}):
comt = ('Group {0} has been removed'.format(name))
ret.update({'comment': comt, 'result': True,
'changes': {name: 'Absent'}})
self.assertDictEqual(postgres_group.absent(name), ret)
comt = ('Group {0} is not present, so it cannot be removed'
.format(name))
ret.update({'comment': comt, 'result': True, 'changes': {}})
self.assertDictEqual(postgres_group.absent(name), ret)
if __name__ == '__main__':
from integration import run_tests
run_tests(PostgresGroupTestCase, needs_daemon=False)

View File

@ -30,9 +30,9 @@ MODS = (
OPTS = {'test': False}
for postgres in MODS:
postgres.__grains__ = None # in order to stub it w/patch below
postgres.__salt__ = None # in order to stub it w/patch below
postgres.__opts__ = OPTS # in order to stub it w/patch below
postgres.__grains__ = {} # in order to stub it w/patch below
postgres.__salt__ = {} # in order to stub it w/patch below
postgres.__opts__ = {} # in order to stub it w/patch below
if NO_MOCK is False:
SALT_STUB = {
@ -48,7 +48,8 @@ else:
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch.multiple(postgres_user,
__grains__={'os_family': 'Linux'},
__salt__=SALT_STUB)
__salt__=SALT_STUB,
__opts__={'test': False})
@patch('salt.utils.which', Mock(return_value='/usr/bin/pgsql'))
class PostgresUserTestCase(TestCase):
@ -58,7 +59,7 @@ class PostgresUserTestCase(TestCase):
})
def test_present__creation(self):
# test=True
with patch.dict(OPTS, {'test': True}):
with patch.dict(postgres_user.__opts__, {'test': True}):
ret = postgres_user.present('foo')
self.assertEqual(
ret,
@ -108,7 +109,7 @@ class PostgresUserTestCase(TestCase):
})
def test_present__update(self):
# test=True
with patch.dict(OPTS, {'test': True}):
with patch.dict(postgres_user.__opts__, {'test': True}):
ret = postgres_user.present('foo', login=True, replication=False)
self.assertEqual(
ret,
@ -180,7 +181,8 @@ class PostgresUserTestCase(TestCase):
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch.multiple(postgres_group,
__grains__={'os_family': 'Linux'},
__salt__=SALT_STUB)
__salt__=SALT_STUB,
__opts__={'test': False})
@patch('salt.utils.which', Mock(return_value='/usr/bin/pgsql'))
class PostgresGroupTestCase(TestCase):
@ -190,7 +192,7 @@ class PostgresGroupTestCase(TestCase):
})
def test_present__creation(self):
# test=True
with patch.dict(OPTS, {'test': True}):
with patch.dict(postgres_group.__opts__, {'test': True}):
ret = postgres_group.present('foo')
self.assertEqual(
ret,
@ -240,7 +242,7 @@ class PostgresGroupTestCase(TestCase):
})
def test_present__update(self):
# test=True
with patch.dict(OPTS, {'test': True}):
with patch.dict(postgres_group.__opts__, {'test': True}):
ret = postgres_group.present('foo', login=True, replication=False)
self.assertEqual(
ret,
@ -312,7 +314,8 @@ class PostgresGroupTestCase(TestCase):
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch.multiple(postgres_extension,
__grains__={'os_family': 'Linux'},
__salt__=SALT_STUB)
__salt__=SALT_STUB,
__opts__={'test': False})
@patch('salt.utils.which', Mock(return_value='/usr/bin/pgsql'))
class PostgresExtensionTestCase(TestCase):
@ -396,26 +399,27 @@ class PostgresExtensionTestCase(TestCase):
scenario of creating upgrading extensions with possible schema and
version specifications
'''
ret = postgres_extension.present('foo')
self.assertEqual(
ret,
{'comment': 'Extension foo is set to be installed',
'changes': {}, 'name': 'foo', 'result': None}
with patch.dict(postgres_extension.__opts__, {'test': True}):
ret = postgres_extension.present('foo')
self.assertEqual(
ret,
{'comment': 'Extension foo is set to be installed',
'changes': {}, 'name': 'foo', 'result': None}
)
ret = postgres_extension.present('foo')
self.assertEqual(
ret,
{'comment': "Extension foo is set to be created",
'changes': {}, 'name': 'foo', 'result': None}
)
ret = postgres_extension.present('foo')
self.assertEqual(
ret,
{'comment': "Extension foo is set to be created",
'changes': {}, 'name': 'foo', 'result': None}
)
ret = postgres_extension.present('foo')
self.assertEqual(
ret,
{'comment': "Extension foo is set to be upgraded",
'changes': {}, 'name': 'foo', 'result': None}
)
)
ret = postgres_extension.present('foo')
self.assertEqual(
ret,
{'comment': "Extension foo is set to be upgraded",
'changes': {}, 'name': 'foo', 'result': None}
)
@patch.dict(SALT_STUB, {
'postgres.is_installed_extension': Mock(side_effect=[
@ -477,7 +481,8 @@ class PostgresExtensionTestCase(TestCase):
]),
})
def test_absent_failedtest(self):
ret = postgres_extension.absent('foo')
with patch.dict(postgres_extension.__opts__, {'test': True}):
ret = postgres_extension.absent('foo')
self.assertEqual(
ret,
{'comment': 'Extension foo is set to be removed',
@ -488,7 +493,8 @@ class PostgresExtensionTestCase(TestCase):
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch.multiple(postgres_schema,
__grains__={'os_family': 'Linux'},
__salt__=SALT_STUB)
__salt__=SALT_STUB,
__opts__={'test': False})
@patch('salt.utils.which', Mock(return_value='/usr/bin/pgsql'))
class PostgresSchemaTestCase(TestCase):