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