check_file_age.t 2.6 KB

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