18 use Test::Class::Load qw ( . ); # run from the t directory
20 clear_test_database();
21 create_test_database();
24 start_zebraqueue_daemon();
26 if ($ENV{'TEST_CLASS'}) {
27 # assume only one test class is specified;
28 # should extend to allow multiples, but that will
29 # mean changing how test classes are loaded.
30 eval "KohaTest::$ENV{'TEST_CLASS'}->runtests";
32 Test::Class->runtests;
35 stop_zebraqueue_daemon();
40 =head3 clear_test_database
42 removes all tables from test database so that install starts with a clean slate
46 sub clear_test_database {
48 diag "removing tables from test database";
50 my $dbh = C4::Context->dbh;
51 my $schema = C4::Context->config("database");
53 my @tables = get_all_tables($dbh, $schema);
54 foreach my $table (@tables) {
55 drop_all_foreign_keys($dbh, $table);
58 foreach my $table (@tables) {
59 drop_table($dbh, $table);
64 my ($dbh, $schema) = @_;
65 my $sth = $dbh->prepare("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?");
67 $sth->execute($schema);
68 while (my ($table) = $sth->fetchrow_array) {
75 sub drop_all_foreign_keys {
76 my ($dbh, $table) = @_;
77 # get the table description
78 my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
80 my $vsc_structure = $sth->fetchrow;
81 # split on CONSTRAINT keyword
82 my @fks = split /CONSTRAINT /,$vsc_structure;
85 # isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
86 $_ = /(.*) FOREIGN KEY.*/;
89 # we have found 1 foreign, drop it
90 $dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
97 my ($dbh, $table) = @_;
98 $dbh->do("DROP TABLE $table");
101 =head3 create_test_database
103 sets up the test database.
107 sub create_test_database {
109 diag 'creating testing database...';
110 my $installer = C4::Installer->new() or die 'unable to create new installer';
111 # warn Data::Dumper->Dump( [ $installer ], [ 'installer' ] );
112 my $all_languages = getAllLanguages();
113 my $error = $installer->load_db_schema();
114 die "unable to load_db_schema: $error" if ( $error );
115 my $list = $installer->sql_file_list('en', 'marc21', { optional => 1,
117 my ($fwk_language, $installed_list) = $installer->load_sql_in_order($all_languages, @$list);
118 $installer->set_version_syspref();
119 $installer->set_marcflavour_syspref('MARC21');
120 $installer->set_indexing_engine(0);
121 diag 'database created.'
125 =head3 start_zebrasrv
127 This method deletes and reinitializes the zebra database directory,
128 and then spans off a zebra server.
135 diag 'cleaning zebrasrv...';
137 foreach my $zebra_server ( qw( biblioserver authorityserver ) ) {
138 my $zebra_config = C4::Context->zebraconfig($zebra_server)->{'config'};
139 my $zebra_db_dir = C4::Context->zebraconfig($zebra_server)->{'directory'};
140 foreach my $zebra_db_name ( qw( biblios authorities ) ) {
141 my $command = "zebraidx -c $zebra_config -d $zebra_db_name init";
142 my $return = system( $command . ' > /dev/null 2>&1' );
143 if ( $return != 0 ) {
144 diag( "command '$command' died with value: " . $? >> 8 );
147 $command = "zebraidx -c $zebra_config -d $zebra_db_name create $zebra_db_name";
149 $return = system( $command . ' > /dev/null 2>&1' );
150 if ( $return != 0 ) {
151 diag( "command '$command' died with value: " . $? >> 8 );
156 diag 'starting zebrasrv...';
158 my $pidfile = File::Spec->catdir( C4::Context->config("logdir"), 'zebra.pid' );
159 my $command = sprintf( 'zebrasrv -f %s -D -l %s -p %s',
161 File::Spec->catdir( C4::Context->config("logdir"), 'zebra.log' ),
165 my $output = qx( $command );
169 if ( -e $pidfile, 'pidfile exists' ) {
170 diag 'zebrasrv started.';
172 die 'unable to start zebrasrv';
179 using the PID file for the zebra server, send it a TERM signal with
180 "kill". We can't tell if the process actually dies or not.
186 my $pidfile = File::Spec->catdir( C4::Context->config("logdir"), 'zebra.pid' );
188 open( my $pidh, '<', $pidfile )
190 if ( defined $pidh ) {
191 my ( $pid ) = <$pidh> or return;
193 my $killed = kill 15, $pid; # 15 is TERM
194 if ( $killed != 1 ) {
195 warn "unable to kill zebrasrv with pid: $pid";
202 =head3 start_zebraqueue_daemon
204 kick off a zebraqueue_daemon.pl process.
208 sub start_zebraqueue_daemon {
210 my $command = q(run/bin/koha-zebraqueue-ctl.sh start);
212 my $started = system( $command );
213 diag "started: $started";
215 # my $command = sprintf( 'KOHA_CONF=%s ../misc/bin/zebraqueue_daemon.pl > %s 2>&1 &',
220 # my $queue = system( $command );
221 # diag "queue: $queue";
225 =head3 stop_zebraqueue_daemon
230 sub stop_zebraqueue_daemon {
232 my $command = q(run/bin/koha-zebraqueue-ctl.sh stop);
234 my $started = system( $command );
235 diag "started: $started";