check_file_age.t 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/perl -w -I ..
  2. #
  3. # check_file_age tests
  4. #
  5. #
  6. use strict;
  7. use Test::More tests => 17;
  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 $performanceOutput = '/ \| age=[0-9]+s;[0-9]+;[0-9]+ size=[0-9]+B;[0-9]+;[0-9]+;0 $/';
  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 100"
  51. );
  52. like( $result->output, $performanceOutput, "Checking for performance output" );
  53. $result = NPTest->testCmd(
  54. "./check_file_age -f /non/existent --ignore-missing"
  55. );
  56. cmp_ok( $result->return_code, '==', 0, "Honors --ignore-missing" );
  57. $result = NPTest->testCmd(
  58. "./check_file_age -f $temp_file -c 1000 -W 101"
  59. );
  60. cmp_ok( $result->return_code, '==', 1, "One byte too short" );
  61. $result = NPTest->testCmd(
  62. "./check_file_age -f $temp_file -c 1000 -C 101"
  63. );
  64. cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" );
  65. symlink $temp_file, $temp_link or die "Cannot create symlink";
  66. $result = NPTest->testCmd("./check_file_age -f $temp_link -c 10");
  67. cmp_ok( $result->return_code, '==', 0, "Works for symlinks" );
  68. unlink $temp_link;
  69. unlink $temp_file;
  70. mkdir $temp_file or die "Cannot create directory";
  71. $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1");
  72. cmp_ok( $result->return_code, '==', 0, "Works for directories" );
  73. rmdir $temp_file;
  74. sub write_chars {
  75. my $size = shift;
  76. open F, "> $temp_file" or die "Cannot write to $temp_file";
  77. print F "A" x $size;
  78. close F;
  79. }