From 7d33c42d35bbd0415bf292438e8c6825e9ec14fc Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 17 Dec 2014 18:37:21 -0600 Subject: [PATCH 1/6] Less obtrusive addition of test hooks --- t/Test/Qpsmtpd/Plugin.pm | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/t/Test/Qpsmtpd/Plugin.pm b/t/Test/Qpsmtpd/Plugin.pm index 6693824..2ab5cdb 100644 --- a/t/Test/Qpsmtpd/Plugin.pm +++ b/t/Test/Qpsmtpd/Plugin.pm @@ -87,7 +87,7 @@ sub validate_password { sub fake_config { my $self = shift; my $fake_config = {@_}; - $self->qp->hooks->{config} = [ + unshift @{ $self->qp->hooks->{config} ||= [] }, { name => '___FakeHook___', code => sub { @@ -95,8 +95,7 @@ sub fake_config { return DECLINED if ! exists $fake_config->{$conf}; return OK, $fake_config->{$conf}; }, - }, - ]; + }; } sub unfake_config { From 46d26848a9dc14ea7490aa22c91a96b71354811e Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 17 Dec 2014 18:43:17 -0600 Subject: [PATCH 2/6] Generalized method for test hooks --- t/Test/Qpsmtpd/Plugin.pm | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/t/Test/Qpsmtpd/Plugin.pm b/t/Test/Qpsmtpd/Plugin.pm index 2ab5cdb..265e00b 100644 --- a/t/Test/Qpsmtpd/Plugin.pm +++ b/t/Test/Qpsmtpd/Plugin.pm @@ -84,26 +84,37 @@ sub validate_password { return $deny, "$file - wrong password"; } +sub fake_hook { + my ( $self, $hook, $sub ) = @_; + unshift @{ $self->qp->hooks->{$hook} ||= [] }, + { + name => '___FakeHook___', + code => $sub, + }; +} + +sub unfake_hook { + my ( $self, $hook ) = @_; + $self->qp->hooks->{$hook} = [ + grep { $_->{name} ne '___FakeHook___' } + @{ $self->qp->hooks->{$hook} || [] } + ]; +} + sub fake_config { my $self = shift; my $fake_config = {@_}; - unshift @{ $self->qp->hooks->{config} ||= [] }, - { - name => '___FakeHook___', - code => sub { - my ( $self, $txn, $conf ) = @_; - return DECLINED if ! exists $fake_config->{$conf}; - return OK, $fake_config->{$conf}; - }, - }; + $self->fake_hook( 'config', + sub { + my ( $self, $txn, $conf ) = @_; + return DECLINED if ! exists $fake_config->{$conf}; + return OK, $fake_config->{$conf}; + } ); } sub unfake_config { my ( $self ) = @_; - $self->qp->hooks->{config} = [ - grep { $_->{name} ne '___FakeHook___' } - @{ $self->qp->hooks->{config} || [] } - ]; + $self->unfake_hook('config'); } 1; From 7f357af1c2184b6887329385644b5bbee3c59cf7 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 17 Dec 2014 21:25:12 -0600 Subject: [PATCH 3/6] Move hook testing stuff to Test::Qpsmtpd This makes it more available to plugins outside of t/plugin_tests/ --- t/Test/Qpsmtpd.pm | 33 +++++++++++++++++++++++++++++++++ t/Test/Qpsmtpd/Plugin.pm | 36 ++++-------------------------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/t/Test/Qpsmtpd.pm b/t/Test/Qpsmtpd.pm index c24cbe1..7005cb8 100644 --- a/t/Test/Qpsmtpd.pm +++ b/t/Test/Qpsmtpd.pm @@ -118,4 +118,37 @@ sub run_plugin_tests { $Test->done_testing(); } +sub fake_hook { + my ( $self, $hook, $sub ) = @_; + unshift @{ $self->hooks->{$hook} ||= [] }, + { + name => '___FakeHook___', + code => $sub, + }; +} + +sub unfake_hook { + my ( $self, $hook ) = @_; + $self->hooks->{$hook} = [ + grep { $_->{name} ne '___FakeHook___' } + @{ $self->hooks->{$hook} || [] } + ]; +} + +sub fake_config { + my $self = shift; + my $fake_config = {@_}; + $self->fake_hook( 'config', + sub { + my ( $self, $txn, $conf ) = @_; + return DECLINED if ! exists $fake_config->{$conf}; + return OK, $fake_config->{$conf}; + } ); +} + +sub unfake_config { + my ( $self ) = @_; + $self->unfake_hook('config'); +} + 1; diff --git a/t/Test/Qpsmtpd/Plugin.pm b/t/Test/Qpsmtpd/Plugin.pm index 265e00b..59961d4 100644 --- a/t/Test/Qpsmtpd/Plugin.pm +++ b/t/Test/Qpsmtpd/Plugin.pm @@ -84,37 +84,9 @@ sub validate_password { return $deny, "$file - wrong password"; } -sub fake_hook { - my ( $self, $hook, $sub ) = @_; - unshift @{ $self->qp->hooks->{$hook} ||= [] }, - { - name => '___FakeHook___', - code => $sub, - }; -} - -sub unfake_hook { - my ( $self, $hook ) = @_; - $self->qp->hooks->{$hook} = [ - grep { $_->{name} ne '___FakeHook___' } - @{ $self->qp->hooks->{$hook} || [] } - ]; -} - -sub fake_config { - my $self = shift; - my $fake_config = {@_}; - $self->fake_hook( 'config', - sub { - my ( $self, $txn, $conf ) = @_; - return DECLINED if ! exists $fake_config->{$conf}; - return OK, $fake_config->{$conf}; - } ); -} - -sub unfake_config { - my ( $self ) = @_; - $self->unfake_hook('config'); -} +sub fake_hook { shift->qp->fake_hook(@_) } +sub unfake_hook { shift->qp->unfake_hook(@_) } +sub fake_config { shift->qp->fake_config(@_) } +sub unfake_config { shift->qp->unfake_config(@_) } 1; From 0204fb0f189301dd5cb6100f585a89f2572cfc3a Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Thu, 18 Dec 2014 14:01:32 -0600 Subject: [PATCH 4/6] Add some comments on uses for fake_{hook,config} --- t/Test/Qpsmtpd.pm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/Test/Qpsmtpd.pm b/t/Test/Qpsmtpd.pm index 7005cb8..8a1958e 100644 --- a/t/Test/Qpsmtpd.pm +++ b/t/Test/Qpsmtpd.pm @@ -119,6 +119,8 @@ sub run_plugin_tests { } sub fake_hook { + # Used to test core code against different potential plugins + # it will interact with my ( $self, $hook, $sub ) = @_; unshift @{ $self->hooks->{$hook} ||= [] }, { @@ -136,6 +138,7 @@ sub unfake_hook { } sub fake_config { + # Used to test code against various possible configurations my $self = shift; my $fake_config = {@_}; $self->fake_hook( 'config', From a5f786c98612166f02adb31a1fbda27224e612e9 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Thu, 18 Dec 2014 14:16:23 -0600 Subject: [PATCH 5/6] Take advantage of fake_hook() in existing tests --- t/qpsmtpd-address.t | 15 +++++---------- t/qpsmtpd.t | 20 +++++++------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/t/qpsmtpd-address.t b/t/qpsmtpd-address.t index 94fa11c..ca056c3 100644 --- a/t/qpsmtpd-address.t +++ b/t/qpsmtpd-address.t @@ -152,13 +152,13 @@ sub __config { my @test_data = ( { pref => 'size_threshold', - result => [], + result => undef, expected => 10000, descr => 'fall back to global config when user_config is absent', }, { pref => 'test_config', - result => [], + result => undef, expected => undef, descr => 'return nothing when no user_config plugins exist', }, @@ -176,16 +176,11 @@ sub __config { }, ); for (@test_data) { - $qp->hooks->{user_config} = @{$_->{result}} - ? [ - { - name => 'test hook', - code => sub { return @{$_->{result}} } - } - ] - : undef; + $qp->fake_hook( 'user_config', sub { return @{$_->{result}} } ) + if $_->{result}; is($sender->config($_->{pref}), $_->{expected}, $_->{descr}); } + $qp->unfake_hook('user_config'); } sub __canonify { diff --git a/t/qpsmtpd.t b/t/qpsmtpd.t index 4abf17c..b2e0ba1 100644 --- a/t/qpsmtpd.t +++ b/t/qpsmtpd.t @@ -258,8 +258,8 @@ sub __config { { pref => 'size_threshold', hooks => { - user_config => [], - config => [], + user_config => undef, + config => undef, }, expected => { user => 10000, @@ -270,8 +270,8 @@ sub __config { { pref => 'timeout', hooks => { - user_config => [], - config => [], + user_config => undef, + config => undef, }, expected => { user => 1200, @@ -329,15 +329,8 @@ sub __config { }, ); for my $t (@test_data) { - for my $hook (qw( config user_config )) { - $qp->hooks->{$hook} = @{$t->{hooks}{$hook}} - ? [ - { - name => 'test hook', - code => sub { return @{$t->{hooks}{$hook}} } - } - ] - : undef; + for my $hook ( grep { $t->{hooks}{$_} } qw( config user_config ) ) { + $qp->fake_hook( $hook, sub { return @{ $t->{hooks}{$hook} } } ); } is( $qp->config($t->{pref}, $a), @@ -348,6 +341,7 @@ sub __config { $t->{expected}{global}, "Global config: $t->{descr}"); } + $qp->unfake_hook($_) for qw( config user_config ); } sub __warn_level { From a7fee58aade5c03acfe0632525991beb5809e30a Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 24 Dec 2014 18:06:22 -0600 Subject: [PATCH 6/6] More explicit explanations --- t/Test/Qpsmtpd.pm | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/t/Test/Qpsmtpd.pm b/t/Test/Qpsmtpd.pm index 8a1958e..9ec569d 100644 --- a/t/Test/Qpsmtpd.pm +++ b/t/Test/Qpsmtpd.pm @@ -119,8 +119,22 @@ sub run_plugin_tests { } sub fake_hook { - # Used to test core code against different potential plugins - # it will interact with + ########################################################################### + # Inserts a given subroutine into the beginning of the set of hooks already + # in place. Used to test code against different potential plugins it will + # interact with. For example, to test behavior against various results of + # the data_post hook: + # + # $self->fake_hook('data_post',sub { return DECLINED }; + # ok(...); + # $self->fake_hook('data_post',sub { return DENYSOFT }; + # ok(...); + # $self->fake_hook('data_post',sub { return DENY }; + # ok(...); + # $self->fake_hook('data_post',sub { return DENY_DISCONNECT }; + # ok(...); + # $self->unfake_hook('data_post'); + ########################################################################### my ( $self, $hook, $sub ) = @_; unshift @{ $self->hooks->{$hook} ||= [] }, { @@ -138,7 +152,18 @@ sub unfake_hook { } sub fake_config { + #################################################################### # Used to test code against various possible configurations + # For example, to test against various possible config('me') values: + # + # $self->fake_config( me => '***invalid***' ); + # ok(...); + # $self->fake_config( me => 'valid-nonfqdn' ); + # ok(...); + # $self->fake_config( me => 'valid-fqdn.com'); + # ok(...); + # $self->unfake_config(); + #################################################################### my $self = shift; my $fake_config = {@_}; $self->fake_hook( 'config',