-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(logger): add first tests to the centreon::common::logger class
these test use test2::V0, but are not set up in the CI for now, this lib is not packaged for debian. Refs:MON-106121
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/perl | ||
use strict; | ||
use warnings; | ||
|
||
use Test2::V0; | ||
use FindBin; | ||
use lib "$FindBin::Bin/../src"; | ||
use centreon::common::logger; | ||
|
||
# Each function test a different aspect of the library (roughtly one public function each). | ||
# To be sure there is no side effect each test create it's own test object | ||
sub test_severity { | ||
my $logger = centreon::common::logger->new(); | ||
is($logger->severity(), "warning", "default severity should be warning."); | ||
|
||
for my $sev ('fatal', 'error', 'warning', 'notice', 'info', 'debug') { | ||
$logger->severity($sev); | ||
is($logger->severity(), $sev, "severity $sev was correctly set."); | ||
} | ||
} | ||
# By default the logger should write to stdout, so we capture stdout to a variable and check what the object have written. | ||
sub test_writeLogInfo { | ||
my $logger = centreon::common::logger->new(); | ||
$logger->flush_output(enabled => 1); | ||
$logger->severity("debug"); | ||
my $out; | ||
my $logExemple = "this is an info log."; | ||
do { | ||
local *STDOUT; | ||
open STDOUT, ">", \$out; | ||
$logger->writeLogInfo($logExemple); | ||
}; | ||
my $log = check_log_format_with_date_no_pid($out, "info"); | ||
|
||
is($log, $logExemple, "log is the same as what we sent."); | ||
|
||
print "written to original STDOUT : $out\n"; | ||
} | ||
|
||
sub main { | ||
test_severity(); | ||
test_writeLogInfo(); | ||
#test_file_mode(); | ||
#test_is_file_mode(); | ||
|
||
|
||
done_testing; | ||
} | ||
# Helper function | ||
sub check_log_format_with_date_no_pid { | ||
# the best way to check the date would be to mock the time() function, and to run get_date(). | ||
# as it is a builtin function, it is possible but hard to setup and prone various side effect. | ||
my $log = shift; | ||
my $severity = shift; | ||
ok(($log =~ /^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] \[$severity\] (.*$)/), "log format is respected."); | ||
ok((defined $1), "the log is not empty"); | ||
return $1; | ||
} | ||
|
||
&main; |