Update copyright for 2022
[pgsql.git] / src / bin / pg_ctl / t / 001_start_stop.pl
bloba084d09cd4975c1ef27d6e1ee9e609c8c935c34f
2 # Copyright (c) 2021-2022, PostgreSQL Global Development Group
4 use strict;
5 use warnings;
7 use Config;
8 use Fcntl ':mode';
9 use File::stat qw{lstat};
10 use PostgreSQL::Test::Cluster;
11 use PostgreSQL::Test::Utils;
12 use Test::More tests => 24;
14 my $tempdir = PostgreSQL::Test::Utils::tempdir;
15 my $tempdir_short = PostgreSQL::Test::Utils::tempdir_short;
17 program_help_ok('pg_ctl');
18 program_version_ok('pg_ctl');
19 program_options_handling_ok('pg_ctl');
21 command_exit_is([ 'pg_ctl', 'start', '-D', "$tempdir/nonexistent" ],
22 1, 'pg_ctl start with nonexistent directory');
24 command_ok([ 'pg_ctl', 'initdb', '-D', "$tempdir/data", '-o', '-N' ],
25 'pg_ctl initdb');
26 command_ok([ $ENV{PG_REGRESS}, '--config-auth', "$tempdir/data" ],
27 'configure authentication');
28 my $node_port = PostgreSQL::Test::Cluster::get_free_port();
29 open my $conf, '>>', "$tempdir/data/postgresql.conf";
30 print $conf "fsync = off\n";
31 print $conf "port = $node_port\n";
32 print $conf PostgreSQL::Test::Utils::slurp_file($ENV{TEMP_CONFIG})
33 if defined $ENV{TEMP_CONFIG};
35 if ($use_unix_sockets)
37 print $conf "listen_addresses = ''\n";
38 $tempdir_short =~ s!\\!/!g if $PostgreSQL::Test::Utils::windows_os;
39 print $conf "unix_socket_directories = '$tempdir_short'\n";
41 else
43 print $conf "listen_addresses = '127.0.0.1'\n";
45 close $conf;
46 my $ctlcmd = [
47 'pg_ctl', 'start', '-D', "$tempdir/data", '-l',
48 "$PostgreSQL::Test::Utils::log_path/001_start_stop_server.log"
50 if ($Config{osname} ne 'msys')
52 command_like($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
54 else
57 # use the version of command_like that doesn't hang on Msys here
58 command_like_safe($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
61 # sleep here is because Windows builds can't check postmaster.pid exactly,
62 # so they may mistake a pre-existing postmaster.pid for one created by the
63 # postmaster they start. Waiting more than the 2 seconds slop time allowed
64 # by wait_for_postmaster() prevents that mistake.
65 sleep 3 if ($windows_os);
66 command_fails([ 'pg_ctl', 'start', '-D', "$tempdir/data" ],
67 'second pg_ctl start fails');
68 command_ok([ 'pg_ctl', 'stop', '-D', "$tempdir/data" ], 'pg_ctl stop');
69 command_fails([ 'pg_ctl', 'stop', '-D', "$tempdir/data" ],
70 'second pg_ctl stop fails');
72 # Log file for default permission test. The permissions won't be checked on
73 # Windows but we still want to do the restart test.
74 my $logFileName = "$tempdir/data/perm-test-600.log";
76 command_ok([ 'pg_ctl', 'restart', '-D', "$tempdir/data", '-l', $logFileName ],
77 'pg_ctl restart with server not running');
79 # Permissions on log file should be default
80 SKIP:
82 skip "unix-style permissions not supported on Windows", 2
83 if ($windows_os);
85 ok(-f $logFileName);
86 ok(check_mode_recursive("$tempdir/data", 0700, 0600));
89 # Log file for group access test
90 $logFileName = "$tempdir/data/perm-test-640.log";
92 SKIP:
94 skip "group access not supported on Windows", 3 if ($windows_os);
96 system_or_bail 'pg_ctl', 'stop', '-D', "$tempdir/data";
98 # Change the data dir mode so log file will be created with group read
99 # privileges on the next start
100 chmod_recursive("$tempdir/data", 0750, 0640);
102 command_ok(
103 [ 'pg_ctl', 'start', '-D', "$tempdir/data", '-l', $logFileName ],
104 'start server to check group permissions');
106 ok(-f $logFileName);
107 ok(check_mode_recursive("$tempdir/data", 0750, 0640));
110 command_ok([ 'pg_ctl', 'restart', '-D', "$tempdir/data" ],
111 'pg_ctl restart with server running');
113 system_or_bail 'pg_ctl', 'stop', '-D', "$tempdir/data";