check_file_age.t 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/perl -w -I ..
  2. #
  3. # check_file_age tests
  4. #
  5. # $Id$
  6. #
  7. use strict;
  8. use Test::More tests => 15;
  9. use NPTest;
  10. my $successOutput = '/^FILE_AGE OK: /';
  11. my $warningOutput = '/^FILE_AGE WARNING: /';
  12. my $criticalOutput = '/^FILE_AGE CRITICAL: /';
  13. my $unknownOutput = '/^FILE_AGE UNKNOWN: /';
  14. my $result;
  15. my $temp_file = "/tmp/check_file_age.tmp";
  16. my $temp_link = "/tmp/check_file_age.link.tmp";
  17. unlink $temp_file, $temp_link;
  18. $result = NPTest->testCmd(
  19. "./check_file_age"
  20. );
  21. cmp_ok( $result->return_code, '==', 3, "Missing parameters" );
  22. like ( $result->output, $unknownOutput, "Output for unknown correct" );
  23. $result = NPTest->testCmd(
  24. "./check_file_age -f $temp_file"
  25. );
  26. cmp_ok( $result->return_code, '==', 2, "File not exists" );
  27. like ( $result->output, $criticalOutput, "Output for file missing correct" );
  28. write_chars(100);
  29. $result = NPTest->testCmd(
  30. "./check_file_age -f $temp_file"
  31. );
  32. cmp_ok( $result->return_code, '==', 0, "File is new enough" );
  33. like ( $result->output, $successOutput, "Output for success correct" );
  34. sleep 2;
  35. $result = NPTest->testCmd(
  36. "./check_file_age -f $temp_file -w 1"
  37. );
  38. cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" );
  39. like ( $result->output, $warningOutput, "Output for warning correct" );
  40. $result = NPTest->testCmd(
  41. "./check_file_age -f $temp_file -c 1"
  42. );
  43. cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" );
  44. like ( $result->output, $criticalOutput, "Output for critical correct" );
  45. $result = NPTest->testCmd(
  46. "./check_file_age -f $temp_file -c 1000 -W 100"
  47. );
  48. cmp_ok( $result->return_code, '==', 0, "Checking file size" );
  49. $result = NPTest->testCmd(
  50. "./check_file_age -f $temp_file -c 1000 -W 101"
  51. );
  52. cmp_ok( $result->return_code, '==', 1, "One byte too short" );
  53. $result = NPTest->testCmd(
  54. "./check_file_age -f $temp_file -c 1000 -C 101"
  55. );
  56. cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" );
  57. symlink $temp_file, $temp_link or die "Cannot create symlink";
  58. $result = NPTest->testCmd("./check_file_age -f $temp_link -c 10");
  59. cmp_ok( $result->return_code, '==', 0, "Works for symlinks" );
  60. unlink $temp_link;
  61. unlink $temp_file;
  62. mkdir $temp_file or die "Cannot create directory";
  63. $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1");
  64. cmp_ok( $result->return_code, '==', 0, "Works for directories" );
  65. rmdir $temp_file;
  66. sub write_chars {
  67. my $size = shift;
  68. open F, "> $temp_file" or die "Cannot write to $temp_file";
  69. print F "A" x $size;
  70. close F;
  71. }