diff --git a/Makefile.PL b/Makefile.PL index 9ed08fed1c..ebb459cdcb 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -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 @@ -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 @@ -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 @@ -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"; } diff --git a/t/Makefile b/t/Makefile new file mode 100644 index 0000000000..fa3c57f9a8 --- /dev/null +++ b/t/Makefile @@ -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 + diff --git a/t/database_dependent.pl b/t/database_dependent.pl new file mode 100644 index 0000000000..42348beaa1 --- /dev/null +++ b/t/database_dependent.pl @@ -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"; + +} diff --git a/t/rewrite-config-test.PL b/t/rewrite-config-test.PL new file mode 100644 index 0000000000..27bb22a0c5 --- /dev/null +++ b/t/rewrite-config-test.PL @@ -0,0 +1,179 @@ +# Copyright 2008 LibLime +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA +# + +use strict; +use warnings; + +use Sys::Hostname; +use Socket; +use Getopt::Long; +use Pod::Usage; +use Data::Dumper; + +# These variables get set from command line options +my ( $fname ); +GetOptions( 'file=s', \$fname ) + or pod2usage(); + + +=head1 NAME + +rewrite-test-config.PL - helper for the Koha packager and installer + +=head1 SYNOPSIS + + perl rewrite-test-config.PL configurationfile + +=head1 DESCRIPTION + +This helper script replaces keywords in the +configuration file with value either supplied through +the environment + +I intend to make this part of hte normal make process eventually. + +=head2 KEYWORDS + +The following configuration keywords are available: + +=head1 EXAMPLES + +=cut + +my $configfile = 'test-config.txt'; +my $configuration = read_config_file( $configfile ); + +# Override configuration from the environment +foreach my $key (keys %$configuration) { + if (defined($ENV{$key})) { + $configuration->{$key} = $ENV{$key}; + } +} + +$configuration = add_underscores( $configuration ); +my %configuration = replace_test_params( $configuration ); + +my $file = read_file($fname); +$file =~ s/__.*?__/exists $configuration{$&} ? $configuration{$&} : $&/seg; + +# At this point, file is in 'blib' and by default +# has mode a-w. Therefore, must change permission +# to make it writable. Note that stat and chmod +# (the Perl functions) should work on Win32 +my $old_perm; +$old_perm = (stat $fname)[2] & 07777; +my $new_perm = $old_perm | 0200; +chmod $new_perm, $fname; + +open(OUTPUT,">$fname") || die "Can't open $fname for write: $!"; +print OUTPUT $file; +close(OUTPUT); + +chmod $old_perm, $fname; + +=head2 read_config_file + + takes the filename pointing to the configuration file that the + top-level Makefile wrote + + returns a hashref that contains the configuration determined by + that file. + +=cut + +sub read_config_file { + my $config_file = shift; + if ( not -e $config_file ) { + die "unable to find configuration file: $config_file"; + } + my $config; + if ( open( my $confighandle, '<', $config_file ) ) { + while ( my $line = <$confighandle> ) { + chomp $line; + next if ( $line eq '' ); + next if ( $line =~ /^\s*#/ ); + my ( $var, $value ) = split( /\s*=\s*/, $line ); + $config->{ $var } = $value; + } + } else { + warn "unable to open configuration file: $config_file"; + return; + } + return $config; +} + +=head2 add_underscores + +=cut + +sub add_underscores { + my $config = shift; + + my $newconfig; + foreach my $key ( keys %$config ) { + $newconfig->{ '__' . $key . '__' } = $config->{ $key }; + } + return $newconfig; +} + + +=head2 replace_test_params + +=cut + +sub replace_test_params { + my $config = shift; + + my $testconfig; + foreach my $key ( keys %$config ) { + if ( $key =~ /^__TEST_/ ) { + my $newkey = $key; + $newkey =~ s/^__TEST_/__/; + $testconfig->{ $newkey } = $config->{ $key }; + } + } + # override variables with the "TEST_" variety. + my %newconfig = ( %$config, %$testconfig ); + return %newconfig; +} + +# Idea taken from perlfaq5 +sub read_file { + local(*INPUT,$/); + open(INPUT,$_[0]) || die "Can't open $_[0] for read"; + my $file = ; + return $file; +} + +__END__ + + +=head1 SEE ALSO + +Makefile.PL, ExtUtils::MakeMaker(3) + +=head1 ACKNOWLEDGEMENTS + +based on rewrite-config.PL by MJ Ray. + +=head1 AUTHOR + +Andrew Moore + +=cut +