16 use File
::Basename
qw(dirname);
19 use Catalyst
::ScriptRunner
;
22 use SGN
::Devel
::MyDevLibs
;
27 my $dumpupdatedfixture;
29 # relative to `sgn/ (or parent of wherever this script is located)
30 my $fixture_path = 't/data/fixture/cxgn_fixture.sql';
33 "carpalways" => \
( my $carpalways = 0 ),
34 "verbose" => \
$verbose ,
35 "nocleanup" => \
$nocleanup,
36 "dumpupdatedfixture" => \
$dumpupdatedfixture,
37 "noserver" => \
$noserver,
38 "noparallel" => \
$noparallel,
39 "fixture_path" => \
$fixture_path,
42 require Carp
::Always
if $carpalways;
44 my @prove_args = @ARGV;
46 @prove_args = map {abs_path
($_)} @prove_args;
49 #Change cwd to `sgn/` (or parent of wherever this script is located)
50 my $sgn_dir = abs_path
(dirname
(abs_path
($0))."/../");
51 print STDERR
"####### ".$sgn_dir." #######";
53 @prove_args = ( 't' ) unless @prove_args;
55 #my $parallel = (grep /^-j\d*$/, @ARGV) ? 1 : 0;
57 $ENV{SGN_CONFIG_LOCAL_SUFFIX
} = 'fixture';
58 #my $conf_file_base = 'sgn_local.conf'; # which conf file the sgn_fixture.conf should be based on
60 my $conf_file_base = 'sgn_local.conf';
61 my $template_file = 'sgn_fixture_template.conf';
62 # get some defaults from sgn_local.conf
64 my $cfg = Config
::Any
->load_files({files
=> [$conf_file_base, $template_file], use_ext
=>1 });
66 my $config = $cfg->[0]->{$conf_file_base};
67 my $template = $cfg->[1]->{$template_file};
69 print STDERR Dumper
($cfg);
70 my $db_user_password = $config->{dbpass
};
71 my $dbhost = $config->{dbhost
} || 'localhost';
72 my $dbport = $config->{dbport
} || '5432';
73 my $db_postgres_password = $config->{DatabaseConnection
}->{sgn_test
}->{password
};
74 print STDERR
"Using $dbhost:$dbport\n";
75 my $test_dsn = $config->{DatabaseConnection
}->{sgn_test
}->{dsn
};
76 my $catalyst_server_port = 3010;
78 # replace the keys in the sgn local file with what's in the template
80 foreach my $k (keys %{$template}) {
81 #print STDERR "Replacing key $k : $config->{$k} with $template->{$k}\n";
82 $config->{$k} = $template->{$k};
85 # load the database fixture
87 my $now = DateTime
->now();
88 my $dbname = join "_", map { $now->$_ } (qw
| year month day hour minute
|);
89 $dbname = 'test_db_'.$dbname;
92 print STDERR
"# Writing a .pgpass file... ";
93 # format = hostname:port:database:username:password
94 open(my $PGPASS, ">", "$ENV{HOME}/.pgpass") || die "Can't open .pgpass for writing.";
95 print $PGPASS "$dbhost:$dbport:$dbname:web_usr:$db_user_password\n";
96 print $PGPASS "$dbhost:$dbport:*:postgres:$db_postgres_password\n";
98 system("chmod 0600 $ENV{HOME}/.pgpass");
99 print STDERR
"Done.\n";
101 my $database_fixture_dump = $ENV{DATABASE_FIXTURE_PATH
} || $fixture_path;
102 print STDERR
"# Loading database fixture... $database_fixture_dump ... ";
103 system("createdb -h $config->{dbhost} -U postgres -T template0 -E SQL_ASCII --no-password $dbname");
104 system("cat $database_fixture_dump | psql -h $config->{dbhost} -U postgres $dbname > /dev/null");
106 print STDERR
"Done.\n";
108 print STDERR
"# Creating sgn_fixture.conf file... ";
109 $config->{dbname
} = $dbname;
110 $test_dsn =~ s/dbname=(.*)$/dbname=$dbname/;
111 $config->{DatabaseConnection
}->{sgn_test
}->{dsn
} = $test_dsn;
113 #print STDERR Dumper($config);
115 my $new_conf = hash2config
($config);
117 open(my $NEWCONF, ">", "sgn_fixture.conf") || die "Can't open sgn_fixture.conf for writing";
118 print $NEWCONF $new_conf;
121 #run fixture and db patches.
122 system("t/data/fixture/patches/run_fixture_and_db_patches.pl -u postgres -p $db_postgres_password -h $config->{dbhost} -d $dbname -e janedoe");
124 # run the materialized views creation script
126 print STDERR
"Running matview refresh with -H $dbhost -D $dbname -U postgres -P $db_postgres_password -m fullview\n";
127 system("perl bin/refresh_matviews.pl -H $dbhost -D $dbname -U postgres -P $db_postgres_password -m fullview");
129 if ($dumpupdatedfixture){
130 print STDERR
"Dumping new updated fixture with all patches run on it to t/data/fixture/cxgn_fixture.sql\n";
131 system("pg_dump -U postgres $dbname > t/data/fixture/cxgn_fixture.sql");
134 print STDERR
"Done.\n";
136 # start the test web server
141 print STDERR
"# [ --noserver option: not starting web server]\n";
145 $logfile = "logfile.$$.txt";
147 unless( $server_pid ) {
151 #$ENV{SGN_TEST_MODE} = 1;
153 -p
=> $catalyst_server_port,
154 ( $noparallel ?
() : ('--fork') ),
158 print STDERR
"# [Server logfile at $logfile]\n";
159 open (STDERR
, ">$logfile") || die "can't open logfile.";
161 Catalyst
::ScriptRunner
->run('SGN', 'Server');
165 print STDERR
"# Starting web server (PID=$server_pid)... ";
169 # wait for the test server to start
172 local $SIG{CHLD
} = sub {
173 waitpid $server_pid, 0;
174 die "\nTest server failed to start. Aborting.\n";
176 print STDERR
"Done.\n";
179 sleep 1 until !kill(0, $server_pid) || get
"http://localhost:$catalyst_server_port";
183 my $prove_pid = fork;
184 unless( $prove_pid ) {
186 # test harness process
188 print STDERR
"# Starting tests... \n";
190 # set up env vars for prove and the tests
192 $ENV{SGN_TEST_SERVER
} = "http://localhost:$catalyst_server_port";
194 $ENV{SGN_PARALLEL_TESTING
} = 1;
195 $ENV{SGN_SKIP_LEAK_TEST
} = 1;
198 # now run the tests against it
200 my $app = App
::Prove
->new;
203 ( map { -I
=> $_ } @INC ),
206 exit( $app->run ?
0 : 1 );
209 #$SIG{CHLD} = 'IGNORE'; # problematic
210 $SIG{INT
} = sub { kill 15, $server_pid, $prove_pid };
211 $SIG{KILL
} = sub { kill 9, $server_pid, $prove_pid };
213 print STDERR
"# Start prove (PID $prove_pid)... \n";
214 waitpid $prove_pid, 0;
215 print STDERR
"# Prove finished, stopping web server PID $server_pid... ";
217 END { kill 15, $server_pid if $server_pid }
218 waitpid $server_pid, 0;
220 print STDERR
"Done.\n";
223 print STDERR
"# Removing test database ($dbname)... ";
224 system("dropdb -h $config->{dbhost} -U postgres --no-password $dbname");
225 print STDERR
"Done.\n";
228 print STDERR
"# [ --noserver option: No logfile to remove]\n";
231 print STDERR
"# Delete server logfile... ";
234 print STDERR
"Done.\n";
236 print STDERR
"# Delete fixture conf file... ";
237 unlink "sgn_fixture.conf";
238 print STDERR
"Done.\n";
242 print STDERR
"# --nocleanup option: not removing db or files.\n";
244 print STDERR
"# Test run complete.\n\n";
252 foreach my $k (keys(%$hash)) {
253 if (ref($hash->{$k}) eq "ARRAY") {
254 foreach my $v (@
{$hash->{$k}}) {
258 elsif (ref($hash->{$k}) eq "HASH") {
259 foreach my $n (keys(%{$hash->{$k}})) {
260 if (ref($hash->{$k}->{$n}) eq "HASH") {
262 $s .= hash2config
($hash->{$k}->{$n});
266 $s .= hash2config
($hash->{$k});
272 $s .= "$k $hash->{$k}\n";
276 # if nothing matched the replace keys, add them here
279 # if (exists($hash->{dbname})) {
280 # $s .= "dbname $dbname\n";
292 test_fixture.pl - start a dev server and run tests against it
296 t/test_fixture.pl --carpalways -- -v -j5 t/mytest.t t/mydiroftests/
300 -v verbose - the output of the server is not re-directed to file,
301 but rather output to the screen.
303 --carpalways Load Carp::Always in both the server and the test process
304 to force backtraces of all warnings and errors
306 --nocleanup Do not clean up database and logfile
308 --noserver Do not start webserver (if running unit_fixture tests only)
310 --noparallel Do not run the server in parallel mode.
312 --fixture_path specify a path to the fixture different from the default
313 (t/data/fixture/cxgn_fixture.pl). Note: You can also set the env
314 variable DATABASE_FIXTURE_PATH, which will overrule this
317 -- -v options specified after two dashes will be passed to prove
318 directly, such -v will run prove in verbose mode.
322 Robert Buels (initial script)
323 Lukas Mueller <lam87@cornell.edu> (fixture implementation)