Merge pull request #2032 from RincewindsHat/cleanup/check_apt
[monitoring-plugins.git] / plugins-scripts / t / check_file_age.t
blob8b87670887fd865c31c92a78ae85c7d826981106
1 #!/usr/bin/perl -w -I ..
3 # check_file_age tests
7 use strict;
8 use Test::More tests => 27;
9 use NPTest;
11 my $successOutput = '/^FILE_AGE OK: /';
12 my $warningOutput = '/^FILE_AGE WARNING: /';
13 my $criticalOutput = '/^FILE_AGE CRITICAL: /';
14 my $unknownOutput = '/^FILE_AGE UNKNOWN: /';
15 my $performanceOutput = '/ \| age=[0-9]+s;[0-9:]+;[0-9:]+ size=[0-9]+B;[0-9:]+;[0-9:]+;0$/';
17 my $result;
18 my $temp_file = "/tmp/check_file_age.tmp";
19 my $temp_link = "/tmp/check_file_age.link.tmp";
21 unlink $temp_file, $temp_link;
23 $result = NPTest->testCmd("./check_file_age");
24 cmp_ok( $result->return_code, '==', 3, "Missing parameters" );
25 like ( $result->output, $unknownOutput, "Output for unknown correct" );
27 $result = NPTest->testCmd("./check_file_age -f $temp_file");
28 cmp_ok( $result->return_code, '==', 2, "File not exists" );
29 like ( $result->output, $criticalOutput, "Output for file missing correct" );
31 write_chars(100);
32 $result = NPTest->testCmd("./check_file_age -f $temp_file");
33 cmp_ok( $result->return_code, '==', 0, "File is new enough" );
34 like ( $result->output, $successOutput, "Output for success correct" );
36 sleep 2;
38 $result = NPTest->testCmd("./check_file_age -f $temp_file -w 1");
39 cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" );
40 like ( $result->output, $warningOutput, "Output for warning correct" );
42 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1");
43 cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" );
44 like ( $result->output, $criticalOutput, "Output for critical correct" );
46 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100");
47 cmp_ok( $result->return_code, '==', 0, "Checking file size" );
49 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100");
50 like( $result->output, $performanceOutput, "Checking for performance Output" );
52 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100");
53 like( $result->output, $performanceOutput, "Checking for performance Output from range" );
55 $result = NPTest->testCmd("./check_file_age -f /non/existent --ignore-missing");
56 cmp_ok( $result->return_code, '==', 0, "Honours --ignore-missing" );
58 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 101");
59 cmp_ok( $result->return_code, '==', 1, "One byte too short" );
61 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 101");
62 cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" );
64 SKIP: {
65 eval 'use Monitoring::Plugin::Range';
66 skip "Monitoring::Plugin::Range module require", 9 if $@;
68 $result = NPTest->testCmd("./check_file_age -f $temp_file -w 0:1");
69 cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old by range" );
70 like ( $result->output, $warningOutput, "Output for warning by range correct" );
72 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 0:1");
73 cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old by range" );
74 like ( $result->output, $criticalOutput, "Output for critical by range correct" );
76 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 0:1000 -W 0:100");
77 cmp_ok( $result->return_code, '==', 0, "Checking file size by range" );
79 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 101:");
80 cmp_ok( $result->return_code, '==', 1, "One byte too short by range" );
82 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 0:99");
83 cmp_ok( $result->return_code, '==', 1, "One byte too long by range" );
85 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 101:");
86 cmp_ok( $result->return_code, '==', 2, "One byte too short by range - critical" );
88 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 0:99");
89 cmp_ok( $result->return_code, '==', 2, "One byte too long by range - critical" );
92 symlink $temp_file, $temp_link or die "Cannot create symlink";
93 $result = NPTest->testCmd("./check_file_age -f $temp_link -c 10");
94 cmp_ok( $result->return_code, '==', 0, "Works for symlinks" );
95 unlink $temp_link;
97 unlink $temp_file;
98 mkdir $temp_file or die "Cannot create directory";
99 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1");
100 cmp_ok( $result->return_code, '==', 0, "Works for directories" );
101 rmdir $temp_file;
104 sub write_chars {
105 my $size = shift;
106 open F, "> $temp_file" or die "Cannot write to $temp_file";
107 print F "A" x $size;
108 close F;