]> git.proxmox.com Git - pve-manager.git/commitdiff
followup: cleanup/refactor mail test
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 19 Nov 2019 14:01:12 +0000 (15:01 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 19 Nov 2019 14:02:50 +0000 (15:02 +0100)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
test/mail_test.pl

index 58bfaafae717fa1c1355c009a4d511825a27a8cd..6c32f0fc12dc1b7217cab0385989151e8893283e 100755 (executable)
@@ -2,15 +2,20 @@
 
 use strict;
 use warnings;
-use PVE::VZDump;
+
+use lib '..';
+
 use Test::More tests => 5;
 use Test::MockModule;
 
+use PVE::VZDump;
+
 my $STATUS = qr/.*status.*/;
 my $NO_LOGFILE = qr/.*Could not open log file.*/;
 my $LOG_TOO_LONG = qr/.*Log output was too long.*/;
 my $TEST_FILE_PATH       = '/tmp/mail_test';
 my $TEST_FILE_WRONG_PATH = '/tmp/mail_test_wrong';
+
 sub prepare_mail_with_status {
     open(TEST_FILE, '>', $TEST_FILE_PATH); # Removes previous content
     print TEST_FILE "start of log file\n";
@@ -20,56 +25,54 @@ sub prepare_mail_with_status {
     print TEST_FILE "end of log file\n";
     close(TEST_FILE);
 }
+
 sub prepare_long_mail {
     open(TEST_FILE, '>', $TEST_FILE_PATH); # Removes previous content
     # 0.5 MB * 2 parts + the overview tables gives more than 1 MB mail
     print TEST_FILE "a" x (1024*1024/2);
     close(TEST_FILE);
 }
+
+my ($result_text, $result_html);
+
+my $mock_module = Test::MockModule->new('PVE::Tools');
+$mock_module->mock('sendmail', sub {
+    my (undef, undef, $text, $html, undef, undef) = @_;
+    $result_text = $text;
+    $result_html = $html;
+});
+
+my $MAILTO = ['test_address@proxmox.com'];
+my $SELF = {
+    opts => { mailto => $MAILTO },
+    cmdline => 'test_command_on_cli',
+};
+
+my $task = { state => 'ok', vmid => '100', };
+my $tasklist;
+sub prepare_test {
+    $result_text = $result_html = undef;
+    $task->{tmplog} = shift;
+    $tasklist = [ $task ];
+}
+
 {
-    my $result_text;
-    my $result_html;
-    my $mock_module = Test::MockModule->new('PVE::Tools');
-    $mock_module->mock('sendmail', sub {
-       my (undef, undef, $text, $html, undef, undef) = @_;
-       $result_text = $text;
-       $result_html = $html;
-    });
-    my $MAILTO = ['test_address@proxmox.com'];
-    my $OPTS->{mailto} = $MAILTO;
-    my $SELF->{opts} = $OPTS;
-    $SELF->{cmdline} = 'test_command_on_cli';
-    my $task;
-    $task->{state} = 'ok';
-    $task->{vmid} = '1';
-    {
-       $result_text = undef;
-       $result_html = undef;
-       $task->{tmplog} = $TEST_FILE_WRONG_PATH;
-       my $tasklist = [$task];
-       PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
-       like($result_text, $NO_LOGFILE, "Missing logfile is detected");
-    }
-    {
-       $result_text = undef;
-       $result_html = undef;
-       $task->{tmplog} = $TEST_FILE_PATH;
-       my $tasklist = [$task];
-       prepare_mail_with_status();
-       PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
-       unlike($result_text, $STATUS, "Status are not in text part of mails");
-       unlike($result_html, $STATUS, "Status are not in HTML part of mails");
-       unlink $TEST_FILE_PATH;
-    }
-    {
-       $result_text = undef;
-       $result_html = undef;
-       $task->{tmplog} = $TEST_FILE_PATH;
-       my $tasklist = [$task];
-       prepare_long_mail();
-       PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
-       like($result_text, $LOG_TOO_LONG, "Text part of mails gets shortened");
-       like($result_html, $LOG_TOO_LONG, "HTML part of mails gets shortened");
-       unlink $TEST_FILE_PATH;
-    }
-}
\ No newline at end of file
+    prepare_test($TEST_FILE_WRONG_PATH);
+    PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
+    like($result_text, $NO_LOGFILE, "Missing logfile is detected");
+}
+{
+    prepare_test($TEST_FILE_PATH);
+    prepare_mail_with_status();
+    PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
+    unlike($result_text, $STATUS, "Status are not in text part of mails");
+    unlike($result_html, $STATUS, "Status are not in HTML part of mails");
+}
+{
+    prepare_test($TEST_FILE_PATH);
+    prepare_long_mail();
+    PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
+    like($result_text, $LOG_TOO_LONG, "Text part of mails gets shortened");
+    like($result_html, $LOG_TOO_LONG, "HTML part of mails gets shortened");
+}
+unlink $TEST_FILE_PATH;