Merge pull request #3226 from solgenomics/topic/passwd-protect-genotypes
[sgn.git] / t / test_fixture.pl
bloba3d47d89b5782d03783f29326a7baa3d3a2d2c50
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 File::Basename qw(dirname);
17 use Cwd qw(abs_path);
19 use Catalyst::ScriptRunner;
21 use lib 'lib';
22 use SGN::Devel::MyDevLibs;
24 my $verbose = 0;
25 my $nocleanup;
26 my $noserver;
27 my $dumpupdatedfixture;
28 my $noparallel = 0;
29 my $list_config = "";
30 # relative to `sgn/ (or parent of wherever this script is located)
31 my $fixture_path = 't/data/fixture/cxgn_fixture.sql';
33 GetOptions(
34 "carpalways" => \( my $carpalways = 0 ),
35 "verbose" => \$verbose ,
36 "nocleanup" => \$nocleanup,
37 "dumpupdatedfixture" => \$dumpupdatedfixture,
38 "noserver" => \$noserver,
39 "noparallel" => \$noparallel,
40 "fixture_path" => \$fixture_path,
41 "list_config" => \$list_config
44 require Carp::Always if $carpalways;
46 my @prove_args = @ARGV;
47 if(@prove_args){
48 @prove_args = map {abs_path($_)} @prove_args;
51 #Change cwd to `sgn/` (or parent of wherever this script is located)
52 my $sgn_dir = abs_path(dirname(abs_path($0))."/../");
53 print STDERR "####### ".$sgn_dir." #######";
54 chdir($sgn_dir);
55 @prove_args = ( 't' ) unless @prove_args;
57 #my $parallel = (grep /^-j\d*$/, @ARGV) ? 1 : 0;
59 $ENV{SGN_CONFIG_LOCAL_SUFFIX} = 'fixture';
60 #my $conf_file_base = 'sgn_local.conf'; # which conf file the sgn_fixture.conf should be based on
61 # relative to `sgn/`
62 my $conf_file_base = 'sgn_local.conf';
63 my $template_file = 'sgn_fixture_template.conf';
64 # get some defaults from sgn_local.conf
66 my $cfg = Config::Any->load_files({files=> [$conf_file_base, $template_file], use_ext=>1 });
68 my $config = $cfg->[0]->{$conf_file_base};
69 my $template = $cfg->[1]->{$template_file};
71 if ($list_config) {
72 print STDERR Dumper($cfg);
75 my $db_user_password = $config->{dbpass};
76 my $dbhost = $config->{dbhost} || 'localhost';
77 my $dbport = $config->{dbport} || '5432';
78 my $db_postgres_password = $config->{DatabaseConnection}->{sgn_test}->{password};
79 print STDERR "Using $dbhost:$dbport\n";
80 my $test_dsn = $config->{DatabaseConnection}->{sgn_test}->{dsn};
81 my $catalyst_server_port = 3010;
83 # replace the keys in the sgn local file with what's in the template
85 foreach my $k (keys %{$template}) {
86 #print STDERR "Replacing key $k : $config->{$k} with $template->{$k}\n";
87 $config->{$k} = $template->{$k};
90 # load the database fixture
92 my $now = DateTime->now();
93 my $dbname = join "_", map { $now->$_ } (qw | year month day hour minute |);
94 $dbname = 'test_db_'.$dbname;
95 $dbname .= $$;
97 print STDERR "# Writing a .pgpass file... ";
98 # format = hostname:port:database:username:password
99 open(my $PGPASS, ">", "$ENV{HOME}/.pgpass") || die "Can't open .pgpass for writing.";
100 print $PGPASS "$dbhost:$dbport:$dbname:web_usr:$db_user_password\n";
101 print $PGPASS "$dbhost:$dbport:*:postgres:$db_postgres_password\n";
102 close($PGPASS);
103 system("chmod 0600 $ENV{HOME}/.pgpass");
104 print STDERR "Done.\n";
106 my $database_fixture_dump = $ENV{DATABASE_FIXTURE_PATH} || $fixture_path;
107 print STDERR "# Loading database fixture... $database_fixture_dump ... ";
108 system("createdb -h $config->{dbhost} -U postgres -T template0 -E SQL_ASCII --no-password $dbname");
109 system("cat $database_fixture_dump | psql -h $config->{dbhost} -U postgres $dbname > /dev/null");
111 print STDERR "Done.\n";
113 print STDERR "# Creating sgn_fixture.conf file... ";
114 $config->{dbname} = $dbname;
115 $test_dsn =~ s/dbname=(.*)$/dbname=$dbname/;
116 $config->{DatabaseConnection}->{sgn_test}->{dsn} = $test_dsn;
118 #print STDERR Dumper($config);
120 my $new_conf = hash2config($config);
122 open(my $NEWCONF, ">", "sgn_fixture.conf") || die "Can't open sgn_fixture.conf for writing";
123 print $NEWCONF $new_conf;
124 close($NEWCONF);
126 #run fixture and db patches.
127 system("t/data/fixture/patches/run_fixture_and_db_patches.pl -u postgres -p $db_postgres_password -h $config->{dbhost} -d $dbname -e janedoe -s 117");
129 # run the materialized views creation script
131 print STDERR "Running matview refresh with -H $dbhost -D $dbname -U postgres -P $db_postgres_password -m fullview\n";
132 system("perl bin/refresh_matviews.pl -H $dbhost -D $dbname -U postgres -P $db_postgres_password -m fullview");
134 if ($dumpupdatedfixture){
135 print STDERR "Dumping new updated fixture with all patches run on it to t/data/fixture/cxgn_fixture.sql\n";
136 system("pg_dump -U postgres $dbname > t/data/fixture/cxgn_fixture.sql");
139 print STDERR "Done.\n";
141 # start the test web server
143 my $server_pid;
144 my $logfile;
145 if ($noserver) {
146 print STDERR "# [ --noserver option: not starting web server]\n";
148 else {
149 $server_pid = fork;
150 $logfile = "logfile.$$.txt";
152 unless( $server_pid ) {
154 # web server process
156 #$ENV{SGN_TEST_MODE} = 1;
157 @ARGV = (
158 -p => $catalyst_server_port,
159 ( $noparallel ? () : ('--fork') ),
162 if (!$verbose) {
163 print STDERR "# [Server logfile at $logfile]\n";
164 open (STDERR, ">$logfile") || die "can't open logfile.";
166 Catalyst::ScriptRunner->run('SGN', 'Server');
168 if (!$nocleanup) {
169 print STDERR "# Removing test database ($dbname)... ";
171 if ($noserver) {
172 print STDERR "# [ --noserver option: No logfile to remove]\n";
174 else {
175 print STDERR "# Delete server logfile... ";
176 close($logfile);
177 unlink $logfile;
178 print STDERR "Done.\n";
182 exit;
184 print STDERR "# Starting web server (PID=$server_pid)... ";
188 # wait for the test server to start
191 local $SIG{CHLD} = sub {
192 waitpid $server_pid, 0;
193 die "\nTest server failed to start. Aborting.\n";
195 print STDERR "Done.\n";
197 if (!$noserver) {
198 sleep 1 until !kill(0, $server_pid) || get "http://localhost:$catalyst_server_port";
202 my $prove_pid = fork;
203 unless( $prove_pid ) {
205 # test harness process
207 print STDERR "# Starting tests... \n";
209 # set up env vars for prove and the tests
211 $ENV{SGN_TEST_SERVER} = "http://localhost:$catalyst_server_port";
212 if(! $noparallel ) {
213 $ENV{SGN_PARALLEL_TESTING} = 1;
214 $ENV{SGN_SKIP_LEAK_TEST} = 1;
217 # now run the tests against it
219 my $app = App::Prove->new;
221 my $v = $verbose ? 'v' : '';
223 $app->process_args(
224 '-lr'.$v,
225 ( map { -I => $_ } @INC ),
226 @prove_args
228 exit( $app->run ? 0 : 1 );
231 #$SIG{CHLD} = 'IGNORE'; # problematic
232 $SIG{INT} = sub { kill 15, $server_pid, $prove_pid };
233 $SIG{KILL} = sub { kill 9, $server_pid, $prove_pid };
235 print STDERR "# Start prove (PID $prove_pid)... \n";
236 waitpid $prove_pid, 0;
237 print STDERR "# Prove finished, stopping web server PID $server_pid... ";
239 END { kill 15, $server_pid if $server_pid }
240 waitpid $server_pid, 0;
241 sleep(3);
242 print STDERR "Done.\n";
244 if (!$nocleanup) {
245 print STDERR "# Removing test database ($dbname)... ";
246 system("dropdb -h $config->{dbhost} -U postgres --no-password $dbname");
247 print STDERR "Done.\n";
249 if ($noserver) {
250 print STDERR "# [ --noserver option: No logfile to remove]\n";
252 else {
253 # print STDERR "# Delete server logfile... ";
254 # close($logfile);
255 # unlink $logfile;
256 # print STDERR "Done.\n";
258 print STDERR "# Delete fixture conf file... ";
259 unlink "sgn_fixture.conf";
260 print STDERR "Done.\n";
263 else {
264 print STDERR "# --nocleanup option: not removing db or files.\n";
266 print STDERR "# Test run complete.\n\n";
270 sub hash2config {
271 my $hash = shift;
273 my $s = "";
274 foreach my $k (keys(%$hash)) {
275 if (ref($hash->{$k}) eq "ARRAY") {
276 foreach my $v (@{$hash->{$k}}) {
277 $s .= "$k $v\n";
280 elsif (ref($hash->{$k}) eq "HASH") {
281 foreach my $n (keys(%{$hash->{$k}})) {
282 if (ref($hash->{$k}->{$n}) eq "HASH") {
283 $s .= "<$k $n>\n";
284 $s .= hash2config($hash->{$k}->{$n});
286 else {
287 $s .= "<$k>\n";
288 $s .= hash2config($hash->{$k});
290 $s .= "</$k>\n";
293 else {
294 $s .= "$k $hash->{$k}\n";
298 # if nothing matched the replace keys, add them here
301 # if (exists($hash->{dbname})) {
302 # $s .= "dbname $dbname\n";
305 return $s;
310 __END__
312 =head1 NAME
314 test_fixture.pl - start a dev server and run tests against it
316 =head1 SYNOPSIS
318 t/test_fixture.pl --carpalways -- -v -j5 t/mytest.t t/mydiroftests/
320 =head1 OPTIONS
322 -v verbose - the output of the server is not re-directed to file,
323 but rather output to the screen.
325 --carpalways Load Carp::Always in both the server and the test process
326 to force backtraces of all warnings and errors
328 --nocleanup Do not clean up database and logfile
330 --noserver Do not start webserver (if running unit_fixture tests only)
332 --noparallel Do not run the server in parallel mode.
334 --fixture_path specify a path to the fixture different from the default
335 (t/data/fixture/cxgn_fixture.pl). Note: You can also set the env
336 variable DATABASE_FIXTURE_PATH, which will overrule this
337 option.
339 --list_config lists the configuration information
341 -- -v options specified after two dashes will be passed to prove
342 directly, such -v will run prove in verbose mode.
344 =head1 AUTHORS
346 Robert Buels (initial script)
347 Lukas Mueller <lam87@cornell.edu> (fixture implementation)
349 =cut