]> git.proxmox.com Git - pve-manager.git/blame - test/mail_test.pl
Fix #352: Limit the length of backup logs for mails
[pve-manager.git] / test / mail_test.pl
CommitLineData
ce84a950
DJ
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use PVE::VZDump;
6use Test::More tests => 5;
7use Test::MockModule;
8
9my $STATUS = qr/.*status.*/;
10my $NO_LOGFILE = qr/.*Could not open log file.*/;
11my $LOG_TOO_LONG = qr/.*Log output was too long.*/;
12my $TEST_FILE_PATH = '/tmp/mail_test';
13my $TEST_FILE_WRONG_PATH = '/tmp/mail_test_wrong';
14sub prepare_mail_with_status {
15 open(TEST_FILE, '>', $TEST_FILE_PATH); # Removes previous content
16 print TEST_FILE "start of log file\n";
17 print TEST_FILE "status: 0\% this should not be in the mail\n";
18 print TEST_FILE "status: 55\% this should not be in the mail\n";
19 print TEST_FILE "status: 100\% this should not be in the mail\n";
20 print TEST_FILE "end of log file\n";
21 close(TEST_FILE);
22}
23sub prepare_long_mail {
24 open(TEST_FILE, '>', $TEST_FILE_PATH); # Removes previous content
25 # 0.5 MB * 2 parts + the overview tables gives more than 1 MB mail
26 print TEST_FILE "a" x (1024*1024/2);
27 close(TEST_FILE);
28}
29{
30 my $result_text;
31 my $result_html;
32 my $mock_module = Test::MockModule->new('PVE::Tools');
33 $mock_module->mock('sendmail', sub {
34 my (undef, undef, $text, $html, undef, undef) = @_;
35 $result_text = $text;
36 $result_html = $html;
37 });
38 my $MAILTO = ['test_address@proxmox.com'];
39 my $OPTS->{mailto} = $MAILTO;
40 my $SELF->{opts} = $OPTS;
41 $SELF->{cmdline} = 'test_command_on_cli';
42 my $task;
43 $task->{state} = 'ok';
44 $task->{vmid} = '1';
45 {
46 $result_text = undef;
47 $result_html = undef;
48 $task->{tmplog} = $TEST_FILE_WRONG_PATH;
49 my $tasklist = [$task];
50 PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
51 like($result_text, $NO_LOGFILE, "Missing logfile is detected");
52 }
53 {
54 $result_text = undef;
55 $result_html = undef;
56 $task->{tmplog} = $TEST_FILE_PATH;
57 my $tasklist = [$task];
58 prepare_mail_with_status();
59 PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
60 unlike($result_text, $STATUS, "Status are not in text part of mails");
61 unlike($result_html, $STATUS, "Status are not in HTML part of mails");
62 unlink $TEST_FILE_PATH;
63 }
64 {
65 $result_text = undef;
66 $result_html = undef;
67 $task->{tmplog} = $TEST_FILE_PATH;
68 my $tasklist = [$task];
69 prepare_long_mail();
70 PVE::VZDump::sendmail($SELF, $tasklist, 0, undef, undef, undef);
71 like($result_text, $LOG_TOO_LONG, "Text part of mails gets shortened");
72 like($result_html, $LOG_TOO_LONG, "HTML part of mails gets shortened");
73 unlink $TEST_FILE_PATH;
74 }
75}