more tests for Qpsmtpd::Config

* fix for cache clearing that didn't work
* imported a sample .cdb file for testing
This commit is contained in:
Matt Simerson 2014-09-16 00:54:47 -07:00
parent 75cb416b62
commit b153c0ab99
4 changed files with 61 additions and 25 deletions

View File

@ -7,6 +7,7 @@ WriteMakefile(
NAME => 'qpsmtpd', NAME => 'qpsmtpd',
VERSION_FROM => 'lib/Qpsmtpd.pm', VERSION_FROM => 'lib/Qpsmtpd.pm',
PREREQ_PM => { PREREQ_PM => {
'CDB_File' => 0,
'Data::Dumper' => 0, 'Data::Dumper' => 0,
'Date::Parse' => 0, 'Date::Parse' => 0,
'File::Temp' => 0, 'File::Temp' => 0,

View File

@ -8,7 +8,7 @@ use lib 'lib';
use parent 'Qpsmtpd::Base'; use parent 'Qpsmtpd::Base';
use Qpsmtpd::Constants; use Qpsmtpd::Constants;
our $_config_cache = {}; our %config_cache = ();
our %dir_memo; our %dir_memo;
our %defaults = ( our %defaults = (
me => hostname, me => hostname,
@ -43,7 +43,7 @@ sub config_dir {
} }
sub clear_cache { sub clear_cache {
$_config_cache = {}; %config_cache = ();
%dir_memo = (); %dir_memo = ();
} }
@ -72,7 +72,8 @@ sub get_qmail_map {
my ($self, $config, $configfile) = @_; my ($self, $config, $configfile) = @_;
if (!-e $configfile . ".cdb") { if (!-e $configfile . ".cdb") {
$_config_cache->{$config} ||= []; $self->log(LOGERROR, "File $configfile.cdb does not exist");
$config_cache{$config} ||= [];
return +{}; return +{};
} }
eval { require CDB_File }; eval { require CDB_File };
@ -99,7 +100,7 @@ sub get_qmail_map {
sub from_file { sub from_file {
my ($self, $configfile, $config, $visited) = @_; my ($self, $configfile, $config, $visited) = @_;
if (!-e $configfile) { if (!-e $configfile) {
$_config_cache->{$config} ||= []; $config_cache{$config} ||= [];
return; return;
} }
@ -145,7 +146,7 @@ sub from_file {
} }
push @{$visited}, $inclusion; push @{$visited}, $inclusion;
for my $inc ($self->expand_inclusion_($inclusion, $configfile)) { for my $inc ($self->expand_inclusion($inclusion, $configfile)) {
my @insertion = $self->from_file($inc, $config, $visited); my @insertion = $self->from_file($inc, $config, $visited);
splice @config, $pos, 0, @insertion; # insert the inclusion splice @config, $pos, 0, @insertion; # insert the inclusion
$pos += @insertion; $pos += @insertion;
@ -156,12 +157,12 @@ sub from_file {
} }
} }
$_config_cache->{$config} = \@config; $config_cache{$config} = \@config;
return wantarray ? @config : $config[0]; return wantarray ? @config : $config[0];
} }
sub expand_inclusion_ { sub expand_inclusion {
my $self = shift; my $self = shift;
my $inclusion = shift; my $inclusion = shift;
my $context = shift; my $context = shift;

BIN
t/config/users.cdb Normal file

Binary file not shown.

View File

@ -4,6 +4,7 @@ use warnings;
use Data::Dumper; use Data::Dumper;
use File::Path; use File::Path;
use Test::More; use Test::More;
use Sys::Hostname;
use lib 'lib'; # test lib/Qpsmtpd (vs site_perl) use lib 'lib'; # test lib/Qpsmtpd (vs site_perl)
use lib 't'; use lib 't';
@ -30,8 +31,12 @@ isa_ok($config, 'Qpsmtpd::Config');
__log(); __log();
__config_dir(); __config_dir();
__clear_cache();
__default();
__from_file(); __from_file();
__get_qmail(); __get_qmail();
__get_qmail_map();
__expand_inclusion();
__config_via_smtpd(); __config_via_smtpd();
foreach my $f (@mes) { unlink $f; } foreach my $f (@mes) { unlink $f; }
@ -52,26 +57,55 @@ sub __log {
is($warned, "$$ test log message\n", 'LOGWARN emitted correct warning'); is($warned, "$$ test log message\n", 'LOGWARN emitted correct warning');
} }
sub __config_dir {
my $dir = $config->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 __clear_cache {
$Qpsmtpd::Config::config_cache{foo} = 2;
$Qpsmtpd::Config::dir_memo{dir1} = 'some/path';
$config->clear_cache();
ok(! $Qpsmtpd::Config::config_cache{foo}, "clear_cache, config_cache")
or diag Data::Dumper::Dumper($Qpsmtpd::Config::config_cache{foo});
ok(! $Qpsmtpd::Config::dir_memo{dir1}, "clear_cache, dir_memo")
};
sub __default {
is($config->default('me'), hostname, "default, my hostname");
is($config->default('timeout'), 1200, "default timeout is 1200");
ok(!$config->default('undefined-test'), "default, undefined");
}
sub __get_qmail { sub __get_qmail {
is($config->get_qmail('me'), 'host.example.org', 'get_qmail("me")'); is($config->get_qmail('me'), 'host.example.org', 'get_qmail("me")');
ok(!$config->get_qmail('not-me'), 'get_qmail("not-me")'); ok(!$config->get_qmail('not-me'), 'get_qmail("not-me")');
} }
sub __get_qmail_map {
eval "require CDB_File"; ## no critic (StringyEval)
if (!$@) {
my $r = $config->get_qmail_map('users', 't/config/users');
ok(keys %$r, 'get_qmail_map("users.cdb")');
ok($r->{'!example.com-'}, "get_qmail_map, known entry");
};
}
sub __from_file { sub __from_file {
my $test_file = 't/config/test_config_file'; my $test_file = 't/config/test_config_file';
my @r = $config->from_file($test_file, 'test_config_file'); my @r = $config->from_file($test_file, 'test_config_file');
ok( @r, "from_file, $test_file"); ok( @r, "from_file, $test_file");
cmp_ok('1st line with content', 'eq', $r[0], "from_file string compare"); cmp_ok('1st line with content', 'eq', $r[0], "from_file string compare");
ok( !$r[1], "from_file"); ok( !$r[1], "from_file");
}; }
sub __config_dir { sub __expand_inclusion {
my $dir = $config->config_dir('logging'); # TODO
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 __config_via_smtpd { sub __config_via_smtpd {