BGPmon-core-2

 view release on metacpan or  search on metacpan

t/00-bgpmon-log.t  view on Meta::CPAN

ok($ret == 1 || $ret == 0, "Removing any old copies of $log_file");


# try initializing the log for writing to $log_file
my %init_params = ( prog_name => "BGPmon::Log - Testing",
                    log_level => BGPmon::Log::LOG_DEBUG,
                    log_facility => BGPmon::Log::LOG_LOCAL0,
                    log_file => $log_file, 
                    use_syslog => 0,
                    use_gmt => 1 );
$ret = BGPmon::Log::log_init(%init_params);
my $ecode = BGPmon::Log::get_error_code("log_init");
my $emsg = BGPmon::Log::get_error_msg("log_init");
ok($ret == 0, 
  "Initializing log with log file: $log_file, result is $ecode: $emsg"); 

# error codes and messages should work
$ret = BGPmon::Log::get_error_code("log_init");
ok($ret == 0, 
  "Checking get_error_code, result is $ret");
$ret = BGPmon::Log::get_error_msg("log_init");
ok($ret eq BGPmon::Log::NO_ERROR_MSG, 
  "Checking get_error_msg, result is $ret");
$ret = BGPmon::Log::get_error_message("log_init");
ok($ret eq BGPmon::Log::NO_ERROR_MSG, 
  "Checking get_error_messag, result is $ret");

# see if we can open the result log file 
my $log_fh;
$ret = open($log_fh, $log_file);
ok($ret != 0, "Opening log file: $log_file");

# see if we can write a LOG_NOTICE message
my $log_msg = "Testing BGPmon::Log with a LOG_NOTICE message";
$ret = BGPmon::Log::log_notice($log_msg);
$ecode = BGPmon::Log::get_error_code("log_notice");
$emsg = BGPmon::Log::get_error_msg("log_notice");
my $line = <$log_fh>;
ok($ret == 0 && defined($line), 
  "Writing LOG_NOTICE message to $log_file, result is $ecode: $emsg"); 

# see if starts with the correct time,  excluding minutes and seconds
my $timeval = strftime ("%Y-%m-%d %H:", gmtime()); 
ok($line =~ /^$timeval/, "Checking the test message has a valid time stamp");

# see if includes the hostname
my $hostname = Sys::Hostname::hostname;
ok($line =~ /$hostname/, "Checking the test message includes the hostname");

# test it includes the prog_name
ok($line =~ /$init_params{"prog_name"}/, "Checking prog_name parameter");

# check it really is a log_notice message
my $descr = $BGPmon::Log::function_description{"log_notice"};
my $index = index($line, $descr);
ok($index >= 0, "Checking message level is LOG_NOTICE");

# check it really includes our message a log_notice message
ok($line =~ /$log_msg/, "Checking the test message was included in the log");

# line looks valid,  now lets try all the log level functions
my @function_names = ("log_emerg", "log_emergency", "log_alert",
                      "log_fatal", "log_crit", "log_critical", "log_err",  
                      "log_error", "log_warn", "log_warning", "log_notice", 
                      "log_info", "log_debug", "debug" ); 
my $fn;
for my $function (@function_names) {
    # call the function
    $fn = "BGPmon::Log::".$function;
    $ret = &$fn($log_msg);
    $ecode = BGPmon::Log::get_error_code($function);
    $emsg = BGPmon::Log::get_error_msg($function);
    # get the resulting line and check its valid
    $line = <$log_fh>;
    ok($ret == 0 && defined($line), 
      "Writing a $function message to file, result is $ecode: $emsg"); 
}

# check closing the file
$ret = BGPmon::Log::log_close();
$ecode = BGPmon::Log::get_error_code("log_close");
$emsg = BGPmon::Log::get_error_msg("log_close");
ok($ret == 0, 
  "Closing the log with log file $log_file, result is $ecode: $emsg"); 

# change the log_level to LOG_WARN and verify log_level works
$init_params{"log_level"} = BGPmon::Log::LOG_WARNING;
$ret = BGPmon::Log::log_init(%init_params);
$ecode = BGPmon::Log::get_error_code("log_init");
$emsg = BGPmon::Log::get_error_msg("log_init");
ok($ret == 0, 
"Re-opening with log level changed to LOG_WARNING, result is $ecode: $emsg"); 
for my $function (@function_names) {
    # call the function
    $fn = "BGPmon::Log::".$function;
    $ret = &$fn($log_msg);
    $ecode = BGPmon::Log::get_error_code($function);
    $emsg = BGPmon::Log::get_error_msg($function);
    # get the resulting line and check its valid
    $line = <$log_fh>;
    if ($BGPmon::Log::function_level{$function} <= $init_params{"log_level"}) {
        ok($ret == 0 && defined($line), 
          "Veriyfing $function still works, result is $ecode: $emsg"); 
    } else {
        ok($ret == 0 && !defined($line), "Verifying $function is skipped"); 
    }
}
$ret = BGPmon::Log::log_close();
$ecode = BGPmon::Log::get_error_code("log_close");
$emsg = BGPmon::Log::get_error_msg("log_close");
ok($ret == 0, 
  "Closing the log file, result is $ecode: $emsg"); 

# test setting use_gmt to 0
$init_params{"use_gmt"} = 0;
$init_params{"log_level"} = BGPmon::Log::LOG_NOTICE;
$ret = BGPmon::Log::log_init(%init_params);
$ecode = BGPmon::Log::get_error_code("log_init");
$emsg = BGPmon::Log::get_error_msg("log_init");
ok($ret == 0, 
"Re-opening with use_gmt=0 and level LOG_NOTICE, result is $ecode: $emsg"); 



( run in 2.129 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )