Skip to contentSkip to conversation

Ktest: Add email support

RFC PATCH In review Tim Tianyang Chen 2 participants 3 messages Nov 21, 2017 – Dec 15, 2017 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

RFC PATCH 1/2 Ktest: Add email support

Users can define optional "MAILER" "USR_EMAIL" variables to get email notifications. Ktest will send emails when the script:
* was started
* was cancelled by Ctrl-C
* failed with fatal errors and called dodie()
* completed all testing

Users have to setup the mailer provided in config prior to using this script. Supported mailers: mailx, mail, sendmail
mailer specific routines are _sendmail_send(), _mailx_send()

---
 ktest.pl | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
ktest.pl +41 -0
index 0c8b61f..2e38647 100755
@@ -22,6 +22,8 @@ my %evals;
 
 #default opts
 my %default = (
+    "USR_EMAIL"			=> "",
+    "MAILER"			=> "mailx",  # default mailer
     "NUM_TESTS"			=> 1,
     "TEST_TYPE"			=> "build",
     "BUILD_TYPE"		=> "randconfig",
@@ -204,6 +206,8 @@ my $install_time;
 my $reboot_time;
 my $test_time;
 
+my $script_start_time = localtime();
+
 # set when a test is something other that just building or install
 # which would require more options.
 my $buildonly = 1;
@@ -1426,6 +1430,9 @@ sub dodie {
 	print " See $opt{LOG_FILE} for more info.\n";
     }
 
+    send_email("KTEST: critical failure for your [$opt{TEST_TYPE}] test",
+                "Your test started at $script_start_time has failed with:\n@_\n");
+
     if ($monitor_cnt) {
 	    # restore terminal settings
 	    system("stty $stty_orig");
@@ -4224,6 +4231,38 @@ sub set_test_option {
     return eval_option($name, $option, $i);
 }
 
+sub _mailx_send {
+    my ($subject, $message) = @_;
+    system("$opt{MAILER} -s \'$subject\' $opt{USR_EMAIL} <<< \'$message\'");
+}
+
+sub _sendmail_send {
+    my ($subject, $message) = @_;
+    system("echo -e \"Subject: $subject\n\n$message\" | sendmail -t $opt{USR_EMAIL}");
+}
+
+sub send_email {
+    if ($opt{USR_EMAIL} ne "" && $opt{MAILER} ne "")
+    {
+        if ($opt{MAILER} eq "mail" || $opt{MAILER} eq "mailx"){ _mailx_send(@_);}
+        elsif ($opt{MAILER} eq "sendmail" ) { _sendmail_send(@_);}
+        else { doprint "\nYour mailer: $opt{MAILER} is not supported.\n" }
+    }
+    else
+    {
+        print "No email sent: email or mailer not specified in config.\n"
+    }
+}
+
+$SIG{INT} = sub {
+    send_email("KTEST: Your [$opt{TEST_TYPE}] test was cancelled",
+                "Your test started at $script_start_time was cancelled: sig int");
+    die "\nCaught Sig Int, test interrupted: $!\n"
+};
+
+send_email("KTEST: Your [$opt{TEST_TYPE}] test was started",
+            "Your test was started on $script_start_time");
+
 # First we need to do is the builds
 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
 
@@ -4429,5 +4468,7 @@ if ($opt{"POWEROFF_ON_SUCCESS"}) {
 
 
 doprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
+send_email("KTEST: Your [$opt{TEST_TYPE}] test has finished!",
+            "$successes of $opt{NUM_TESTS} tests started at $script_start_time were successful!");
 
 exit 0;
Signature · 1 line
1.8.3.1
#2 lore raw

On Tue, 21 Nov 2017 10:53:32 -0800
Tim Tianyang Chen <[email protected]> wrote:

Hi! Sorry for taking so long, but I've been swamped in other aspects. I'm trying to push maintainership off to John, but he's busy too :-p

First comment is that the numbered patches need to be a reply to the cover patch (that is patches 1/2 and 2/2 need to be replies of 0/2). Otherwise, my mailer loses them, because I always have my email sorted by threads, and a reply to 0/2 will move it away from the other patches if the other patches are not replies.

Quoted 1 line

Users can define optional "MAILER" "USR_EMAIL" variables to get email notifications.

Use "MAILTO" for the variable for email. That's the common name in other tools.

Quoted 3 lines

Ktest will send emails when the script:
* was started
* was cancelled by Ctrl-C

Hmm, I'm thinking it doesn't get sent when we hit Ctrl-C. Or at last have these as options to when to send them.

I think by default, it should only send when finished, or failed. But have options like:

EMAIL_WHEN_STARTED, EMAIL_WHEN_CANCELED (both default false) EMAIL_WHEN_FINIHSED and EMAIL_ON_ERROR (both default true).

Quoted 18 lines

* failed with fatal errors and called dodie()
* completed all testing

Users have to setup the mailer provided in config prior to using this script. Supported mailers: mailx, mail, sendmail
mailer specific routines are _sendmail_send(), _mailx_send()

---
  ktest.pl | 41 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 41 insertions(+)
ktest.pl +0 -0
index 0c8b61f..2e38647 100755

ktest.pl my %evals; · line 26

Quoted 5 lines
ktest.pl +2 -0
@@ -22,6 +22,8 @@ my %evals;
 
  #default opts
  my %default = (
+    "USR_EMAIL"			=> "",
+    "MAILER"			=> "mailx",  # default mailer

Other tools (namely quilt) use sendmail (and I always make sure that is functional on my system), please default to that.

	"EMAIL_ON_ERROR"		=> 1,
	"EMAIL_WHEN_FINISHED"		=> 1,
	"EMAIL_WHEN_CANCELED"		=> 0,
	"EMAIL_WHEN_STARTED"		=> 0,
Quoted 13 lines
ktest.pl +2 -0
      "NUM_TESTS"			=> 1,
      "TEST_TYPE"			=> "build",
      "BUILD_TYPE"		=> "randconfig",
@@ -204,6 +206,8 @@ my $install_time;
  my $reboot_time;
  my $test_time;
 
+my $script_start_time = localtime();
+
  # set when a test is something other that just building or install
  # which would require more options.
  my $buildonly = 1;

ktest.pl sub dodie { · line 1434

Quoted 5 lines
ktest.pl +2 -0
@@ -1426,6 +1430,9 @@ sub dodie {
  	print " See $opt{LOG_FILE} for more info.\n";
      }
 
+    send_email("KTEST: critical failure for your [$opt{TEST_TYPE}] test",
+                "Your test started at $script_start_time has failed with:\n@_\n")
		if ($email_on_error);
Quoted 10 lines
ktest.pl +3 -0
+
      if ($monitor_cnt) {
  	    # restore terminal settings
  	    system("stty $stty_orig");
@@ -4224,6 +4231,38 @@ sub set_test_option {
      return eval_option($name, $option, $i);
  }
 
+sub _mailx_send {
+    my ($subject, $message) = @_;

ktest.pl sub set_test_option { · line 4245

Quoted 10 lines
ktest.pl +10 -0
+    system("$opt{MAILER} -s \'$subject\' $opt{USR_EMAIL} <<< \'$message\'");
+}
+
+sub _sendmail_send {
+    my ($subject, $message) = @_;
+    system("echo -e \"Subject: $subject\n\n$message\" | sendmail -t $opt{USR_EMAIL}");
+}
+
+sub send_email {
+    if ($opt{USR_EMAIL} ne "" && $opt{MAILER} ne "")

Add these to the option map:

[..]

my $mailto;
my $mailer;
my $email_on_error;
my $email_when_finished;
my $email_when_started;
my $email_when_canceled;

[..]

my %option_map {
[..]

	"MAILTO"		=> \$mailto,
	"MAILER"		=> \$mailer,
	"EMAIL_ON_ERROR"	=> \$email_on_error,
	"EMAIL_WHEN_FINISHED"	=> \$email_when_finished,
	"EMAIL_WHEN_STARTED"	=> \$email_when_started,
	"EMAIL_WHEN_CANCELED"	=> \$email_when_canceled,

Then you can do:

	if ($mailto ne "" && $mailer ne "")

And use these variables through out, it makes it much easier to read.

Also, all variables also need to be commented in the samples.conf file.

Quoted 4 lines
ktest.pl +4 -0
+    {
+        if ($opt{MAILER} eq "mail" || $opt{MAILER} eq "mailx"){ _mailx_send(@_);}
+        elsif ($opt{MAILER} eq "sendmail" ) { _sendmail_send(@_);}
+        else { doprint "\nYour mailer: $opt{MAILER} is not supported.\n" }

ktest.pl sub set_test_option {

Quoted 10 lines
ktest.pl +10 -0
+    }
+    else
+    {
+        print "No email sent: email or mailer not specified in config.\n"
+    }
+}
+
+$SIG{INT} = sub {
+    send_email("KTEST: Your [$opt{TEST_TYPE}] test was cancelled",
+                "Your test started at $script_start_time was cancelled: sig int")
		if ($email_when_canceled);

ktest.pl sub set_test_option {

Quoted 5 lines
ktest.pl +5 -0
+    die "\nCaught Sig Int, test interrupted: $!\n"
+};
+
+send_email("KTEST: Your [$opt{TEST_TYPE}] test was started",
+            "Your test was started on $script_start_time")
		if ($email_when_started);

ktest.pl if ($opt{"POWEROFF_ON_SUCCESS"}) { · line 4472

Quoted 10 lines
ktest.pl +3 -0
+
  # First we need to do is the builds
  for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
 
@@ -4429,5 +4468,7 @@ if ($opt{"POWEROFF_ON_SUCCESS"}) {
 
 
  doprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
+send_email("KTEST: Your [$opt{TEST_TYPE}] test has finished!",
+            "$successes of $opt{NUM_TESTS} tests started at $script_start_time were successful!")
		if ($email_when_finished);

Thanks for doing this!

-- Steve

Quoted 3 lines
ktest.pl +0 -0
  exit 0;
#3 lore raw

Thanks for the review Steven!

Quoted 5 lines

First comment is that the numbered patches need to be a reply to the cover patch (that is patches 1/2 and 2/2 need to be replies of 0/2). Otherwise, my mailer loses them, because I always have my email sorted by threads, and a reply to 0/2 will move it away from the other patches if the other patches are not replies.

Not sure what happened. I used --thread option for git send-email, and in thunderbird I can see all the patches in one thread replying to 0/2.

Quoted 8 lines

Hmm, I'm thinking it doesn't get sent when we hit Ctrl-C. Or at last have these as options to when to send them.

I think by default, it should only send when finished, or failed. But have options like:

EMAIL_WHEN_STARTED, EMAIL_WHEN_CANCELED (both default false) EMAIL_WHEN_FINIHSED and EMAIL_ON_ERROR (both default true).

Yeah. It's a good idea to add them as options.

Thanks,
Tim