with Test::Simple and JSON::XS.
Add more tests especially for incremental mode and input from stdin that
was broken in release 10.0.
Thanks a lot to Etienne Bersac who have initiate the pgbadger test suite
and added the contributing on pgBadger documentation.
name: Install debian packages
command: >
apt update -y &&
- apt install -y --no-install-recommends bats git jq libjson-xs-perl openssh-client
+ apt install -y --no-install-recommends git libjson-xs-perl openssh-client
- checkout
- run:
name: Executing tests
# Contributing on pgBadger
-Thanks for your attention on pgBadger !
+Thanks for your attention on pgBadger ! You need Perl Module JSON::XS
+to run the full test suite. You can install it on a Debian like system
+using:
-pgBadger has a TAP compatible test suite executed by `prove(1)` on CI. You can
-write tests in many language including perl and bash.
+ sudo apt-get install libjson-xs-perl
+
+or in RPM like system using:
+
+ sudo yum install perl-JSON-XS
+
+pgBadger has a TAP compatible test suite executed by `prove`:
- $ apt install bats
- ...
$ prove
- t/basics.t .. ok
- t/syntax.t .. ok
+ t/01_lint.t ......... ok
+ t/02_basics.t ....... ok
+ t/03_consistency.t .. ok
All tests successful.
- Files=2, Tests=4, 2 wallclock secs ( 0.02 usr 0.00 sys + 2.38 cusr 0.04 csys = 2.44 CPU)
+ Files=3, Tests=13, 6 wallclock secs ( 0.01 usr 0.01 sys + 5.31 cusr 0.16 csys = 5.49 CPU)
Result: PASS
$
+or if you prefer to run test manually:
+
+ $ perl Makefile.PL && make test
+ Checking if your kit is complete...
+ Looks good
+ Generating a Unix-style Makefile
+ Writing Makefile for pgBadger
+ Writing MYMETA.yml and MYMETA.json
+ cp pgbadger blib/script/pgbadger
+ "/usr/bin/perl" -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/pgbadger
+ PERL_DL_NONLAZY=1 "/usr/bin/perl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
+ t/01_lint.t ......... ok
+ t/02_basics.t ....... ok
+ t/03_consistency.t .. ok
+ All tests successful.
+ Files=3, Tests=13, 6 wallclock secs ( 0.03 usr 0.00 sys + 5.39 cusr 0.14 csys = 5.56 CPU)
+ Result: PASS
+ $ make clean && rm Makefile.old
+
Please contribute a regression test when you fix a bug or add a feature. Thanks!
--- /dev/null
+use Test::Simple tests => 2;
+
+my $ret = `perl -wc pgbadger 2>&1`;
+ok( $? == 0, "PERL syntax check");
+
+$ret = `podchecker doc/*.pod 2>&1`;
+ok( $? == 0, "pod syntax check");
+
--- /dev/null
+use Test::Simple tests => 8;
+
+my $LOG = 't/fixtures/light.postgres.log.bz2';
+my $BIN = 't/fixtures/light.postgres.bin';
+my $JSON = 't/out.json';
+
+my $ret = `perl pgbadger --help`;
+ok( $? == 0, "Inline help");
+
+$ret = `perl pgbadger -q -o - $LOG`;
+ok( $? == 0 && length($ret) > 0, "Light log report to stdout");
+
+`rm -f out.html`;
+$ret = `perl pgbadger -q --outdir '.' $LOG`;
+ok( $? == 0 && -e "out.html", "Light log report to HTML");
+
+$ret = `perl pgbadger -q -o $BIN $LOG`;
+ok( $? == 0 && -e "$BIN", "Light log to binary");
+
+`rm -f $JSON`;
+$ret = `perl pgbadger -q -o $JSON --format binary $BIN`;
+$ret = `cat $JSON | perl -pe 's/.*"SELECT":(\\d+),.*/\$1/'`;
+ok( $? == 0 && $ret > 0, "From binary to JSON");
+
+`mkdir t/test_incr/`;
+$ret = `perl pgbadger -q -O t/test_incr -I --extra-files $LOG`;
+ok( $? == 0 && -e "t/test_incr/2017/09/06/index.html"
+ && -e "t/test_incr/2017/week-37/index.html", "Incremental mode report");
+$ret = `grep 'src="../../../.*/bootstrap.min.js"' t/test_incr/2017/09/06/index.html`;
+ok( $? == 0 && substr($ret, 32, 14) eq 'src="../../../', "Ressources files in incremental mode");
+
+`rm -f $JSON`;
+$ret = `bunzip2 -c $LOG | perl pgbadger -q -o $JSON -`;
+$ret = `cat $JSON | perl -pe 's/.*"SELECT":(\\d+),.*/\$1/'`;
+ok( $? == 0 && $ret > 0, "Light log from STDIN");
+
+# Remove files generated during the tests
+`rm -f out.html`;
+`rm -r $JSON`;
+`rm -f $BIN`;
+`rm -rf t/test_incr/`;
+
--- /dev/null
+use Test::Simple tests => 5;
+use JSON::XS;
+
+my $json = new JSON::XS;
+
+my $LOG = 't/fixtures/light.postgres.log.bz2';
+my $BIN = 'out.bin';
+my $OUT = 'out.json';
+
+my $ret = `perl pgbadger -q -o $BIN $LOG`;
+ok( $? == 0, "Generate intermediate binary file from log");
+
+$ret = `perl pgbadger -q -o $OUT --format binary $BIN`;
+ok( $? == 0, "Generate json report from binary file");
+
+`rm -f $BIN`;
+
+my $json_ref = $json->decode(`cat $OUT`);
+
+#
+# Assert that analyzing json file provide the right results
+#
+ok( $json_ref->{database_info}{postgres}{count} == 629, "Consistent count");
+
+ok( $json_ref->{overall_stat}{histogram}{query_total} == 629, "Consistent query_total");
+
+ok( $json_ref->{overall_stat}{peak}{"2017-09-06 08:48:45"}{write} == 1, "Consistent peak write");
+
+`rm -f $OUT`;
+
+++ /dev/null
-#!/usr/bin/env bats
-# -*- shell-script -*-
-
-@test "Inline help" {
- ./pgbadger --help
-}
-
-@test "Light log report to stdout" {
- ./pgbadger -o - t/fixtures/light.postgres.log.bz2
-}
-
-@test "Light log report to HTML" {
- rm -f out.html
- ./pgbadger --outdir $BATS_TMPDIR t/fixtures/light.postgres.log.bz2
- # Assert out.html is not created in PWD.
- ! test -f out.html
-}
-
-@test "Light log to binary" {
- ./pgbadger -o t/fixtures/light.postgres.bin t/fixtures/light.postgres.log.bz2
-}
-
-@test "From binary to JSON" {
- ./pgbadger --outdir $BATS_TMPDIR -o test-out.json \
- --format binary t/fixtures/light.postgres.bin
- test -f $BATS_TMPDIR/test-out.json
- SELECT=$(jq .database_info.postgres.SELECT < $BATS_TMPDIR/test-out.json)
- test $SELECT -gt 0
-}
+++ /dev/null
-#!/usr/bin/env bats
-# -*- shell-script -*-
-
-setup() {
- IN=t/fixtures/light.postgres.log.bz2
- BIN=$BATS_TMPDIR/out.bin
- OUT=$BATS_TMPDIR/out.json
- OUT_FROM_BIN=$BATS_TMPDIR/out_from_bin.json
- # Generate report from plain-text log
- ./pgbadger $IN -o $OUT
- # Generate intermediate binary file
- ./pgbadger $IN -o $BIN
- # Generate report from binary file
- ./pgbadger $BIN --format=binary -o $OUT_FROM_BIN
-}
-
-#
-# Assert that analyzing from a plain-text log and from the intermediate binary file
-# will provide the exact same results
-#
-
-@test "Consistent count" {
- N=$(jq .database_info.postgres.count < $OUT)
- test $N -eq 629
- N=$(jq .database_info.postgres.count < $OUT_FROM_BIN)
- test $N -eq 629
-}
-
-
-@test "Consistent query_total" {
- N=$(jq .overall_stat.histogram.query_total < $OUT)
- test $N -eq 629
- N=$(jq .overall_stat.histogram.query_total < $OUT_FROM_BIN)
- test $N -eq 629
-}
-
-
-@test "Consistent peak.write" {
- N=$(jq '.overall_stat.peak."2017-09-06 08:49:19".write' < $OUT)
- test $N -eq 1
- N=$(jq '.overall_stat.peak."2017-09-06 08:49:19".write' < $OUT_FROM_BIN)
- test $N -eq 1
-}
+++ /dev/null
-#!/usr/bin/env bats
-
-@test "PERL syntax check" {
- perl -c pgbadger
-}
-
-@test "pod syntax check" {
- podchecker doc/*.pod
-}