Merge pull request #3912 from solgenomics/topic/fix_xss_error
[sgn.git] / t / test_fixture.pl
blob2a1d3cade2984fb40e6c2e3e02d7874dd1b19791
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 $nopatch;
30 my $list_config = "";
31 my $logfile = "logfile.$$.txt";
32 my $print_environment;
33 # relative to `sgn/ (or parent of wherever this script is located)
34 my $fixture_path = 't/data/fixture/cxgn_fixture.sql';
36 GetOptions(
37 "carpalways" => \(my $carpalways = 0),
38 "verbose" => \$verbose,
39 "nocleanup" => \$nocleanup,
40 "dumpupdatedfixture" => \$dumpupdatedfixture,
41 "noserver" => \$noserver,
42 "noparallel" => \$noparallel,
43 "nopatch" => \$nopatch,
44 "fixture_path" => \$fixture_path,
45 "list_config" => \$list_config,
46 "logfile=s" => \$logfile,
47 "env" => \$print_environment,
50 require Carp::Always if $carpalways;
52 if ($print_environment) { print STDERR "CURRENT ENV: ".Dumper(\%ENV); }
54 my @prove_args = @ARGV;
55 if(@prove_args){
56 @prove_args = map {abs_path($_)} @prove_args;
59 #Change cwd to `sgn/` (or parent of wherever this script is located)
60 my $sgn_dir = abs_path(dirname(abs_path($0))."/../");
61 print STDERR "####### ".$sgn_dir." #######";
62 chdir($sgn_dir);
63 @prove_args = ( 't' ) unless @prove_args;
65 #my $parallel = (grep /^-j\d*$/, @ARGV) ? 1 : 0;
67 $ENV{SGN_CONFIG_LOCAL_SUFFIX} = 'fixture';
68 #my $conf_file_base = 'sgn_local.conf'; # which conf file the sgn_fixture.conf should be based on
69 # relative to `sgn/`
70 my $conf_file_base = $ENV{SGN_TEST_CONF} || 'sgn_test.conf';
71 my $template_file = 'sgn_fixture_template.conf';
72 # get some defaults from sgn_local.conf
74 my $cfg = Config::Any->load_files({files=> [$conf_file_base, $template_file], use_ext=>1 });
76 my $config = $cfg->[0]->{$conf_file_base};
77 my $template = $cfg->[1]->{$template_file};
79 if ($list_config) {
80 print STDERR Dumper($cfg);
83 my $db_user_password = $config->{dbpass};
84 my $dbhost = $config->{dbhost} || 'localhost';
85 my $dbport = $config->{dbport} || '5432';
86 my $db_postgres_password = $config->{DatabaseConnection}->{sgn_test}->{password};
87 print STDERR "Using $dbhost:$dbport\n";
88 my $test_dsn = $config->{DatabaseConnection}->{sgn_test}->{dsn};
89 my $catalyst_server_port = 3010;
91 # replace the keys in the sgn local file with what's in the template
93 foreach my $k (keys %{$template}) {
94 #print STDERR "Replacing key $k : $config->{$k} with $template->{$k}\n";
95 $config->{$k} = $template->{$k};
98 # load the database fixture
100 my $dbname;
102 if ($ENV{TEST_DB_NAME}) { $dbname = $ENV{TEST_DB_NAME}; }
104 else {
105 my $now = DateTime->now();
106 $dbname = join "_", map { $now->$_ } (qw | year month day hour minute |);
107 $dbname = 'test_db_'.$dbname;
108 $dbname .= $$;
111 print STDERR "# Writing a .pgpass file... ";
112 # format = hostname:port:database:username:password
113 open(my $PGPASS, ">", "$ENV{HOME}/.pgpass") || die "Can't open .pgpass for writing.";
114 print $PGPASS "$dbhost:$dbport:$dbname:web_usr:$db_user_password\n";
115 print $PGPASS "$dbhost:$dbport:*:postgres:$db_postgres_password\n";
116 close($PGPASS);
117 system("chmod 0600 $ENV{HOME}/.pgpass");
118 print STDERR "Done.\n";
120 # load fixture only if no TEST_DB_NAME env var was provided
121 if (! $ENV{TEST_DB_NAME}) {
122 my $database_fixture_dump = $ENV{DATABASE_FIXTURE_PATH} || $fixture_path;
123 print STDERR "# Loading database fixture... $database_fixture_dump ... ";
124 system("createdb -h $config->{dbhost} -U postgres -T template0 -E UTF8 --no-password $dbname");
125 # will emit an error if web_usr role already exists, but that's OK
126 system("psql -h $config->{dbhost} -U postgres $dbname -c \"CREATE USER web_usr PASSWORD '$db_user_password'\"");
127 system("cat $database_fixture_dump | psql -h $config->{dbhost} -U postgres $dbname > /dev/null");
129 print STDERR "Done.\n";
132 print STDERR "# Creating sgn_fixture.conf file... ";
133 $config->{dbname} = $dbname;
134 $test_dsn =~ s/dbname=(.*)$/dbname=$dbname/;
135 $config->{DatabaseConnection}->{sgn_test}->{dsn} = $test_dsn;
137 #print STDERR Dumper($config);
139 my $new_conf = hash2config($config);
141 open(my $NEWCONF, ">", "sgn_fixture.conf") || die "Can't open sgn_fixture.conf for writing";
142 print $NEWCONF $new_conf;
143 close($NEWCONF);
145 #run fixture and db patches.
146 if (! $nopatch) {
147 system("t/data/fixture/patches/run_fixture_and_db_patches.pl -u postgres -p $db_postgres_password -h $dbhost -d $dbname -e janedoe -s 145");
150 # run the materialized views creation script
152 print STDERR "Running matview refresh with -H $dbhost -D $dbname -U postgres -P $db_postgres_password -m fullview\n";
153 system("perl bin/refresh_matviews.pl -H $dbhost -D $dbname -U postgres -P $db_postgres_password -m fullview");
155 if ($dumpupdatedfixture){
156 print STDERR "Dumping new updated fixture with all patches run on it to t/data/fixture/cxgn_fixture.sql\n";
157 system("pg_dump -h $config->{dbhost} -U postgres $dbname > t/data/fixture/cxgn_fixture.sql");
160 print STDERR "Done.\n";
162 # start the test web server
164 my $server_pid;
165 if ($noserver) {
166 print STDERR "# [ --noserver option: not starting web server]\n";
168 else {
169 $server_pid = fork;
171 unless( $server_pid ) {
173 # web server process
175 #$ENV{SGN_TEST_MODE} = 1;
176 @ARGV = (
177 -p => $catalyst_server_port,
178 ( $noparallel ? () : ('--fork') ),
181 if (!$verbose) {
182 print STDERR "# [Server logfile at $logfile]\n";
183 open (STDERR, ">$logfile") || die "can't open logfile.";
185 Catalyst::ScriptRunner->run('SGN', 'Server');
187 if (!$nocleanup) {
188 print STDERR "# Removing test database ($dbname)... ";
190 if ($noserver) {
191 print STDERR "# [ --noserver option: No logfile to remove]\n";
193 else {
194 print STDERR "# Delete server logfile... ";
195 close($logfile);
196 unlink $logfile;
197 print STDERR "Done.\n";
201 exit;
203 print STDERR "# Starting web server (PID=$server_pid)... ";
207 # wait for the test server to start
210 local $SIG{CHLD} = sub {
211 waitpid $server_pid, 0;
212 die "\nTest server failed to start. Aborting.\n";
214 print STDERR "Done.\n";
216 if (!$noserver) {
217 sleep 1 until !kill(0, $server_pid) || get "http://localhost:$catalyst_server_port";
221 my $prove_pid = fork;
222 unless( $prove_pid ) {
224 # test harness process
226 print STDERR "# Starting tests... \n";
228 # set up env vars for prove and the tests
230 $ENV{SGN_TEST_SERVER} ||= "http://localhost:$catalyst_server_port";
231 if(! $noparallel ) {
232 $ENV{SGN_PARALLEL_TESTING} = 1;
233 $ENV{SGN_SKIP_LEAK_TEST} = 1;
236 # now run the tests against it
238 my $app = App::Prove->new;
240 my $v = $verbose ? 'v' : '';
242 $app->process_args(
243 '-lr'.$v,
244 ( map { -I => $_ } @INC ),
245 @prove_args
247 exit( $app->run ? 0 : 1 );
250 #$SIG{CHLD} = 'IGNORE'; # problematic
251 $SIG{INT} = sub { kill 15, $server_pid, $prove_pid };
252 $SIG{KILL} = sub { kill 9, $server_pid, $prove_pid };
254 print STDERR "# Start prove (PID $prove_pid)... \n";
255 waitpid $prove_pid, 0;
256 my $prove_pid_exit_status = $? >> 8;
257 print STDERR "# Prove finished, stopping web server PID $server_pid... ";
259 END { kill 15, $server_pid if $server_pid }
260 waitpid $server_pid, 0;
261 sleep(3);
262 print STDERR "Done.\n";
264 if (!$nocleanup) {
265 if ($ENV{TEST_DB_NAME}) {
266 print STDERR "Not removing test database (TEST_DB_NAME = $ENV{TEST_DB_NAME} is set.\n";
268 else {
269 print STDERR "# Removing test database ($dbname)... ";
270 system("dropdb -h $config->{dbhost} -U postgres --no-password $dbname");
271 print STDERR "Done.\n";
274 if ($noserver) {
275 print STDERR "# [ --noserver option: No logfile to remove]\n";
277 else {
278 # print STDERR "# Delete server logfile... ";
279 # close($logfile);
280 # unlink $logfile;
281 # print STDERR "Done.\n";
283 print STDERR "# Delete fixture conf file... ";
284 unlink "sgn_fixture.conf";
285 print STDERR "Done.\n";
288 else {
289 print STDERR "# --nocleanup option: not removing db or files.\n";
291 print STDERR "# Test run complete.\n\n";
292 exit($prove_pid_exit_status); # exit with non-zero exit status if any tests failed
296 sub hash2config {
297 my $hash = shift;
299 my $s = "";
300 foreach my $k (keys(%$hash)) {
301 if (ref($hash->{$k}) eq "ARRAY") {
302 foreach my $v (@{$hash->{$k}}) {
303 $s .= "$k $v\n";
306 elsif (ref($hash->{$k}) eq "HASH") {
307 foreach my $n (keys(%{$hash->{$k}})) {
308 if (ref($hash->{$k}->{$n}) eq "HASH") {
309 $s .= "<$k $n>\n";
310 $s .= hash2config($hash->{$k}->{$n});
312 else {
313 $s .= "<$k>\n";
314 $s .= hash2config($hash->{$k});
316 $s .= "</$k>\n";
319 else {
320 $s .= "$k $hash->{$k}\n";
324 # if nothing matched the replace keys, add them here
327 # if (exists($hash->{dbname})) {
328 # $s .= "dbname $dbname\n";
331 return $s;
336 __END__
338 =head1 NAME
340 test_fixture.pl - start a dev server and run tests against it
342 =head1 SYNOPSIS
344 t/test_fixture.pl --carpalways -- -v -j5 t/mytest.t t/mydiroftests/
346 =head1 OPTIONS
348 -v verbose - the output of the server is not re-directed to file,
349 but rather output to the screen.
351 --carpalways Load Carp::Always in both the server and the test process
352 to force backtraces of all warnings and errors
354 --nocleanup Do not clean up database and logfile
356 --noserver Do not start webserver (if running unit_fixture tests only)
358 --noparallel Do not run the server in parallel mode.
360 --nopatch Do not run fixture and database patches
362 --fixture_path specify a path to the fixture different from the default
363 (t/data/fixture/cxgn_fixture.pl). Note: You can also set the env
364 variable DATABASE_FIXTURE_PATH, which will overrule this
365 option.
367 --list_config lists the configuration information
369 -- -v options specified after two dashes will be passed to prove
370 directly, such -v will run prove in verbose mode.
372 By default, the configuration will be taken from the file sgn_test.conf. To use another configuration file, set the environment variable SGN_TEST_CONF to the name of the file you would like to use.
374 To use an existing database as the fixture, set the environment variable TEST_DB_NAME to the name of the database you would like to use.
376 =head1 AUTHORS
378 Robert Buels (initial script)
379 Lukas Mueller <lam87@cornell.edu> (fixture implementation)
381 =cut