Code

Allow directories and links to be tested by check_file_age. Sanitise output.
[nagiosplug.git] / plugins-scripts / t / check_file_age.t
1 #!/usr/bin/perl -w -I ..
2 #
3 # check_file_age tests
4 #
5 # $Id$
6 #
8 use strict;
9 use Test::More tests => 15;
10 use NPTest;
12 my $successOutput = '/^FILE_AGE OK: /';
13 my $warningOutput = '/^FILE_AGE WARNING: /';
14 my $criticalOutput = '/^FILE_AGE CRITICAL: /';
15 my $unknownOutput = '/^FILE_AGE UNKNOWN: /';
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(
24         "./check_file_age"
25         );
26 cmp_ok( $result->return_code, '==', 3, "Missing parameters" );
27 like  ( $result->output, $unknownOutput, "Output for unknown correct" );
29 $result = NPTest->testCmd(
30         "./check_file_age -f $temp_file"
31         );
32 cmp_ok( $result->return_code, '==', 2, "File not exists" );
33 like  ( $result->output, $criticalOutput, "Output for file missing correct" );
35 write_chars(100);
36 $result = NPTest->testCmd(
37         "./check_file_age -f $temp_file"
38         );
39 cmp_ok( $result->return_code, '==', 0, "File is new enough" );
40 like  ( $result->output, $successOutput, "Output for success correct" );
42 sleep 2;
44 $result = NPTest->testCmd(
45         "./check_file_age -f $temp_file -w 1"
46         );
47 cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" );
48 like  ( $result->output, $warningOutput, "Output for warning correct" );
50 $result = NPTest->testCmd(
51         "./check_file_age -f $temp_file -c 1"
52         );
53 cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" );
54 like  ( $result->output, $criticalOutput, "Output for critical correct" );
56 $result = NPTest->testCmd(
57         "./check_file_age -f $temp_file -c 1000 -W 100"
58         );
59 cmp_ok( $result->return_code, '==', 0, "Checking file size" );
61 $result = NPTest->testCmd(
62         "./check_file_age -f $temp_file -c 1000 -W 101"
63         );
64 cmp_ok( $result->return_code, '==', 1, "One byte too short" );
66 $result = NPTest->testCmd(
67         "./check_file_age -f $temp_file -c 1000 -C 101"
68         );
69 cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" );
71 symlink $temp_file, $temp_link or die "Cannot create symlink";
72 $result = NPTest->testCmd("./check_file_age -f $temp_link -c 10");
73 cmp_ok( $result->return_code, '==', 0, "Works for symlinks" );
74 unlink $temp_link;
76 unlink $temp_file;
77 mkdir $temp_file or die "Cannot create directory";
78 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1");
79 cmp_ok( $result->return_code, '==', 0, "Works for directories" );
80 rmdir $temp_file;
83 sub write_chars {
84         my $size = shift;
85         open F, "> $temp_file" or die "Cannot write to $temp_file";
86         print F "A" x $size;
87         close F;
88 }