16 use Catalyst
::ScriptRunner
;
19 use SGN
::Devel
::MyDevLibs
;
26 "carpalways" => \
( my $carpalways = 0 ),
27 "verbose" => \
$verbose ,
28 "nocleanup" => \
$nocleanup,
29 "noserver" => \
$noserver,
30 "noparallel" => \
$noparallel,
33 require Carp
::Always
if $carpalways;
35 my @prove_args = @ARGV;
36 @prove_args = ( 't' ) unless @prove_args;
38 #my $parallel = (grep /^-j\d*$/, @ARGV) ? 1 : 0;
40 $ENV{SGN_CONFIG_LOCAL_SUFFIX
} = 'fixture';
41 #my $conf_file_base = 'sgn_local.conf'; # which conf file the sgn_fixture.conf should be based on
42 my $conf_file_base = 'sgn_local.conf';
43 my $template_file = 'sgn_fixture_template.conf';
44 # get some defaults from sgn_local.conf
46 my $cfg = Config
::Any
->load_files({files
=> [$conf_file_base, $template_file], use_ext
=>1 });
48 my $config = $cfg->[0]->{$conf_file_base};
49 my $template = $cfg->[1]->{$template_file};
51 #print STDERR Dumper($cfg);
52 my $db_user_password = $config->{dbpass
};
53 my $dbhost = $config->{dbhost
};
54 my $db_postgres_password = $config->{DatabaseConnection
}->{sgn_test
}->{password
};
55 my $test_dsn = $config->{DatabaseConnection
}->{sgn_test
}->{dsn
};
56 my $catalyst_server_port = 3010;
58 # replace the keys in the sgn local file with what's in the template
60 foreach my $k (keys %{$template}) {
61 #print STDERR "Replacing key $k : $config->{$k} with $template->{$k}\n";
62 $config->{$k} = $template->{$k};
65 # load the database fixture
67 my $now = DateTime
->now();
68 my $dbname = join "_", map { $now->$_ } (qw
| year month day hour minute
|);
69 $dbname = 'test_db_'.$dbname;
72 print STDERR
"# Writing a .pgpass file... ";
73 # format = hostname:port:database:username:password
74 open(my $PGPASS, ">", "$ENV{HOME}/.pgpass") || die "Can't open .pgpass for writing.";
75 print $PGPASS "$dbhost:5432:$dbname:web_usr:$db_user_password\n";
76 print $PGPASS "$dbhost:5432:*:postgres:$db_postgres_password\n";
78 system("chmod 0600 $ENV{HOME}/.pgpass");
79 print STDERR
"Done.\n";
81 print STDERR
"# Loading database fixture... ";
82 my $database_fixture_dump = $ENV{DATABASE_FIXTURE_PATH
} || '../cxgn_fixture.sql';
83 system("createdb -h $config->{dbhost} -U postgres -T template0 -E SQL_ASCII --no-password $dbname");
84 system("cat $database_fixture_dump | psql -h $config->{dbhost} -U postgres $dbname > /dev/null");
86 print STDERR
"Done.\n";
88 print STDERR
"# Creating sgn_fixture.conf file... ";
89 $config->{dbname
} = $dbname;
90 $test_dsn =~ s/dbname=(.*)$/dbname=$dbname/;
91 $config->{DatabaseConnection
}->{sgn_test
}->{dsn
} = $test_dsn;
93 #print STDERR Dumper($config);
95 my $new_conf = hash2config
($config);
97 open(my $NEWCONF, ">", "sgn_fixture.conf") || die "Can't open sgn_fixture.conf for writing";
98 print $NEWCONF $new_conf;
101 print STDERR
"Done.\n";
103 # start the test web server
108 print STDERR
"# [ --noserver option: not starting web server]\n";
112 $logfile = "logfile.$$.txt";
114 unless( $server_pid ) {
118 #$ENV{SGN_TEST_MODE} = 1;
120 -p
=> $catalyst_server_port,
121 ( $noparallel ?
() : ('--fork') ),
125 print STDERR
"# [Server logfile at $logfile]\n";
126 open (STDERR
, ">$logfile") || die "can't open logfile.";
128 Catalyst
::ScriptRunner
->run('SGN', 'Server');
132 print STDERR
"# Starting web server (PID=$server_pid)... ";
136 # wait for the test server to start
139 local $SIG{CHLD
} = sub {
140 waitpid $server_pid, 0;
141 die "\nTest server failed to start. Aborting.\n";
143 print STDERR
"Done.\n";
146 sleep 1 until !kill(0, $server_pid) || get
"http://localhost:$catalyst_server_port";
150 my $prove_pid = fork;
151 unless( $prove_pid ) {
153 # test harness process
155 print STDERR
"# Starting tests... \n";
157 # set up env vars for prove and the tests
159 $ENV{SGN_TEST_SERVER
} = "http://localhost:$catalyst_server_port";
161 $ENV{SGN_PARALLEL_TESTING
} = 1;
162 $ENV{SGN_SKIP_LEAK_TEST
} = 1;
165 # now run the tests against it
167 my $app = App
::Prove
->new;
170 ( map { -I
=> $_ } @INC ),
173 exit( $app->run ?
0 : 1 );
176 #$SIG{CHLD} = 'IGNORE'; # problematic
177 $SIG{INT
} = sub { kill 15, $server_pid, $prove_pid };
178 $SIG{KILL
} = sub { kill 9, $server_pid, $prove_pid };
180 print STDERR
"# Start prove (PID $prove_pid)... \n";
181 waitpid $prove_pid, 0;
182 print STDERR
"# Prove finished, stopping web server PID $server_pid... ";
184 END { kill 15, $server_pid if $server_pid }
185 waitpid $server_pid, 0;
187 print STDERR
"Done.\n";
190 print STDERR
"# Removing test database ($dbname)... ";
191 system("dropdb -h $config->{dbhost} -U postgres --no-password $dbname");
192 print STDERR
"Done.\n";
195 print STDERR
"# [ --noserver option: No logfile to remove]\n";
198 print STDERR
"# Delete server logfile... ";
201 print STDERR
"Done.\n";
203 print STDERR
"# Delete fixture conf file... ";
204 unlink "sgn_fixture.conf";
205 print STDERR
"Done.\n";
209 print STDERR
"# --nocleanup option: not removing db or files.\n";
211 print STDERR
"# Test run complete.\n\n";
219 foreach my $k (keys(%$hash)) {
220 if (ref($hash->{$k}) eq "ARRAY") {
221 foreach my $v (@
{$hash->{$k}}) {
225 elsif (ref($hash->{$k}) eq "HASH") {
226 foreach my $n (keys(%{$hash->{$k}})) {
227 if (ref($hash->{$k}->{$n}) eq "HASH") {
229 $s .= hash2config
($hash->{$k}->{$n});
233 $s .= hash2config
($hash->{$k});
239 $s .= "$k $hash->{$k}\n";
243 # if nothing matched the replace keys, add them here
246 # if (exists($hash->{dbname})) {
247 # $s .= "dbname $dbname\n";
259 test_fixture.pl - start a dev server and run tests against it
263 t/test_fixture.pl --carpalways -- -v -j5 t/mytest.t t/mydiroftests/
267 -v verbose - the output of the server is not re-directed to file,
268 but rather output to the screen.
270 --carpalways Load Carp::Always in both the server and the test process
271 to force backtraces of all warnings and errors
273 --nocleanup Do not clean up database and logfile
275 --noserver Do not start webserver (if running unit_fixture tests only)
277 --noparallel Do not run the server in parallel mode.
281 Robert Buels (initial script)
282 Lukas Mueller <lam87@cornell.edu> (fixture implementation)