]> git.proxmox.com Git - pve-manager.git/blame - test/vzdump_notification_test.pl
ui: lvm: expose saferemove setting
[pve-manager.git] / test / vzdump_notification_test.pl
CommitLineData
ce84a950
DJ
1#!/usr/bin/perl
2
3use strict;
4use warnings;
f1ea8ca4
TL
5
6use lib '..';
7
c4afde55 8use Test::More tests => 3;
ce84a950
DJ
9use Test::MockModule;
10
f1ea8ca4
TL
11use PVE::VZDump;
12
ce84a950
DJ
13my $STATUS = qr/.*status.*/;
14my $NO_LOGFILE = qr/.*Could not open log file.*/;
15my $LOG_TOO_LONG = qr/.*Log output was too long.*/;
16my $TEST_FILE_PATH = '/tmp/mail_test';
17my $TEST_FILE_WRONG_PATH = '/tmp/mail_test_wrong';
f1ea8ca4 18
ce84a950
DJ
19sub 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
29sub 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
c4afde55 32 print TEST_FILE "a" x (1024*1024);
ce84a950
DJ
33 close(TEST_FILE);
34}
f1ea8ca4 35
c4afde55
LW
36my $result_text;
37my $result_properties;
38
39my $mock_notification_module = Test::MockModule->new('PVE::Notify');
3571d98b 40my $mocked_notify = sub {
a63ecef5 41 my ($severity, $title, $text, $properties, $metadata) = @_;
f1ea8ca4 42
f1ea8ca4 43 $result_text = $text;
c4afde55 44 $result_properties = $properties;
3571d98b
WB
45};
46my $mocked_notify_short = sub {
a63ecef5
LW
47 my (@params) = @_;
48 return $mocked_notify->('<some severity>', @params);
3571d98b 49};
f1ea8ca4 50
3571d98b
WB
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 {
6a545c32
FG
59 my $path = shift;
60
61 if ($path eq 'datacenter.cfg') {
c4afde55
LW
62 return {};
63 } elsif ($path eq 'notifications.cfg' || $path eq 'priv/notifications.cfg') {
64 return '';
6a545c32
FG
65 } else {
66 die "unexpected cfs_read_file\n";
67 }
68});
69
f1ea8ca4
TL
70my $MAILTO = ['test_address@proxmox.com'];
71my $SELF = {
72 opts => { mailto => $MAILTO },
73 cmdline => 'test_command_on_cli',
74};
75
76my $task = { state => 'ok', vmid => '100', };
77my $tasklist;
78sub prepare_test {
c4afde55 79 $result_text = undef;
f1ea8ca4
TL
80 $task->{tmplog} = shift;
81 $tasklist = [ $task ];
82}
83
ce84a950 84{
f1ea8ca4 85 prepare_test($TEST_FILE_WRONG_PATH);
c4afde55
LW
86 PVE::VZDump::send_notification($SELF, $tasklist, 0, undef, undef, undef);
87 like($result_properties->{logs}, $NO_LOGFILE, "Missing logfile is detected");
f1ea8ca4
TL
88}
89{
90 prepare_test($TEST_FILE_PATH);
91 prepare_mail_with_status();
c4afde55
LW
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");
f1ea8ca4
TL
94}
95{
96 prepare_test($TEST_FILE_PATH);
97 prepare_long_mail();
c4afde55
LW
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");
f1ea8ca4
TL
100}
101unlink $TEST_FILE_PATH;