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