How to run a command or script before or after a test case
A shell command or user-defined script (or multiple scripts) can be executed before or after any test case using the built-in buddy::pre_test_command or buddy::post_test_command functions. These functions should be included in your CDRouter configuration file and can be used to execute any block of Tcl code.
To execute an external command or shell script within these functions, the SCRIPT_exec proc should be used.
Note each buddy::pre_test_command or buddy::post_test_command function corresponds to a single test case. You must redefine the function in your config file for each test case where you want it to run.
Example 1: Execute a user-defined script after cdrouter_basic_1
To execute a script located in /usr/cdrouter-data/custom/scripts/myScript.sh
after the cdrouter_basic_1 test case, place the following
buddy::post_test_command function in your configuration file:
buddy::post_test_command cdrouter_basic_1 {
# Display a log message
buddy::loginfo "Running my custom script myScript.sh"
# Use Tcl 'exec' command to execute my shell script
SCRIPT_exec "/usr/cdrouter-data/custom/scripts/myScript.sh"
}
Example 2: Execute two user-defined scripts before cdrouter_nat_100
Likewise, to execute the scripts located in
/usr/cdrouter-data/custom/scripts/myScript.sh
and /usr/tmp/getStats.sh
before the cdrouter_nat_100 test case, place the following
buddy::pre_test_command function in your configuration file:
buddy::pre_test_command cdrouter_nat_100 {
# display a log message
buddy::loginfo "Running my custom scripts myScript.sh and getStats.sh"
# Use Tcl 'exec' command to execute my shell scripts
SCRIPT_exec "/usr/cdrouter-data/custom/scripts/myScript.sh"
SCRIPT_exec "/usr/tmp/getStats.sh"
}
Example 3: Execute a user-defined script before or after every test case
It is also possible to run a script before or after every test case by utilizing CDRouter’s testcase_enter and testcase_exit procedures as shown in the following example. This code block should be included in your CDRouter configuration file.
Please see the CDRouter Developer’s Guide for more information.
buddy::testcase_enter my_test_case_enter
proc my_test_case_enter { testNum testName } {
# Call the old test case enter (this must be included)
cdrouter_testcase_enter $testNum $testName
# Add new test case enter or test case exit commands here
# Display a log message
buddy::loginfo "Running my custom script myScript.sh"
# Use Tcl 'exec' command to execute my shell script
SCRIPT_exec "/usr/cdrouter-data/custom/scripts/myScript.sh"
}
Example 4: Insert a delay before or after a specific test case
If you would like to insert a delay within any of these functions, you must use the Event_wait or Event_wait_seconds procedures.
- The argument to Event_wait_seconds must be in seconds
- The argument to Event_wait must be in milliseconds
Do Not use the built-in TCL sleep command. This will cause the entire CDRouter process to be suspended and may cause unexpected results or errors.
Here’s an example where a 60 second delay is added to the end of the test case cdrouter_nat_100:
buddy::post_test_command cdrouter_nat_100 {
# display a log message
buddy::loginfo "Waiting 60 seconds"
# Event_wait argument is the number of milliseconds
Event_wait 60000
SCRIPT_exec "/usr/tmp/getStats.sh"
}
More details on SCRIPT_exec
-
When calling SCRIPT_exec, the entire command and any arguments must all be specified within the same set of double-quotes. For example:
SCRIPT_exec "/usr/cdrouter-data/custom/scripts/myScript.sh arg1 arg2"
-
Do Not use TCL’s built-in exec command to run your external commands or shell scripts. The SCRIPT_exec proc works with CDRouter’s event loop to ensure that CDRouter remains responsive while your script is running. Using TCL’s exec command will cause CDRouter to block while the script is running, and it will not be able to respond to packets or events until the script is finished.
-
By default, SCRIPT_exec does not print any output. Use one of the following methods to capture or display output produced by your script:
-
SCRIPT_exec returns the output of the script when it has finished. You may capture this output in a variable for other processing. For example:
set output [ SCRIPT_exec "/usr/cdrouter-data/custom/scripts/myScript.sh" ] buddy::loginfo "Here is the result of my script:" buddy::loginfo $output
-
SCRIPT_exec can also display the output of the script in real-time as it is read from the external process. In order to turn on the real-time display, the name of a log prefix should be passed to the SCRIPT_exec command.
SCRIPT_exec "/usr/cdrouter-data/custom/scritps/myScript.sh" myScript
-