Improve check in LDAP test to find the OpenLDAP installation
authorHeikki Linnakangas
Sun, 7 Apr 2024 17:21:21 +0000 (20:21 +0300)
committerHeikki Linnakangas
Sun, 7 Apr 2024 17:22:00 +0000 (20:22 +0300)
If the OpenLDAP installation directory is not found, set $setup to 0
so that the LDAP tests are skipped. The macOS checks were already
doing that, but the checks on other OS's were not. While we're at it,
improve the error message when the tests are skipped, to specify
whether the OS is supported at all, or if we just didn't find the
installation directory.

This was accidentally "working" without this, i.e. we were skipping
the tests if the OpenLDAP installation was not found, because of a bug
in the LdapServer test module: the END block clobbered the exit code
so if the script die()s before running the first subtest, the whole
test script was marked as SKIPped. The next commit will fix that bug,
but we need to fix the setup code first.

These checks should probably go into configure/meson, but this is
better than nothing and allows fixing the bug in the END block.

Backpatch to all supported versions.

Discussion: https://www.postgresql.org/message-id/fb898a70-3a88-4629-88e9-f2375020061d@iki.fi

src/test/ldap/LdapServer.pm
src/test/ldap/t/001_auth.pl
src/test/ldap/t/002_bindpasswd.pl

index a4c1a1843c8b8dfbda87231835c3e66a064bc650..1d680d82900aaf444cf5579100440f84c22221ab 100644 (file)
@@ -57,49 +57,88 @@ use File::Basename;
 # private variables
 my ($slapd, $ldap_schema_dir, @servers);
 
-# visible variable
-our ($setup);
+# visible variables
+our ($setup, $setup_error);
 
 INIT
 {
+   # Find the OpenLDAP server binary and directory containing schema
+   # definition files.  On success, $setup is set to 1. On failure,
+   # it's set to 0, and an error message is set in $setup_error.
    $setup = 1;
-   if ($^O eq 'darwin' && -d '/opt/homebrew/opt/openldap')
+   if ($^O eq 'darwin')
    {
-       # typical paths for Homebrew on ARM
-       $slapd = '/opt/homebrew/opt/openldap/libexec/slapd';
-       $ldap_schema_dir = '/opt/homebrew/etc/openldap/schema';
-   }
-   elsif ($^O eq 'darwin' && -d '/usr/local/opt/openldap')
-   {
-       # typical paths for Homebrew on Intel
-       $slapd = '/usr/local/opt/openldap/libexec/slapd';
-       $ldap_schema_dir = '/usr/local/etc/openldap/schema';
-   }
-   elsif ($^O eq 'darwin' && -d '/opt/local/etc/openldap')
-   {
-       # typical paths for MacPorts
-       $slapd = '/opt/local/libexec/slapd';
-       $ldap_schema_dir = '/opt/local/etc/openldap/schema';
+       if (-d '/opt/homebrew/opt/openldap')
+       {
+           # typical paths for Homebrew on ARM
+           $slapd = '/opt/homebrew/opt/openldap/libexec/slapd';
+           $ldap_schema_dir = '/opt/homebrew/etc/openldap/schema';
+       }
+       elsif (-d '/usr/local/opt/openldap')
+       {
+           # typical paths for Homebrew on Intel
+           $slapd = '/usr/local/opt/openldap/libexec/slapd';
+           $ldap_schema_dir = '/usr/local/etc/openldap/schema';
+       }
+       elsif (-d '/opt/local/etc/openldap')
+       {
+           # typical paths for MacPorts
+           $slapd = '/opt/local/libexec/slapd';
+           $ldap_schema_dir = '/opt/local/etc/openldap/schema';
+       }
+       else
+       {
+           $setup_error = "OpenLDAP server installation not found";
+           $setup = 0;
+       }
    }
    elsif ($^O eq 'linux')
    {
-       $slapd = '/usr/sbin/slapd';
-       $ldap_schema_dir = '/etc/ldap/schema' if -d '/etc/ldap/schema';
-       $ldap_schema_dir = '/etc/openldap/schema'
-         if -d '/etc/openldap/schema';
+       if (-d '/etc/ldap/schema')
+       {
+           $slapd = '/usr/sbin/slapd';
+           $ldap_schema_dir = '/etc/ldap/schema';
+       }
+       elsif (-d '/etc/openldap/schema')
+       {
+           $slapd = '/usr/sbin/slapd';
+           $ldap_schema_dir = '/etc/openldap/schema';
+       }
+       else
+       {
+           $setup_error = "OpenLDAP server installation not found";
+           $setup = 0;
+       }
    }
    elsif ($^O eq 'freebsd')
    {
-       $slapd = '/usr/local/libexec/slapd';
-       $ldap_schema_dir = '/usr/local/etc/openldap/schema';
+       if (-d '/usr/local/etc/openldap/schema')
+       {
+           $slapd = '/usr/local/libexec/slapd';
+           $ldap_schema_dir = '/usr/local/etc/openldap/schema';
+       }
+       else
+       {
+           $setup_error = "OpenLDAP server installation not found";
+           $setup = 0;
+       }
    }
    elsif ($^O eq 'openbsd')
    {
-       $slapd = '/usr/local/libexec/slapd';
-       $ldap_schema_dir = '/usr/local/share/examples/openldap/schema';
+       if (-d '/usr/local/share/examples/openldap/schema')
+       {
+           $slapd = '/usr/local/libexec/slapd';
+           $ldap_schema_dir = '/usr/local/share/examples/openldap/schema';
+       }
+       else
+       {
+           $setup_error = "OpenLDAP server installation not found";
+           $setup = 0;
+       }
    }
    else
    {
+       $setup_error = "ldap tests not supported on $^O";
        $setup = 0;
    }
 }
index 3e113fd6ebb535209a6014c5e423e10619427d38..ef8ad1762aa90a4c0632431f39c7f5ed34712a2c 100644 (file)
@@ -25,8 +25,7 @@ elsif ($ENV{PG_TEST_EXTRA} !~ /\bldap\b/)
 }
 elsif (!$LdapServer::setup)
 {
-   plan skip_all =>
-     "ldap tests not supported on $^O or dependencies not installed";
+   plan skip_all => $LdapServer::setup_error;
 }
 
 note "setting up LDAP server";
index bcd4aa2b742fe0def7d294cfcd970f2feee7b661..9242fafacaecab0c976db51016a87abbc4e69173 100644 (file)
@@ -25,8 +25,7 @@ elsif ($ENV{PG_TEST_EXTRA} !~ /\bldap\b/)
 }
 elsif (!$LdapServer::setup)
 {
-   plan skip_all =>
-     "ldap tests not supported on $^O or dependencies not installed";
+   plan skip_all => $LdapServer::setup_error;
 }
 
 note "setting up LDAP server";