1use strict;
2use warnings;
3
4use Config;
5use PostgresNode;
6use TestLib;
7use Test::More tests => 19;
8
9my $tempdir       = TestLib::tempdir;
10my $tempdir_short = TestLib::tempdir_short;
11
12program_help_ok('pg_ctl');
13program_version_ok('pg_ctl');
14program_options_handling_ok('pg_ctl');
15
16command_exit_is([ 'pg_ctl', 'start', '-D', "$tempdir/nonexistent" ],
17	1, 'pg_ctl start with nonexistent directory');
18
19command_ok([ 'pg_ctl', 'initdb', '-D', "$tempdir/data", '-o', '-N' ],
20	'pg_ctl initdb');
21command_ok([ $ENV{PG_REGRESS}, '--config-auth', "$tempdir/data" ],
22	'configure authentication');
23open my $conf, '>>', "$tempdir/data/postgresql.conf";
24print $conf "fsync = off\n";
25print $conf TestLib::slurp_file($ENV{TEMP_CONFIG})
26  if defined $ENV{TEMP_CONFIG};
27if (!$windows_os)
28{
29	print $conf "listen_addresses = ''\n";
30	print $conf "unix_socket_directories = '$tempdir_short'\n";
31}
32else
33{
34	print $conf "listen_addresses = '127.0.0.1'\n";
35}
36close $conf;
37my $ctlcmd = [
38	'pg_ctl', 'start', '-D', "$tempdir/data", '-l',
39	"$TestLib::log_path/001_start_stop_server.log" ];
40if ($Config{osname} ne 'msys')
41{
42	command_like($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
43}
44else
45{
46
47	# use the version of command_like that doesn't hang on Msys here
48	command_like_safe($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
49}
50
51# sleep here is because Windows builds can't check postmaster.pid exactly,
52# so they may mistake a pre-existing postmaster.pid for one created by the
53# postmaster they start.  Waiting more than the 2 seconds slop time allowed
54# by wait_for_postmaster() prevents that mistake.
55sleep 3 if ($windows_os);
56command_fails([ 'pg_ctl', 'start', '-D', "$tempdir/data" ],
57	'second pg_ctl start fails');
58command_ok([ 'pg_ctl', 'stop', '-D', "$tempdir/data" ], 'pg_ctl stop');
59command_fails([ 'pg_ctl', 'stop', '-D', "$tempdir/data" ],
60	'second pg_ctl stop fails');
61
62command_ok(
63	[ 'pg_ctl', 'restart', '-D', "$tempdir/data" ],
64	'pg_ctl restart with server not running');
65command_ok([ 'pg_ctl', 'restart', '-D', "$tempdir/data" ],
66	'pg_ctl restart with server running');
67
68system_or_bail 'pg_ctl', 'stop', '-D', "$tempdir/data";
69