Skip to content

Commit 642ec32

Browse files
committed
Split test database cluster initialization
Improve readbaility of the code by splitting it into multiple functions, one for initializing the PostgreSQL cluster and another for starting the database server.
1 parent 3d961d7 commit 642ec32

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

spec/helpers.rb

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -236,47 +236,10 @@ def initialize(name, port: 23456, postgresql_conf: '')
236236

237237
trace "Command output logged to #{@logfile}"
238238

239-
@pgdata.mkpath
240-
241239
begin
242-
unless (@pgdata+"postgresql.conf").exist?
243-
FileUtils.rm_rf( @pgdata, :verbose => $DEBUG )
244-
trace "Running initdb"
245-
log_and_run @logfile, pg_bin_path('initdb'), '-E', 'UTF8', '--no-locale', '-D', @pgdata.to_s
246-
end
247-
248-
unless (@pgdata+"ruby-pg-server-cert").exist?
249-
trace "Enable SSL"
250-
# Enable SSL in server config
251-
File.open(@pgdata+"postgresql.conf", "a+") do |fd|
252-
fd.puts <<-EOT
253-
ssl = on
254-
ssl_ca_file = 'ruby-pg-ca-cert'
255-
ssl_cert_file = 'ruby-pg-server-cert'
256-
ssl_key_file = 'ruby-pg-server-key'
257-
#{postgresql_conf}
258-
EOT
259-
end
260-
261-
# Enable MD5 authentication in hba config
262-
hba_content = File.read(@pgdata+"pg_hba.conf")
263-
File.open(@pgdata+"pg_hba.conf", "w") do |fd|
264-
fd.puts <<-EOT
265-
# TYPE DATABASE USER ADDRESS METHOD
266-
host all testusermd5 ::1/128 md5
267-
EOT
268-
fd.puts hba_content
269-
end
270-
271-
trace "Generate certificates"
272-
generate_ssl_certs(@pgdata.to_s)
273-
end
274-
275-
trace "Starting postgres"
276-
sopt = "-p #{@port}"
277-
sopt += " -k #{@test_dir.to_s.dump}" unless RUBY_PLATFORM=~/mingw|mswin/i
278-
log_and_run @logfile, pg_bin_path('pg_ctl'), '-w', '-o', sopt,
279-
'-D', @pgdata.to_s, 'start'
240+
@pgdata.mkpath
241+
setup_cluster(postgresql_conf)
242+
start_cluster
280243
rescue => err
281244
$stderr.puts "%p during test setup: %s" % [ err.class, err.message ]
282245
$stderr.puts "See #{@logfile} for details:"
@@ -312,6 +275,47 @@ def ca_file
312275

313276
private
314277

278+
def setup_cluster(postgresql_conf)
279+
return if (@pgdata+"ruby-pg-server-cert").exist?
280+
281+
FileUtils.rm_rf(@pgdata, verbose: $DEBUG)
282+
283+
trace "Running initdb"
284+
log_and_run @logfile, pg_bin_path('initdb'), '-E', 'UTF8', '--no-locale', '-D', @pgdata.to_s
285+
286+
trace "Enable SSL"
287+
# Enable SSL in server config
288+
File.open(@pgdata+"postgresql.conf", "a+") do |fd|
289+
fd.puts <<-EOT
290+
ssl = on
291+
ssl_ca_file = 'ruby-pg-ca-cert'
292+
ssl_cert_file = 'ruby-pg-server-cert'
293+
ssl_key_file = 'ruby-pg-server-key'
294+
#{postgresql_conf}
295+
EOT
296+
end
297+
298+
# Enable MD5 authentication in hba config
299+
hba_content = File.read(@pgdata+"pg_hba.conf")
300+
File.open(@pgdata+"pg_hba.conf", "w") do |fd|
301+
fd.puts <<-EOT
302+
# TYPE DATABASE USER ADDRESS METHOD
303+
host all testusermd5 ::1/128 md5
304+
EOT
305+
fd.puts hba_content
306+
end
307+
308+
trace "Generate certificates"
309+
generate_ssl_certs(@pgdata.to_s)
310+
end
311+
312+
def start_cluster
313+
trace "Starting postgres"
314+
sopt = "-p #{@port}"
315+
sopt += " -k #{@test_dir.to_s.dump}" unless RUBY_PLATFORM=~/mingw|mswin/i
316+
log_and_run @logfile, pg_bin_path('pg_ctl'), '-w', '-o', sopt, '-D', @pgdata.to_s, 'start'
317+
end
318+
315319
def generate_ssl_certs(output_dir)
316320
gen = CertGenerator.new(output_dir)
317321

0 commit comments

Comments
 (0)