Merge branch 'topic/accesions_to_seedlot_tool' into topic/trials_from_seedlots
[sgn.git] / t / test_fixture.pl
blob2d230a7c6a0f5ef19906ab9b83c4683f27bdda04
1 #!/usr/bin/env perl
3 use strict;
4 use warnings;
6 use DateTime;
7 use LWP::Simple;
8 use App::Prove;
9 use Data::Dumper;
11 use Pod::Usage;
12 use Getopt::Long;
13 use File::Slurp;
14 use Config::Any;
16 use Catalyst::ScriptRunner;
18 use lib 'lib';
19 use SGN::Devel::MyDevLibs;
21 my $verbose = 0;
22 my $nocleanup;
23 my $noserver;
24 my $noparallel = 0;
25 my $fixture_path = '../fixture/cxgn_fixture.sql';
26 my $use_brapi_fixture;
27 my $brapi_fixture = '../fixture/brapi_fixture.sql';
29 GetOptions(
30 "carpalways" => \( my $carpalways = 0 ),
31 "verbose" => \$verbose ,
32 "nocleanup" => \$nocleanup,
33 "noserver" => \$noserver,
34 "noparallel" => \$noparallel,
35 "fixture_path" => \$fixture_path,
36 "brapi_fixture" => \$use_brapi_fixture,
39 require Carp::Always if $carpalways;
41 my @prove_args = @ARGV;
42 @prove_args = ( 't' ) unless @prove_args;
44 #my $parallel = (grep /^-j\d*$/, @ARGV) ? 1 : 0;
46 $ENV{SGN_CONFIG_LOCAL_SUFFIX} = 'fixture';
47 #my $conf_file_base = 'sgn_local.conf'; # which conf file the sgn_fixture.conf should be based on
48 my $conf_file_base = 'sgn_local.conf';
49 my $template_file = 'sgn_fixture_template.conf';
50 # get some defaults from sgn_local.conf
52 my $cfg = Config::Any->load_files({files=> [$conf_file_base, $template_file], use_ext=>1 });
54 my $config = $cfg->[0]->{$conf_file_base};
55 my $template = $cfg->[1]->{$template_file};
57 #print STDERR Dumper($cfg);
58 my $db_user_password = $config->{dbpass};
59 my $dbhost = $config->{db_host} || 'localhost';
60 my $db_postgres_password = $config->{DatabaseConnection}->{sgn_test}->{password};
61 my $test_dsn = $config->{DatabaseConnection}->{sgn_test}->{dsn};
62 my $catalyst_server_port = 3010;
64 # replace the keys in the sgn local file with what's in the template
66 foreach my $k (keys %{$template}) {
67 #print STDERR "Replacing key $k : $config->{$k} with $template->{$k}\n";
68 $config->{$k} = $template->{$k};
71 # load the database fixture
73 my $now = DateTime->now();
74 my $dbname = join "_", map { $now->$_ } (qw | year month day hour minute |);
75 $dbname = 'test_db_'.$dbname;
76 $dbname .= $$;
78 print STDERR "# Writing a .pgpass file... ";
79 # format = hostname:port:database:username:password
80 open(my $PGPASS, ">", "$ENV{HOME}/.pgpass") || die "Can't open .pgpass for writing.";
81 print $PGPASS "$dbhost:5432:$dbname:web_usr:$db_user_password\n";
82 print $PGPASS "$dbhost:5432:*:postgres:$db_postgres_password\n";
83 close($PGPASS);
84 system("chmod 0600 $ENV{HOME}/.pgpass");
85 print STDERR "Done.\n";
87 my $dump_path = $use_brapi_fixture ? $brapi_fixture : $fixture_path;
88 my $database_fixture_dump = $ENV{DATABASE_FIXTURE_PATH} || $dump_path;
89 print STDERR "# Loading database fixture... $database_fixture_dump ... ";
90 system("createdb -h $config->{dbhost} -U postgres -T template0 -E SQL_ASCII --no-password $dbname");
91 system("cat $database_fixture_dump | psql -h $config->{dbhost} -U postgres $dbname > /dev/null");
93 print STDERR "Done.\n";
95 print STDERR "# Creating sgn_fixture.conf file... ";
96 $config->{dbname} = $dbname;
97 $test_dsn =~ s/dbname=(.*)$/dbname=$dbname/;
98 $config->{DatabaseConnection}->{sgn_test}->{dsn} = $test_dsn;
100 #print STDERR Dumper($config);
102 my $new_conf = hash2config($config);
104 open(my $NEWCONF, ">", "sgn_fixture.conf") || die "Can't open sgn_fixture.conf for writing";
105 print $NEWCONF $new_conf;
106 close($NEWCONF);
108 # run the materialized views creation script
110 print STDERR "Running matview refresh with -H $dbhost -D $dbname -U postgres -P $db_postgres_password\n";
111 system("perl bin/refresh_matviews.pl -H $dbhost -D $dbname -U postgres -P $db_postgres_password");
114 print STDERR "Done.\n";
116 # start the test web server
118 my $server_pid;
119 my $logfile;
120 if ($noserver) {
121 print STDERR "# [ --noserver option: not starting web server]\n";
123 else {
124 $server_pid = fork;
125 $logfile = "logfile.$$.txt";
127 unless( $server_pid ) {
129 # web server process
131 #$ENV{SGN_TEST_MODE} = 1;
132 @ARGV = (
133 -p => $catalyst_server_port,
134 ( $noparallel ? () : ('--fork') ),
137 if (!$verbose) {
138 print STDERR "# [Server logfile at $logfile]\n";
139 open (STDERR, ">$logfile") || die "can't open logfile.";
141 Catalyst::ScriptRunner->run('SGN', 'Server');
143 exit;
145 print STDERR "# Starting web server (PID=$server_pid)... ";
149 # wait for the test server to start
152 local $SIG{CHLD} = sub {
153 waitpid $server_pid, 0;
154 die "\nTest server failed to start. Aborting.\n";
156 print STDERR "Done.\n";
158 if (!$noserver) {
159 sleep 1 until !kill(0, $server_pid) || get "http://localhost:$catalyst_server_port";
163 my $prove_pid = fork;
164 unless( $prove_pid ) {
166 # test harness process
168 print STDERR "# Starting tests... \n";
170 # set up env vars for prove and the tests
172 $ENV{SGN_TEST_SERVER} = "http://localhost:$catalyst_server_port";
173 if(! $noparallel ) {
174 $ENV{SGN_PARALLEL_TESTING} = 1;
175 $ENV{SGN_SKIP_LEAK_TEST} = 1;
178 # now run the tests against it
180 my $app = App::Prove->new;
181 $app->process_args(
182 '-lr',
183 ( map { -I => $_ } @INC ),
184 @prove_args
186 exit( $app->run ? 0 : 1 );
189 #$SIG{CHLD} = 'IGNORE'; # problematic
190 $SIG{INT} = sub { kill 15, $server_pid, $prove_pid };
191 $SIG{KILL} = sub { kill 9, $server_pid, $prove_pid };
193 print STDERR "# Start prove (PID $prove_pid)... \n";
194 waitpid $prove_pid, 0;
195 print STDERR "# Prove finished, stopping web server PID $server_pid... ";
197 END { kill 15, $server_pid if $server_pid }
198 waitpid $server_pid, 0;
199 sleep(3);
200 print STDERR "Done.\n";
202 if (!$nocleanup) {
203 print STDERR "# Removing test database ($dbname)... ";
204 system("dropdb -h $config->{dbhost} -U postgres --no-password $dbname");
205 print STDERR "Done.\n";
207 if ($noserver) {
208 print STDERR "# [ --noserver option: No logfile to remove]\n";
210 else {
211 print STDERR "# Delete server logfile... ";
212 close($logfile);
213 unlink $logfile;
214 print STDERR "Done.\n";
216 print STDERR "# Delete fixture conf file... ";
217 unlink "sgn_fixture.conf";
218 print STDERR "Done.\n";
221 else {
222 print STDERR "# --nocleanup option: not removing db or files.\n";
224 print STDERR "# Test run complete.\n\n";
228 sub hash2config {
229 my $hash = shift;
231 my $s = "";
232 foreach my $k (keys(%$hash)) {
233 if (ref($hash->{$k}) eq "ARRAY") {
234 foreach my $v (@{$hash->{$k}}) {
235 $s .= "$k $v\n";
238 elsif (ref($hash->{$k}) eq "HASH") {
239 foreach my $n (keys(%{$hash->{$k}})) {
240 if (ref($hash->{$k}->{$n}) eq "HASH") {
241 $s .= "<$k $n>\n";
242 $s .= hash2config($hash->{$k}->{$n});
244 else {
245 $s .= "<$k>\n";
246 $s .= hash2config($hash->{$k});
248 $s .= "</$k>\n";
251 else {
252 $s .= "$k $hash->{$k}\n";
256 # if nothing matched the replace keys, add them here
259 # if (exists($hash->{dbname})) {
260 # $s .= "dbname $dbname\n";
263 return $s;
268 __END__
270 =head1 NAME
272 test_fixture.pl - start a dev server and run tests against it
274 =head1 SYNOPSIS
276 t/test_fixture.pl --carpalways -- -v -j5 t/mytest.t t/mydiroftests/
278 =head1 OPTIONS
280 -v verbose - the output of the server is not re-directed to file,
281 but rather output to the screen.
283 --carpalways Load Carp::Always in both the server and the test process
284 to force backtraces of all warnings and errors
286 --nocleanup Do not clean up database and logfile
288 --noserver Do not start webserver (if running unit_fixture tests only)
290 --noparallel Do not run the server in parallel mode.
292 --fixture_path specify a path to the fixture different from the default
293 (../fixture/cxgn_fixture.pl). Note: You can also set the env
294 variable DATABASE_FIXTURE_PATH, which will overrule this
295 option.
297 -- -v options specified after two dashes will be passed to prove
298 directly, such -v will run prove in verbose mode.
300 =head1 AUTHORS
302 Robert Buels (initial script)
303 Lukas Mueller <lam87@cornell.edu> (fixture implementation)
305 =cut