回页首
准备测试环境
建立 Resource Pool 并使用 STAF/STAX 驱动 VM Slave
大型软件均支持多种平台,因此回归测试就必须覆盖软件支持的各种情况。不可避免的,在一次回归中需要用到多台测试机。VMware 虚拟机是一种被广泛使用的,支持多种操作系统的虚拟机解决方案软件。它可以方便地保存和还原各种的系统状态(Snapshot),可以大大降低配置测试环境的工作量,并且显著减少测试环境带来的问题。
我们把一台具有唯一主机名(Host Name)的虚拟机称为一台 Slave。为了方便的查询和管理这些 Slave,我们在数据库中建立了一个表作为资源池(Resource Pool)。这张表管理了所有 Slave 机器的标识(ID),主机名(HostName),状态(State),用户名(User)和密码(Password)。
其中 Slave 的状态包含以下几种:
使用中(onjob)
在线且空闲(online&free)
离线(offline)
不可用(no-available)
我们使用 STAF/STAX 实现对 VM Slave 的自动地控制。这部分的 STAX 脚本如下:
清单 1. 启动 VM Slave 并还原 Snapshot
<stax>
<function name="prepareVM">
<function-list-args>
<function-required-arg name="VMWareServer">VMWareServer</function-required-arg>
<function-required-arg name="vmIP">vmIP</function-required-arg>
<function-required-arg name="delayTime">delayTime</function-required-arg>
<function-required-arg name="vmxFile">vmxFile</function-required-arg>
<function-required-arg name="snapshotName">snapshotName</function-required-arg>
</function-list-args>
<sequence>
<!-- Revert Slave to a saved snapshot-->
<process name="'revertVM'">
<location>VMWareServer</location>
<command>'vmrun'</command>
<parms>'revertToSnapshot %s %s' %(vmxFile,snapshotName)</parms>
</process>
<!-- Start VM -->
<process name="'startVM'">
<location>VMWareServer</location>
<command>'vmrun'</command>
<parms>'start %s' %(vmxFile)</parms>
</process>
<!-- Check if startVM cmd is finished normally -->
<if expr="RC != 0">
<throw exception="'eTerminateFunction'">'Failed to start VM'</throw>
</if>
<!-- Ping the target machine and ensure it open -->
<timer duration="'10m'">
<sequence>
<script>pingRC = 1</script>
<loop until="pingRC == 0">
<sequence>
<stafcmd name="'PingTargetMachine'">
<location>vmIP</location>
<service>'PING'</service>
<request>'PING'</request>
</stafcmd>
<if expr="RC != 0">
<sequence>
<stafcmd name="'delay'">
<location>'local'</location>
<service>'DELAY'</service>
<request>'DELAY %s' %(delayTime)</request>
</stafcmd>
</sequence>
<else>
<sequence>
<log>'%s is opened' %(vmIP)</log>
<script>pingRC = 0</script>
</sequence>
</else>
</if>
</sequence>
</loop>
</sequence>
</timer>
<!-- If the Slave cannot ping within the given time -->
<if expr="RC != 0">
<throw exception="'systemException'">
'Fail to start VM Slave, please do a further check!'
</throw>
</if>
</sequence>
</function>
</stax>
|
使用 STAF Java API 下载和安装 test builds 和 testcase
STAF Java API 是 STAF 提供的 Java 编程接口。它可以用来通过 Java 程序提交 STAF 命令或者注册诸如 STAX,Event 和 Email 之类的服务。STAF 通过 JSTAF.jar 来提供 Java 支持,只要将此 jar 包导入 CLASSPATH 中就可以使用。用户可以在 ..\STAFInstallFolder\bin\ 目录下找到这个 jar 包。
系统在自动启动 VM Slave 并还原到我们需要的镜像后,就需要下载和安装待测试的软件了。假设待测试的软件安装包以压缩文件的形式存放在一个 FTP 服务器上,自动化测试框架通过 STAF Java API 提供自动下载、解压和静默安装功能。所需代码如下:
清单 2. 测试软件的自动下载和安装
// 使用 STAF FTP 服务自动下载待测试软件安装包
private boolean downESE(STAFHandle handler) {
db.updateRequestStatus(form.getRid(),
"Test ongoing: downloading build file...");
String stafcmd = "GET HOST " + GlobalVar.ftpURl + " URLPATH " + this.url
+ " FILE " + this.localURL + "\\" + GlobalVar.clientFileName;
STAFResult stafResult = handler
.submit2(GlobalVar.clientURl, "ftp", stafcmd);
if (stafResult.rc != 0) {
db.updateRequestStatus(form.getRid(),
"Test stop:down build file failed!");
return false;
}
System.out.println("down success");
db.updateRequestStatus(form.getRid(),
"Test ongoing:down build file successed!");
return true;
}
// 使用 STAF Zip 服务自动解压安装包
private boolean unzipESE(STAFHandle handler) {
db.updateRequestStatus(form.getRid(),
"Test ongoing: unzipping ese file...");
String stafcmd = "unzip zipfile " + this.localURL + "\\"
+ GlobalVar.clientFileName + " todirectory " + this.localURL;
STAFResult stafResult = handler
.submit2(GlobalVar.clientURl, "zip", stafcmd);
if (stafResult.rc != 0) {
db.updateRequestStatus(form.getRid(),
"Test stop:unzip ese file failed!");
return false;
}
System.out.println("unzip success");
db.updateRequestStatus(form.getRid(),
"Test ongoing:unzip ese file successed!");
return true;
}
// 使用 STAF Process 服务实现软件的静默安装
private boolean silentInstall(STAFHandle handler) {
db.updateRequestStatus(form.getRid(),
"Test ongoing: installing db2 client...");
String stafcmd = "start command " + this.localURL + GlobalVar.setupFile
+ " /f /l " + GlobalVar.logFile + " /u "
+ generateScrips(this.form) + " /t " + GlobalVar.db2LogFile
+ " /m wait returnstdout";
STAFResult stafResult = handler.submit2(GlobalVar.clientURl, "process",
stafcmd);
if (stafResult.rc != 0) {
db.updateRequestStatus(form.getRid(),
"Test stop:install db2 client failed!");
return false;
}
System.out.println("silent success");
db.updateRequestStatus(form.getRid(),
"Test ongoing:install db2 client successed!");
return true;
}
|