added reindexing routine to KohaTest
[koha.git] / t / database_dependent.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 =head2
7
8
9
10 =cut
11
12 use C4::Context;
13 use C4::Installer;
14 use C4::Languages;
15 use Data::Dumper;
16 use Test::More;
17
18 use Test::Class::Load qw ( . ); # run from the t directory
19
20 create_test_database();
21
22 start_zebrasrv();
23 start_zebraqueue_daemon();
24
25 Test::Class->runtests;
26
27 stop_zebraqueue_daemon();
28 stop_zebrasrv();
29
30 # stop_zebrasrv();
31
32 =head3 create_test_database
33
34   sets up the test database.
35
36 =cut
37
38 sub create_test_database {
39
40     diag 'creating testing database...';
41     my $installer = C4::Installer->new() or die 'unable to create new installer';
42     # warn Data::Dumper->Dump( [ $installer ], [ 'installer' ] );
43     my $all_languages = getAllLanguages();
44     my $error = $installer->load_db_schema();
45     die "unable to load_db_schema: $error" if ( $error );
46     my $list = $installer->sql_file_list('en', 'marc21', { optional  => 1,
47                                                            mandatory => 1 } );
48     my ($fwk_language, $installed_list) = $installer->load_sql_in_order($all_languages, @$list);
49     $installer->set_version_syspref();
50     $installer->set_marcflavour_syspref('MARC21');
51     $installer->set_indexing_engine(0);
52     diag 'database created.'
53 }
54
55
56 =head3 start_zebrasrv
57
58   This method deletes and reinitializes the zebra database directory,
59   and then spans off a zebra server.
60
61 =cut
62
63 sub start_zebrasrv {
64
65     stop_zebrasrv();
66     diag 'cleaning zebrasrv...';
67
68     foreach my $zebra_server ( qw( biblioserver authorityserver ) ) {
69         my $zebra_config  = C4::Context->zebraconfig($zebra_server)->{'config'};
70         my $zebra_db_dir  = C4::Context->zebraconfig($zebra_server)->{'directory'};
71         foreach my $zebra_db_name ( qw( biblios authorities ) ) {
72             my $command = "zebraidx -c $zebra_config -d $zebra_db_name init";
73             my $return = system( $command . ' > /dev/null 2>&1' );
74             if ( $return != 0 ) {
75                 diag( "command '$command' died with value: " . $? >> 8 );
76             }
77             
78             $command = "zebraidx -c $zebra_config -d $zebra_db_name create $zebra_db_name";
79             diag $command;
80             $return = system( $command . ' > /dev/null 2>&1' );
81             if ( $return != 0 ) {
82                 diag( "command '$command' died with value: " . $? >> 8 );
83             }
84         }
85     }
86     
87     diag 'starting zebrasrv...';
88
89     my $pidfile = File::Spec->catdir( C4::Context->config("logdir"), 'zebra.pid' );
90     my $command = sprintf( 'zebrasrv -f %s -D -l %s -p %s',
91                            $ENV{'KOHA_CONF'},
92                            File::Spec->catdir( C4::Context->config("logdir"), 'zebra.log' ),
93                            $pidfile,
94                       );
95     diag $command;
96     my $output = qx( $command );
97     if ( $output ) {
98         diag $output;
99     }
100     if ( -e $pidfile, 'pidfile exists' ) {
101         diag 'zebrasrv started.';
102     } else {
103         die 'unable to start zebrasrv';
104     }
105     return $output;
106 }
107
108 =head3 stop_zebrasrv
109
110   using the PID file for the zebra server, send it a TERM signal with
111   "kill". We can't tell if the process actually dies or not.
112
113 =cut
114
115 sub stop_zebrasrv {
116
117     my $pidfile = File::Spec->catdir( C4::Context->config("logdir"), 'zebra.pid' );
118     if ( -e $pidfile ) {
119         open( my $pidh, '<', $pidfile )
120           or return;
121         if ( defined $pidh ) {
122             my ( $pid ) = <$pidh> or return;
123             close $pidh;
124             my $killed = kill 15, $pid; # 15 is TERM
125             if ( $killed != 1 ) {
126                 warn "unable to kill zebrasrv with pid: $pid";
127             }
128         }
129     }
130 }
131
132
133 =head3 start_zebraqueue_daemon
134
135   kick off a zebraqueue_daemon.pl process.
136
137 =cut
138
139 sub start_zebraqueue_daemon {
140
141     my $command = q(run/bin/koha-zebraqueue-ctl.sh start);
142     diag $command;
143     my $started = system( $command );
144     diag "started: $started";
145     
146 #     my $command = sprintf( 'KOHA_CONF=%s ../misc/bin/zebraqueue_daemon.pl > %s 2>&1 &',
147 #                            $ENV{'KOHA_CONF'},
148 #                            'zebra.log',
149 #                       );
150 #     diag $command;
151 #     my $queue = system( $command );
152 #     diag "queue: $queue";
153
154 }
155
156 =head3 stop_zebraqueue_daemon
157
158
159 =cut
160
161 sub stop_zebraqueue_daemon {
162
163     my $command = q(run/bin/koha-zebraqueue-ctl.sh stop);
164     diag $command;
165     my $started = system( $command );
166     diag "started: $started";
167
168 }