This folder contains all the tests related to `check_pgactivity`.

# Environment setting

The test are written using well known and used `Test::More` perl module.
Below are instructions to setup basic environment to run them.

CentOS 7:

~~~console
# yum install -y git perl-core "perl(IPC::Run)" "perl(Test::More)"
~~~

CentOS 8:

~~~console
# dnf install -y perl-core "perl(Test::More)"
# dnf install -y --enablerepo=PowerTools "perl(IPC::Run)"
~~~

Debian 9 & 10:

~~~console
# apt install -y libipc-run-perl
~~~

# Running tests

The tests must be run from the root folder of the project using `perl` or
`prove`.

The tests are run against the PostgreSQL version available in your `PATH` and
reported by `pg_config --version`.

~~~console
check_pgactivity$ export PATH="/usr/pgsql-13/bin:$PATH"
check_pgactivity$ perl t/01-connection.t
1..8
ok 1 - connection successful status (got 0 vs expected 0)
[...]

check_pgactivity$ prove t/10-streaming_delta.t
t/10-streaming_delta.t .. ok
All tests successful.
Files=1, Tests=121,  3 wallclock secs ( 0.03 usr  0.01 sys +  2.09 cusr  0.52 csys =  2.65 CPU)
Result: PASS

check_pgactivity$ prove
t/00-copyright-year.t ... ok
t/01-archive_folder.t ... ok
t/01-connection.t ....... ok
t/01-pga_version.t ...... ok
t/01-streaming_delta.t .. ok
All tests successful.
Files=5, Tests=206, 10 wallclock secs ( 0.03 usr  0.01 sys +  6.33 cusr  1.15 csys =  7.52 CPU)
Result: PASS
~~~

# Logs

If tests are failing, log files are kept under `tmp_check/log` folder.

~~~
# regression tests log
check_pgactivity$ less tmp_check/log/regress_log_01-streaming_delta

# PostgreSQL logs
check_pgactivity$ less tmp_check/log/01-streaming_delta_prim.log
~~~

Make sure to clean or move away folder `tmp_check` before running new tests.

# Devel

The typical boilerplate to create a new test file is:

~~~perl
use lib 't/lib';
use pgNode;

# declare instance named "prod"
my $node = pgNode->get_new_node('prod');

# create the instance and start it
$node->init;
$node->start;

$node->command_checks_all( [
    # command to run
    './check_pgactivity', '--service'  => 'connection',
                          '--username' => getlogin
    ],
    # expected return code
    0,
    # array of regex matching expected standard output
    [ qr/^POSTGRES_CONNECTION OK: Connection successful at [-+:\. \d]+, on PostgreSQL [\d\.]+.*$/ ],
    # array of regex matching expected error output
    [ qr/^$/ ],
    # a name for this test
    'connection successful'
);

# stop instance as fast as possible
$node->stop('immediate');
~~~

Class `pgNode` is facet class creating and returning the appropriate
PostgresNode object depending on the PostgreSQL backend version returned by
`pg_config --version`. It helps extending the PostgresNode classes with
new methods needed in our tests.

Class PostgresNode comes from https://gitlab.com/adunstan/postgresnodeng/
which is currently a dead project. The class has been patched to fix some
various incompatibilities with older PostgreSQL releases. As TAP test modules
has moved a lot in PostgreSQL code during v14, we would need to check if it is
possible to resync with upstream or just keep it that way.

The PostgresNode class and methods are described in its embedded documentation.
See: `perldoc t/lib/PostgresNode.pm`. Any method addition and other changes are
documented in pgNode class. See: `perldoc t/lib/pgNode.pm`.

Some of the methods in these classes are just wrappers around functions coming
from TestLib, but adding some environment context of the instance (eg. setting
`PGHOST`). See embeded documentation of TestLib for more details about these
functions: `perldoc t/lib/TestLib.pm`.
