Debian Codegraph @

src/bin/pg_ctl/t/001_start_stop.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 use strict;
5 use warnings FATAL => 'all';
6
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 my $tempdir = PostgreSQL::Test::Utils::tempdir;
12 my $tempdir_short = PostgreSQL::Test::Utils::tempdir_short;
13
14 program_help_ok('pg_ctl');
15 program_version_ok('pg_ctl');
16 program_options_handling_ok('pg_ctl');
17
18 command_exit_is([ 'pg_ctl', 'start', '--pgdata' => "$tempdir/nonexistent" ],
19 1, 'pg_ctl start with nonexistent directory');
20
21 command_ok(
22 [
23 'pg_ctl', 'initdb',
24 '--pgdata' => "$tempdir/data",
25 '--options' => '--no-sync'
26 ],
27 'pg_ctl initdb');
28 command_ok([ $ENV{PG_REGRESS}, '--config-auth', "$tempdir/data" ],
29 'configure authentication');
30 my $node_port = PostgreSQL::Test::Cluster::get_free_port();
31 open my $conf, '>>', "$tempdir/data/postgresql.conf" or die $!;
32 print $conf "fsync = off\n";
33 print $conf "port = $node_port\n";
34 print $conf PostgreSQL::Test::Utils::slurp_file($ENV{TEMP_CONFIG})
35 if defined $ENV{TEMP_CONFIG};
36
37 if ($use_unix_sockets)
38 {
39 print $conf "listen_addresses = ''\n";
40 $tempdir_short =~ s!\\!/!g if $PostgreSQL::Test::Utils::windows_os;
41 print $conf "unix_socket_directories = '$tempdir_short'\n";
42 }
43 else
44 {
45 print $conf "listen_addresses = '127.0.0.1'\n";
46 }
47 close $conf;
48 my $ctlcmd = [
49 'pg_ctl', 'start',
50 '--pgdata' => "$tempdir/data",
51 '--log' => "$PostgreSQL::Test::Utils::log_path/001_start_stop_server.log"
52 ];
53 command_like($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
54
55 # sleep here is because Windows builds can't check postmaster.pid exactly,
56 # so they may mistake a pre-existing postmaster.pid for one created by the
57 # postmaster they start. Waiting more than the 2 seconds slop time allowed
58 # by wait_for_postmaster() prevents that mistake.
59 sleep 3 if ($windows_os);
60 command_fails([ 'pg_ctl', 'start', '--pgdata' => "$tempdir/data" ],
61 'second pg_ctl start fails');
62 command_ok([ 'pg_ctl', 'stop', '--pgdata' => "$tempdir/data" ],
63 'pg_ctl stop');
64 command_fails([ 'pg_ctl', 'stop', '--pgdata' => "$tempdir/data" ],
65 'second pg_ctl stop fails');
66
67 # Log file for default permission test. The permissions won't be checked on
68 # Windows but we still want to do the restart test.
69 my $logFileName = "$tempdir/data/perm-test-600.log";
70
71 command_ok(
72 [
73 'pg_ctl', 'restart',
74 '--pgdata' => "$tempdir/data",
75 '--log' => $logFileName
76 ],
77 'pg_ctl restart with server not running');
78
79 # Permissions on log file should be default
80 SKIP:
81 {
82 skip "unix-style permissions not supported on Windows", 2
83 if ($windows_os);
84
85 ok(-f $logFileName);
86 ok(check_mode_recursive("$tempdir/data", 0700, 0600));
87 }
88
89 # Log file for group access test
90 $logFileName = "$tempdir/data/perm-test-640.log";
91
92 SKIP:
93 {
94 skip "group access not supported on Windows", 3
95 if ($windows_os || $Config::Config{osname} eq 'cygwin');
96
97 system_or_bail 'pg_ctl', 'stop', '--pgdata' => "$tempdir/data";
98
99 # Change the data dir mode so log file will be created with group read
100 # privileges on the next start
101 chmod_recursive("$tempdir/data", 0750, 0640);
102
103 command_ok(
104 [
105 'pg_ctl', 'start',
106 '--pgdata' => "$tempdir/data",
107 '--log' => $logFileName
108 ],
109 'start server to check group permissions');
110
111 ok(-f $logFileName);
112 ok(check_mode_recursive("$tempdir/data", 0750, 0640));
113 }
114
115 command_ok([ 'pg_ctl', 'restart', '--pgdata' => "$tempdir/data" ],
116 'pg_ctl restart with server running');
117
118 system_or_bail 'pg_ctl', 'stop', '--pgdata' => "$tempdir/data";
119
120 done_testing();