Tcl/keep CT automated test script instance 2-Main Program

Source: Internet
Author: User
Now we will introduce the test main program: Test. Exp.
To facilitate the addition of new test projects, the main program uses a flexible mechanism, which calls the corresponding sub-test programs through the source command as needed. In this way, each test point can be put into a single file and then referenced by the main program.
First look at the Code:

#! /Usr/bin/CT --

# $ ID $
# Usage:
#./Test [-uuser] [-ppassward] [-iip_address] test_001...
# Or./test [-uuser] [-ppassward] [-iip_address] [-ccommand_file] cmd
# Or./test [-uuser] [-ppassward] [-iip_address] [-sscript_file] script

Source global. Exp
Source commonlib. Exp

# Initialize Variables
Set external file ""
Set tlist $ argv
Set execScript ""

# Process options
Set endoptindex-1
Foreach Arg $ argv {
If {! [String Match "-/[A-Za-Z] *" $ Arg]} {
Break
}

# Inc end option Index
Incr endoptindex

# Get option flag and option value
Set optflg [String range $ Arg 1]
Set optval [String range $ Arg 2 end]
Dbglog "$ optflg $ optval"
If {$ optval = ""}{
Dbglog "option value is null:-$ optflg"
Return-1
}

Switch $ optflg {
"U "{
Set g_user $ optval
Dbglog "User: $ g_user"
}
"P "{
Set g_passwd $ optval
Dbglog "Password: $ g_passwd"
}
"I "{
Set g_devip $ optval
Dbglog "devip: $ g_devip"
}
"C "{
Set destination file $ optval
Dbglog "invalid file: $ invalid file"
}
"S "{
Set execScript $ optval
Dbglog "execScript: $ execScript"
}
Default {
Puts "unknown option:-$ optflg"
Return-1
}
}; # End Switch
}; # End foreach

# Remove options from List
If {$ endoptindex! =-1 }{
Set tlist [lreplace $ argv 0 $ endoptindex]
}

Dbglog "tlist is: $ tlist"

# Create log dir
If {! |! }{
Puts "Please create directory/" log /""
Return-1
}

# Read current time
Set clicks [Clock clicks]
Set tstr [Clock format $ clicks-format "% Y % m % d % I % m % s"]

# Open Log File
LOG_FILE "log/vlog $ tstr. log"

# Open brief Log File
Set g_blogfd [open "log/blog. log" W]

# Start testing
Foreach titem $ tlist {
Switch $ titem {
"Sys_001" {; # Test Group sys_001
Source SNMP. Exp
}
"Cmd" {; # exec CMD file
Source tcmd. Exp
}
"Script" {; # exec script file
If {$ execScript = ""}{
Puts "Please specify Script Name using-s option"
Return-1
}
Source $ execScript
}

Default {
Puts "do you want to test/" $ titem /"/? "
}
}
}

Close $ g_blogfd

At the beginning of the program, import two files through source, where global. exp mainly stores some definitions of global variables, because these global variables may be different for each test device, so they are extracted. The commonlib. Exp file contains some common subroutines that can be called by various test programs. The login subroutine we introduced earlier is stored in this file.
Next, analyze the command line parameters and extract all the option parameters. Currently, the supported command line options include:
-U: This option is used to change the Login User Name
-P: This option is used to change the logon password.
-I: This option is used to change the IP address of Telnet.
-C: This option is used to specify the file name of a batch file. The usage is described later.
-S: Specifies the file name of the script file. The usage is described later.

Finally, all the non-option parts of the command line parameters are used as test items to test these test items respectively.
For example, in test_001, the source command is used to call the SNMP. Exp script to test the SNMP community.
You can add test items as needed.

There are two special test item names: CMD and script.
The cmd test item calls the cmd. Exp script, which is described later. Its main function is to execute all the commands in a text file. The text file name is provided by the-C option.
Script test item. It will call the source command to execute the $ execScript script. You can use the-s option to assign values to the $ execScript variable.

This test script provides two types of logs, one is the detailed log (vlog *), including all the Telnet interaction processes, and the other is the simple log, it only contains information output by using errlog in the program. The log file is put in the subdirectory log, and its file name contains the script execution time for easy searching.
Dbglog is used in this script, and the errlog that will be used later are all subfunctions defined in the commonlib. Exp file. The Code is as follows:

#*************************************** *********
# Debug output routine
#
# @ Params
# Arg-variable length arguments
#*************************************** *********
Proc dbglog Arg {
Global g_dbgflag

If {$ g_dbgflag }{
Puts $ ARG
}
}

#*************************************** *********
# Error output routine
#
# @ Params
# Arg-variable length arguments
#*************************************** *********
Proc errlog Arg {
Global g_blogfd
Global g_dbgflag

If {$ g_dbgflag }{
Puts $ ARG
}

If {$ g_blogfd! = 0 }{
Puts $ g_blogfd $ ARG
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.