mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Server always returns json which contains some status of postgres: * instance type: master/slave * xlog location for master * xlog received_location, replayed_location This server could be used by haproxy GET / returns 200 if there is postgres master behind governor. GET /slave returns 200 is there is postgres slave behind governor In all other cases it returns 503 Currently server always listening on 0.0.0.0:8080, but it should be configurable. In the next versions this rest api could also report some status of etcd and could be used by is_healthiest_node method
67 lines
1.4 KiB
Python
67 lines
1.4 KiB
Python
import psycopg2
|
|
import sys
|
|
import unittest
|
|
|
|
from helpers.api import RestApiHandler, RestApiServer
|
|
from test_postgresql import psycopg2_connect
|
|
|
|
if sys.hexversion >= 0x03000000:
|
|
from io import BytesIO as IO
|
|
else:
|
|
from StringIO import StringIO as IO
|
|
|
|
|
|
def false(*args, **kwargs):
|
|
return False
|
|
|
|
|
|
def throws(*args, **kwargs):
|
|
raise psycopg2.OperationalError()
|
|
|
|
|
|
class MockPostgresql:
|
|
|
|
def connection(self):
|
|
return psycopg2_connect()
|
|
|
|
def is_running(self):
|
|
return True
|
|
|
|
|
|
class MockGovernor:
|
|
|
|
def __init__(self):
|
|
self.postgresql = MockPostgresql()
|
|
|
|
|
|
class MockRequest:
|
|
|
|
def __init__(self, path):
|
|
self.path = path
|
|
|
|
def makefile(self, *args, **kwargs):
|
|
return IO(self.path)
|
|
|
|
|
|
class MockRestApiServer(RestApiServer):
|
|
|
|
def __init__(self, Handler, path, *args):
|
|
self.governor = MockGovernor()
|
|
if len(args) > 0:
|
|
self.governor.postgresql.is_running = args[0]
|
|
self._cursor_holder = None
|
|
Handler(MockRequest(path), ('0.0.0.0', 8080), self)
|
|
|
|
|
|
class TestRestApiHandler(unittest.TestCase):
|
|
|
|
def __init__(self, method_name='runTest'):
|
|
super(TestRestApiHandler, self).__init__(method_name)
|
|
|
|
def test_do_GET(self):
|
|
MockRestApiServer(RestApiHandler, b'GET /')
|
|
MockRestApiServer(RestApiHandler, b'GET /', throws)
|
|
|
|
def test_get_postgresql_status(self):
|
|
MockRestApiServer(RestApiHandler, b'GET /', false)
|