1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
import tests.pgautofailover_utils as pgautofailover
from nose.tools import *
import os
import subprocess
cluster = None
node1 = None
node2 = None
node3 = None
def setup_module():
global cluster
cluster = pgautofailover.Cluster()
def teardown_module():
cluster.destroy()
def test_000_create_monitor():
monitor = cluster.create_monitor("/tmp/sb-from-pgdata/monitor")
monitor.run()
def test_001_init_primary():
global node1
node1 = cluster.create_datanode("/tmp/sb-from-pgdata/node1")
node1.create()
node1.run()
assert node1.wait_until_state(target_state="single")
node1.wait_until_pg_is_running()
def test_002_create_t1():
node1.run_sql_query("CREATE TABLE t1(a int)")
node1.run_sql_query("INSERT INTO t1 VALUES (1), (2)")
def test_003_init_secondary():
global node2
# fail the registration of a node2 by using a PGDATA directory that has
# already been created, with another system_identifier (initdb creates a
# new one each time)
p = subprocess.Popen(
[
"sudo",
"-E",
"-u",
os.getenv("USER"),
"env",
"PATH=" + os.getenv("PATH"),
"pg_ctl",
"initdb",
"-s",
"-D",
"/tmp/sb-from-pgdata/node2",
]
)
assert p.wait() == 0
node2 = cluster.create_datanode("/tmp/sb-from-pgdata/node2")
@raises(Exception)
def test_004_create_raises_error():
try:
node2.create()
except Exception as e:
# we want to see the failure here
print(e)
raise
def test_005_cleanup_after_failure():
print("Failed as expected, cleaning up")
print("rm -rf /tmp/sb-from-pgdata/node2")
p = subprocess.Popen(
[
"sudo",
"-E",
"-u",
os.getenv("USER"),
"env",
"PATH=" + os.getenv("PATH"),
"rm",
"-rf",
"/tmp/sb-from-pgdata/node2",
]
)
assert p.wait() == 0
def test_006_init_secondary():
global node3
# create node3 from a manual copy of node1 to test creating a standby
# from an existing PGDATA (typically PGDATA would be deployed from a
# backup and recovery mechanism)
p = subprocess.Popen(
[
"sudo",
"-E",
"-u",
os.getenv("USER"),
"env",
"PATH=" + os.getenv("PATH"),
"cp",
"-a",
"/tmp/sb-from-pgdata/node1",
"/tmp/sb-from-pgdata/node3",
]
)
assert p.wait() == 0
os.remove("/tmp/sb-from-pgdata/node3/postmaster.pid")
node3 = cluster.create_datanode("/tmp/sb-from-pgdata/node3")
node3.create()
node3.run()
cluster.monitor.print_state()
assert node3.wait_until_state(target_state="secondary")
assert node1.wait_until_state(target_state="primary")
def test_007_failover():
print()
print("Calling pgautofailover.failover() on the monitor")
cluster.monitor.failover()
assert node3.wait_until_state(target_state="primary")
assert node1.wait_until_state(target_state="secondary")
|