Files
patroni/tests/test_governor.py
T
Alexander Kukushkin c79fba7656 Support work with etcd as a cluster
In case if one member of a cluster is not available it will retry with
another one and fetch the new cluster configuration. Default timeout for
all requests to etcd is 5 seconds.
Initial cluster configuration can be resolved through:
1) /v2/members call on one of the cluster members on a client port
2) when it is possible to resolve hostname into multiple ip's  it will
iterate through list and try to perform action from 1)
3) If there is discovery_srv defined in etcd section of config file it
will resolve peer addresses of all cluster members and will fetch
cluster configuration with using peer protocol by doing /members call on
a peer port
2015-06-09 09:51:53 +02:00

88 lines
2.6 KiB
Python

import psycopg2
import requests
import subprocess
import sys
import time
import unittest
import yaml
from governor import Governor, main
from test_ha import true, false
from test_postgresql import Postgresql, subprocess_call, psycopg2_connect
from test_etcd import requests_get, requests_put, requests_delete
if sys.hexversion >= 0x03000000:
import http.server as BaseHTTPServer
else:
import BaseHTTPServer
def nop(*args, **kwargs):
pass
def time_sleep(_):
raise Exception()
class TestGovernor(unittest.TestCase):
def __init__(self, method_name='runTest'):
self.setUp = self.set_up
self.tearDown = self.tear_down
super(TestGovernor, self).__init__(method_name)
def set_up(self):
self.touched = False
subprocess.call = subprocess_call
psycopg2.connect = psycopg2_connect
requests.get = requests_get
requests.put = requests_put
requests.delete = requests_delete
self.time_sleep = time.sleep
time.sleep = nop
self.write_pg_hba = Postgresql.write_pg_hba
self.write_recovery_conf = Postgresql.write_recovery_conf
Postgresql.write_pg_hba = nop
Postgresql.write_recovery_conf = nop
BaseHTTPServer.HTTPServer.__init__ = nop
with open('postgres0.yml', 'r') as f:
config = yaml.load(f)
self.g = Governor(config)
def tear_down(self):
time.sleep = self.time_sleep
Postgresql.write_pg_hba = self.write_pg_hba
Postgresql.write_recovery_conf = self.write_recovery_conf
def test_governor_main(self):
main()
sys.argv = ['governor.py', 'postgres0.yml']
time.sleep = time_sleep
self.assertRaises(Exception, main)
def touch_member(self):
if not self.touched:
self.touched = True
return False
return True
def test_governor_initialize(self):
self.g.etcd.client._base_uri = 'http://remote'
self.g.postgresql.data_directory_empty = true
self.g.etcd.race = true
self.g.initialize()
self.g.etcd.race = false
self.g.initialize()
self.g.postgresql.data_directory_empty = false
self.g.touch_member = self.touch_member
self.g.initialize()
self.g.postgresql.data_directory_empty = true
time.sleep = time_sleep
self.g.postgresql.sync_from_leader = false
self.assertRaises(Exception, self.g.initialize)
def test_schedule_next_run(self):
self.g.next_run = time.time() - self.g.nap_time - 1
self.g.schedule_next_run()