]>
git.proxmox.com Git - pve-manager.git/blob - test/vzdump_notification_test.pl
8 use Test
::More tests
=> 3;
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';
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";
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);
37 my $result_properties;
39 my $mock_notification_module = Test
::MockModule-
>new('PVE::Notify');
40 my $mocked_notify = sub {
41 my ($severity, $title, $text, $properties, $metadata) = @_;
44 $result_properties = $properties;
46 my $mocked_notify_short = sub {
48 return $mocked_notify->('<some severity>', @params);
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,
58 $mock_notification_module->mock('cfs_read_file', sub {
61 if ($path eq 'datacenter.cfg') {
63 } elsif ($path eq 'notifications.cfg' || $path eq 'priv/notifications.cfg') {
66 die "unexpected cfs_read_file\n";
70 my $MAILTO = ['test_address@proxmox.com'];
72 opts
=> { mailto
=> $MAILTO },
73 cmdline
=> 'test_command_on_cli',
76 my $task = { state => 'ok', vmid
=> '100', };
80 $task->{tmplog
} = shift;
81 $tasklist = [ $task ];
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");
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");
96 prepare_test
($TEST_FILE_PATH);
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");
101 unlink $TEST_FILE_PATH;