Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #7234: Add a generic method for command execution with return c…
…ode handling
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
tests/acceptance/30_generic_methods/command_execution_result.cf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
####################################################### | ||
# | ||
# Launch the /bin/date command | ||
# | ||
####################################################### | ||
|
||
bundle common acc_path | ||
{ | ||
vars: | ||
"root" string => getenv("NCF_TESTS_ACCEPTANCE", 1024); | ||
} | ||
|
||
body common control | ||
{ | ||
inputs => { "${acc_path.root}/default.cf.sub", "${acc_path.root}/default_ncf.cf.sub", "@{ncf_inputs.default_files}" }; | ||
bundlesequence => { configuration, default("${this.promise_filename}") }; | ||
version => "1.0"; | ||
} | ||
|
||
####################################################### | ||
|
||
bundle agent init | ||
{ | ||
vars: | ||
"command_0" string => "exit 0"; | ||
"canonified_command_0" string => canonify("${command_name}"); | ||
} | ||
|
||
####################################################### | ||
|
||
bundle agent test | ||
{ | ||
vars: | ||
"kept_codes" string => "0,1,2"; | ||
"repaired_codes" string => "3,4"; | ||
|
||
methods: | ||
"phA" usebundle => command_execution_result("exit 0", "${kept_codes}", "${repaired_codes}"); | ||
"phB" usebundle => command_execution_result("exit 2", "${kept_codes}", "${repaired_codes}"); | ||
"phC" usebundle => command_execution_result("exit 3", "${kept_codes}", "${repaired_codes}"); | ||
"phD" usebundle => command_execution_result("exit 4", "${kept_codes}", "${repaired_codes}"); | ||
"phE" usebundle => command_execution_result("exit 5", "${kept_codes}", "${repaired_codes}"); | ||
} | ||
|
||
####################################################### | ||
|
||
bundle agent check | ||
{ | ||
classes: | ||
"ok_A" expression => "command_execution_result_exit_0_kept.!command_execution_result_exit_0_repaired.!command_execution_result_exit_0_error"; | ||
"ok_B" expression => "command_execution_result_exit_2_kept.!command_execution_result_exit_2_repaired.!command_execution_result_exit_2_error"; | ||
"ok_C" expression => "!command_execution_result_exit_3_kept.command_execution_result_exit_3_repaired.!command_execution_result_exit_3_error"; | ||
"ok_D" expression => "!command_execution_result_exit_4_kept.command_execution_result_exit_4_repaired.!command_execution_result_exit_4_error"; | ||
"ok_E" expression => "!command_execution_result_exit_5_kept.!command_execution_result_exit_5_repaired.command_execution_result_exit_5_error"; | ||
|
||
"ok" expression => "ok_A.ok_B.ok_C.ok_D.ok_E"; | ||
|
||
reports: | ||
ok:: | ||
"$(this.promise_filename) Pass"; | ||
!ok:: | ||
"$(this.promise_filename) FAIL"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
##################################################################################### | ||
# Copyright 2013 Normation SAS | ||
##################################################################################### | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, Version 3. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
##################################################################################### | ||
|
||
# @name Command execution result | ||
# @description Execute a command and create outcome classes depending on its exit code | ||
# | ||
# @parameter command The command to run | ||
# @parameter kept_codes List of codes that produce a kept status separated with commas (ex: 1,2,5) | ||
# @parameter repaired_codes List of codes that produce a repaired status separated with commas (ex: 3,4,6) | ||
# | ||
# @class_prefix command_execution_result | ||
# @class_parameter command_name | ||
# | ||
# This bundle will define a class command_execution_result_${command_name}_{kept,repaired,not_ok,ok,reached} | ||
# depending on the exit codes given in parameters. | ||
# If an exit code is not in the list it will lead to an error status. | ||
# If you want 0 to be a success you have to list it in the kept_codes list | ||
|
||
bundle agent command_execution_result(command, kept_codes, repaired_codes) | ||
{ | ||
vars: | ||
"class_prefix" string => canonify("command_execution_result_${command}"); | ||
|
||
"kept_list" slist => string_split("${kept_codes}", ",", "256"); | ||
"repaired_list" slist => string_split("${repaired_codes}", ",", "256"); | ||
|
||
methods: | ||
"report" | ||
usebundle => _logger("Execute the command ${command}", "${class_prefix}"), | ||
ifvarclass => "${class_prefix}_reached"; | ||
|
||
commands: | ||
"${command}" | ||
contain => in_shell, | ||
classes => classes_generic_return_code_list("${class_prefix}", @{kept_list}, @{repaired_list}); | ||
} |