SHARE
TWEET

Untitled

a guest Nov 14th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Subject: [PATCH] Ktest: Added email support
  2.  
  3. Ktest script will send emails when:
  4.     1: the script is started
  5.     2: the script is cancelled by Ctrl-C
  6.     3: the script failed with fatal errors
  7.     4: the script completes all testing
  8.  
  9. Users have to setup the mailer provided in config prior to using this script.
  10.  
  11. Supported mailers: mail, mailx, sendmail
  12.  
  13. ---
  14.  ktest.pl | 38 ++++++++++++++++++++++++++++++++++++++
  15.  1 file changed, 38 insertions(+)
  16.  
  17. diff --git a/ktest.pl b/ktest.pl
  18. index 0c8b61f..ce38212 100755
  19. --- a/ktest.pl
  20. +++ b/ktest.pl
  21. @@ -22,6 +22,8 @@ my %evals;
  22.  
  23.  #default opts
  24.  my %default = (
  25. +    "USR_EMAIL"                        => "",
  26. +    "MAILER"                   => "mailx",  # default mailer
  27.      "NUM_TESTS"                        => 1,
  28.      "TEST_TYPE"                        => "build",
  29.      "BUILD_TYPE"               => "randconfig",
  30. @@ -204,6 +206,8 @@ my $install_time;
  31.  my $reboot_time;
  32.  my $test_time;
  33.  
  34. +my $script_start_time = localtime();
  35. +
  36.  # set when a test is something other that just building or install
  37.  # which would require more options.
  38.  my $buildonly = 1;
  39. @@ -1426,6 +1430,8 @@ sub dodie {
  40.         print " See $opt{LOG_FILE} for more info.\n";
  41.      }
  42.  
  43. +    send_email("KTEST: critical failure", "Your test on $script_start_time has failed.\n");
  44. +    
  45.      if ($monitor_cnt) {
  46.             # restore terminal settings
  47.             system("stty $stty_orig");
  48. @@ -4224,6 +4230,37 @@ sub set_test_option {
  49.      return eval_option($name, $option, $i);
  50.  }
  51.  
  52. +sub _mailx_send {
  53. +    my ($subject, $message) = @_;
  54. +    system("$opt{MAILER} -s \'$subject\' $opt{USR_EMAIL} <<< \'$message\'");
  55. +}
  56. +
  57. +sub _sendmail_send {
  58. +    my ($subject, $message) = @_;
  59. +    system("echo -e \"Subject: $subject\n\n$message\" | sendmail -t $opt{USR_EMAIL}");
  60. +}
  61. +
  62. +sub send_email {
  63. +    if ($opt{USR_EMAIL} ne "" &&
  64. +        $opt{MAILER} ne "")
  65. +    {
  66. +        if ($opt{MAILER} eq "mail" || $opt{MAILER} eq "mailx"){ _mailx_send(@_);}
  67. +        elsif ($opt{MAILER} eq "sendmail" ) { _sendmail_send(@_);}
  68. +        else { doprint "\nYour mailer: $opt{MAILER} is not supported.\n" }
  69. +    }    
  70. +    else
  71. +    {
  72. +        print "No email sent: email or mailer not specified in config."
  73. +    }
  74. +}
  75. +
  76. +$SIG{INT} = sub {
  77. +    send_email("KTEST: Your [$opt{TEST_TYPE}]test was cancelled", "Your test started on $script_start_time was cancelled: sig int");
  78. +    die "\nCaught Sig Int, test interrupted: $!\n"
  79. +};
  80. +
  81. +send_email("KTEST: Your [$opt{TEST_TYPE}]test was started", "Your test was started on $script_start_time");
  82. +
  83.  # First we need to do is the builds
  84.  for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
  85.  
  86. @@ -4429,5 +4466,6 @@ if ($opt{"POWEROFF_ON_SUCCESS"}) {
  87.  
  88.  
  89.  doprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
  90. +send_email("KTEST: Your [$opt{TEST_TYPE}]test has finished!", "$successes of $opt{NUM_TESTS} tests started at $script_start_time were successful!");
  91.  
  92.  exit 0;
  93. --
  94. 1.8.3.1
RAW Paste Data
Top