Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7554721

Browse files
committed
write output to log; tests added
1 parent 1c9b389 commit 7554721

File tree

5 files changed

+90
-20
lines changed

5 files changed

+90
-20
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
#download_url = 'https://github.com/postgrespro/testgres/tarball/0.1.1',
1111
keywords = ['testing', 'postgresql'],
1212
classifiers = [],
13-
#install_requires = ["pg8000"]
13+
install_requires = ["pg8000"]
1414
)

testgres/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .testgres import PostgresNode, get_new_node, clean_all
1+
from testgres import PostgresNode, get_new_node, clean_all

testgres/testgres.py

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,29 @@ class PostgresNode:
5353
def __init__(self, name, port):
5454
self.name = name
5555
self.port = port
56-
self.data_dir = tempfile.mkdtemp()
56+
self.base_dir = tempfile.mkdtemp()
57+
os.makedirs(self.logs_dir)
58+
os.makedirs(self.data_dir)
5759
self.working = False
5860
self.config = {}
5961
self.load_pg_config()
6062

63+
@property
64+
def data_dir(self):
65+
return "%s/data" % self.base_dir
66+
67+
@property
68+
def logs_dir(self):
69+
return "%s/logs" % self.base_dir
70+
71+
@property
72+
def output_filename(self):
73+
return "%s/stdout.log" % self.logs_dir
74+
75+
@property
76+
def error_filename(self):
77+
return "%s/stderr.log" % self.logs_dir
78+
6179
def load_pg_config(self):
6280
""" Loads pg_config output into dict """
6381
pg_config = os.environ.get("PG_CONFIG") \
@@ -81,9 +99,15 @@ def init(self):
8199

82100
# initialize cluster
83101
initdb = self.get_bin_path("initdb")
84-
ret = subprocess.call([initdb, self.data_dir, "-N"])
85-
if ret:
86-
raise ClusterException("Cluster initialization failed")
102+
with open(self.output_filename, "a") as file_out, \
103+
open(self.error_filename, "a") as file_err:
104+
ret = subprocess.call(
105+
[initdb, self.data_dir, "-N"],
106+
stdout=file_out,
107+
stderr=file_err
108+
)
109+
if ret:
110+
raise ClusterException("Cluster initialization failed")
87111

88112
# add parameters to config file
89113
config_name = "%s/postgresql.conf" % self.data_dir
@@ -102,32 +126,57 @@ def append_conf(self, filename, string):
102126
with open(config_name, "a") as conf:
103127
conf.write(string)
104128

105-
def start(self):
106-
""" Starts cluster """
129+
def pg_ctl(self, command, params):
130+
"""Runs pg_ctl with specified params
131+
132+
This function is a workhorse for start(), stop() and reload()
133+
functions"""
107134
pg_ctl = self.get_bin_path("pg_ctl")
108-
logfile = self.data_dir + "/postgresql.log"
109-
params = [
110-
pg_ctl, "-D", self.data_dir,
111-
"-w", "start", "-l", logfile
112-
]
113135

114-
ret = subprocess.call(params)
115-
if ret:
136+
arguments = ['pg_ctl']
137+
for key, value in params.iteritems():
138+
arguments.append(key)
139+
if value:
140+
arguments.append(value)
141+
142+
with open(self.output_filename, "a") as file_out, \
143+
open(self.error_filename, "a") as file_err:
144+
return subprocess.call(
145+
arguments + [command],
146+
stdout=file_out,
147+
stderr=file_err
148+
)
149+
150+
def start(self):
151+
""" Starts cluster """
152+
logfile = self.logs_dir + "/postgresql.log"
153+
params = {
154+
"-D": self.data_dir,
155+
"-w": None,
156+
"-l": logfile,
157+
}
158+
if self.pg_ctl("start", params):
116159
raise ClusterException("Cluster startup failed")
117160

118161
self.working = True
119162

120163
def stop(self):
121164
""" Stops cluster """
122-
pg_ctl = self.get_bin_path("pg_ctl")
123-
params = [pg_ctl, "-D", self.data_dir, "-w", "stop"]
124-
125-
ret = subprocess.call(params)
126-
if ret:
165+
params = {
166+
"-D": self.data_dir,
167+
"-w": None
168+
}
169+
if self.pg_ctl("stop", params):
127170
raise ClusterException("Cluster stop failed")
128171

129172
self.working = False
130173

174+
def reload(self):
175+
"""Reloads config files"""
176+
params = {"-D": self.data_dir}
177+
if self.pg_ctl("reload", params):
178+
raise ClusterException("Cluster reload failed")
179+
131180
def cleanup(self):
132181
"""Stops cluster if needed and removes the data directory"""
133182

testgres/tests/__init__.py

Whitespace-only changes.

testgres/tests/test_simple.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from testgres import get_new_node, clean_all
3+
4+
class SimpleTest(unittest.TestCase):
5+
6+
def teardown(self):
7+
# clean_all()
8+
pass
9+
10+
def test_start_stop(self):
11+
assert(1)
12+
node = get_new_node('test')
13+
node.init()
14+
node.start()
15+
res = node.execute('postgres', 'select 1')
16+
self.assertEqual(len(res), 1)
17+
self.assertEqual(res[0][0], 1)
18+
node.stop()
19+
20+
if __name__ == '__main__':
21+
unittest.main()

0 commit comments

Comments
 (0)