Skip to content

Commit

Permalink
Merge pull request #1 from makinacorpus/postgresql
Browse files Browse the repository at this point in the history
lint
  • Loading branch information
arthurzenika committed Mar 6, 2015
2 parents 592e0a1 + f1111bd commit f34b2a3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
35 changes: 22 additions & 13 deletions salt/modules/deb_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import salt.utils

# Import 3rd-party libs
import salt.ext.six as six


log = logging.getLogger(__name__)

__virtualname__ = 'postgres'


def __virtual__():
'''
Only load this module if the pg_createcluster bin exists
Expand All @@ -28,14 +27,15 @@ def __virtual__():
return __virtualname__
return False


def cluster_create(version,
name='main',
port=None,
locale=None,
encoding=None,
datadir=None):
'''
Adds a cluster to the Postgres server.
Adds a cluster to the Postgres server.
.. warning:
Expand All @@ -52,8 +52,6 @@ def cluster_create(version,
salt '*' postgres.cluster_create '9.3' locale='fr_FR'
'''
# XXX version should be a string, but it requires to use "'9.3'" in cmd line
# assert isinstance(version, six.string_types)
cmd = [salt.utils.which('pg_createcluster')]
if port:
cmd += ['--port', str(port)]
Expand All @@ -67,10 +65,12 @@ def cluster_create(version,
cmdstr = ' '.join([pipes.quote(c) for c in cmd])
ret = __salt__['cmd.run_all'](cmdstr, python_shell=False)
if ret.get('retcode', 0) != 0:
log.error('Error creating a Postgresql cluster {0}/{1}'.format(version, name))
log.error('Error creating a Postgresql'
' cluster {0}/{1}'.format(version, name))
return False
return ret


def cluster_list(verbose=False):
'''
Return a list of cluster of Postgres server (tuples of version and name).
Expand Down Expand Up @@ -108,6 +108,7 @@ def cluster_exists(version,
'''
return '{0}/{1}'.format(version, name) in cluster_list()


def cluster_remove(version,
name='main',
stop=False):
Expand All @@ -132,20 +133,28 @@ def cluster_remove(version,
cmd += [version, name]
cmdstr = ' '.join([pipes.quote(c) for c in cmd])
ret = __salt__['cmd.run_all'](cmdstr, python_shell=False)
# FIXME - return Boolean ?
# FIXME - return Boolean ?
if ret.get('retcode', 0) != 0:
log.error('Error removing a Postgresql cluster {0}/{1}'.format(version, name))
log.error('Error removing a Postgresql'
' cluster {0}/{1}'.format(version, name))
else:
ret['changes'] = 'Successfully removed cluster {0}/{1}'.format(version, name)
ret['changes'] = ('Successfully removed'
' cluster {0}/{1}').format(version, name)
return ret



def _parse_pg_lscluster(output):
'''
Helper function to parse the output of pg_lscluster
'''
cluster_dict = {}
for line in output.splitlines():
v,n,p,s,u,d,l = line.split()
cluster_dict['{0}/{1}'.format(v,n)] = {'port':int(p), 'status':s,
'user':u, 'datadir':d, 'log':l}
version, name, port, status, user, datadir, log = (
line.split())
cluster_dict['{0}/{1}'.format(version, name)] = {
'port': int(port),
'status': status,
'user': user,
'datadir': datadir,
'log': log}
return cluster_dict
55 changes: 32 additions & 23 deletions tests/unit/modules/deb_postgres_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

# Import python libs
from __future__ import absolute_import, print_function
import re
import os

# Import Salt Testing libs
from salttesting import skipIf, TestCase
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, Mock, patch
ensure_in_syspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../../'))
ensure_in_syspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../'))
ensure_in_syspath(
os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../../'))
ensure_in_syspath(
os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../'))

# Import salt libs
from salt.modules import deb_postgres
Expand All @@ -19,19 +20,23 @@
deb_postgres.__salt__ = None # in order to stub it w/patch below


LSCLUSTER = '''8.4 main 5432 online postgres /srv/8.4/main /var/log/postgresql/postgresql-8.4-main.log
9.1 main 5433 online postgres /srv/9.1/main /var/log/postgresql/postgresql-9.1-main.log
LSCLUSTER = '''\
8.4 main 5432 online postgres /srv/8.4/main \
/var/log/postgresql/postgresql-8.4-main.log
9.1 main 5433 online postgres /srv/9.1/main \
/var/log/postgresql/postgresql-9.1-main.log
'''
if NO_MOCK is False:
SALT_STUB = {
'config.option': Mock(),
'cmd.run_all': Mock(return_value={'stdout':LSCLUSTER}),
'cmd.run_all': Mock(return_value={'stdout': LSCLUSTER}),
'file.chown': Mock(),
'file.remove': Mock(),
}
else:
SALT_STUB = {}


@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch.multiple(deb_postgres,
__salt__=SALT_STUB)
Expand All @@ -57,13 +62,14 @@ def test_cluster_create(self):

# XXX version should be a string but from cmdline you get a float
# def test_cluster_create_with_float(self):
# self.assertRaises(AssertionError, deb_postgres.cluster_create,
# (9.3,'main',),
# self.assertRaises(AssertionError, deb_postgres.cluster_create,
# (9.3,'main',),
# dict(port='5432',
# locale='fr_FR',
# encoding='UTF-8',
# datadir='/opt/postgresql'))


@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch.multiple(deb_postgres,
__salt__=SALT_STUB)
Expand All @@ -73,17 +79,20 @@ class PostgresLsClusterTestCase(TestCase):
def test_parse_pg_lsclusters(self):
stdout = LSCLUSTER
self.maxDiff = None
self.assertDictEqual({('8.4', 'main'):{'port':5432,
'status': 'online',
'user': 'postgres',
'datadir':'/srv/8.4/main',
'log':'/var/log/postgresql/postgresql-8.4-main.log'},
('9.1', 'main'):{'port':5433,
'status': 'online',
'user': 'postgres',
'datadir':'/srv/9.1/main',
'log':'/var/log/postgresql/postgresql-9.1-main.log'},},
deb_postgres._parse_pg_lscluster(stdout))
self.assertDictEqual(
{('8.4', 'main'): {
'port': 5432,
'status': 'online',
'user': 'postgres',
'datadir': '/srv/8.4/main',
'log': '/var/log/postgresql/postgresql-8.4-main.log'},
('9.1', 'main'): {
'port': 5433,
'status': 'online',
'user': 'postgres',
'datadir': '/srv/9.1/main',
'log': '/var/log/postgresql/postgresql-9.1-main.log'}},
deb_postgres._parse_pg_lscluster(stdout))

def test_cluster_list(self):
return_list = deb_postgres.cluster_list()
Expand All @@ -105,18 +114,18 @@ def test_cluster_exists(self):
__salt__=SALT_STUB)
@patch('salt.utils.which', Mock(return_value='/usr/bin/pg_dropcluster'))
class PostgresDeleteClusterTestCase(TestCase):

def test_cluster_delete(self):
return_list = deb_postgres.cluster_remove('9.3', 'main')
deb_postgres.cluster_remove('9.3', 'main')
cmd = SALT_STUB['cmd.run_all']
self.assertEqual('/usr/bin/pg_dropcluster 9.3 main',
cmd.call_args[0][0])
return_list = deb_postgres.cluster_remove('9.3', 'main', stop=True)
deb_postgres.cluster_remove('9.3', 'main', stop=True)
cmd = SALT_STUB['cmd.run_all']
self.assertEqual('/usr/bin/pg_dropcluster --stop 9.3 main',
cmd.call_args[0][0])

if __name__ == '__main__':
from integration import run_tests
run_tests(PostgresClusterTestCase, PostgresLsClusterTestCase,
run_tests(PostgresClusterTestCase, PostgresLsClusterTestCase,
PostgresDeleteClusterTestCase, needs_daemon=False)

0 comments on commit f34b2a3

Please sign in to comment.