16 use File
::Basename
qw(dirname);
19 use Catalyst
::ScriptRunner
;
22 use SGN
::Devel
::MyDevLibs
;
27 my $dumpupdatedfixture;
30 # relative to `sgn/ (or parent of wherever this script is located)
31 my $fixture_path = 't/data/fixture/cxgn_fixture.sql';
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;
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." #######";
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
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};
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;
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";
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;
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
146 print STDERR
"# [ --noserver option: not starting web server]\n";
150 $logfile = "logfile.$$.txt";
152 unless( $server_pid ) {
156 #$ENV{SGN_TEST_MODE} = 1;
158 -p
=> $catalyst_server_port,
159 ( $noparallel ?
() : ('--fork') ),
163 print STDERR
"# [Server logfile at $logfile]\n";
164 open (STDERR
, ">$logfile") || die "can't open logfile.";
166 Catalyst
::ScriptRunner
->run('SGN', 'Server');
169 print STDERR
"# Removing test database ($dbname)... ";
172 print STDERR
"# [ --noserver option: No logfile to remove]\n";
175 print STDERR
"# Delete server logfile... ";
178 print STDERR
"Done.\n";
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";
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";
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' : '';
225 ( map { -I
=> $_ } @INC ),
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;
242 print STDERR
"Done.\n";
245 print STDERR
"# Removing test database ($dbname)... ";
246 system("dropdb -h $config->{dbhost} -U postgres --no-password $dbname");
247 print STDERR
"Done.\n";
250 print STDERR
"# [ --noserver option: No logfile to remove]\n";
253 # print STDERR "# Delete server logfile... ";
256 # print STDERR "Done.\n";
258 print STDERR
"# Delete fixture conf file... ";
259 unlink "sgn_fixture.conf";
260 print STDERR
"Done.\n";
264 print STDERR
"# --nocleanup option: not removing db or files.\n";
266 print STDERR
"# Test run complete.\n\n";
274 foreach my $k (keys(%$hash)) {
275 if (ref($hash->{$k}) eq "ARRAY") {
276 foreach my $v (@
{$hash->{$k}}) {
280 elsif (ref($hash->{$k}) eq "HASH") {
281 foreach my $n (keys(%{$hash->{$k}})) {
282 if (ref($hash->{$k}->{$n}) eq "HASH") {
284 $s .= hash2config
($hash->{$k}->{$n});
288 $s .= hash2config
($hash->{$k});
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";
314 test_fixture.pl - start a dev server and run tests against it
318 t/test_fixture.pl --carpalways -- -v -j5 t/mytest.t t/mydiroftests/
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
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.
346 Robert Buels (initial script)
347 Lukas Mueller <lam87@cornell.edu> (fixture implementation)