Skip to content

Commit

Permalink
improvements to automated tests, including a testing database
Browse files Browse the repository at this point in the history
I'm adding a bunch of additions to the test suite. There is now a test
database that is created, and a test instance of the zebra server and
daemon that are run before the tests in t/database-dependent.pl are
run. This means that you can test things that insert (or destroy)
things in the database.

To use these tests, after you 'make' your koha installation, you can
change to the 't' directory and 'make test' there.

There is now an additional question asked during installation. It asks
whether you would like to run the data-dependent tests. If so, It asks
you for some login information to a test database. I recommend that
you do not not use your production database here. It will delete all
of the data in it.

Things that need improvement:
* I suspect that there are large parts that are not very platform
  independent, so they will need to be improved.
* There are some parts of the installer that will probably be
  refactored to let this work a little bit better, including being run
  directly from a main-level 'make' target at some point.
* Lots more tests to add. Be bold! (see the t/lib directory)
* other tests in t/*.t can possibly benefit from being included in here.

There are two required perl modules for this part of the test suite. I
use:
'Test::Class' => 0.028,
'Test::Class::Load' => 0.02,
They are not listed as requirements in the top-level Makefile.PL
because they're not actually required to install or use Koha, but if
you want to run the test suite, you can install them from CPAN.

Signed-off-by: Galen Charlton <galen.charlton@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
  • Loading branch information
amoore authored and Joshua Ferraro committed Apr 21, 2008
1 parent ecc779f commit 8056846
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 4 deletions.
62 changes: 58 additions & 4 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ my %config_defaults = (
'MERGE_SERVER_PORT' => '11001',
'PAZPAR2_HOST' => 'localhost',
'PAZPAR2_PORT' => '11002',
'RUN_DATABASE_TESTS' => 'no',
);

# set some default configuratio options based on OS
Expand Down Expand Up @@ -438,6 +439,7 @@ my %valid_config_values = (
'AUTH_INDEX_MODE' => { 'grs1' => 1, 'dom' => 1 },
'ZEBRA_MARC_FORMAT' => { 'marc21' => 1, 'unimarc' => 1 }, # FIXME should generate from contents of distributation
'ZEBRA_LANGUAGE' => { 'en' => 1, 'fr' => 1 }, # FIXME should generate from contents of distribution
'RUN_DATABASE_TESTS' => { 'yes' => 1, 'no' => 1 },
);

# get settings from command-line
Expand Down Expand Up @@ -1040,6 +1042,30 @@ PazPar2 port?);
}
}

$msg = q(
Would you like to run the database-dependent test suite?);
$msg .= _add_valid_values_disp( 'RUN_DATABASE_TESTS', $valid_values );
$config{'RUN_DATABASE_TESTS'} = _get_value( 'RUN_DATABASE_TESTS', $msg, $defaults->{'RUN_DATABASE_TESTS'}, $valid_values, $install_log_values );

if ( $config{'RUN_DATABASE_TESTS'} eq 'yes' ) {
$config{'TEST_DB_TYPE'} = $config{'DB_TYPE'};
$config{'TEST_DB_HOST'} = $config{'DB_HOST'};
$msg = q(
Please specify the name of the test database to be
used by Koha);
$config{'TEST_DB_NAME'} = _get_value('TEST_DB_NAME', $msg, $defaults->{'TEST_DB_NAME'}, $valid_values, $install_log_values);

$msg = q(
Please specify the user that owns the database to be
used by Koha);
$config{'TEST_DB_USER'} = _get_value('TEST_DB_USER', $msg, $defaults->{'TEST_DB_USER'}, $valid_values, $install_log_values);

$msg = q(
Please specify the password of the user that owns the
database to be used by Koha);
$config{'TEST_DB_PASS'} = _get_value('TEST_DB_PASS', $msg, $defaults->{'TEST_DB_PASS'}, $valid_values, $install_log_values);
}

print "\n\n";

# add version number
Expand Down Expand Up @@ -1426,20 +1452,48 @@ sub postamble {
$config{'ZEBRA_PASS'} =~ s/\$/\$\$/g;
}

my $env;
# Hereagain, we must alter syntax per platform...
if ( $^O eq 'MSWin32' ) {
# NOTE: it is imperative that there be no whitespaces in ENV=value...
my $env = join("\n", map { "__${_}__=$target_directories->{$_}" } keys %$target_directories);
$env = join("\n", map { "__${_}__=$target_directories->{$_}" } keys %$target_directories);
$env .= "\n\n";
$env .= join("\n", map { "__${_}__=$config{$_}" } keys %config);
return "$env\n";
}
else {
my $env = join("\n", map { "export __${_}__ := $target_directories->{$_}" } keys %$target_directories);
$env = join("\n", map { "export __${_}__ := $target_directories->{$_}" } keys %$target_directories);
$env .= "\n\n";
$env .= join("\n", map { "export __${_}__ := $config{$_}" } keys %config);
return "$env\n";
}

if ( $config{'RUN_DATABASE_TESTS'} eq 'yes' ) {
if ( open( my $confhandle, '>', 't/test-config.txt' ) ) {
print $confhandle "# This configuration file lets the t/Makefile prepare a test koha-conf.xml file.\n";
print $confhandle "# It is generated by the top-level Makefile.PL.\n";
print $confhandle "# It is separate from the standard koha-conf.xml so that you can edit this by hand and test with different configurations.\n";

# these directories will be relocated to the 't' directory
my %move_to_t = map { $_ => 1 } qw( KOHA_CONF_DIR LOG_DIR SCRIPT_DIR ZEBRA_CONF_DIR ZEBRA_LOCK_DIR ZEBRA_DATA_DIR ZEBRA_RUN_DIR );
my $oldbasedir = substr( $target_directories->{'KOHA_CONF_DIR'}, 0, - length( '/etc' ) );
my $newbasedir = $target_directories->{'KOHA_CONF_DIR'};
$newbasedir =~ s/etc$/t/;
foreach my $dirname ( keys %$target_directories ) {
my $dir = $target_directories->{ $dirname };
if ( $move_to_t{ $dirname } ) {
$dir =~ s/$oldbasedir/$newbasedir/;
}
print $confhandle "$dirname = $dir\n"
}
print $confhandle "\n";

print $confhandle join( "\n", map { "$_ = $config{$_}" } keys( %config ) ), "\n";
close( $confhandle );
} else {
warn 'unable to open conf file for database dependent tests: $!';
}

}
return "$env\n";
}


Expand Down
76 changes: 76 additions & 0 deletions t/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

SHELL = /bin/sh
NOOP = $(SHELL) -c true
NOECHO = @
ECHO = echo
MKDIR = /bin/mkdir
CP = cp
SED = /bin/sed

# some of these are pretty questionable.
PERL = /usr/bin/perl
# TEST_FILES = *.t
TEST_FILES = database_dependent.pl
PROVE = /usr/bin/prove
PROVE_FLAGS = -v
KOHA_CONF_DIR = ../etc
CONF_FILE_TEMPLATE = $(KOHA_CONF_DIR)/koha-conf.xml
TEST_CONF_FILE = etc/koha-conf.xml
MKPATH = $(PERL) "-MExtUtils::Command" -e mkpath

TEST_REWRITE_SCRIPT = rewrite-config-test.PL
REAL_REWRITE_SCRIPT = ../rewrite-config.PL

ZEBRA_CONF_DIR = etc/zebradb
ZEBRA_CONF_FILES = $(ZEBRA_CONF_DIR)/etc/passwd $(ZEBRA_CONF_DIR)/zebra-biblios.cfg $(ZEBRA_CONF_DIR)/zebra-authorities.cfg $(ZEBRA_CONF_DIR)/zebra-authorities-dom.cfg $(ZEBRA_CONF_DIR)/explain-authorities.xml $(ZEBRA_CONF_DIR)/explain-biblios.xml $(ZEBRA_CONF_DIR)/retrieval-info-auth-grs1.xml $(ZEBRA_CONF_DIR)/retrieval-info-auth-dom.xml $(ZEBRA_CONF_DIR)/ccl.properties $(ZEBRA_CONF_DIR)/cql.properties $(ZEBRA_CONF_DIR)/pqf.properties

SCRIPTS = koha-zebra-ctl.sh koha-pazpar2-ctl.sh koha-zebraqueue-ctl.sh zebraqueue_daemon.pl
SRC_SCRIPT_DIR = ../misc/bin
TEST_SCRIPT_DIR = bin


all ::
$(NOECHO) $(ECHO) RUNNING THIS MAKEFILE MAY CAUSE LOSS OF DATA
$(NOECHO) $(ECHO)
$(NOECHO) $(ECHO) This makefile is completely beta.
$(NOECHO) $(ECHO) Please read it first and edit the variables at the top.
$(NOECHO) $(ECHO) Then, you can run \'make test\'

config_file :: $(CONF_FILE_TEMPLATE) test_run_dirs
$(CP) $(CONF_FILE_TEMPLATE) $(TEST_CONF_FILE)
$(PERL) $(TEST_REWRITE_SCRIPT) --file $(TEST_CONF_FILE)
$(PERL) $(REAL_REWRITE_SCRIPT) $(TEST_CONF_FILE)

zebra_conf_files :: test_run_dirs $(ZEBRA_CONF_FILES)


$(ZEBRA_CONF_FILES) ::
$(CP) ../$@ $@
$(PERL) $(TEST_REWRITE_SCRIPT) --file $@
$(PERL) $(REAL_REWRITE_SCRIPT) $@

$(SCRIPTS) ::
$(SED) s/--user=\$$USER.\$$GROUP// $(SRC_SCRIPT_DIR)/$@ > $(TEST_SCRIPT_DIR)/$@
$(PERL) $(TEST_REWRITE_SCRIPT) --file $(TEST_SCRIPT_DIR)/$@
$(PERL) $(REAL_REWRITE_SCRIPT) $(TEST_SCRIPT_DIR)/$@

test :: config_file $(ZEBRA_CONF_FILES) $(SCRIPTS)
KOHA_CONF=$(TEST_CONF_FILE) $(PROVE) $(PROVE_FLAGS) $(TEST_FILES)

test_run_dirs ::
$(CP) -a ../etc/zebradb etc
$(MKPATH) etc/zebradb/etc
$(MKPATH) var/lib/zebradb/biblios/key
$(MKPATH) var/lib/zebradb/biblios/register
$(MKPATH) var/lib/zebradb/biblios/shadow
$(MKPATH) var/lib/zebradb/biblios/tmp
$(MKPATH) var/lib/zebradb/authorities/key
$(MKPATH) var/lib/zebradb/authorities/register
$(MKPATH) var/lib/zebradb/authorities/shadow
$(MKPATH) var/lib/zebradb/authorities/tmp
$(MKPATH) var/lock/zebradb/biblios
$(MKPATH) var/lock/zebradb/authorities
$(MKPATH) var/run/zebradb
$(MKPATH) var/log/zebradb
$(MKPATH) bin

170 changes: 170 additions & 0 deletions t/database_dependent.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#!/usr/bin/perl

use warnings;
use strict;

=head2
=cut

use C4::Context;
use C4::Installer;
use C4::Languages;
use Data::Dumper;
use Test::More;

use lib q( . .. );

use Test::Class::Load qw ( . ); # run from the t directory

create_test_database();

start_zebrasrv();
start_zebraqueue_daemon();

Test::Class->runtests;

stop_zebraqueue_daemon();
stop_zebrasrv();

# stop_zebrasrv();

=head3 create_test_database
sets up the test database.
=cut

sub create_test_database {

diag 'creating testing datbase...';
my $installer = C4::Installer->new() or die 'unable to create new installer';
# warn Data::Dumper->Dump( [ $installer ], [ 'installer' ] );
my $all_languages = getAllLanguages();
my $error = $installer->load_db_schema();
die "unable to load_db_schema: $error" if ( $error );
my $list = $installer->sql_file_list('en', 'marc21', { optional => 1,
mandatory => 1 } );
my ($fwk_language, $installed_list) = $installer->load_sql_in_order($all_languages, @$list);
$installer->set_version_syspref();
$installer->set_marcflavour_syspref('MARC21');
$installer->set_indexing_engine(0);
diag 'database created.'
}


=head3 start_zebrasrv
This method deletes and reinitializes the zebra database directory,
and then spans off a zebra server.
=cut

sub start_zebrasrv {

stop_zebrasrv();
diag 'cleaning zebrasrv...';

foreach my $zebra_server ( qw( biblioserver authorityserver ) ) {
my $zebra_config = C4::Context->zebraconfig($zebra_server)->{'config'};
my $zebra_db_dir = C4::Context->zebraconfig($zebra_server)->{'directory'};
foreach my $zebra_db_name ( qw( biblios authorities ) ) {
my $command = "zebraidx -c $zebra_config -d $zebra_db_name init";
my $return = system( $command . ' > /dev/null 2>&1' );
if ( $return != 0 ) {
diag( "command '$command' died with value: " . $? >> 8 );
}

$command = "zebraidx -c $zebra_config -d $zebra_db_name create $zebra_db_name";
diag $command;
$return = system( $command . ' > /dev/null 2>&1' );
if ( $return != 0 ) {
diag( "command '$command' died with value: " . $? >> 8 );
}
}
}

diag 'starting zebrasrv...';

my $pidfile = File::Spec->catdir( C4::Context->config("logdir"), 'zebra.pid' );
my $command = sprintf( 'zebrasrv -f %s -D -l %s -p %s',
$ENV{'KOHA_CONF'},
File::Spec->catdir( C4::Context->config("logdir"), 'zebra.log' ),
$pidfile,
);
diag $command;
my $output = qx( $command );
if ( $output ) {
diag $output;
}
if ( -e $pidfile, 'pidfile exists' ) {
diag 'zebrasrv started.';
} else {
die 'unable to start zebrasrv';
}
return $output;
}

=head3 stop_zebrasrv
using the PID file for the zebra server, send it a TERM signal with
"kill". We can't tell if the process actually dies or not.
=cut

sub stop_zebrasrv {

my $pidfile = File::Spec->catdir( C4::Context->config("logdir"), 'zebra.pid' );
if ( -e $pidfile ) {
open( my $pidh, '<', $pidfile )
or return;
if ( defined $pidh ) {
my ( $pid ) = <$pidh> or return;
close $pidh;
my $killed = kill 15, $pid; # 15 is TERM
if ( $killed != 1 ) {
warn "unable to kill zebrasrv with pid: $pid";
}
}
}
}


=head3 start_zebraqueue_daemon
kick off a zebraqueue_daemon.pl process.
=cut

sub start_zebraqueue_daemon {

my $command = q(bin/koha-zebraqueue-ctl.sh start);
diag $command;
my $started = system( $command );
diag "started: $started";

# my $command = sprintf( 'KOHA_CONF=%s ../misc/bin/zebraqueue_daemon.pl > %s 2>&1 &',
# $ENV{'KOHA_CONF'},
# 'zebra.log',
# );
# diag $command;
# my $queue = system( $command );
# diag "queue: $queue";

}

=head3 stop_zebraqueue_daemon
=cut

sub stop_zebraqueue_daemon {

my $command = q(bin/koha-zebraqueue-ctl.sh stop);
diag $command;
my $started = system( $command );
diag "started: $started";

}
Loading

0 comments on commit 8056846

Please sign in to comment.