|
| 1 | +# Set of tests for pg_upgrade, including cross-version checks. |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | + |
| 5 | +use Cwd qw(abs_path getcwd); |
| 6 | +use File::Basename qw(dirname); |
| 7 | + |
| 8 | +use PostgreSQL::Test::Cluster; |
| 9 | +use PostgreSQL::Test::Utils; |
| 10 | +use Test::More; |
| 11 | + |
| 12 | +# Generate a database with a name made of a range of ASCII characters. |
| 13 | +sub generate_db |
| 14 | +{ |
| 15 | + my ($node, $from_char, $to_char) = @_; |
| 16 | + |
| 17 | + my $dbname = ''; |
| 18 | + for my $i ($from_char .. $to_char) |
| 19 | + { |
| 20 | + next if $i == 7 || $i == 10 || $i == 13; # skip BEL, LF, and CR |
| 21 | + $dbname = $dbname . sprintf('%c', $i); |
| 22 | + } |
| 23 | + $node->run_log( |
| 24 | + [ 'createdb', '--host', $node->host, '--port', $node->port, $dbname ] |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +# The test of pg_upgrade requires two clusters, an old one and a new one |
| 29 | +# that gets upgraded. Before running the upgrade, a logical dump of the |
| 30 | +# old cluster is taken, and a second logical dump of the new one is taken |
| 31 | +# after the upgrade. The upgrade test passes if there are no differences |
| 32 | +# in these two dumps. |
| 33 | + |
| 34 | +# Testing upgrades with an older version of PostgreSQL requires setting up |
| 35 | +# two environment variables, as of: |
| 36 | +# - "olddump", to point to a dump file that will be used to set up the old |
| 37 | +# instance to upgrade from. |
| 38 | +# - "oldinstall", to point to the installation path of the old cluster. |
| 39 | +if ( (defined($ENV{olddump}) && !defined($ENV{oldinstall})) |
| 40 | + || (!defined($ENV{olddump}) && defined($ENV{oldinstall}))) |
| 41 | +{ |
| 42 | + # Not all variables are defined, so leave and die if test is |
| 43 | + # done with an older installation. |
| 44 | + die "olddump or oldinstall is undefined"; |
| 45 | +} |
| 46 | + |
| 47 | +# Temporary location for the dumps taken |
| 48 | +my $tempdir = PostgreSQL::Test::Utils::tempdir; |
| 49 | + |
| 50 | +# Initialize node to upgrade |
| 51 | +my $oldnode = PostgreSQL::Test::Cluster->new('old_node', |
| 52 | + install_path => $ENV{oldinstall}); |
| 53 | + |
| 54 | +# To increase coverage of non-standard segment size and group access without |
| 55 | +# increasing test runtime, run these tests with a custom setting. |
| 56 | +# --allow-group-access and --wal-segsize have been added in v11. |
| 57 | +$oldnode->init(extra => [ '--wal-segsize', '1', '--allow-group-access' ]); |
| 58 | +$oldnode->start; |
| 59 | + |
| 60 | +# The default location of the source code is the root of this directory. |
| 61 | +my $srcdir = abs_path("../../.."); |
| 62 | + |
| 63 | +# Set up the data of the old instance with a dump or pg_regress. |
| 64 | +if (defined($ENV{olddump})) |
| 65 | +{ |
| 66 | + # Use the dump specified. |
| 67 | + my $olddumpfile = $ENV{olddump}; |
| 68 | + die "no dump file found!" unless -e $olddumpfile; |
| 69 | + |
| 70 | + # Load the dump using the "postgres" database as "regression" does |
| 71 | + # not exist yet, and we are done here. |
| 72 | + $oldnode->command_ok( |
| 73 | + [ |
| 74 | + 'psql', '-X', '-f', $olddumpfile, |
| 75 | + '--port', $oldnode->port, '--host', $oldnode->host, |
| 76 | + 'postgres' |
| 77 | + ]); |
| 78 | +} |
| 79 | +else |
| 80 | +{ |
| 81 | + # Default is to use pg_regress to set up the old instance. |
| 82 | + |
| 83 | + # Create databases with names covering most ASCII bytes |
| 84 | + generate_db($oldnode, 1, 45); |
| 85 | + generate_db($oldnode, 46, 90); |
| 86 | + generate_db($oldnode, 91, 127); |
| 87 | + |
| 88 | + # Grab any regression options that may be passed down by caller. |
| 89 | + my $extra_opts_val = $ENV{EXTRA_REGRESS_OPT} || ""; |
| 90 | + my @extra_opts = split(/\s+/, $extra_opts_val); |
| 91 | + |
| 92 | + # --dlpath is needed to be able to find the location of regress.so |
| 93 | + # and any libraries the regression tests require. |
| 94 | + my $dlpath = dirname($ENV{REGRESS_SHLIB}); |
| 95 | + |
| 96 | + # --outputdir points to the path where to place the output files. |
| 97 | + my $outputdir = $PostgreSQL::Test::Utils::tmp_check; |
| 98 | + |
| 99 | + # --inputdir points to the path of the input files. |
| 100 | + my $inputdir = "$srcdir/src/test/regress"; |
| 101 | + |
| 102 | + my @regress_command = [ |
| 103 | + $ENV{PG_REGRESS}, @extra_opts, |
| 104 | + '--dlpath', $dlpath, |
| 105 | + '--max-concurrent-tests', '20', |
| 106 | + '--bindir=', '--host', |
| 107 | + $oldnode->host, '--port', |
| 108 | + $oldnode->port, '--schedule', |
| 109 | + "$srcdir/src/test/regress/parallel_schedule", '--outputdir', |
| 110 | + $outputdir, '--inputdir', |
| 111 | + $inputdir |
| 112 | + ]; |
| 113 | + |
| 114 | + $oldnode->command_ok(@regress_command, |
| 115 | + 'regression test run on old instance'); |
| 116 | +} |
| 117 | + |
| 118 | +# Before dumping, get rid of objects not existing or not supported in later |
| 119 | +# versions. This depends on the version of the old server used, and matters |
| 120 | +# only if different major versions are used for the dump. |
| 121 | +if (defined($ENV{oldinstall})) |
| 122 | +{ |
| 123 | + # Note that upgrade_adapt.sql from the new version is used, to |
| 124 | + # cope with an upgrade to this version. |
| 125 | + $oldnode->run_log( |
| 126 | + [ |
| 127 | + 'psql', '-X', |
| 128 | + '-f', "$srcdir/src/bin/pg_upgrade/upgrade_adapt.sql", |
| 129 | + '--port', $oldnode->port, |
| 130 | + '--host', $oldnode->host, |
| 131 | + 'regression' |
| 132 | + ]); |
| 133 | +} |
| 134 | + |
| 135 | +# Initialize a new node for the upgrade. |
| 136 | +my $newnode = PostgreSQL::Test::Cluster->new('new_node'); |
| 137 | +$newnode->init(extra => [ '--wal-segsize', '1', '--allow-group-access' ]); |
| 138 | +my $newbindir = $newnode->config_data('--bindir'); |
| 139 | +my $oldbindir = $oldnode->config_data('--bindir'); |
| 140 | + |
| 141 | +# Take a dump before performing the upgrade as a base comparison. Note |
| 142 | +# that we need to use pg_dumpall from the new node here. |
| 143 | +$newnode->command_ok( |
| 144 | + [ |
| 145 | + 'pg_dumpall', '--no-sync', |
| 146 | + '-d', $oldnode->connstr('postgres'), |
| 147 | + '-f', "$tempdir/dump1.sql" |
| 148 | + ], |
| 149 | + 'dump before running pg_upgrade'); |
| 150 | + |
| 151 | +# After dumping, update references to the old source tree's regress.so |
| 152 | +# to point to the new tree. |
| 153 | +if (defined($ENV{oldinstall})) |
| 154 | +{ |
| 155 | + # First, fetch all the references to libraries that are not part |
| 156 | + # of the default path $libdir. |
| 157 | + my $output = $oldnode->safe_psql('regression', |
| 158 | + "SELECT DISTINCT probin::text FROM pg_proc WHERE probin NOT LIKE '\$libdir%';" |
| 159 | + ); |
| 160 | + chomp($output); |
| 161 | + my @libpaths = split("\n", $output); |
| 162 | + |
| 163 | + my $dump_data = slurp_file("$tempdir/dump1.sql"); |
| 164 | + |
| 165 | + my $newregresssrc = "$srcdir/src/test/regress"; |
| 166 | + foreach (@libpaths) |
| 167 | + { |
| 168 | + my $libpath = $_; |
| 169 | + $libpath = dirname($libpath); |
| 170 | + $dump_data =~ s/$libpath/$newregresssrc/g; |
| 171 | + } |
| 172 | + |
| 173 | + open my $fh, ">", "$tempdir/dump1.sql" or die "could not open dump file"; |
| 174 | + print $fh $dump_data; |
| 175 | + close $fh; |
| 176 | + |
| 177 | + # This replaces any references to the old tree's regress.so |
| 178 | + # the new tree's regress.so. Any references that do *not* |
| 179 | + # match $libdir are switched so as this request does not |
| 180 | + # depend on the path of the old source tree. This is useful |
| 181 | + # when using an old dump. Do the operation on all the databases |
| 182 | + # that allow connections so as this includes the regression |
| 183 | + # database and anything the user has set up. |
| 184 | + $output = $oldnode->safe_psql('postgres', |
| 185 | + "SELECT datname FROM pg_database WHERE datallowconn;"); |
| 186 | + chomp($output); |
| 187 | + my @datnames = split("\n", $output); |
| 188 | + foreach (@datnames) |
| 189 | + { |
| 190 | + my $datname = $_; |
| 191 | + $oldnode->safe_psql( |
| 192 | + $datname, "UPDATE pg_proc SET probin = |
| 193 | + regexp_replace(probin, '.*/', '$newregresssrc/') |
| 194 | + WHERE probin NOT LIKE '\$libdir/%'"); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +# Upgrade the instance. |
| 199 | +$oldnode->stop; |
| 200 | +command_ok( |
| 201 | + [ |
| 202 | + 'pg_upgrade', '--no-sync', '-d', $oldnode->data_dir, |
| 203 | + '-D', $newnode->data_dir, '-b', $oldbindir, |
| 204 | + '-B', $newbindir, '-p', $oldnode->port, |
| 205 | + '-P', $newnode->port |
| 206 | + ], |
| 207 | + 'run of pg_upgrade for new instance'); |
| 208 | +$newnode->start; |
| 209 | + |
| 210 | +# Check if there are any logs coming from pg_upgrade, that would only be |
| 211 | +# retained on failure. |
| 212 | +my $log_path = $newnode->data_dir . "/pg_upgrade_output.d/log"; |
| 213 | +if (-d $log_path) |
| 214 | +{ |
| 215 | + foreach my $log (glob("$log_path/*")) |
| 216 | + { |
| 217 | + note "###########################"; |
| 218 | + note "Contents of log file $log"; |
| 219 | + note "###########################"; |
| 220 | + my $log_contents = slurp_file($log); |
| 221 | + print "$log_contents\n"; |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +# Second dump from the upgraded instance. |
| 226 | +$newnode->run_log( |
| 227 | + [ |
| 228 | + 'pg_dumpall', '--no-sync', |
| 229 | + '-d', $newnode->connstr('postgres'), |
| 230 | + '-f', "$tempdir/dump2.sql" |
| 231 | + ]); |
| 232 | + |
| 233 | +# Compare the two dumps, there should be no differences. |
| 234 | +command_ok([ 'diff', '-q', "$tempdir/dump1.sql", "$tempdir/dump2.sql" ], |
| 235 | + 'old and new dump match after pg_upgrade'); |
| 236 | + |
| 237 | +done_testing(); |
0 commit comments