Allow passing extra options to initdb for tests
authorPeter Eisentraut
Thu, 15 Feb 2024 08:52:08 +0000 (09:52 +0100)
committerPeter Eisentraut
Thu, 15 Feb 2024 09:29:18 +0000 (10:29 +0100)
Setting the environment variable PG_TEST_INITDB_EXTRA_OPTS passes
extra options to initdb run by pg_regress or
PostgreSQL::Test::Cluster's init.

This can be useful for a wide variety of uses, like running all tests
with checksums enabled, or with JIT enabled, or with different GUC
settings, or with different locale settings.  (Not all tests are going
to pass with arbitrary options, but it is useful to run this against
specific test suites.)

Reviewed-by: Ian Lawrence Barwick
Discussion: https://www.postgresql.org/message-id/flat/d4d2ad9f-1c1d-47a1-bb4d-c10a747d4f15%40eisentraut.org

doc/src/sgml/regress.sgml
src/test/perl/PostgreSQL/Test/Cluster.pm
src/test/regress/pg_regress.c

index 70d9bdefe1bcaa5fd2a453ee9870ec3532e02801..6a27aae31951278964b16a849cb8666a676546cc 100644 (file)
@@ -390,12 +390,37 @@ make check LANG=C ENCODING=EUC_JP
    Custom Server Settings
 
    
-    Custom server settings to use when running a regression test suite can be
+    There are several ways to use custom server settings when running a test
+    suite.  This can be useful to enable additional logging, adjust resource
+    limits, or enable extra run-time checks such as 
+    linkend="guc-debug-discard-caches"/>.  But note that not all tests can be
+    expected to pass cleanly with arbitrary settings.
+   
+
+   
+    Extra options can be passed to the various initdb
+    commands that are run internally during test setup using the environment
+    variable PG_TEST_INITDB_EXTRA_OPTS.  For example, to run a
+    test with checksums enabled and a custom WAL segment size and
+    work_mem setting, use:
+
+make check PG_TEST_INITDB_EXTRA_OPTS='-k --wal-segsize=4 -c work_mem=50MB'
+
+   
+
+   
+    For the core regression test suite and other tests driven by
+    pg_regress, custom run-time server settings can also be
     set in the PGOPTIONS environment variable (for settings
-    that allow this):
+    that allow this), for example:
 
 make check PGOPTIONS="-c debug_parallel_query=regress -c work_mem=50MB"
 
+    (This makes use of functionality provided by libpq; see 
+    linkend="libpq-connect-options"/> for details.)
+   
+
+   
     When running against a temporary installation, custom settings can also be
     set by supplying a pre-written postgresql.conf:
 
@@ -405,11 +430,6 @@ make check EXTRA_REGRESS_OPTS="--temp-config=test_postgresql.conf"
 
    
 
-   
-    This can be useful to enable additional logging, adjust resource limits,
-    or enable extra run-time checks such as 
-    linkend="guc-debug-discard-caches"/>.
-   
   
 
   
index e2e70d0dbf9ad24c58e8c9830975081159219779..07da74cf562381bcad3d056ac85bb79642c56174 100644 (file)
@@ -114,6 +114,7 @@ use Socket;
 use Test::More;
 use PostgreSQL::Test::Utils          ();
 use PostgreSQL::Test::BackgroundPsql ();
+use Text::ParseWords                 qw(shellwords);
 use Time::HiRes                      qw(usleep);
 use Scalar::Util                     qw(blessed);
 
@@ -519,6 +520,12 @@ sub init
    $params{allows_streaming} = 0 unless defined $params{allows_streaming};
    $params{has_archiving} = 0 unless defined $params{has_archiving};
 
+   my $initdb_extra_opts_env = $ENV{PG_TEST_INITDB_EXTRA_OPTS};
+   if (defined $initdb_extra_opts_env)
+   {
+       push @{ $params{extra} }, shellwords($initdb_extra_opts_env);
+   }
+
    mkdir $self->backup_dir;
    mkdir $self->archive_dir;
 
index c894005dac06bc233896520c83c18e51af8ace9c..f1f6011ae0a337c4c6c24cf90218c57a730d4818 100644 (file)
@@ -2306,6 +2306,7 @@ regression_main(int argc, char *argv[],
        const char *keywords[4];
        const char *values[4];
        PGPing      rv;
+       const char *initdb_extra_opts_env;
 
        /*
         * Prepare the temp instance
@@ -2327,6 +2328,8 @@ regression_main(int argc, char *argv[],
        if (!directory_exists(buf))
            make_directory(buf);
 
+       initdb_extra_opts_env = getenv("PG_TEST_INITDB_EXTRA_OPTS");
+
        initStringInfo(&cmd);
 
        /*
@@ -2339,7 +2342,7 @@ regression_main(int argc, char *argv[],
         * duplicate it until we require perl at build time.
         */
        initdb_template_dir = getenv("INITDB_TEMPLATE");
-       if (initdb_template_dir == NULL || nolocale || debug)
+       if (initdb_template_dir == NULL || nolocale || debug || initdb_extra_opts_env)
        {
            note("initializing database system by running initdb");
 
@@ -2352,6 +2355,8 @@ regression_main(int argc, char *argv[],
                appendStringInfoString(&cmd, " --debug");
            if (nolocale)
                appendStringInfoString(&cmd, " --no-locale");
+           if (initdb_extra_opts_env)
+               appendStringInfo(&cmd, " %s", initdb_extra_opts_env);
            appendStringInfo(&cmd, " > \"%s/log/initdb.log\" 2>&1", outputdir);
            fflush(NULL);
            if (system(cmd.data))