added tests for lib/Qpsmtpd.pm (Issue #67)

This commit is contained in:
Matt Simerson 2014-09-08 22:58:27 -07:00
parent 08ed943dcf
commit c9a780576f
2 changed files with 58 additions and 15 deletions

View File

@ -1,13 +1,14 @@
package Qpsmtpd; package Qpsmtpd;
use strict; use strict;
use vars qw($VERSION $TraceLevel $Spool_dir $Size_threshold); #use warnings;
use vars qw($TraceLevel $Spool_dir $Size_threshold);
use Sys::Hostname; use Sys::Hostname;
use Qpsmtpd::Constants; use Qpsmtpd::Constants;
#use DashProfiler; #use DashProfiler;
$VERSION = "0.94"; our $VERSION = "0.94";
my $git; my $git;
@ -17,17 +18,17 @@ if (-e ".git") {
$git && chomp $git; $git && chomp $git;
} }
my $hooks = {}; our $hooks = {};
my %defaults = ( my %defaults = (
me => hostname, me => hostname,
timeout => 1200, timeout => 1200,
); );
my $_config_cache = {}; my $_config_cache = {};
my %config_dir_memo; our %config_dir_memo;
#DashProfiler->add_profile("qpsmtpd"); #DashProfiler->add_profile("qpsmtpd");
#my $SAMPLER = DashProfiler->prepare("qpsmtpd"); #my $SAMPLER = DashProfiler->prepare("qpsmtpd");
my $LOGGING_LOADED = 0; our $LOGGING_LOADED = 0;
sub _restart { sub _restart {
my $self = shift; my $self = shift;
@ -57,12 +58,11 @@ sub TRACE_LEVEL { $TraceLevel }; # leave for plugin compatibility
sub hooks { $hooks; } sub hooks { $hooks; }
sub load_logging { sub load_logging {
# need to do this differently than other plugins so as to
# not trigger logging activity
return if $LOGGING_LOADED;
my $self = shift; my $self = shift;
return if $hooks->{"logging"};
# avoid triggering log activity
return if ($LOGGING_LOADED || $hooks->{'logging'});
my $configdir = $self->config_dir("logging"); my $configdir = $self->config_dir("logging");
my $configfile = "$configdir/logging"; my $configfile = "$configdir/logging";
my @loggers = $self->_config_from_file($configfile, 'logging'); my @loggers = $self->_config_from_file($configfile, 'logging');
@ -97,10 +97,7 @@ sub load_logging {
return @loggers; return @loggers;
} }
sub trace_level { sub trace_level { return $TraceLevel; }
my $self = shift;
return $TraceLevel;
}
sub init_logger { # needed for compatibility purposes sub init_logger { # needed for compatibility purposes
shift->trace_level(); shift->trace_level();
@ -205,7 +202,7 @@ sub config_dir {
} }
my $configdir = ($ENV{QMAIL} || '/var/qmail') . '/control'; my $configdir = ($ENV{QMAIL} || '/var/qmail') . '/control';
my ($path) = ($ENV{PROCESS} ? $ENV{PROCESS} : $0) =~ m!(.*?)/([^/]+)$!; my ($path) = ($ENV{PROCESS} ? $ENV{PROCESS} : $0) =~ m!(.*?)/([^/]+)$!;
$configdir = "$path/config" if (-e "$path/config/$config"); $configdir = "$path/config" if -e "$path/config/$config";
if (exists $ENV{QPSMTPD_CONFIG}) { if (exists $ENV{QPSMTPD_CONFIG}) {
$ENV{QPSMTPD_CONFIG} =~ /^(.*)$/; # detaint $ENV{QPSMTPD_CONFIG} =~ /^(.*)$/; # detaint
$configdir = $1 if -e "$1/$config"; $configdir = $1 if -e "$1/$config";

46
t/qpsmtpd.t Normal file
View File

@ -0,0 +1,46 @@
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Test::More;
use lib 'lib'; # test lib/Qpsmtpd (vs site_perl)
BEGIN { use_ok('Qpsmtpd'); }
BEGIN { use_ok('Qpsmtpd::Constants'); }
my $package = 'Qpsmtpd';
my $qp = bless {}, $package;
ok( $qp->version(), "version, " . $qp->version());
is_deeply( Qpsmtpd::hooks(), {}, 'hooks, empty');
__config_dir();
__log();
__load_logging();
done_testing();
sub __log {
ok( $qp->log(LOGWARN, "test log message"), 'log');
}
sub __config_dir {
my $dir = $qp->config_dir('logging');
ok( $dir, "config_dir, $dir");
#warn Data::Dumper::Dumper($Qpsmtpd::config_dir_memo{logging});
$dir = $Qpsmtpd::config_dir_memo{logging};
ok( $dir, "config_dir, $dir (memo)");
};
sub __load_logging {
$Qpsmtpd::LOGGING_LOADED = 1;
ok( !$qp->load_logging(), "load_logging, loaded");
$Qpsmtpd::LOGGING_LOADED = 0;
$Qpsmtpd::hooks->{logging} = 1;
ok( !$qp->load_logging(), "load_logging, logging hook");
$Qpsmtpd::hooks->{logging} = undef; # restore
}