diff --git a/features/standby_cluster.feature b/features/standby_cluster.feature index 78e488b6..a2773e64 100644 --- a/features/standby_cluster.feature +++ b/features/standby_cluster.feature @@ -25,6 +25,7 @@ Feature: standby cluster Then postgres1 is a leader of batman1 after 10 seconds When I add the table foo to postgres0 Then table foo is present on postgres1 after 20 seconds + And I sleep for 3 seconds When I issue a GET request to http://127.0.0.1:8009/master Then I receive a response code 503 When I issue a GET request to http://127.0.0.1:8009/standby_leader diff --git a/patroni/postgresql/rewind.py b/patroni/postgresql/rewind.py index 937cdae2..40949c8d 100644 --- a/patroni/postgresql/rewind.py +++ b/patroni/postgresql/rewind.py @@ -283,9 +283,9 @@ class Rewind(object): if b > -1: b += len(pattern) e = line.find('": ', b) - if e > -1: - waldir, wal_filename = os.path.split(line[b:e]) - if waldir.endswith(os.path.sep + 'pg_' + self._postgresql.wal_name) and len(wal_filename) == 24: + if e > -1 and '/' in line[b:e]: + waldir, wal_filename = line[b:e].rsplit('/', 1) + if waldir.endswith('/pg_' + self._postgresql.wal_name) and len(wal_filename) == 24: return wal_filename def pg_rewind(self, r): diff --git a/tests/test_raft.py b/tests/test_raft.py index eceef8b6..58427037 100644 --- a/tests/test_raft.py +++ b/tests/test_raft.py @@ -7,6 +7,21 @@ from patroni.dcs.raft import DynMemberSyncObj, KVStoreTTL, Raft, SyncObjUtility from pysyncobj import SyncObjConf, FAIL_REASON +def remove_files(prefix): + for f in ('journal', 'dump'): + f = prefix + f + if os.path.isfile(f): + for i in range(0, 15): + try: + if os.path.isfile(f): + os.unlink(f) + break + else: + break + except Exception: + time.sleep(1.0) + + @patch('pysyncobj.tcp_server.TcpServer.bind', Mock()) class TestDynMemberSyncObj(unittest.TestCase): @@ -44,7 +59,7 @@ class TestDynMemberSyncObj(unittest.TestCase): class TestKVStoreTTL(unittest.TestCase): def setUp(self): - self.conf = SyncObjConf(appendEntriesUseBatch=False, appendEntriesPeriod=0.001, journalFile='foo.journal', + self.conf = SyncObjConf(appendEntriesUseBatch=False, appendEntriesPeriod=0.001, raftMinTimeout=0.004, raftMaxTimeout=0.005, autoTickPeriod=0.001) callback = Mock() callback.replicated = False @@ -59,8 +74,6 @@ class TestKVStoreTTL(unittest.TestCase): def tearDown(self): if self.so: self.destroy(self.so) - if os.path.exists('foo.journal'): - os.unlink('foo.journal') def test_set(self): self.assertTrue(self.so.set('foo', 'bar', prevExist=False, ttl=30)) @@ -69,6 +82,7 @@ class TestKVStoreTTL(unittest.TestCase): self.assertTrue(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1})) def test_delete(self): + self.conf.autoTickPeriod = 0.1 self.so.set('foo', 'bar') self.so.set('fooo', 'bar') self.assertFalse(self.so.delete('foo', prevValue='buz')) @@ -128,10 +142,7 @@ class TestRaft(unittest.TestCase): raft._sync_obj._SyncObj__thread.join() def tearDown(self): - for f in ('journal', 'dump'): - f = '127.0.0.1:1234.' + f - if os.path.exists(f): - os.unlink(f) + remove_files('127.0.0.1:1234.') def setUp(self): self.tearDown() diff --git a/tests/test_raft_controller.py b/tests/test_raft_controller.py index 92c4d1d4..347517a1 100644 --- a/tests/test_raft_controller.py +++ b/tests/test_raft_controller.py @@ -8,6 +8,7 @@ from patroni.config import Config from patroni.raft_controller import RaftController, main as _main from . import SleepException +from .test_raft import remove_files class TestPatroniRaftController(unittest.TestCase): @@ -15,10 +16,7 @@ class TestPatroniRaftController(unittest.TestCase): SELF_ADDR = '127.0.0.1:5360' def remove_files(self): - for f in ('journal', 'dump'): - f = self.SELF_ADDR + '.' + f - if os.path.exists(f): - os.unlink(f) + remove_files(self.SELF_ADDR + '.') @patch('pysyncobj.tcp_server.TcpServer.bind', Mock()) def setUp(self): diff --git a/tests/test_utils.py b/tests/test_utils.py index e08891fe..0c6b21e4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -36,9 +36,10 @@ class TestUtils(unittest.TestCase): def test_enable_keepalive(self): with patch('socket.SIO_KEEPALIVE_VALS', 1, create=True): self.assertIsNotNone(enable_keepalive(Mock(), 10, 5)) - for platform in ('linux2', 'darwin', 'other'): - with patch('sys.platform', platform): - self.assertIsNone(enable_keepalive(Mock(), 10, 5)) + with patch('socket.SIO_KEEPALIVE_VALS', None, create=True): + for platform in ('linux2', 'darwin', 'other'): + with patch('sys.platform', platform): + self.assertIsNone(enable_keepalive(Mock(), 10, 5)) @patch('time.sleep', Mock()) diff --git a/tests/test_validator.py b/tests/test_validator.py index df225857..1a27514a 100644 --- a/tests/test_validator.py +++ b/tests/test_validator.py @@ -153,6 +153,7 @@ class TestValidator(unittest.TestCase): self.assertEqual(['etcd.hosts.1', 'etcd.hosts.2', 'kubernetes.pod_ip', 'postgresql.bin_dir', 'postgresql.data_dir', 'restapi.connect_address'], parse_output(output)) + @patch('socket.inet_pton', Mock(), create=True) def test_bin_dir_is_empty(self, mock_out, mock_err): directories.append(config["postgresql"]["data_dir"]) directories.append(config["postgresql"]["bin_dir"])