check
changeset 9 db2a8a5b8202
parent 8 44b39238d741
equal deleted inserted replaced
8:44b39238d741 9:db2a8a5b8202
     1 #! /usr/bin/perl
       
     2 # © Heiko Schlittermann <hs@schlittermann.de>
       
     3 # global source: https://keller.schlittermann.de/hg/exim-checks
       
     4 # local source :
       
     5 
       
     6 use strict;
       
     7 use warnings;
       
     8 
       
     9 use File::Temp;
       
    10 use File::Basename;
       
    11 use Getopt::Long;
       
    12 use Pod::Usage;
       
    13 use FindBin qw($Bin);
       
    14 
       
    15 my $ME = basename $0;
       
    16 
       
    17 my $opt_init    = 0;
       
    18 my $opt_refresh = 0;
       
    19 my $opt_dir     = \'dirname($opt_config) . "/checks"';
       
    20 
       
    21 my $opt_config = undef;
       
    22 my $opt_exim   = undef;
       
    23 
       
    24 sub slurp { local ($/, @ARGV) = (undef, @_); <> };
       
    25 
       
    26 MAIN: {
       
    27 
       
    28     GetOptions(
       
    29         "i|init"        => \$opt_init,
       
    30         "r|refresh"     => \$opt_refresh,
       
    31         "d|dir=s"       => \$opt_dir,
       
    32         "c|config=s"    => \$opt_config,
       
    33         "exim|binary=s" => \$opt_exim,
       
    34         "h|help"        => sub { pod2usage(-exitval => 0, -verbose => 1) },
       
    35         "m|man"         => sub { pod2usage(-exitval => 0, -verbose => 3) },
       
    36     ) or pod2usage();
       
    37 
       
    38     defined $opt_exim
       
    39       or $opt_exim = (
       
    40         grep { -x }
       
    41           map { ("$_/exim", "$_/exim4") }
       
    42           qw(
       
    43           /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin)
       
    44       )[0]
       
    45       or die "$ME: Can't find the exim(4) binary\n";
       
    46 
       
    47     defined $opt_config
       
    48       or $opt_config = (
       
    49         map { /.*\s(\S+)$/ && $1 } grep /Configuration file is\s+/,
       
    50         qx/$opt_exim -v -bV/
       
    51       )[0]
       
    52       or die "$ME: Can't find exim config\n";
       
    53 
       
    54     $opt_dir = eval $$opt_dir if ref $opt_dir;
       
    55 
       
    56     print "% using $opt_config and dir $opt_dir\n";
       
    57 
       
    58     if ($opt_init or $opt_refresh) {
       
    59         foreach my $address (@ARGV) {
       
    60             if ($opt_init) {
       
    61                 mkdir $_ = "$opt_dir/$address", 0777
       
    62                   or die "Can't mkdir $_: $!\n";
       
    63             }
       
    64             else {
       
    65                 -d ($_ = "$opt_dir/$address") or die "Can't find die $_: $!\n";
       
    66             }
       
    67             foreach my $opt ("v", "t") {
       
    68                 my $file = "$opt_dir/$address/$opt";
       
    69                 system("exim4 -C $opt_config -v -b$opt '$address' >$file");
       
    70             }
       
    71         }
       
    72         exit;
       
    73     }
       
    74 
       
    75     foreach my $dir (
       
    76         @ARGV
       
    77         ? map { /\// ? $_ : "$opt_dir/$_" } @ARGV
       
    78         : glob("$opt_dir/*")
       
    79       )
       
    80     {
       
    81         my $address = basename $dir;
       
    82         print "checking $address:";
       
    83 
       
    84         for my $opt ("v", "t") {
       
    85 
       
    86             print $opt eq "v" ? " verify:" : " test:";
       
    87 
       
    88             my $checkfile = "$dir/$opt";
       
    89             my $expect    = slurp $checkfile;
       
    90             my $result    = qx/exim4 -C $opt_config -v -b$opt '$address'/;
       
    91 
       
    92             foreach ($expect, $result) {
       
    93                 s/^\s+host\s.*//m;
       
    94                 s/^\s+host\s.*//m;
       
    95             }
       
    96 
       
    97             print "ok" and next if $result eq $expect;
       
    98 
       
    99             my $tmp_expect = new File::Temp;
       
   100             my $tmp_result = new File::Temp;
       
   101 
       
   102             $tmp_expect->print($expect);
       
   103             $tmp_result->print($result);
       
   104 
       
   105             print "* DIFFERS *\n";
       
   106 
       
   107             $_ = join " " => "diff", "-u",
       
   108               "--label=got",
       
   109               "--label=expected",
       
   110               $tmp_result->filename,
       
   111               $tmp_expect->filename;
       
   112             chomp;
       
   113 
       
   114             $_ = qx/$_/;
       
   115             s/^/\t/mg;
       
   116             print;
       
   117         }
       
   118 
       
   119         print "\n";
       
   120     }
       
   121 }
       
   122 
       
   123 __END__
       
   124 
       
   125 =head1 NAME
       
   126 
       
   127 check - checks exim routing agains pre-defined values
       
   128 
       
   129 =head1 SYNOPSIS
       
   130 
       
   131     check [options] [<address>...]
       
   132 
       
   133     check [options] -i|--init <address>...
       
   134     check [options] -r|--refresh <address>...
       
   135 
       
   136     check {-h|-m}|{--help|--man}
       
   137 
       
   138 =head1 DESCRIPTION
       
   139 
       
   140 This tools calls exim4 and checks the output of C<exim4 -v -bv <address>> 
       
   141 and C<exim4 -v -bt <address>> against some pre-defined values.
       
   142 
       
   143 =head1 OPTIONS
       
   144 
       
   145 =over 4
       
   146 
       
   147 =item B<-c>|B<--config> I<config_file>
       
   148 
       
   149 The location of the exim config to use. (default: autodetected by C<exim
       
   150 -bV>)
       
   151 
       
   152 =item B<-d>|B<--dir> I<directory>
       
   153 
       
   154 The directory containing the checks (default: C<dirname($config)/checks>)
       
   155 
       
   156 =item B<-i>|B<--init> I<address>
       
   157 
       
   158 Initialize the tests (run them for the first time and remember
       
   159 the results. (default: not used)
       
   160 
       
   161 =item B<-r>|B<--refresh> I<address>
       
   162 
       
   163 Refresh the results (should be used after a verified change).
       
   164 (default: not used)
       
   165 
       
   166 =item B<--exim> I<binary_file>
       
   167 
       
   168 The exim binary to use. This option should be rarely used. (default:
       
   169 autodetected in some standard locations).
       
   170 
       
   171 =back
       
   172 
       
   173 =cut
       
   174 
       
   175 # vim:sts=4 sw=4 aw ai sm: