From cd312de2527e0d168ff0fad27978120c17919383 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Thu, 10 Sep 2015 17:15:43 +0200 Subject: [PATCH] Fix a flake8 warning. Improve some unit tests by expecting specific exceptions. --- patroni/exceptions.py | 4 ++++ patroni/postgresql.py | 10 ++++++---- tests/test_patroni.py | 5 +++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/patroni/exceptions.py b/patroni/exceptions.py index e6159d47..507edfc3 100644 --- a/patroni/exceptions.py +++ b/patroni/exceptions.py @@ -13,5 +13,9 @@ class PatroniException(Exception): return repr(self.value) +class PostgresException(PatroniException): + pass + + class DCSError(PatroniException): pass diff --git a/patroni/postgresql.py b/patroni/postgresql.py index 3c1630a8..b7f76fc3 100644 --- a/patroni/postgresql.py +++ b/patroni/postgresql.py @@ -6,6 +6,7 @@ import shutil import subprocess import time +from patroni.exceptions import PostgresException from patroni.utils import sleep from six.moves.urllib_parse import urlparse @@ -410,7 +411,7 @@ recovery_target_timeline = 'latest' self.create_replication_user() self.create_connection_users() else: - raise Exception("Could not bootstrap master PostgreSQL") + raise PostgresException("Could not bootstrap master PostgreSQL") else: if self.sync_from_leader(current_leader): self.write_recovery_conf(current_leader) @@ -419,6 +420,7 @@ recovery_target_timeline = 'latest' def move_data_directory(self): if os.path.isdir(self.data_dir) and not self.is_running(): - os.rename(self.data_dir, '{0}_{1}'.format(self.data_dir, str(long(time.time())))) - return True - return False + try: + os.rename(self.data_dir, '{0}_{1}'.format(self.data_dir, time.strftime('%Y-%m-%d-%H-%M-%S'))) + except: + logger.exception("Could not rename data directory {0}".format(self.data_dir)) diff --git a/tests/test_patroni.py b/tests/test_patroni.py index 75d47fe4..d5740e9c 100644 --- a/tests/test_patroni.py +++ b/tests/test_patroni.py @@ -11,6 +11,7 @@ from mock import Mock, patch from patroni.api import RestApiServer from patroni.dcs import Cluster, Member from patroni.etcd import Etcd +from patroni.exceptions import PostgresException from patroni import Patroni, main from patroni.zookeeper import ZooKeeper from six.moves import BaseHTTPServer @@ -141,7 +142,7 @@ class TestPatroni(unittest.TestCase): self.p.initialize() self.p.ha.dcs.current_leader = nop - self.assertRaises(Exception, self.p.initialize) + self.assertRaises(SleepException, self.p.initialize) self.p.postgresql.data_directory_empty = false self.p.initialize() @@ -162,5 +163,5 @@ class TestPatroni(unittest.TestCase): self.p.postgresql.start = false self.p.ha.dcs.cancel_initialization = self.cancel_initialization - self.assertRaises(Exception, self.p.initialize) + self.assertRaises(PostgresException, self.p.initialize) self.assertTrue(self.init_cancelled)