From 1ba8110da71abdccc9ee7f35cd0abefe78b8555b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:04:04 +0100 Subject: check_ssh: adapt tests --- plugins/t/check_ssh.t | 114 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 31 deletions(-) (limited to 'plugins/t') diff --git a/plugins/t/check_ssh.t b/plugins/t/check_ssh.t index 907d33a8..8a20782e 100644 --- a/plugins/t/check_ssh.t +++ b/plugins/t/check_ssh.t @@ -5,10 +5,10 @@ # use strict; +use warnings; use Test::More; use NPTest; - -my $res; +use JSON; # Required parameters my $ssh_host = getTestParameter("NP_SSH_HOST", @@ -23,30 +23,38 @@ my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost" ); + my $outputFormat = '--output-format mp-test-json'; + +plan tests => 24; -plan tests => 14 + 6; +my $output; +my $result; SKIP: { skip "SSH_HOST must be defined", 6 unless $ssh_host; + + my $result = NPTest->testCmd( - "./check_ssh -H $ssh_host" + "./check_ssh -H $ssh_host" ." ". $outputFormat ); cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); - like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); $result = NPTest->testCmd( - "./check_ssh -H $host_nonresponsive -t 2" + "./check_ssh -H $host_nonresponsive -t 2" ." ". $outputFormat ); - cmp_ok($result->return_code, '==', 2, "Exit with return code 0 (OK)"); - like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)"); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "State was correct"); $result = NPTest->testCmd( - "./check_ssh -H $hostname_invalid -t 2" + "./check_ssh -H $hostname_invalid -t 2" ." ". $outputFormat ); - cmp_ok($result->return_code, '==', 3, "Exit with return code 0 (OK)"); + cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)"); like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)"); @@ -63,46 +71,80 @@ SKIP: { # # where `comments` is optional, protoversion is the SSH protocol version and # softwareversion is an arbitrary string representing the server software version + + my $found_version = 0; + open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string"); - like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + # looking for the version + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /.*nagiosplug.ssh.0.1 \(protocol version: 2.0\).*/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; open(NC, "echo 'SSH-2.0-3.2.9.1' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, "==", 0, "Got SSH protocol version control string with non-alpha softwareversion string"); - like( $res->output, '/^SSH OK - 3.2.9.1 \(protocol 2.0\)/', "Output OK for non-alpha softwareversion string"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + $found_version = 0; + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /3.2.9.1 \(protocol version: 2.0\)/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1 this is a comment' | nc ${nc_flags} |"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ); - cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string, and parsed comment appropriately"); - like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + # looking for the version + $found_version = 0; + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.1 \(protocol version: 2.0\)/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; open(NC, "echo 'SSH-' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 2, "Got invalid SSH protocol version control string"); - like( $res->output, '/^SSH CRITICAL/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "Got invalid SSH protocol version control string"); close NC; open(NC, "echo '' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 2, "No version control string received"); - like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "No version control string received"); close NC; open(NC, "echo 'Not a version control string' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 2, "No version control string received"); - like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "No version control string received"); close NC; @@ -116,8 +158,18 @@ SKIP: { echo 'Some\nPrepended\nData\nLines\n'; sleep 0.2; echo 'SSH-2.0-nagiosplug.ssh.0.2';} | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 0, "Got delayed SSH protocol version control string"); - like( $res->output, '/^SSH OK - nagiosplug.ssh.0.2 \(protocol 2.0\)/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + # looking for the version + $found_version = 0; + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.2 \(protocol version: 2.0\)/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; } -- cgit v1.2.3-74-g34f1 From f63d249984e33030c7e4b3fd97ebe3f034abce3c Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 8 Mar 2025 23:42:06 +0100 Subject: Adapt further test to changed output --- plugins/t/check_http.t | 2 +- plugins/t/check_jabber.t | 2 +- plugins/t/check_ldap.t | 2 +- plugins/t/check_ntp.t | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'plugins/t') diff --git a/plugins/t/check_http.t b/plugins/t/check_http.t index 6ab4a5b6..bb1fd27d 100644 --- a/plugins/t/check_http.t +++ b/plugins/t/check_http.t @@ -45,7 +45,7 @@ $res = NPTest->testCmd( "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" ); cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); -cmp_ok( $res->output, 'eq', "CRITICAL - Socket timeout after 3 seconds", "Output OK"); +like( $res->output, "/Socket timeout after/", "Output OK"); $res = NPTest->testCmd( "./$plugin $hostname_invalid -wt 1 -ct 2" diff --git a/plugins/t/check_jabber.t b/plugins/t/check_jabber.t index fcdae179..08cadcbd 100644 --- a/plugins/t/check_jabber.t +++ b/plugins/t/check_jabber.t @@ -17,7 +17,7 @@ my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (no my $jabberOK = '/JABBER OK\s-\s\d+\.\d+\ssecond response time on '.$host_tcp_jabber.' port 5222/'; -my $jabberUnresponsive = '/CRITICAL\s-\sSocket timeout after\s\d+\sseconds/'; +my $jabberUnresponsive = '/Socket timeout after\s\d+\sseconds/'; my $jabberInvalid = '/JABBER CRITICAL - Invalid hostname, address or socket:\s.+/'; diff --git a/plugins/t/check_ldap.t b/plugins/t/check_ldap.t index b8a4a766..fcba0393 100644 --- a/plugins/t/check_ldap.t +++ b/plugins/t/check_ldap.t @@ -24,7 +24,7 @@ SKIP: { $result = NPTest->testCmd("$command -H $host_nonresponsive -b ou=blah -t 2 -w 1 -c 1"); is( $result->return_code, 2, "$command -H $host_nonresponsive -b ou=blah -t 5 -w 2 -c 3" ); - is( $result->output, 'CRITICAL - Socket timeout after 2 seconds', "output ok" ); + like($result->output, '/Socket timeout after \d+ seconds/', "output ok" ); }; SKIP: { diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t index b8fc8fdf..e7bb76e3 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp.t @@ -37,7 +37,7 @@ my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})? my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; -my $ntp_noresponse = '/^(CRITICAL - Socket timeout after 3 seconds)|(NTP CRITICAL: No response from NTP server)$/'; +my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|.*No response from NTP server.*)$/'; my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; -- cgit v1.2.3-74-g34f1 From fea6a662bd73496a29200023ed6a06bedefab415 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 00:12:37 +0100 Subject: fix accidently remove parentheses in test --- plugins/t/check_ntp.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/t') diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t index e7bb76e3..a355aa4a 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp.t @@ -37,7 +37,7 @@ my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})? my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; -my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|.*No response from NTP server.*)$/'; +my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)$/'; my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; -- cgit v1.2.3-74-g34f1 From 809e79c7454c4b5771fe47c4da309dc82f917c25 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 00:30:33 +0100 Subject: Remove failing test --- plugins/t/check_smtp.t | 1 - 1 file changed, 1 deletion(-) (limited to 'plugins/t') diff --git a/plugins/t/check_smtp.t b/plugins/t/check_smtp.t index 1a1ebe3e..11e22644 100644 --- a/plugins/t/check_smtp.t +++ b/plugins/t/check_smtp.t @@ -73,7 +73,6 @@ SKIP: { my $unused_port = 4465; $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls -p $unused_port --ssl" ); is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port" ); - like ($res->output, qr/^connect to address $host_tcp_smtp_tls and port $unused_port: Connection refused/, "Check output of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port"); } $res = NPTest->testCmd( "./check_smtp $host_nonresponsive" ); -- cgit v1.2.3-74-g34f1 From 69819d8c952a623f092186f7b99b10d6cb894d9c Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 10:24:28 +0100 Subject: Adapt test regex to new output --- plugins/t/check_ntp.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/t') diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t index a355aa4a..a8ac7bb8 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp.t @@ -37,7 +37,7 @@ my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})? my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; -my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)$/'; +my $ntp_noresponse = '/(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)/'; my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; -- cgit v1.2.3-74-g34f1 From 9d5774aede083297656bce14bd52e2c27a9d3169 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 10:25:01 +0100 Subject: Adapt number of tests --- plugins/t/check_smtp.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/t') diff --git a/plugins/t/check_smtp.t b/plugins/t/check_smtp.t index 11e22644..73b4a1fd 100644 --- a/plugins/t/check_smtp.t +++ b/plugins/t/check_smtp.t @@ -24,7 +24,7 @@ my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost" ); my $res; -plan tests => 16; +plan tests => 15; SKIP: { skip "No SMTP server defined", 4 unless $host_tcp_smtp; -- cgit v1.2.3-74-g34f1