]>
Commit | Line | Data |
---|---|---|
ce84a950 DJ |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
f1ea8ca4 TL |
5 | |
6 | use lib '..'; | |
7 | ||
ce84a950 DJ |
8 | use Test::More tests => 5; |
9 | use Test::MockModule; | |
10 | ||
f1ea8ca4 TL |
11 | use PVE::VZDump; |
12 | ||
ce84a950 DJ |
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'; | |
f1ea8ca4 | 18 | |
ce84a950 DJ |
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 | } | |
f1ea8ca4 | 28 | |
ce84a950 DJ |
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 | } | |
f1ea8ca4 TL |
35 | |
36 | my ($result_text, $result_html); | |
37 | ||
6a545c32 FG |
38 | my $mock_tools_module = Test::MockModule->new('PVE::Tools'); |
39 | $mock_tools_module->mock('sendmail', sub { | |
f1ea8ca4 TL |
40 | my (undef, undef, $text, $html, undef, undef) = @_; |
41 | $result_text = $text; | |
42 | $result_html = $html; | |
43 | }); | |
44 | ||
6a545c32 FG |
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 | ||
f1ea8ca4 TL |
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 | ||
ce84a950 | 70 | { |
f1ea8ca4 TL |
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; |