check_apt.t 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/perl -w -I ..
  2. #
  3. # Test check_apt using input files.
  4. # Contributed by Alex Bradley, October 2012
  5. #
  6. use strict;
  7. use Test::More;
  8. use NPTest;
  9. sub make_result_regexp {
  10. my ($warning, $critical) = @_;
  11. my $status;
  12. if ($warning == 0 && $critical == 0) {
  13. $status = "OK";
  14. } elsif ($critical == 0) {
  15. $status = "WARNING";
  16. } else {
  17. $status = "CRITICAL";
  18. }
  19. return sprintf('/^APT %s: %d packages available for upgrade \(%d critical updates\)\. |available_upgrades=%d;;;0 critical_updates=%d;;;0$/',
  20. $status, $warning, $critical, $warning, $critical);
  21. }
  22. if (-x "./check_apt") {
  23. plan tests => 36;
  24. } else {
  25. plan skip_all => "No check_apt compiled";
  26. }
  27. my $result;
  28. my $testfile_command = "./check_apt %s --input-file=t/check_apt_input/%s";
  29. $result = NPTest->testCmd( sprintf($testfile_command, "", "debian1") );
  30. is( $result->return_code, 0, "No upgrades" );
  31. like( $result->output, make_result_regexp(0, 0), "Output correct" );
  32. $result = NPTest->testCmd( sprintf($testfile_command, "", "debian2") );
  33. is( $result->return_code, 1, "Debian apt output, warning" );
  34. like( $result->output, make_result_regexp(13, 0), "Output correct" );
  35. $result = NPTest->testCmd( sprintf($testfile_command, "-o", "debian2") );
  36. is( $result->return_code, 0, "Debian apt output, no critical" );
  37. like( $result->output, make_result_regexp(13, 0), "Output correct" );
  38. $result = NPTest->testCmd( sprintf($testfile_command, "", "debian3") );
  39. is( $result->return_code, 2, "Debian apt output, some critical" );
  40. like( $result->output, make_result_regexp(19, 4), "Output correct" );
  41. $result = NPTest->testCmd( sprintf($testfile_command, "-o", "debian3") );
  42. is( $result->return_code, 2, "Debian apt output, some critical" );
  43. like( $result->output, make_result_regexp(19, 4), "Output correct" );
  44. $result = NPTest->testCmd( sprintf($testfile_command, "-c '^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)'", "debian3") );
  45. is( $result->return_code, 2, "Debian apt output - should have same result when default security regexp specified via -c" );
  46. like( $result->output, make_result_regexp(19, 4), "Output correct" );
  47. $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6", "debian3") );
  48. is( $result->return_code, 1, "Debian apt output, filter for libc6" );
  49. like( $result->output, make_result_regexp(3, 0), "Output correct" );
  50. $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6", "debian3") );
  51. is( $result->return_code, 1, "Debian apt output, filter for libc6, not critical" );
  52. like( $result->output, make_result_regexp(3, 0), "Output correct" );
  53. $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6 -i xen", "debian3") );
  54. is( $result->return_code, 2, "Debian apt output, filter for libc6 and xen" );
  55. like( $result->output, make_result_regexp(9, 4), "Output correct" );
  56. $result = NPTest->testCmd( sprintf($testfile_command, "-i libc6 -i xen -i linux", "debian3") );
  57. is( $result->return_code, 2, "Debian apt output, filter for libc6, xen, linux" );
  58. like( $result->output, make_result_regexp(12, 4), "Output correct" );
  59. $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6", "debian3") );
  60. is( $result->return_code, 2, "Debian apt output, filter out libc6" );
  61. like( $result->output, make_result_regexp(16, 4), "Output correct" );
  62. $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -o", "debian3") );
  63. is( $result->return_code, 2, "Debian apt output, filter out libc6, critical" );
  64. like( $result->output, make_result_regexp(16, 4), "Output correct" );
  65. $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -e xen", "debian3") );
  66. is( $result->return_code, 1, "Debian apt output, filter out libc6 and xen" );
  67. like( $result->output, make_result_regexp(10, 0), "Output correct" );
  68. $result = NPTest->testCmd( sprintf($testfile_command, "-e libc6 -e xen -e linux", "debian3") );
  69. is( $result->return_code, 1, "Debian apt output, filter out libc6, xen, linux" );
  70. like( $result->output, make_result_regexp(7, 0), "Output correct" );
  71. $result = NPTest->testCmd( sprintf($testfile_command, "-c Debian-Security -c linux", "debian3") );
  72. is( $result->return_code, 2, "Debian apt output, critical on Debian-Security or linux" );
  73. like( $result->output, make_result_regexp(19, 9), "Output correct" );
  74. $result = NPTest->testCmd( sprintf($testfile_command, "-i lib -i linux -e gc1c -c linux-image", "debian3") );
  75. is( $result->return_code, 2, "Debian apt output, include lib and linux, exclude gc1c, critical on linux-image" );
  76. like( $result->output, make_result_regexp(10, 2), "Output correct" );
  77. $result = NPTest->testCmd( sprintf($testfile_command, "", "ubuntu1") );
  78. is( $result->return_code, 1, "Ubuntu apt output, warning" );
  79. like( $result->output, make_result_regexp(5, 0), "Output correct" );
  80. $result = NPTest->testCmd( sprintf($testfile_command, "", "ubuntu2") );
  81. is( $result->return_code, 2, "Ubuntu apt output, some critical" );
  82. like( $result->output, make_result_regexp(25, 14), "Output correct" );