]>
Commit | Line | Data |
---|---|---|
ce84a950 DJ |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
f1ea8ca4 TL |
5 | |
6 | use lib '..'; | |
7 | ||
c4afde55 | 8 | use Test::More tests => 3; |
ce84a950 DJ |
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 | |
c4afde55 | 32 | print TEST_FILE "a" x (1024*1024); |
ce84a950 DJ |
33 | close(TEST_FILE); |
34 | } | |
f1ea8ca4 | 35 | |
c4afde55 LW |
36 | my $result_text; |
37 | my $result_properties; | |
38 | ||
39 | my $mock_notification_module = Test::MockModule->new('PVE::Notify'); | |
3571d98b | 40 | my $mocked_notify = sub { |
c4afde55 | 41 | my ($channel, $severity, $title, $text, $properties) = @_; |
f1ea8ca4 | 42 | |
f1ea8ca4 | 43 | $result_text = $text; |
c4afde55 | 44 | $result_properties = $properties; |
3571d98b WB |
45 | }; |
46 | my $mocked_notify_short = sub { | |
47 | my ($channel, @rest) = @_; | |
48 | return $mocked_notify->($channel, '<some severity>', @rest); | |
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 |
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 { | |
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 | } |
101 | unlink $TEST_FILE_PATH; |