]> git.proxmox.com Git - pve-manager.git/blob - test/vzdump_notification_test.pl
test: fix vzdump notification test
[pve-manager.git] / test / vzdump_notification_test.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib '..';
7
8 use Test::More tests => 3;
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);
33 close(TEST_FILE);
34 }
35
36 my $result_text;
37 my $result_properties;
38
39 my $mock_notification_module = Test::MockModule->new('PVE::Notify');
40 my $mocked_notify = sub {
41 my ($severity, $title, $text, $properties, $metadata) = @_;
42
43 $result_text = $text;
44 $result_properties = $properties;
45 };
46 my $mocked_notify_short = sub {
47 my (@params) = @_;
48 return $mocked_notify->('<some severity>', @params);
49 };
50
51 $mock_notification_module->mock(
52 'notify' => $mocked_notify,
53 'info' => $mocked_notify_short,
54 'notice' => $mocked_notify_short,
55 'warning' => $mocked_notify_short,
56 'error' => $mocked_notify_short,
57 );
58 $mock_notification_module->mock('cfs_read_file', sub {
59 my $path = shift;
60
61 if ($path eq 'datacenter.cfg') {
62 return {};
63 } elsif ($path eq 'notifications.cfg' || $path eq 'priv/notifications.cfg') {
64 return '';
65 } else {
66 die "unexpected cfs_read_file\n";
67 }
68 });
69
70 my $MAILTO = ['test_address@proxmox.com'];
71 my $SELF = {
72 opts => { mailto => $MAILTO },
73 cmdline => 'test_command_on_cli',
74 };
75
76 my $task = { state => 'ok', vmid => '100', };
77 my $tasklist;
78 sub prepare_test {
79 $result_text = undef;
80 $task->{tmplog} = shift;
81 $tasklist = [ $task ];
82 }
83
84 {
85 prepare_test($TEST_FILE_WRONG_PATH);
86 PVE::VZDump::send_notification($SELF, $tasklist, 0, undef, undef, undef);
87 like($result_properties->{logs}, $NO_LOGFILE, "Missing logfile is detected");
88 }
89 {
90 prepare_test($TEST_FILE_PATH);
91 prepare_mail_with_status();
92 PVE::VZDump::send_notification($SELF, $tasklist, 0, undef, undef, undef);
93 unlike($result_properties->{"status-text"}, $STATUS, "Status are not in text part of mails");
94 }
95 {
96 prepare_test($TEST_FILE_PATH);
97 prepare_long_mail();
98 PVE::VZDump::send_notification($SELF, $tasklist, 0, undef, undef, undef);
99 like($result_properties->{logs}, $LOG_TOO_LONG, "Text part of mails gets shortened");
100 }
101 unlink $TEST_FILE_PATH;