NEWS: Mention "contrib" removal
[monitoring-plugins.git] / plugins-scripts / t / check_file_age.t
blob384c276bf577384fbd8a318bd3af13fc428b55e4
1 #!/usr/bin/perl -w -I ..
3 # check_file_age tests
7 use strict;
8 use Test::More tests => 15;
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: /';
16 my $result;
17 my $temp_file = "/tmp/check_file_age.tmp";
18 my $temp_link = "/tmp/check_file_age.link.tmp";
20 unlink $temp_file, $temp_link;
22 $result = NPTest->testCmd(
23 "./check_file_age"
25 cmp_ok( $result->return_code, '==', 3, "Missing parameters" );
26 like ( $result->output, $unknownOutput, "Output for unknown correct" );
28 $result = NPTest->testCmd(
29 "./check_file_age -f $temp_file"
31 cmp_ok( $result->return_code, '==', 2, "File not exists" );
32 like ( $result->output, $criticalOutput, "Output for file missing correct" );
34 write_chars(100);
35 $result = NPTest->testCmd(
36 "./check_file_age -f $temp_file"
38 cmp_ok( $result->return_code, '==', 0, "File is new enough" );
39 like ( $result->output, $successOutput, "Output for success correct" );
41 sleep 2;
43 $result = NPTest->testCmd(
44 "./check_file_age -f $temp_file -w 1"
46 cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" );
47 like ( $result->output, $warningOutput, "Output for warning correct" );
49 $result = NPTest->testCmd(
50 "./check_file_age -f $temp_file -c 1"
52 cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" );
53 like ( $result->output, $criticalOutput, "Output for critical correct" );
55 $result = NPTest->testCmd(
56 "./check_file_age -f $temp_file -c 1000 -W 100"
58 cmp_ok( $result->return_code, '==', 0, "Checking file size" );
60 $result = NPTest->testCmd(
61 "./check_file_age -f $temp_file -c 1000 -W 101"
63 cmp_ok( $result->return_code, '==', 1, "One byte too short" );
65 $result = NPTest->testCmd(
66 "./check_file_age -f $temp_file -c 1000 -C 101"
68 cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" );
70 symlink $temp_file, $temp_link or die "Cannot create symlink";
71 $result = NPTest->testCmd("./check_file_age -f $temp_link -c 10");
72 cmp_ok( $result->return_code, '==', 0, "Works for symlinks" );
73 unlink $temp_link;
75 unlink $temp_file;
76 mkdir $temp_file or die "Cannot create directory";
77 $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1");
78 cmp_ok( $result->return_code, '==', 0, "Works for directories" );
79 rmdir $temp_file;
82 sub write_chars {
83 my $size = shift;
84 open F, "> $temp_file" or die "Cannot write to $temp_file";
85 print F "A" x $size;
86 close F;