16 use Catalyst
::ScriptRunner
;
19 use SGN
::Devel
::MyDevLibs
;
25 my $fixture_path = '../fixture/cxgn_fixture.sql';
26 my $use_brapi_fixture;
27 my $brapi_fixture = '../fixture/brapi_fixture.sql';
30 "carpalways" => \
( my $carpalways = 0 ),
31 "verbose" => \
$verbose ,
32 "nocleanup" => \
$nocleanup,
33 "noserver" => \
$noserver,
34 "noparallel" => \
$noparallel,
35 "fixture_path" => \
$fixture_path,
36 "brapi_fixture" => \
$use_brapi_fixture,
39 require Carp
::Always
if $carpalways;
41 my @prove_args = @ARGV;
42 @prove_args = ( 't' ) unless @prove_args;
44 #my $parallel = (grep /^-j\d*$/, @ARGV) ? 1 : 0;
46 $ENV{SGN_CONFIG_LOCAL_SUFFIX
} = 'fixture';
47 #my $conf_file_base = 'sgn_local.conf'; # which conf file the sgn_fixture.conf should be based on
48 my $conf_file_base = 'sgn_local.conf';
49 my $template_file = 'sgn_fixture_template.conf';
50 # get some defaults from sgn_local.conf
52 my $cfg = Config
::Any
->load_files({files
=> [$conf_file_base, $template_file], use_ext
=>1 });
54 my $config = $cfg->[0]->{$conf_file_base};
55 my $template = $cfg->[1]->{$template_file};
57 #print STDERR Dumper($cfg);
58 my $db_user_password = $config->{dbpass
};
59 my $dbhost = $config->{db_host
} || 'localhost';
60 my $db_postgres_password = $config->{DatabaseConnection
}->{sgn_test
}->{password
};
61 my $test_dsn = $config->{DatabaseConnection
}->{sgn_test
}->{dsn
};
62 my $catalyst_server_port = 3010;
64 # replace the keys in the sgn local file with what's in the template
66 foreach my $k (keys %{$template}) {
67 #print STDERR "Replacing key $k : $config->{$k} with $template->{$k}\n";
68 $config->{$k} = $template->{$k};
71 # load the database fixture
73 my $now = DateTime
->now();
74 my $dbname = join "_", map { $now->$_ } (qw
| year month day hour minute
|);
75 $dbname = 'test_db_'.$dbname;
78 print STDERR
"# Writing a .pgpass file... ";
79 # format = hostname:port:database:username:password
80 open(my $PGPASS, ">", "$ENV{HOME}/.pgpass") || die "Can't open .pgpass for writing.";
81 print $PGPASS "$dbhost:5432:$dbname:web_usr:$db_user_password\n";
82 print $PGPASS "$dbhost:5432:*:postgres:$db_postgres_password\n";
84 system("chmod 0600 $ENV{HOME}/.pgpass");
85 print STDERR
"Done.\n";
87 my $dump_path = $use_brapi_fixture ?
$brapi_fixture : $fixture_path;
88 my $database_fixture_dump = $ENV{DATABASE_FIXTURE_PATH
} || $dump_path;
89 print STDERR
"# Loading database fixture... $database_fixture_dump ... ";
90 system("createdb -h $config->{dbhost} -U postgres -T template0 -E SQL_ASCII --no-password $dbname");
91 system("cat $database_fixture_dump | psql -h $config->{dbhost} -U postgres $dbname > /dev/null");
93 print STDERR
"Done.\n";
95 print STDERR
"# Creating sgn_fixture.conf file... ";
96 $config->{dbname
} = $dbname;
97 $test_dsn =~ s/dbname=(.*)$/dbname=$dbname/;
98 $config->{DatabaseConnection
}->{sgn_test
}->{dsn
} = $test_dsn;
100 #print STDERR Dumper($config);
102 my $new_conf = hash2config
($config);
104 open(my $NEWCONF, ">", "sgn_fixture.conf") || die "Can't open sgn_fixture.conf for writing";
105 print $NEWCONF $new_conf;
108 # run the materialized views creation script
110 print STDERR
"Running matview refresh with -H $dbhost -D $dbname -U postgres -P $db_postgres_password\n";
111 system("perl bin/refresh_matviews.pl -H $dbhost -D $dbname -U postgres -P $db_postgres_password");
114 print STDERR
"Done.\n";
116 # start the test web server
121 print STDERR
"# [ --noserver option: not starting web server]\n";
125 $logfile = "logfile.$$.txt";
127 unless( $server_pid ) {
131 #$ENV{SGN_TEST_MODE} = 1;
133 -p
=> $catalyst_server_port,
134 ( $noparallel ?
() : ('--fork') ),
138 print STDERR
"# [Server logfile at $logfile]\n";
139 open (STDERR
, ">$logfile") || die "can't open logfile.";
141 Catalyst
::ScriptRunner
->run('SGN', 'Server');
145 print STDERR
"# Starting web server (PID=$server_pid)... ";
149 # wait for the test server to start
152 local $SIG{CHLD
} = sub {
153 waitpid $server_pid, 0;
154 die "\nTest server failed to start. Aborting.\n";
156 print STDERR
"Done.\n";
159 sleep 1 until !kill(0, $server_pid) || get
"http://localhost:$catalyst_server_port";
163 my $prove_pid = fork;
164 unless( $prove_pid ) {
166 # test harness process
168 print STDERR
"# Starting tests... \n";
170 # set up env vars for prove and the tests
172 $ENV{SGN_TEST_SERVER
} = "http://localhost:$catalyst_server_port";
174 $ENV{SGN_PARALLEL_TESTING
} = 1;
175 $ENV{SGN_SKIP_LEAK_TEST
} = 1;
178 # now run the tests against it
180 my $app = App
::Prove
->new;
183 ( map { -I
=> $_ } @INC ),
186 exit( $app->run ?
0 : 1 );
189 #$SIG{CHLD} = 'IGNORE'; # problematic
190 $SIG{INT
} = sub { kill 15, $server_pid, $prove_pid };
191 $SIG{KILL
} = sub { kill 9, $server_pid, $prove_pid };
193 print STDERR
"# Start prove (PID $prove_pid)... \n";
194 waitpid $prove_pid, 0;
195 print STDERR
"# Prove finished, stopping web server PID $server_pid... ";
197 END { kill 15, $server_pid if $server_pid }
198 waitpid $server_pid, 0;
200 print STDERR
"Done.\n";
203 print STDERR
"# Removing test database ($dbname)... ";
204 system("dropdb -h $config->{dbhost} -U postgres --no-password $dbname");
205 print STDERR
"Done.\n";
208 print STDERR
"# [ --noserver option: No logfile to remove]\n";
211 print STDERR
"# Delete server logfile... ";
214 print STDERR
"Done.\n";
216 print STDERR
"# Delete fixture conf file... ";
217 unlink "sgn_fixture.conf";
218 print STDERR
"Done.\n";
222 print STDERR
"# --nocleanup option: not removing db or files.\n";
224 print STDERR
"# Test run complete.\n\n";
232 foreach my $k (keys(%$hash)) {
233 if (ref($hash->{$k}) eq "ARRAY") {
234 foreach my $v (@
{$hash->{$k}}) {
238 elsif (ref($hash->{$k}) eq "HASH") {
239 foreach my $n (keys(%{$hash->{$k}})) {
240 if (ref($hash->{$k}->{$n}) eq "HASH") {
242 $s .= hash2config
($hash->{$k}->{$n});
246 $s .= hash2config
($hash->{$k});
252 $s .= "$k $hash->{$k}\n";
256 # if nothing matched the replace keys, add them here
259 # if (exists($hash->{dbname})) {
260 # $s .= "dbname $dbname\n";
272 test_fixture.pl - start a dev server and run tests against it
276 t/test_fixture.pl --carpalways -- -v -j5 t/mytest.t t/mydiroftests/
280 -v verbose - the output of the server is not re-directed to file,
281 but rather output to the screen.
283 --carpalways Load Carp::Always in both the server and the test process
284 to force backtraces of all warnings and errors
286 --nocleanup Do not clean up database and logfile
288 --noserver Do not start webserver (if running unit_fixture tests only)
290 --noparallel Do not run the server in parallel mode.
292 --fixture_path specify a path to the fixture different from the default
293 (../fixture/cxgn_fixture.pl). Note: You can also set the env
294 variable DATABASE_FIXTURE_PATH, which will overrule this
297 -- -v options specified after two dashes will be passed to prove
298 directly, such -v will run prove in verbose mode.
302 Robert Buels (initial script)
303 Lukas Mueller <lam87@cornell.edu> (fixture implementation)