Skip to content

Commit

Permalink
WIP -- times out in "prove -l -v t/03-auth.t" with no output from spa…
Browse files Browse the repository at this point in the history
…wn_postgres -- Start postgreSQL instance directly in tests

openQA tests like `prove -l t/basic.t` already include a line
`OpenQA::Test::Database->new->create;` but this still needs a database
process to be present. This is detected and flagged as an error
accordingly but we could make the tests themselves more convenient and
less reliant on the Makefile by spawning a postgresql process itself
within tests if not already running.

Related progress issue: https://progress.opensuse.org/issues/102524
  • Loading branch information
okurz committed Nov 23, 2021
1 parent eab2bad commit c01d66e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion t/03-auth.t
Expand Up @@ -10,7 +10,7 @@ use Test::Mojo;
use Test::Output 'combined_like';
use Test::Warnings;
use OpenQA::Test::Database;
use OpenQA::Test::TimeLimit '10';
use OpenQA::Test::TimeLimit '30';
use OpenQA::WebAPI::Auth::OAuth2;
use Mojo::File qw(tempdir path);
use Mojo::Transaction;
Expand Down
18 changes: 17 additions & 1 deletion t/lib/OpenQA/Test/Database.pm
Expand Up @@ -17,15 +17,27 @@ use Try::Tiny;

has fixture_path => 't/fixtures';

plan skip_all => 'set TEST_PG to e.g. "DBI:Pg:dbname=test" to enable this test' unless $ENV{TEST_PG};
plan skip_all => 'set TEST_PG to e.g. "DBI:Pg:dbname=test" to enable this test' if $ENV{USE_EXTERNAL_PG} && !$ENV{TEST_PG};

sub generate_schema_name {
return 'tmp_' . random_string();
}

sub spawn_postgres {
$ENV{TEST_PG_PATH} //= '/dev/shm/tpg';
# this times out in `prove -l -v t/03-auth.t` and I don't see any output.
# Maybe it's better to use IPC::Run and show the output unbuffered or
# something
my $out = qx{test -d $ENV{TEST_PG_PATH} && (pg_ctl -D $ENV{TEST_PG_PATH} -s status >&/dev/null || pg_ctl -D $ENV{TEST_PG_PATH} -s start) || ./t/test_postgresql $ENV{TEST_PG_PATH}};

This comment has been minimized.

Copy link
@kalikiana

kalikiana Nov 30, 2021

Member

I'd use separate invocations here rather than chaining commands in the same shell

This comment has been minimized.

Copy link
@okurz

okurz Nov 30, 2021

Author Member

The point is that this line starts an existing DB process if the DB already exists and creates one otherwise. So it must be one logical compound evaluation that is executed

This comment has been minimized.

Copy link
@Martchus

Martchus Nov 30, 2021

Contributor

This line is a weird mix between using pg_ctl manually and letting ./t/test_postgresql handle it. I suppose this is required to avoid having ./t/test_postgresql restart the database. The mix of && and || makes it hard to follow the logic, though. So maybe it is worth to extend ./t/test_postgresql to be able to reuse an already started database instead of introducing unreadable one-liners?

I also think $ENV{TEST_PG_PATH} needs to be quoted.

This comment has been minimized.

Copy link
@okurz

okurz Nov 30, 2021

Author Member

It's just the same instruction as in the Makefile

diag $out;
$ENV{TEST_PG} = "DBI:Pg:dbname=openqa_test;host=$ENV{TEST_PG_PATH}";
}

sub create {
my ($self, %options) = @_;

spawn_postgres;

# create new database connection
my $schema = OpenQA::Schema::connect_db(mode => 'test', deploy => 0);

Expand All @@ -52,6 +64,10 @@ sub create {
return $schema;
}

END {
qx{pg_ctl -D $ENV{TEST_PG_PATH} stop} unless $ENV{USE_EXTERNAL_PG} || $ENV{KEEP_DB};
}

sub insert_fixtures {
my ($self, $schema, $fixtures_glob) = @_;

Expand Down

0 comments on commit c01d66e

Please sign in to comment.