-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_pg_tmp.rb
executable file
·241 lines (217 loc) · 8.68 KB
/
test_pg_tmp.rb
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env ruby
require 'fileutils'
require 'open3'
# Test Utilities
@tests = 0
@test_description = 0
def try(descr)
start = Time.now
@tests += 1
@test_description = descr
yield
delta = format('%.3f', (Time.now - start))
# highlight slow tests
delta = "\e[7m#{delta}\e[27m" if (Time.now - start) > 0.1
puts "#{delta}: #{descr}"
end
def eq(result, expected)
a = result.to_s.gsub(/^/, '> ')
b = expected.to_s.gsub(/^/, '< ')
raise "\"#{@test_description}\"\n#{a}\n#{b}" unless result == expected
end
# Setup
ENV['LANG'] = 'C'
@altpath = "stubs:#{ENV['PATH']}"
@systmp = `mktemp -d /tmp/ephemeralpg-test.XXXXXX`.chomp
FileUtils.mkdir_p "#{@systmp}/ephemeralpg.XXXXXX/16.3"
FileUtils.touch "#{@systmp}/ephemeralpg.XXXXXX/NEW"
at_exit { FileUtils.rm_r @systmp }
puts "\e[32m---\e[39m"
# Option Parsing
try 'Catch unknown options' do
cmd = './pg_tmp -t -z'
out, _, status = Open3.capture3(cmd)
eq out.empty?, true
eq status.exitstatus, 1
end
try 'Bogus arguments mixed with valid positional' do
cmd = './pg_tmp -t -z initdb -w 20'
out, _, status = Open3.capture3(cmd)
eq out.empty?, true
eq status.exitstatus, 1
end
try 'Run with missing Postgres binaries' do
getopt_path = File.dirname `which getopt`.chomp
cmd = './pg_tmp'
out, err, status = Open3.capture3({ 'PATH' => "/bin:#{getopt_path}" }, cmd)
err.sub!(/.+initdb.+not found/, 'initdb: not found')
eq err, "initdb: not found\n"
eq out.empty?, true
eq status.success?, false
end
# Invocation Traces
try 'Create a new database on disk' do
cmd = './pg_tmp initdb'
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
err.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
out.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
eq out, "#{@systmp}/ephemeralpg.012345\n"
eq err, <<~COMMANDS
initdb --nosync -D #{@systmp}/ephemeralpg.012345/16.3 -E UNICODE -A trust
COMMANDS
eq status.success?, true
end
try 'Start a new instance with a specified datadir and multiple options' do
File.write "#{@systmp}/nice.trace", ''
cmd = "./pg_tmp start -d #{@systmp}/ephemeralpg.XXXXXX -o '-c track_commit_timestamp=true -c shared_buffers=12MB'"
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
out.sub!(/ephemeralpg-test\.[a-zA-Z0-9]{6}%2F/, '')
eq out, 'postgresql:///test?host=%2Ftmp%2Fephemeralpg.XXXXXX'
eq err, <<~COMMANDS
pg_ctl -W -o " -c track_commit_timestamp=true -c shared_buffers=12MB"\
-s -D #{@systmp}/ephemeralpg.XXXXXX/16.3 -l #{@systmp}/ephemeralpg.XXXXXX/16.3/postgres.log start
sleep 0.1
COMMANDS
eq status.success?, true
# background tasks kicked off by starting an instance
nice = File.readlines("#{@systmp}/nice.trace").sort.join
nice.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
eq nice, <<~COMMANDS
nice ./pg_tmp -w 60 -d #{@systmp}/ephemeralpg.012345 -p 5432 stop
COMMANDS
end
try 'Start a new instance on a TCP port using a specified datadir' do
File.write "#{@systmp}/nice.trace", ''
cmd = "./pg_tmp start -d #{@systmp}/ephemeralpg.XXXXXX -t"
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
eq out, 'postgresql://[email protected]:55550/test'
eq err, <<~COMMANDS
pg_ctl -W -o "-c listen_addresses='*' -c port=55550 "\
-s -D #{@systmp}/ephemeralpg.XXXXXX/16.3 -l #{@systmp}/ephemeralpg.XXXXXX/16.3/postgres.log start
sleep 0.1
COMMANDS
eq status.success?, true
# background tasks kicked off by starting an instance
nice = File.readlines("#{@systmp}/nice.trace").sort.join
nice.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
eq nice, <<~COMMANDS
nice ./pg_tmp -w 60 -d #{@systmp}/ephemeralpg.012345 -p 55550 stop
COMMANDS
end
try 'Start a new instance without a pre-initialized datadir' do
File.write "#{@systmp}/nice.trace", ''
cmd = './pg_tmp start '
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
out.sub!(/ephemeralpg-test\.[a-zA-Z0-9]{6}%2F/, '')
out.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
err.gsub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
eq out, 'postgresql:///test?host=%2Ftmp%2Fephemeralpg.012345'
eq err, <<~COMMANDS
initdb --nosync -D #{@systmp}/ephemeralpg.012345/16.3 -E UNICODE -A trust
rm #{@systmp}/ephemeralpg.012345/NEW
pg_ctl -W -o " " -s -D #{@systmp}/ephemeralpg.012345/16.3 -l #{@systmp}/ephemeralpg.012345/16.3/postgres.log start
sleep 0.1
COMMANDS
eq status.success?, true
# background tasks kicked off by starting an instance
nice = File.readlines("#{@systmp}/nice.trace").sort.join
nice.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
eq nice, <<~COMMANDS
nice ./pg_tmp -w 60 -d #{@systmp}/ephemeralpg.012345 -p 5432 stop
nice ./pg_tmp initdb
COMMANDS
end
try 'Start a new instance and leave server running' do
File.write "#{@systmp}/nice.trace", ''
cmd = "./pg_tmp start -d #{@systmp}/ephemeralpg.XXXXXX -w 0"
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
out.sub!(/ephemeralpg-test\.[a-zA-Z0-9]{6}%2F/, '')
eq out, 'postgresql:///test?host=%2Ftmp%2Fephemeralpg.XXXXXX'
eq err, <<~COMMANDS
pg_ctl -W -o " "\
-s -D #{@systmp}/ephemeralpg.XXXXXX/16.3 -l #{@systmp}/ephemeralpg.XXXXXX/16.3/postgres.log start
sleep 0.1
COMMANDS
eq status.success?, true
# background tasks kicked off by starting an instance
nice = File.readlines("#{@systmp}/nice.trace").sort.join
nice.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
eq nice, ''
end
try 'Start a new instance and keep the tmp datadir' do
File.write "#{@systmp}/nice.trace", ''
cmd = "./pg_tmp start -d #{@systmp}/ephemeralpg.XXXXXX -k"
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
out.sub!(/ephemeralpg-test\.[a-zA-Z0-9]{6}%2F/, '')
eq out, 'postgresql:///test?host=%2Ftmp%2Fephemeralpg.XXXXXX'
eq err, <<~COMMANDS
pg_ctl -W -o " "\
-s -D #{@systmp}/ephemeralpg.XXXXXX/16.3 -l #{@systmp}/ephemeralpg.XXXXXX/16.3/postgres.log start
sleep 0.1
COMMANDS
eq status.success?, true
# background tasks kicked off by starting an instance
nice = File.readlines("#{@systmp}/nice.trace").sort.join
nice.sub!(/ephemeralpg\.[a-zA-Z0-9]{6}/, 'ephemeralpg.012345')
eq nice, <<~COMMANDS
nice ./pg_tmp -k -w 60 -d #{@systmp}/ephemeralpg.012345 -p 5432 stop
COMMANDS
end
try 'Stop a running instance' do
File.write "#{@systmp}/ephemeralpg.XXXXXX/16.3/postgresql.conf", ''
cmd = "./pg_tmp stop -d #{@systmp}/ephemeralpg.XXXXXX"
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
eq out.empty?, true
eq err, <<~COMMANDS
sleep 5
psql test --no-psqlrc -At -c SELECT count(*) FROM pg_stat_activity WHERE datname IS NOT NULL AND state IS NOT NULL;
pg_ctl -W -D #{@systmp}/ephemeralpg.XXXXXX/16.3 stop
sleep 1
rm -r #{@systmp}/ephemeralpg.XXXXXX
COMMANDS
eq status.success?, true
end
try 'Stop a running instance and remove tmp datadir' do
File.write "#{@systmp}/ephemeralpg.XXXXXX/16.3/postgresql.conf", ''
cmd = "./pg_tmp stop -d #{@systmp}/ephemeralpg.XXXXXX -w 60"
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
eq out.empty?, true
eq err, <<~COMMANDS
sleep 60
psql test --no-psqlrc -At -c SELECT count(*) FROM pg_stat_activity WHERE datname IS NOT NULL AND state IS NOT NULL;
pg_ctl -W -D #{@systmp}/ephemeralpg.XXXXXX/16.3 stop
sleep 1
rm -r #{@systmp}/ephemeralpg.XXXXXX
COMMANDS
eq status.success?, true
File.unlink "#{@systmp}/ephemeralpg.XXXXXX/16.3/postgresql.conf"
end
try 'Stop a running instance if query fails' do
File.write "#{@systmp}/ephemeralpg.XXXXXX/16.3/postgresql.conf", ''
File.write "#{@systmp}/ephemeralpg.XXXXXX/STOP", ''
cmd = "./pg_tmp stop -d #{@systmp}/ephemeralpg.XXXXXX"
out, err, status = Open3.capture3({ 'PATH' => @altpath }, cmd)
eq out.empty?, true
eq err.sub(/on line \d+/, 'on line 100'), <<~COMMANDS
sleep 5
pg_ctl -W -D #{@systmp}/ephemeralpg.XXXXXX/16.3 stop
sleep 1
rm -r #{@systmp}/ephemeralpg.XXXXXX
COMMANDS
eq status.success?, true
File.unlink "#{@systmp}/ephemeralpg.XXXXXX/16.3/postgresql.conf"
end
try 'Stop a running instance and keep tmp datadir' do
File.write "#{@systmp}/ephemeralpg.XXXXXX/16.3/postgresql.conf", ''
cmd = "./pg_tmp stop -k -d #{@systmp}/ephemeralpg.XXXXXX -w 60"
out, err, status = Open3.capture3({ 'SYSTMP' => @systmp, 'PATH' => @altpath }, cmd)
eq out.empty?, true
eq err, <<~COMMANDS
sleep 60
pg_ctl -W -D #{@systmp}/ephemeralpg.XXXXXX/16.3 stop
sleep 1
COMMANDS
eq status.success?, true
File.unlink "#{@systmp}/ephemeralpg.XXXXXX/16.3/postgresql.conf"
end
puts "#{@tests} tests PASSED"