]> git.proxmox.com Git - pve-manager.git/blame - test/vzdump_notification_test.pl
test: rename mail_test.pl to vzdump_notification_test.pl
[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');
40$mock_notification_module->mock('send_notification', sub {
41 my ($channel, $severity, $title, $text, $properties) = @_;
f1ea8ca4 42
f1ea8ca4 43 $result_text = $text;
c4afde55 44 $result_properties = $properties;
f1ea8ca4
TL
45});
46
6a545c32
FG
47my $mock_cluster_module = Test::MockModule->new('PVE::Cluster');
48$mock_cluster_module->mock('cfs_read_file', sub {
49 my $path = shift;
50
51 if ($path eq 'datacenter.cfg') {
c4afde55
LW
52 return {};
53 } elsif ($path eq 'notifications.cfg' || $path eq 'priv/notifications.cfg') {
54 return '';
6a545c32
FG
55 } else {
56 die "unexpected cfs_read_file\n";
57 }
58});
59
f1ea8ca4
TL
60my $MAILTO = ['test_address@proxmox.com'];
61my $SELF = {
62 opts => { mailto => $MAILTO },
63 cmdline => 'test_command_on_cli',
64};
65
66my $task = { state => 'ok', vmid => '100', };
67my $tasklist;
68sub prepare_test {
c4afde55 69 $result_text = undef;
f1ea8ca4
TL
70 $task->{tmplog} = shift;
71 $tasklist = [ $task ];
72}
73
ce84a950 74{
f1ea8ca4 75 prepare_test($TEST_FILE_WRONG_PATH);
c4afde55
LW
76 PVE::VZDump::send_notification($SELF, $tasklist, 0, undef, undef, undef);
77 like($result_properties->{logs}, $NO_LOGFILE, "Missing logfile is detected");
f1ea8ca4
TL
78}
79{
80 prepare_test($TEST_FILE_PATH);
81 prepare_mail_with_status();
c4afde55
LW
82 PVE::VZDump::send_notification($SELF, $tasklist, 0, undef, undef, undef);
83 unlike($result_properties->{"status-text"}, $STATUS, "Status are not in text part of mails");
f1ea8ca4
TL
84}
85{
86 prepare_test($TEST_FILE_PATH);
87 prepare_long_mail();
c4afde55
LW
88 PVE::VZDump::send_notification($SELF, $tasklist, 0, undef, undef, undef);
89 like($result_properties->{logs}, $LOG_TOO_LONG, "Text part of mails gets shortened");
f1ea8ca4
TL
90}
91unlink $TEST_FILE_PATH;