From cd40cf713991569e51b42742177e57f3dca148bc Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Mon, 11 Mar 2013 16:04:17 +0000 Subject: [PATCH] php: Fix tests. - Use ./run script to run the tests. - Set environment variables correctly, including $PATH. - Test the locally built, not installed, copy of libguestfs. --- .gitignore | 1 + php/Makefile.am | 3 +++ php/extension/guestfs_php_001.phpt | 10 ++++++++ php/extension/guestfs_php_002.phpt | 10 ++++++++ php/extension/guestfs_php_003.phpt | 10 ++++++++ php/run-php-tests.sh | 40 +++++++++++++++++++----------- 6 files changed, 60 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index c49658d45..cd129dfd5 100644 --- a/.gitignore +++ b/.gitignore @@ -298,6 +298,7 @@ Makefile.in /php/extension/config.sub /php/extension/configure /php/extension/configure.in +/php/extension/env /php/extension/guestfs_php.c /php/extension/guestfs_php_*.diff /php/extension/guestfs_php_*.exp diff --git a/php/Makefile.am b/php/Makefile.am index a3690608e..3f95c83f8 100644 --- a/php/Makefile.am +++ b/php/Makefile.am @@ -51,6 +51,9 @@ extension/config.h: extension/config.m4 ../config.status cd extension && ./configure --prefix=$(prefix) --libdir=$(libdir) test -f "$@" && touch -- $@ +# Don't use --test because PHP always returns a success code. +TESTS_ENVIRONMENT = $(top_builddir)/run + TESTS = run-php-tests.sh # Before PHP 5.4.0, the extension directory didn't have a 'make clean' diff --git a/php/extension/guestfs_php_001.phpt b/php/extension/guestfs_php_001.phpt index 771592ffb..65723a6f4 100644 --- a/php/extension/guestfs_php_001.phpt +++ b/php/extension/guestfs_php_001.phpt @@ -5,6 +5,16 @@ Load the module and create a handle. // See comment in php/run-php-tests.sh. //putenv ('LIBGUESTFS_DEBUG=1'); +if (! $fp = fopen ("env", "r")) { + die ("Error: cannot open environment file 'env'\n"); +} +while (($buffer = fgets ($fp)) != false) { + putenv (chop ($buffer, "\n")); +} +if (!feof ($fp)) { + die ("Error: unexpected failure of fgets\n"); +} +fclose ($fp); $g = guestfs_create (); if ($g == false) { diff --git a/php/extension/guestfs_php_002.phpt b/php/extension/guestfs_php_002.phpt index a6e74ad4b..0fa29b0c4 100644 --- a/php/extension/guestfs_php_002.phpt +++ b/php/extension/guestfs_php_002.phpt @@ -5,6 +5,16 @@ Launch the appliance and run basic tests. // See comment in php/run-php-tests.sh. //putenv ('LIBGUESTFS_DEBUG=1'); +if (! $fp = fopen ("env", "r")) { + die ("Error: cannot open environment file 'env'\n"); +} +while (($buffer = fgets ($fp)) != false) { + putenv (chop ($buffer, "\n")); +} +if (!feof ($fp)) { + die ("Error: unexpected failure of fgets\n"); +} +fclose ($fp); $g = guestfs_create (); if ($g == false) { diff --git a/php/extension/guestfs_php_003.phpt b/php/extension/guestfs_php_003.phpt index 610adda23..8cb78dad6 100644 --- a/php/extension/guestfs_php_003.phpt +++ b/php/extension/guestfs_php_003.phpt @@ -5,6 +5,16 @@ Check function with optional arguments. // See comment in php/run-php-tests.sh. //putenv ('LIBGUESTFS_DEBUG=1'); +if (! $fp = fopen ("env", "r")) { + die ("Error: cannot open environment file 'env'\n"); +} +while (($buffer = fgets ($fp)) != false) { + putenv (chop ($buffer, "\n")); +} +if (!feof ($fp)) { + die ("Error: unexpected failure of fgets\n"); +} +fclose ($fp); $g = guestfs_create (); if ($g == false) { diff --git a/php/run-php-tests.sh b/php/run-php-tests.sh index 66159854d..46a18a42e 100755 --- a/php/run-php-tests.sh +++ b/php/run-php-tests.sh @@ -1,5 +1,5 @@ -#!/bin/sh - -# Copyright (C) 2010 Red Hat Inc. +#!/bin/bash - +# Copyright (C) 2010-2013 Red Hat Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,21 +18,33 @@ set -e cd extension +# The PHP test script cleans the environment. (This is, apparently, +# to fully simulate how PHP runs when it runs in the context of +# Apache, and not only because PHP is written by morons). We +# therefore have to load the environment (from php/extension/env which +# is generated below) at the start of each test script. + +# As a consequence of above, LIBGUESTFS_DEBUG=1 and LIBGUESTFS_TRACE=1 +# won't get passed down to the script. Furthermore, setting debug or +# trace isn't very useful anyway because the PHP test script mixes +# stdout and stderr together and compares this to the expected output, +# so you'd just get failures for every test. So there is no good way +# to debug libguestfs failures in PHP tests, but if an individual test +# fails locally then you can edit the guestfs_php_*.phpt.in and +# uncomment the putenv statement, then look at the output. + +unset LIBGUESTFS_DEBUG +unset LIBGUESTFS_TRACE + +# This makes a file containing the environment variables we want to set. +rm -f env +echo "PATH=$PATH" > env +printenv | grep -E '^(LIBGUESTFS_|LIBVIRT_|LD_|MALLOC_)' >> env + TESTS=$(echo guestfs_php_*.phpt) echo TESTS: $TESTS -# The PHP test script cleans the environment, so LIBGUESTFS_DEBUG=1 -# won't get passed down to the script. Furthermore, setting -# LIBGUESTFS_DEBUG=1 isn't very useful anyway because the PHP test -# script mixes stdout and stderr together and compares this to the -# expected output, so you'd just get failures for every test. So -# there is no good way to debug libguestfs failures in PHP tests, but -# if an individual test fails locally then you can edit the -# guestfs_php_*.phpt and uncomment the putenv statement, then look at -# the output. -unset LIBGUESTFS_DEBUG - # By the way, we're actually testing the installed version of # libguestfs. But don't worry, because PHP ignores the result of the -# tests anyway! ** Gah, PHP is written by morons ... ** +# tests anyway! make test TESTS="$TESTS"