STAF学习笔记-A

来源:互联网 发布:淘宝可以用什么支付 编辑:程序博客网 时间:2024/05/01 00:36


    主要内容包含:

    1. STAX的相关介绍         

    2. 将STAF注册为windows 平台服务   

   3.STAF主要类解析:例子位于staf的安装目录\STAF\samples\demo之下。如果需要将java文件导入eclipse 需要加载staf的jar文件,位于C:\STAF\services\stax下。主要有STAFEvent.jar,STAX.jar,STAXDoc.jar,STAXMon.jar


3STAF主要类解析:

以 STAFDemoController为例学习,这个例子位于staf的安装目录\STAF\samples\demo之下。如果需要将java文件导入eclipse 需要加载staf的jar文件,位于C:\STAF\services\stax下。主要有STAFEvent.jar,STAX.jar,STAXDoc.jar,STAXMon.jar

对于Java应用来说主要有四个类:

STAFHandle: This class handles registering and unregistering with STAF as well as submitting service requests.
STAFException: This class is thrown by STAFHandle when errors are encountered.
STAFResult : This class contains the result of the STAFHandle.submit2() method as well as the STAF error constants.
STAFUtil: This class contains STAF utility functions.

每一个java应用程序有且只能建立一个STAFHandle对象。

例子:创建一个STAFHandle 对象。

static STAFHandle handle;
try{
    handle = new STAFHandle("STAFDemo_Controller");
}catch(STAFException e)
 {
    System.out.println("Error registering with STAF, RC: " + e.rc);
    System.exit(e.rc);
}

如果要注销service,可以使用handle.unRegister()


提交service请求:

Service requests may be submitted by one of two methods:
l The submit() method works in the traditional Java fashion, in that it throws an exception (a STAFException in
particular) if it encounters an error, and it returns a result string on success.
l The submit2() method returns a STAFResult object in all cases. This object contains the real STAF return code as
well as the  result string. It is typically used in places where you wish to avoid catching exceptions when using
STAF.


STAF命令的格式:

The first parameter is where the STAF request is to be executed. In this case, it will be executed locally.
The second parameter is the STAF Service where the request should be routed. In this case, it is the "var"
(Variable) service.
The third parameter is the actual request to be sent to the STAF Service. In this case the request is to resolve the
string "{STAF/Config/Machine}".

即:

每个请求都有三个参数,以系统-服务-参数的形式出现。第一个参数表示此请求需要被发送到的 STAF 系统,这个参数被 STAFProc 解析以便确定请求应该被本地处理还是发送到其他的 STAF 系统。 当这个请求被发送到需要处理的 STAF 系统后,STAFProc 解析第二个参数来判断哪个服务会被调用。最后,STAFProc 会把第三个参数转发给需要调用的服务,服务处理这个请求。 

一些术语的解释:

external services:    Services for which the executable code for the service resides outside of STAFProc, and which must be registered in the STAF configuration file. Some external services, such as LOG, are
provided with STAF. Others are available from the STAF web site and must be downloaded and installed.
handle A:     unique identifier which is used when submitting requests to STAF.
internal services:     Services for which the executable code resides within STAFProc    
process:        A process is an object which can be executed on a test machine. Examples of processes are:executables, shell scripts, etc.
request:         A string, sent to a service, which describes the operation the service is to perform.
services:        Reusable components that provide all the capability in STAF. Each STAF service provides a specific set of functionality and defines a set of requests that it will accept.
STAF:         An automation framework designed around the idea of reusable components
STAF command:    A STAF Command consists of a machine, service, and request. The request is sent to the machine,the service on the machine processes the request, and returns a return code and a result, if any.
STAFPro:         The daemon process which runs on each STAF system.
    
4 STAF的配置文件详解:

默认的配置文件位于STAF安装目录\STAF\bin

# Turn on tracing of internal errors and deprecated options
trace enable tracepoints "error deprecated"

# Enable TCP/IP connections
interface ssl library STAFTCP option Secure=Yes option Port=6550
interface tcp library STAFTCP option Secure=No  option Port=6500

# Set default local trust
trust machine local://local level 5

# Add default service loader
serviceloader library STAFDSLS

你也可以自己选择其他的配置文件,但是使用自己的配置文件的时候再启动的时候必须加上参数名称。

例如:C:\Documents and Settings\Administrator>stafproc C:\STAF\bin\test.cfg

Machine          : IBM-R86M1CV.cn.ibm.com
Machine nickname : testmachine1
Startup time     : 20110914-14:21:14

STAFProc version 3.4.2 initialized

配置文件中的意义:

trace enable tracepoints "error deprecated".当错误条件发生的时候形成trace信息

interface tcp library STAFTCP:在网络上发送和接受STAF请求,默认端口为6500。如果需要定义自己的端口可以使用interface tcp library STAFTCP option Secure=No  option Port=6600
trust machine local://local level: 设置本地机器为full access 信任级别
serviceloader Library STAFDSLS: 注册默认的Service Loader。可以动态加载Log,Monitor等服务
此外:
MACHINENICKNAME 可以用来设置机器的别名
利用set可以设置var变量,查看STAF 的var list:STAF local var list


TRUST LEVEL 5 MACHINE tcp://client1.austin.ibm.com用来设置信任等级。利用命令staf local trust list. 可以查看本机的信任机器。

5 STAF的常用命令:

很多命令已经在前面的内容中讲述过了,不再重复。

1.List handle:STAF local handle list handles

2.查看service的帮助:STAF local service help

C:\Documents and Settings\Administrator>STAF LOCAL SHUTDOWN help
Response
--------
*** SHUTDOWN Service Help ***

SHUTDOWN

NOTIFY REGISTER   [MACHINE <Machine>] [HANDLE <Handle> | NAME <Name>]
                  [PRIORITY <Priority>]
NOTIFY UNREGISTER [MACHINE <Machine>] [HANDLE <Handle> | NAME <Name>]
                  [PRIORITY <Priority>]
NOTIFY LIST

HELP

3ping机器以确定能否通过staf 访问

C:\Documents and Settings\Administrator>staf local ping ping
Response
--------
PONG

C:\Documents and Settings\Administrator>staf 10.6.125.164 ping ping
Response
--------
PONG

4 shutdown staf:

STAF local shutdown shutdown

5 通过命令行提交request:

The syntax of this command is:
STAF <Endpoint> <Service> <Request>
<Endpoint> is either LOCAL, if you wish to make a request of the local machine, or the name of the machine of
which you wish to make a request
<Service> is the name of the service that will receive and process the request
<Request> is the service request


6 最后补充staf的架构:

一句话:点对点

STAF runs as a daemon process (called STAFProc) on each system. So, for example, if you wanted to run STAF on your
office machine and 5 test machines in a lab, you would install STAF on all 6 systems. Then, to use STAF in this
environment, you would start STAFProc on all 6 machines. The collection of machines on which you have installed
STAF is referred to as the STAF Environment.
STAF operates in a peer-to-peer environment; in other words, there is no client-server hierarchy among machines running
STAF. Figure 1 illustrates that the STAFProc daemons serve as the communication mechanism over the network






 二 

STAX 使用了三种技术:STAF, XML 和 Python。简单来说,STAX 在 STAF之 上提供了一些接口,方便用户来操纵STAF进行自动化测试的实现。

介绍一下 STAF 和 STAX 中所用到的概念和机制。

1.1 Services (服务)

STAF 基于可重用的组件来构建自动化测试框架,这些可重用的组件就是 Services(服务)。STAF 中所有的组件都是服务。服务是一系列功能的集合。STAF 本身是一个后台程序 (STAFProc),提供一种轻量级的分发机制,负责把请求转发给这些服务。

STAF 中的服务分为两种:internal (内部服务)和 external(外部服务)。内部服务被集成进 STAFProc 中,提供一些关键性的功能,比如数据管理和同步。外部服务由 STAFProc 动态装入,通过共享库(shared libraries)来访问。

STAF 提供了如下几种常用服务:

  • 程序调用服务(Process Service):内部服务,利用此服务,STAF 可以调用外部程序。
  • 文件系统服务(FileSystem Service):内部服务,利用此服务,STAF 可以对文件系统进行操作,比如复制,删除,查看等操作。
  • 日志服务(Log Service):外部服务,帮助用户进行日志的记录和查看。
  • 资源池服务(ResPool Service):外部服务,提供了对于资源池的管理和操作,如查看,创建和删除操作。
  • 监控服务(Monitor Service):外部服务,提供对于 STAF 运行时的监控功能。
  • 信号量服务(Sem Service):内部服务,提供了两种信号量的操作,mutex 和 event。
  • 压缩服务(Zip Service):外部服务,提供了压缩和解压的功能。
  • Ping服务(Ping Service):内部服务,类似于操作系统的 ping 功能,用于检测远程的 STAF 是否运行。
  • 变量服务(Var Service):内部服务,提供对于系统或者用户级别的环境变量的操作。

STAF 还提供了延迟(Delay Service), 帮助(Help Service), 跟踪(Trace Service)等服务,这里不一一列举。

1.2 请求-响应格式

每个服务都定义了它能接受的请求格式。STAF 通过请求来调用服务的功能,每个请求都以字符串的形式发送,这样可以保证 STAF 能够跨平台的运行。 每个请求都有三个参数,以系统-服务-参数的形式出现。第一个参数表示此请求需要被发送到的 STAF 系统,这个参数被 STAFProc 解析以便确定请求应该被本地处理还是发送到其他的 STAF 系统。 当这个请求被发送到需要处理的 STAF 系统后,STAFProc 解析第二个参数来判断哪个服务会被调用。最后,STAFProc 会把第三个参数转发给需要调用的服务,服务处理这个请求。

当处理完请求后,服务会返回两种数据:返回码和特定于请求的信息。返回码表示服务处理的结果。特定于请求的信息表示服务返回的具体数据,如果请求成功返回,这些信息将包括这次请求所请求的数据,如果请求出现错误,这些信息将包含额外的诊断信息。

完全使用字符串作为请求响应格式可以简化 STAF 的很多方面,包括与其他语言的接口,服务之间的通信,跨平台的操作等。 其他语言只需要通过一个接口 STAFSubmit() 来请求 STAF 的服务,并且只需传递三个字符串参数。服务之间也只需要通过字符串发送接收请求。

1.3 STAX

STAX 是基于 STAF 的执行引擎,它提供了一种 XML 格式的工作流语言。用户可以编写 XML 的脚本文件来通过 STAX 调用 STAF 的服务已完成自动化测试。用户可以不需要和编程语言打交道就可以开发出自己的自动化测试环境。STAX 提供如下的功能:支持并行运行,用户自定义的运行控制粒度,嵌套测试用例,控制运行时间,支持现有的 Java 和 Python 模块等。STAX 还提供了一个图形化的监控工具,通过这个工具,用户可以清晰的看出测试运行的位置,状态和出错信息等。 下面我们将通过与 FTP 和 CVS 的协作完成自动化部署来展示 STAF 和 STAX 的功能。

3 将STAF注册为windows 平台服务。

STAF 并没有提供开机自动启动的功能,在 Windows 平台上,只有当某个用户登录后,才会启动 STAF。这对于自动化测试的环境来说不是一个好消息。 因此我们需要自动启动 STAF 的功能,这在 Linux 上比较简单,只要在 /etc/rc.d/rc.local(如果是 SuSE Linux,就是 /etc/rc.d/boot.local)中加入 STAF 的启动命令/usr/local/staf/bin/STAFProc &就可以了。 Windows 平台上就没有那么方便,因此本小节介绍如何将 STAF 注册为 Windows 的服务,以便能开机自动重启。

  1. 首先使用 instsrv 命令注册一个基本的服务 STAF:instsrv STAF c:\winnt\system32\srvany.exe
  2. 打开注册表编辑器(regedit),找到键值 My Computer\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\STAF。在 STAF 下创建一个键,名字为 Parameters。
  3. 在 Parameters 键下面,创建一个字符串值(String Value),名字为 Application,值为 STAFProc 的完整路径,比如 C:\STAF\bin\STAFProc.exe。
  4. 使用命令services.msc启动Windows服务窗口,找到STAF,右键选择属性,然后定位到登录窗口,选择“允许服务与桌面交互”。
  5. 使用命令net start staf或者重启机器来启动STAF服务。
  6. 使用命令staf local service list来验证STAF是否已经成功启动。
(这个方法应该也可以将其他服务注册为window平台的服务,记下来,以后可能用得到)

参考的文章:

http://www.ibm.com/developerworks/cn/opensource/os-cn-staf/



一、

STAF的安装与下载:

http://staf.sourceforge.net/

STAF 的安装比较简单,只需要按照向导提示进行操作即可。安装完毕后,可以通过 STAFProc 命令启动 STAF。

关闭 STAF 可以用如下的命令: staf local shutdown shutdown

下载完 STAX 后,将其解压到 $STAF_Install_Directory\services\stax 目录中,然后更改 STAF 的配置文件 STAF.cfg。此文件在 $STAF_Install_Directory\bin 目录下。 在 STAF.cfg 文件末尾加上如下的代码,然后重启 STAF。

SERVICE STAX LIBRARY JSTAF EXECUTE {STAF/Config/STAFRoot}/services/stax/STAX.jar \
OPTION J2=-Xmx384m
SERVICE EVENT LIBRARY JSTAF EXECUTE \
{STAF/Config/STAFRoot}/services/stax/STAFEvent.jar
SET MAXQUEUESIZE 10000

安装完毕后可以通过命令查看staf的 service

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>staf local service list
Response
--------
Name      Library    Executable
--------- ---------- -----------------------------------
DELAY     <Internal> <None>
DIAG      <Internal> <None>
ECHO      <Internal> <None>
EVENT     JSTAF      C:\STAF/services/stax/STAFEvent.jar
FS        <Internal> <None>
HANDLE    <Internal> <None>
HELP      <Internal> <None>
LIFECYCLE <Internal> <None>
LOG       STAFLog    <None>
MISC      <Internal> <None>
PING      <Internal> <None>
PROCESS   <Internal> <None>
QUEUE     <Internal> <None>
SEM       <Internal> <None>
SERVICE   <Internal> <None>
SHUTDOWN  <Internal> <None>
STAX      JSTAF      C:\STAF/services/stax/STAX.jar
TRACE     <Internal> <None>
TRUST     <Internal> <None>
VAR       <Internal> <None>

0 0
原创粉丝点击