Merge pull request #2056 from monitoring-plugins/fix_check_http_state_regex
[monitoring-plugins.git] / plugins / t / check_apt.t
blob430eb53e3ed225e9c2f85e4c21ec75b58240d972
1 #!/usr/bin/perl -w -I ..
3 # Test check_apt using input files.
4 # Contributed by Alex Bradley, October 2012
7 use strict;
8 use Test::More;
9 use NPTest;
11 sub make_result_regexp {
12 my ($warning, $critical) = @_;
13 my $status;
14 if ($warning == 0 && $critical == 0) {
15 $status = "OK";
16 } elsif ($critical == 0) {
17 $status = "WARNING";
18 } else {
19 $status = "CRITICAL";
21 return sprintf('/^APT %s: %d packages available for upgrade \(%d critical updates\)\. |available_upgrades=%d;;;0 critical_updates=%d;;;0$/',
22 $status, $warning, $critical, $warning, $critical);
25 if (-x "./check_apt") {
26 plan tests => 36;
27 } else {
28 plan skip_all => "No check_apt compiled";
31 my $result;
33 my $testfile_command = "./check_apt %s --input-file=t/check_apt_input/%s";
35 $result = NPTest->testCmd( sprintf($testfile_command, "", "debian1") );
36 is( $result->return_code, 0, "No upgrades" );
37 like( $result->output, make_result_regexp(0, 0), "Output correct" );
39 $result = NPTest->testCmd( sprintf($testfile_command, "", "debian2") );
40 is( $result->return_code, 1, "Debian apt output, warning" );
41 like( $result->output, make_result_regexp(13, 0), "Output correct" );
43 $result = NPTest->testCmd( sprintf($testfile_command, "-o", "debian2") );
44 is( $result->return_code, 0, "Debian apt output, no critical" );
45 like( $result->output, make_result_regexp(13, 0), "Output correct" );
47 $result = NPTest->testCmd( sprintf($testfile_command, "", "debian3") );
48 is( $result->return_code, 2, "Debian apt output, some critical" );
49 like( $result->output, make_result_regexp(19, 4), "Output correct" );
51 $result = NPTest->testCmd( sprintf($testfile_command, "-o", "debian3") );
52 is( $result->return_code, 2, "Debian apt output, some critical" );
53 like( $result->output, make_result_regexp(19, 4), "Output correct" );
55 $result = NPTest->testCmd( sprintf($testfile_command, "-c '^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)'", "debian3") );
56 is( $result->return_code, 2, "Debian apt output - should have same result when default security regexp specified via -c" );
57 like( $result->output, make_result_regexp(19, 4), "Output correct" );
59 $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6", "debian3") );
60 is( $result->return_code, 1, "Debian apt output, filter for libc6" );
61 like( $result->output, make_result_regexp(3, 0), "Output correct" );
63 $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6", "debian3") );
64 is( $result->return_code, 1, "Debian apt output, filter for libc6, not critical" );
65 like( $result->output, make_result_regexp(3, 0), "Output correct" );
67 $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6 -i xen", "debian3") );
68 is( $result->return_code, 2, "Debian apt output, filter for libc6 and xen" );
69 like( $result->output, make_result_regexp(9, 4), "Output correct" );
71 $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6 -i xen -i linux", "debian3") );
72 is( $result->return_code, 2, "Debian apt output, filter for libc6, xen, linux" );
73 like( $result->output, make_result_regexp(12, 4), "Output correct" );
75 $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6", "debian3") );
76 is( $result->return_code, 2, "Debian apt output, filter out libc6" );
77 like( $result->output, make_result_regexp(16, 4), "Output correct" );
79 $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -o", "debian3") );
80 is( $result->return_code, 2, "Debian apt output, filter out libc6, critical" );
81 like( $result->output, make_result_regexp(16, 4), "Output correct" );
83 $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -e xen", "debian3") );
84 is( $result->return_code, 1, "Debian apt output, filter out libc6 and xen" );
85 like( $result->output, make_result_regexp(10, 0), "Output correct" );
87 $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -e xen -e linux", "debian3") );
88 is( $result->return_code, 1, "Debian apt output, filter out libc6, xen, linux" );
89 like( $result->output, make_result_regexp(7, 0), "Output correct" );
91 $result = NPTest->testCmd( sprintf($testfile_command, "-c Debian-Security -c linux", "debian3") );
92 is( $result->return_code, 2, "Debian apt output, critical on Debian-Security or linux" );
93 like( $result->output, make_result_regexp(19, 9), "Output correct" );
95 $result = NPTest->testCmd( sprintf($testfile_command, "-i lib -i linux -e gc1c -c linux-image", "debian3") );
96 is( $result->return_code, 2, "Debian apt output, include lib and linux, exclude gc1c, critical on linux-image" );
97 like( $result->output, make_result_regexp(10, 2), "Output correct" );
99 $result = NPTest->testCmd( sprintf($testfile_command, "", "ubuntu1") );
100 is( $result->return_code, 1, "Ubuntu apt output, warning" );
101 like( $result->output, make_result_regexp(5, 0), "Output correct" );
103 $result = NPTest->testCmd( sprintf($testfile_command, "", "ubuntu2") );
104 is( $result->return_code, 2, "Ubuntu apt output, some critical" );
105 like( $result->output, make_result_regexp(25, 14), "Output correct" );