STAF學習筆記

Just4life發表於2013-07-23

1 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>


2 關於什麼是STAF(摘自他人文章)

才開始看,後續會根據自己的理解更新。

1.STAF(STAX)

Software Test Automation Framework (STAF) 是開源、跨平臺、支援多語言並且基於可重用的元件來構建的自動化測試框架。它為自動化測試建立了基礎,並且提供了一種可插撥的機制支援不同的平臺和語言。STAF 採用點對點的實現機制,被用來減輕自動化測試的工作負擔,加快自動化測試的程式。在 STAF 的環境中,所有的機器都是對等的,沒有客戶端和伺服器的區分。

Software Test Automation eXecution Engine (STAX)是基於 STAF 的執行引擎。它在 STAF 的基礎上,幫助使用者實現測試用例的分發、部署、執行以及結果分析。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/


總的來說這一節不算是原創,主要取自別人的資料

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