From 7cef845e9544f44de564c8b34fba4e39e9d3053d Mon Sep 17 00:00:00 2001 From: EvanAdam Date: Mon, 19 May 2025 17:01:27 +0200 Subject: [PATCH] test(nmap): Add test to unix_execute and nmap plugin --- tests/apps/nmap/cli/discovery.robot | 33 ++++++++++ tests/centreon/plugins/misc.t | 96 +++++++++++++++++++++++++++++ tests/resources/resources.resource | 10 +++ 3 files changed, 139 insertions(+) create mode 100644 tests/apps/nmap/cli/discovery.robot create mode 100644 tests/centreon/plugins/misc.t diff --git a/tests/apps/nmap/cli/discovery.robot b/tests/apps/nmap/cli/discovery.robot new file mode 100644 index 0000000000..cb47a15ad9 --- /dev/null +++ b/tests/apps/nmap/cli/discovery.robot @@ -0,0 +1,33 @@ +*** Settings *** +Documentation Test the Podman container-usage mode +Library Collections + +Resource ${CURDIR}${/}..${/}..${/}..${/}resources/import.resource + +Test Timeout 120s + +*** Variables *** +${MOCKOON_JSON} ${CURDIR}${/}podman.json + +${cmd} ${CENTREON_PLUGINS} +... --plugin=apps::nmap::cli::plugin +... --mode=discovery + +*** Test Cases *** +Container usage ${tc} + [Documentation] Check nmap discovery + [Tags] apps nmap + Log To Console \n + ${command} Catenate + ... ${cmd} + ... --subnet='127.0.0.1/32' + ... ${extraoptions} + + Ctn Run Command And Check Result As Json ${command} ${expected_result} + + Examples: tc extraoptions expected_result -- + ... 1 ${EMPTY} {"end_time":1747232859,"discovered_items":1,"results":[{"hostname":"localhost","ip":"127.0.0.1","hostnames":[{"type":"PTR","name":"localhost"}],"vendor":null,"status":"up","addresses":[{"address":"127.0.0.1","type":"ipv4"}],"os_accuracy":null,"os":null,"services":null,"type":"unknown"}],"duration":0,"start_time":1747232859} + ... 2 --nmap-options='-sS -sU -R -O --osscan-limit --osscan-guess -p U:161,162,T:21-25,80,139,443,3306,5985,5986,8080,8443 -oX - ' {"end_time":1747232859,"discovered_items":1,"results":[{"hostname":"localhost","ip":"127.0.0.1","hostnames":[{"type":"PTR","name":"localhost"}],"vendor":null,"status":"up","addresses":[{"address":"127.0.0.1","type":"ipv4"}],"os_accuracy":null,"os":null,"services":null,"type":"unknown"}],"duration":1,"start_time":1747232859} + ... 3 --nmap-options='-sS -oX - ' {"results":[{"ip":"127.0.0.1","type":"unknown","os_accuracy":null,"status":"up","services":null,"hostnames":[{"type":"PTR","name":"localhost"}],"os":null,"hostname":"localhost","vendor":null,"addresses":[{"address":"127.0.0.1","type":"ipv4"}]}],"duration":0,"discovered_items":1,"start_time":1747234100,"end_time":1747234100} + +*** Keywords *** diff --git a/tests/centreon/plugins/misc.t b/tests/centreon/plugins/misc.t new file mode 100644 index 0000000000..6171ff8da5 --- /dev/null +++ b/tests/centreon/plugins/misc.t @@ -0,0 +1,96 @@ +use strict; +use warnings; +use Test2::V0; +use Test2::Tools::Compare qw{is like match}; +use FindBin; +use lib "$FindBin::RealBin/../../../src"; +use centreon::plugins::misc; +use centreon::plugins::output; +use centreon::plugins::options; +# in real world one should use execute(), but as many options are not supported by windows_execute, +# the signature is not coherent, and we want to test everything on this unix_execute() +sub test_unix_execute { + my $mock_output = mock 'centreon::plugins::output'; # this is from Test2::Tools::Mock, included by Test2::V0 + + my $option = centreon::plugins::options->new(); + my $output = centreon::plugins::output->new(options => $option); + + my @tests = ( + { + expect => '"string to" output "$( echo noworking)"', + msg => 'double quote stay when no interpretation', + args => { + command_path => "/bin", + command => 'echo', + command_options => '"string to" output "$( echo noworking)"', + no_shell_interpretation => 1, + } + }, + { + expect => 'string to output noworking', + msg => 'double quote diseapear when interpretation is enabled', + args => { + command_path => "/bin", + command => 'echo', + command_options => '"string to" output "$( echo noworking)"', + } + }, + { + expect => 'stringToOutput adding', + msg => 'interpretation by default active', + args => { + command => 'echo stringToOutput $(echo adding)', + } + }, + { + expect => 'stringToOutput $(echo adding)', + msg => 'interpretation by default active', + args => { + command => 'echo stringToOutput $(echo adding)', + no_shell_interpretation => 1, + } + }, + { + expect => '', + msg => "no error when no argument given to command without interpolation", + args => { + command => 'echo', + no_shell_interpretation => 1, + } + } + + ); + for my $test (@tests) { + my ($stdout, $exit_code) = centreon::plugins::misc::unix_execute( + output => $output, + options => { timeout => 10 }, + no_quit => 1, + %{$test->{args}}, + ); + is($stdout, $test->{expect}, $test->{msg}); + } + + my ($stdout, $exit_code) = centreon::plugins::misc::unix_execute( + output => $output, + options => { timeout => 10 }, + command => 'NoBinary', + command_output => '"string to" output "$( echo noworking)"', + no_shell_interpretation => 1, + no_quit => 1 + ); + like($stdout, qr/Can't exec "NoBinary": No such file or directory at.*/, 'no_quit option always return'); + ($stdout, $exit_code) = centreon::plugins::misc::unix_execute( + output => $output, + options => { timeout => 10 }, + sudo => 1, + command => 'NoBinary', + command_output => '"string to" output "$( echo noworking)"', + no_shell_interpretation => 1, + no_quit => 1 + ); + like($stdout, qr/Can't exec "sudo": No such file or directory at.*/, 'sudo option add sudo binary before command'); + +} + +test_unix_execute(); +done_testing(); diff --git a/tests/resources/resources.resource b/tests/resources/resources.resource index d3c585046d..e3e40ee14f 100644 --- a/tests/resources/resources.resource +++ b/tests/resources/resources.resource @@ -85,3 +85,13 @@ Ctn Verify Command Output ... Wrong output result for command:\n${command}\n\nObtained:\n${output}\n\nExpected:\n${expected_result}\n ... values=False ... collapse_spaces=True + +Ctn Run Command And Check Result As Json + [Arguments] ${command} ${expected} + Log To Console ${command} + ${output} Run ${command} + ${output} Strip String ${output} + ${json_output}= evaluate json.loads('''${output}''') json + ${json_expected}= evaluate json.loads('''${expected}''') json + Dictionaries Should Be Equal ${json_output} ${json_expected} ignore_keys=['end_time', 'start_time', 'duration'] + Log Dictionary ${json_output} \ No newline at end of file