1# To test successful data directory creation with an additional feature, first
2# try to elaborate the "successful creation" test instead of adding a test.
3# Successful initdb consumes much time and I/O.
4
5use strict;
6use warnings;
7use Fcntl ':mode';
8use File::stat qw{lstat};
9use PostgresNode;
10use TestLib;
11use Test::More tests => 22;
12
13my $tempdir = TestLib::tempdir;
14my $xlogdir = "$tempdir/pgxlog";
15my $datadir = "$tempdir/data";
16
17program_help_ok('initdb');
18program_version_ok('initdb');
19program_options_handling_ok('initdb');
20
21command_fails([ 'initdb', '-S', "$tempdir/nonexistent" ],
22	'sync missing data directory');
23
24mkdir $xlogdir;
25mkdir "$xlogdir/lost+found";
26command_fails(
27	[ 'initdb', '-X', $xlogdir, $datadir ],
28	'existing nonempty xlog directory');
29rmdir "$xlogdir/lost+found";
30command_fails(
31	[ 'initdb', '-X', 'pgxlog', $datadir ],
32	'relative xlog directory not allowed');
33
34command_fails(
35	[ 'initdb', '-U', 'pg_test', $datadir ],
36	'role names cannot begin with "pg_"');
37
38mkdir $datadir;
39
40# make sure we run one successful test without a TZ setting so we test
41# initdb's time zone setting code
42{
43
44	# delete local only works from perl 5.12, so use the older way to do this
45	local (%ENV) = %ENV;
46	delete $ENV{TZ};
47
48	command_ok([ 'initdb', '-N', '-T', 'german', '-X', $xlogdir, $datadir ],
49		'successful creation');
50
51	# Permissions on PGDATA should be default
52  SKIP:
53	{
54		skip "unix-style permissions not supported on Windows", 1
55		  if ($windows_os);
56
57		ok(check_mode_recursive($datadir, 0700, 0600),
58			"check PGDATA permissions");
59	}
60}
61
62# Control file should tell that data checksums are disabled by default.
63command_like(
64	[ 'pg_controldata', $datadir ],
65	qr/Data page checksum version:.*0/,
66	'checksums are disabled in control file');
67# pg_checksums fails with checksums disabled by default.  This is
68# not part of the tests included in pg_checksums to save from
69# the creation of an extra instance.
70command_fails([ 'pg_checksums', '-D', $datadir ],
71	"pg_checksums fails with data checksum disabled");
72
73command_ok([ 'initdb', '-S', $datadir ], 'sync only');
74command_fails([ 'initdb', $datadir ], 'existing data directory');
75
76# Check group access on PGDATA
77SKIP:
78{
79	skip "unix-style permissions not supported on Windows", 2
80	  if ($windows_os);
81
82	# Init a new db with group access
83	my $datadir_group = "$tempdir/data_group";
84
85	command_ok(
86		[ 'initdb', '-g', $datadir_group ],
87		'successful creation with group access');
88
89	ok(check_mode_recursive($datadir_group, 0750, 0640),
90		'check PGDATA permissions');
91}
92