Allow using Unix-domain sockets on Windows in tests
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 30 Mar 2020 15:30:44 +0000 (17:30 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 30 Mar 2020 15:35:29 +0000 (17:35 +0200)
The test suites currently don't use Unix-domain sockets on Windows.
This optionally allows enabling that by setting the environment
variable PG_TEST_USE_UNIX_SOCKETS.

This should currently be considered experimental.  In particular,
pg_regress.c contains some comments that the cleanup code for
Unix-domain sockets doesn't work correctly under Windows, which hasn't
been an problem until now.  But it's good enough for locally
supervised testing of the functionality.

Reviewed-by: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/54bde68c-d134-4eb8-5bd3-8af33b72a010@2ndquadrant.com

src/bin/pg_ctl/t/001_start_stop.pl
src/test/authentication/t/001_password.pl
src/test/authentication/t/002_saslprep.pl
src/test/perl/PostgresNode.pm
src/test/perl/TestLib.pm
src/test/regress/pg_regress.c

index 6a1619e171bf926efcd1499ef82ceee53f0dfa0b..b1e419f02e95ce2ffa8cd9980278b3964657a419 100644 (file)
@@ -29,7 +29,7 @@ print $conf "port = $node_port\n";
 print $conf TestLib::slurp_file($ENV{TEMP_CONFIG})
   if defined $ENV{TEMP_CONFIG};
 
-if (!$windows_os)
+if ($use_unix_sockets)
 {
    print $conf "listen_addresses = ''\n";
    print $conf "unix_socket_directories = '$tempdir_short'\n";
index 5985130e3d51674a80ce96bc1b9b518bd7a768db..b8d6cc52e9d4fddca187dc1dfc84e6d84cdc9573 100644 (file)
@@ -3,17 +3,16 @@
 # - Plain
 # - MD5-encrypted
 # - SCRAM-encrypted
-# This test cannot run on Windows as Postgres cannot be set up with Unix
-# sockets and needs to go through SSPI.
+# This test can only run with Unix-domain sockets.
 
 use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
 use Test::More;
-if ($windows_os)
+if (!$use_unix_sockets)
 {
-   plan skip_all => "authentication tests cannot run on Windows";
+   plan skip_all => "authentication tests cannot run without Unix-domain sockets";
 }
 else
 {
index c4b335c45fe986a66e71446eec871ecdc0247d0d..bf57933d94b010d971e7138e43faebfe55ab1650 100644 (file)
@@ -1,16 +1,15 @@
 # Test password normalization in SCRAM.
 #
-# This test cannot run on Windows as Postgres cannot be set up with Unix
-# sockets and needs to go through SSPI.
+# This test can only run with Unix-domain sockets.
 
 use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
 use Test::More;
-if ($windows_os)
+if (!$use_unix_sockets)
 {
-   plan skip_all => "authentication tests cannot run on Windows";
+   plan skip_all => "authentication tests cannot run without Unix-domain sockets";
 }
 else
 {
index 9575268bd779dcfcbdcc5505f48df9b7665ba934..1d5450758e48cc402c7808665f89843f22220ae0 100644 (file)
@@ -116,7 +116,7 @@ INIT
 
    # Set PGHOST for backward compatibility.  This doesn't work for own_host
    # nodes, so prefer to not rely on this when writing new tests.
-   $use_tcp            = $TestLib::windows_os;
+   $use_tcp            = !$TestLib::use_unix_sockets;
    $test_localhost     = "127.0.0.1";
    $last_host_assigned = 1;
    $test_pghost        = $use_tcp ? $test_localhost : TestLib::tempdir_short;
@@ -387,7 +387,7 @@ sub set_replication_conf
 
    open my $hba, '>>', "$pgdata/pg_hba.conf";
    print $hba "\n# Allow replication (set up by PostgresNode.pm)\n";
-   if ($TestLib::windows_os)
+   if ($TestLib::windows_os && !$TestLib::use_unix_sockets)
    {
        print $hba
          "host replication all $test_localhost/32 sspi include_realm=1 map=regress\n";
index 65ee0425b06ee90376a1207ea9a374a5d59d32dd..0e6c4819e4cf733bf3b792e382a68ff411940daf 100644 (file)
@@ -83,9 +83,10 @@ our @EXPORT = qw(
   command_checks_all
 
   $windows_os
+  $use_unix_sockets
 );
 
-our ($windows_os, $tmp_check, $log_path, $test_logfile);
+our ($windows_os, $use_unix_sockets, $tmp_check, $log_path, $test_logfile);
 
 BEGIN
 {
@@ -117,6 +118,11 @@ BEGIN
        require Win32API::File;
        Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
    }
+
+   # Specifies whether to use Unix sockets for test setups.  On
+   # Windows we don't use them by default since it's not universally
+   # supported, but it can be overridden if desired.
+   $use_unix_sockets = (!$windows_os || defined $ENV{PG_TEST_USE_UNIX_SOCKETS});
 }
 
 =pod
index 1e2aa4865474d5b9fe7d15ba1129f2fca8c67c10..38b2b1e8e1ba4ea1acb79442c0ef7c99cb7bd2e8 100644 (file)
@@ -292,7 +292,7 @@ stop_postmaster(void)
  * remove the directory.  Ignore errors; leaking a temporary directory is
  * unimportant.  This can run from a signal handler.  The code is not
  * acceptable in a Windows signal handler (see initdb.c:trapsig()), but
- * on Windows, pg_regress does not use Unix sockets.
+ * on Windows, pg_regress does not use Unix sockets by default.
  */
 static void
 remove_temp(void)
@@ -2106,6 +2106,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
        {NULL, 0, NULL, 0}
    };
 
+   bool        use_unix_sockets;
    _stringlist *sl;
    int         c;
    int         i;
@@ -2121,15 +2122,23 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 
    atexit(stop_postmaster);
 
-#if !defined(HAVE_UNIX_SOCKETS) || defined(WIN32)
+#if !defined(HAVE_UNIX_SOCKETS)
+   use_unix_sockets = false;
+#elif defined(WIN32)
+
    /*
-    * No Unix-domain sockets available, so change default.  For now, we also
-    * don't use them on Windows, even if the build supports them.  (See
-    * comment at remove_temp() for a reason.)
+    * We don't use Unix-domain sockets on Windows by default, even if the
+    * build supports them.  (See comment at remove_temp() for a reason.)
+    * Override at your own risk.
     */
-   hostname = "localhost";
+   use_unix_sockets = getenv("PG_TEST_USE_UNIX_SOCKETS") ? true : false;
+#else
+   use_unix_sockets = true;
 #endif
 
+   if (!use_unix_sockets)
+       hostname = "localhost";
+
    /*
     * We call the initialization function here because that way we can set
     * default parameters and let them be overwritten by the commandline.
@@ -2243,7 +2252,8 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
    if (config_auth_datadir)
    {
 #ifdef ENABLE_SSPI
-       config_sspi_auth(config_auth_datadir, user);
+       if (!use_unix_sockets)
+           config_sspi_auth(config_auth_datadir, user);
 #endif
        exit(0);
    }
@@ -2364,13 +2374,15 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
        fclose(pg_conf);
 
 #ifdef ENABLE_SSPI
-
-       /*
-        * Since we successfully used the same buffer for the much-longer
-        * "initdb" command, this can't truncate.
-        */
-       snprintf(buf, sizeof(buf), "%s/data", temp_instance);
-       config_sspi_auth(buf, NULL);
+       if (!use_unix_sockets)
+       {
+           /*
+            * Since we successfully used the same buffer for the much-longer
+            * "initdb" command, this can't truncate.
+            */
+           snprintf(buf, sizeof(buf), "%s/data", temp_instance);
+           config_sspi_auth(buf, NULL);
+       }
 #elif !defined(HAVE_UNIX_SOCKETS)
 #error Platform has no means to secure the test installation.
 #endif