Trog-Log-DBI.t 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use Test::More;
  6. use Test::Fatal qw{exception};
  7. use Capture::Tiny qw{capture_stderr};
  8. use File::Temp qw{tempdir};
  9. use FindBin;
  10. use FindBin::libs;
  11. use Trog::SQLite;
  12. require_ok('Trog::Log::DBI') or BAIL_OUT("Can't find SUT");
  13. # The metrics database the way Trog::Log opens it, in a directory of its own.
  14. my $dir = tempdir( CLEANUP => 1 );
  15. my $dbh = Trog::SQLite::dbh( "$FindBin::Bin/../schema/log.schema", "$dir/log.db" );
  16. my $output = Trog::Log::DBI->new( name => 'dbi', min_level => 'info', dbh => $dbh );
  17. my $line = "2026-09-29T12:00:00Z [INFO]: RequestId r-1 From 192.0.2.1 |nobody| GET 200 512 /posts\n";
  18. subtest 'a request line is a row' => sub {
  19. ok( $output->log_message( message => $line ), 'the write reports success' );
  20. my ($count) = $dbh->selectrow_array("SELECT count(*) FROM all_requests WHERE uuid='r-1'");
  21. is( $count, 1, 'and the request is in all_requests' );
  22. };
  23. # The handle raises on every error, and log_message runs inside every log
  24. # call, so a refused write must not take the caller down with it.
  25. subtest 'a write the database refuses is a warning, not a die' => sub {
  26. $dbh->do("CREATE TEMP TRIGGER refuse_request BEFORE INSERT ON requests BEGIN SELECT RAISE(ABORT, 'refused for the test'); END");
  27. my ( $res, $died );
  28. my $stderr = capture_stderr {
  29. $died = exception { $res = $output->log_message( message => $line =~ s/r-1/r-2/r ) };
  30. };
  31. $dbh->do("DROP TRIGGER temp.refuse_request");
  32. is( $died, undef, 'the caller of the log does not die' );
  33. is( $res, undef, 'the write reports that it failed' );
  34. my $why = 'Could not record request r-2 in the metrics database: ';
  35. like( $stderr, qr/\Q$why\E\N*refused[ ]for[ ]the[ ]test/, 'and says why on stderr' );
  36. };
  37. subtest 'a message waits for the line of its request' => sub {
  38. is( $output->log_message( message => "2026-09-29T12:00:01Z [WARN]: RequestId r-3 From 192.0.2.1 |nobody| a thing went sideways\n" ), 1, 'the message is buffered' );
  39. my ($early) = $dbh->selectrow_array("SELECT count(*) FROM messages WHERE uuid='r-3'");
  40. is( $early, 0, 'and not written before its request' );
  41. ok( $output->log_message( message => $line =~ s/r-1/r-3/r ), 'the request line is written' );
  42. my $messages = $dbh->selectcol_arrayref("SELECT message FROM messages WHERE uuid='r-3'");
  43. is_deeply( $messages, ['a thing went sideways'], 'and the message with it' );
  44. };
  45. subtest 'a line that names no request is dropped' => sub {
  46. is( $output->log_message( message => "not a log line at all\n" ), undef, 'text that is not a log line' );
  47. is( $output->log_message( message => $line =~ s/r-1/r|4/r ), undef, 'a request id with a pipe in it' );
  48. is( $output->log_message( message => $line =~ s/192\.0\.2\.1/192.0.2.1|x/r ), undef, 'an address with a pipe in it' );
  49. my ($count) = $dbh->selectrow_array("SELECT count(*) FROM all_requests WHERE uuid LIKE 'r|%' OR ip_address LIKE '%|%'");
  50. is( $count, 0, 'and none of them is a row' );
  51. };
  52. done_testing();