CDRouter Support

Can I run one of my scripts before or after a test case?

knowledge-base version 11.1

Yes. A 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 a shell script using these commands the API proc SCRIPT_exec should be used.

Note that one buddy::pre_test_command or buddy::post_test _command must be included in your configuration file for each point where you would like to execute your script(s).

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: Insert a delay before or after a specific test case

Please see this Knowledge Base article for an example using buddy::pre_test_command and buddy::post_test_command to insert a specific delay before or after a test case.

Example 4: Execute a user-defined script before or after every test case

It’s 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"

}

More details on SCRIPT_exec

The SCRIPT_exec proc works similar to Tcl’s exec command. However, the SCRIPT_exec proc works with CDRouter’s event loop instead of blocking while the script is running. The SCRIPT_exec proc will run the external script as a seperate process and read the output of the process. If Tcl’s exec command is used, CDRouter will block while the script is running. During this time, CDRouter will not be able to respond to packets or events. For any external script that will take longer than a second to execute, SCRIPT_exec should be used.

The SCRIPT_exec proc returns the output of the script so that it is accessible in a test case or 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:"
puts $output

The SCRIPT_exec proc 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

Contents

×

About CDRouter

CDRouter is made by QA Cafe, a technology company based in Portsmouth, NH.

Get in touch via our Contact page or by following us on your favorite service: