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