aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Rostedt (VMware) <rostedt@goodmis.org>2018-04-07 20:11:19 -0400
committerSteven Rostedt (VMware) <rostedt@goodmis.org>2018-04-07 20:21:06 -0400
commitc2d84ddb338c829e3ee9d1af6a55325998fcdb82 (patch)
tree8c814f6b3a1e327c7937c4fa1596678c477ec901
parent59f89eb1e3ddccdef9a154dd667facfc15bceab1 (diff)
ktest.pl: Add MAIL_COMMAND option to define how to send email
Allow the user to override the default way to send email. This will allow the user to add their own mailer and format for sending email. Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
-rwxr-xr-xtools/testing/ktest/ktest.pl54
-rw-r--r--tools/testing/ktest/sample.conf12
2 files changed, 46 insertions, 20 deletions
diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index f487f91ccf03..a14fc309d140 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -219,6 +219,7 @@ my $dirname = $FindBin::Bin;
219my $mailto; 219my $mailto;
220my $mailer; 220my $mailer;
221my $mail_path; 221my $mail_path;
222my $mail_command;
222my $email_on_error; 223my $email_on_error;
223my $email_when_finished; 224my $email_when_finished;
224my $email_when_started; 225my $email_when_started;
@@ -254,6 +255,7 @@ my %option_map = (
254 "MAILTO" => \$mailto, 255 "MAILTO" => \$mailto,
255 "MAILER" => \$mailer, 256 "MAILER" => \$mailer,
256 "MAIL_PATH" => \$mail_path, 257 "MAIL_PATH" => \$mail_path,
258 "MAIL_COMMAND" => \$mail_command,
257 "EMAIL_ON_ERROR" => \$email_on_error, 259 "EMAIL_ON_ERROR" => \$email_on_error,
258 "EMAIL_WHEN_FINISHED" => \$email_when_finished, 260 "EMAIL_WHEN_FINISHED" => \$email_when_finished,
259 "EMAIL_WHEN_STARTED" => \$email_when_started, 261 "EMAIL_WHEN_STARTED" => \$email_when_started,
@@ -4133,16 +4135,6 @@ sub set_test_option {
4133 return eval_option($name, $option, $i); 4135 return eval_option($name, $option, $i);
4134} 4136}
4135 4137
4136sub _mailx_send {
4137 my ($subject, $message) = @_;
4138 run_command "$mail_path/$mailer -s \'$subject\' $mailto <<< \'$message\'";
4139}
4140
4141sub _sendmail_send {
4142 my ($subject, $message) = @_;
4143 run_command "echo \'Subject: $subject\n\n$message\' | $mail_path/sendmail -t $mailto";
4144}
4145
4146sub find_mailer { 4138sub find_mailer {
4147 my ($mailer) = @_; 4139 my ($mailer) = @_;
4148 4140
@@ -4160,22 +4152,44 @@ sub find_mailer {
4160 return undef; 4152 return undef;
4161} 4153}
4162 4154
4155sub do_send_mail {
4156 my ($subject, $message) = @_;
4157
4158 if (!defined($mail_path)) {
4159 # find the mailer
4160 $mail_path = find_mailer $mailer;
4161 if (!defined($mail_path)) {
4162 die "\nCan not find $mailer in PATH\n";
4163 }
4164 }
4165
4166 if (!defined($mail_command)) {
4167 if ($mailer eq "mail" || $mailer eq "mailx") {
4168 $mail_command = "\$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO <<< \'\$MESSAGE\'";
4169 } elsif ($mailer eq "sendmail" ) {
4170 $mail_command = "echo \'Subject: \$SUBJECT\n\n\$MESSAGE\' | \$MAIL_PATH/\$MAILER -t \$MAILTO";
4171 } else {
4172 die "\nYour mailer: $mailer is not supported.\n";
4173 }
4174 }
4175
4176 $mail_command =~ s/\$MAILER/$mailer/g;
4177 $mail_command =~ s/\$MAIL_PATH/$mail_path/g;
4178 $mail_command =~ s/\$MAILTO/$mailto/g;
4179 $mail_command =~ s/\$SUBJECT/$subject/g;
4180 $mail_command =~ s/\$MESSAGE/$message/g;
4181
4182 run_command $mail_command;
4183}
4184
4163sub send_email { 4185sub send_email {
4186
4164 if (defined($mailto)) { 4187 if (defined($mailto)) {
4165 if (!defined($mailer)) { 4188 if (!defined($mailer)) {
4166 doprint "No email sent: email or mailer not specified in config.\n"; 4189 doprint "No email sent: email or mailer not specified in config.\n";
4167 return; 4190 return;
4168 } 4191 }
4169 if (!defined($mail_path)) { 4192 do_send_mail @_;
4170 # find the mailer
4171 $mail_path = find_mailer $mailer;
4172 if (!defined($mail_path)) {
4173 die "\nCan not find $mailer in PATH\n";
4174 }
4175 }
4176 if ($mailer eq "mail" || $mailer eq "mailx"){ _mailx_send(@_);}
4177 elsif ($mailer eq "sendmail" ) { _sendmail_send(@_);}
4178 else { die "\nYour mailer: $mailer is not supported.\n" }
4179 } 4193 }
4180} 4194}
4181 4195
diff --git a/tools/testing/ktest/sample.conf b/tools/testing/ktest/sample.conf
index 86e7cffc45c0..6ca6ca0ce695 100644
--- a/tools/testing/ktest/sample.conf
+++ b/tools/testing/ktest/sample.conf
@@ -415,6 +415,18 @@
415# (default: for sendmail "/usr/sbin/sendmail", otherwise equals ${MAILER}) 415# (default: for sendmail "/usr/sbin/sendmail", otherwise equals ${MAILER})
416#MAIL_EXEC = /usr/sbin/sendmail 416#MAIL_EXEC = /usr/sbin/sendmail
417# 417#
418# The command used to send mail, which uses the above options
419# can be modified. By default if the mailer is "sendmail" then
420# MAIL_COMMAND = echo \'Subject: $SUBJECT\n\n$MESSAGE\' | $MAIL_PATH/$MAILER -t $MAILTO
421# For mail or mailx:
422# MAIL_COMMAND = "$MAIL_PATH/$MAILER -s \'$SUBJECT\' $MAILTO <<< \'$MESSAGE\'
423# ktest.pl will do the substitution for MAIL_PATH, MAILER, MAILTO at the time
424# it sends the mail if "$FOO" format is used. If "${FOO}" format is used,
425# then the substitutions will occur at the time the config file is read.
426# But note, MAIL_PATH and MAILER require being set by the config file if
427# ${MAIL_PATH} or ${MAILER} are used, but not if $MAIL_PATH or $MAILER are.
428#MAIL_COMMAND = echo \'Subject: $SUBJECT\n\n$MESSAGE\' | $MAIL_PATH/$MAILER -t $MAILTO
429#
418# Errors are defined as those would terminate the script 430# Errors are defined as those would terminate the script
419# (default 1) 431# (default 1)
420#EMAIL_ON_ERROR = 1 432#EMAIL_ON_ERROR = 1