Running LDAP tests on travis-ci

After making PEAR compatible with PHP 7, I saw that my ISDN call montior did not start up anymore because of constructor visibility issues in the Net_LDAP2 package.

The original author did not have time to do the fixes anymore, so I stepped up to make the package compatible with PHP 7 and PEAR 1.10.1.

When fixing bugs in a package, I made it a rule to always set up automatic unit test runs when a commit or a git pull request come in; travis-ci makes that particularly easy and has nice integration into github. You see the build status directly in the pull requests and get images to embed into your README to show the current build status.

Net_LDAP2 is a library to interface with LDAP servers, and has a number of tests that require such a server. So getting slapd running on travis-ci was my task, for better test coverage.

LDAP setup


travis-ci is switching to a docker-based environment, which means that tests start very fast (because no full VMs need to be started up), but also do not allow root access via sudo.

Fortunately for us, the Debian packages slapd (LDAP server) and ldap-utils (Programs to interact with slapd) are on travis' white list, which means that you can install them via a command in your .travis.yml file:

language: php
sudo: false
    - ldap-utils
    - slapd


The LDAP server needs some configuration: The root DN, admin username and password (use slappasswd) as well as some standard schemas like inetorgperson:

# See slapd.conf(5) for details on configuration options.
include   /etc/ldap/schema/core.schema
include   /etc/ldap/schema/cosine.schema
include   /etc/ldap/schema/inetorgperson.schema
include   /etc/ldap/schema/nis.schema
pidfile  /tmp/slapd/
argsfile /tmp/slapd/slapd.args
modulepath /usr/lib/openldap
database  ldif
directory /tmp/slapd
suffix    "dc=example,dc=com"
rootdn    "cn=admin,dc=example,dc=com"
rootpw    {SSHA}AIzygLSXlArhAMzddUriXQxf7UlkqopP

And because we're not runing as root, the server has to run on a port > 1024 . The server has to run in the background, otherwise it would block the following script commands completely.

Apart from the basic configuration, the unit tests require some test data to exist, so we have to import them before. PHP on travis-ci does not have the LDAP module loaded, so we have to take care of this, too.

The resulting travis-ci configuration is the following:

  - phpenv config-add tests/travis/enable-ldap.ini
  - mkdir /tmp/slapd
  - slapd -f tests/ldif_data/slapd.conf -h ldap://localhost:3389 &
  - sleep 3
  - ldapadd -h localhost:3389 -D cn=admin,dc=example,dc=com -w test -f tests/ldif_data/base.ldif
  - ldapadd -h localhost:3389 -D cn=admin,dc=example,dc=com -w test -f tests/ldif_data/INITIAL_TESTDATA.ldif

I chose to sleep 3 seconds after starting slapd since one time I had the issue that ldapadd failed because the LDAP server wasn't up.


After all that works now, you can see Net_LDAP2 unit test results on travis-ci, and inspect the full .travis.yml file.

Written by Christian Weiske.

Comments? Please send an e-mail.