您的位置:首页 > 编程语言 > Python开发

使用STAF进行自动化安装测试

2014-08-26 16:57 651 查看
首先安装STAF

在实体机和目标机器中均需要安装,STAF拥有对Python支持的库函数,可以直接调用,比较方便,所以测试脚本决定用Python来写。

需要注意的是,在/staf/bin中有一个STAF.cfg文件需要修改server和client端的trustlevel = 5.注意不同的command的trust level的要求不一样,所以在我的测试环境里面直接设定成为5比较方便。

安装之后,需要将STAF在实验机中设定为开机自启动。以便可以直接进行连接操作,不过我是使用虚拟机进行测试,只要直接载入一个开机的snapshot就可以了。

具体可以参照这个网址的开机自启动设置。

点击打开链接

这次测试环境为windows,方法是目标机要从server上面检查并且下载一个安装文件,并且执行安装。所以,windows 必须执行静默安装,并且通过STAF执行ftp命令时候需要通过载入一个conf文件来执行下载任务。

同时在Python中需要操作exsi,这个API可以直接Google到。Python真的很方便额。。。

需要注意的是在操作时候,安装和check的代码之间需要有一个sleep等待,以保证client端执行成功。

#-------------------------------------------------------------------------------
# Name:        STAF_test
# Purpose:     Used for staf automation test. rollback a snapshot, download and
#              install a program from server
#
# Author:      Levin
#
# Requirement: You need to install the Staf on your local machine and add your
#              machine's ip to the */staf/bin/STAF.cfg. And add the */staf/bin
#              to your python path.
#
# Created:     25/08/2014
# Copyright:   (c) Levin 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------

from PySTAF import *
import pysphere
from xml.etree import ElementTree as ET
import sys
from time import sleep

##-----------Prepare the variables need to use-----------------##
nodeall = ET.parse('config.xml')
IPnode = nodeall.find('SETTING')
EXSIIP = IPnode.getchildren()[0].text
UserName = IPnode.getchildren()[1].text
PassWord = IPnode.getchildren()[2].text
ClientIP = IPnode.getchildren()[3].text
VirtualMachine = IPnode.getchildren()[4].text
Snapshot = IPnode.getchildren()[5].text

Pathnode = nodeall.find('WORKINGPATH')
ClientPath = Pathnode.getchildren()[0].text
LocalPath = Pathnode.getchildren()[1].text

##------------------installation process------------##
def staf_install():
try:
handle = STAFHandle("test")
except STAFException, e:
print "Error registering with STAF, RC: %d" % e.rc
sys.exit(e.rc)

while (True):
result = handle.submit(ClientIP, "ping", "ping")
if (result.rc == 0):
break
print "Error submitting request, RC: %d, Result: %s" % (result.rc, result.result)
print "connected"

result = handle.submit("local", "FS", "COPY File "+LocalPath+r"\ftpSample.conf TODIRECTORY "+ClientPath+" TOMACHINE "+ClientIP)
if (result.rc == 0):
print "Upload config file succeed!"
else :
print "Upload config file failed!"
print "%s" % result.result
sys.exit(1)
result = handle.submit(ClientIP, "process", "start shell command" +  "\"ftp -s:ftpSample.conf\"" + "workdir " + ClientPath)
if (result.rc == 0):
print "Download install file succeed!"
else :
print "Download install file failed!"
print "%s" % result.result
sys.exit(2)
print "Now wait 6 seconds"
sleep(6)
result = handle.submit(ClientIP,"process", "start shell command" + "\"ActivePerl-5.16.3.1604-MSWin32-x86-298023.msi/qn\""+ " workdir " + ClientPath)
print "Now wait 23 seconds for installation complete"
sleep(23)
result = handle.submit(ClientIP,"fs", "list directory " + r"C:\Perl\bin")
if ("perl.exe" in result.resultObj):
print "Installation succeed!"
else :
print "Installation failed!"
sys.exit(3)

##--------------reset the exsi to clean the environment-------##
def Reset_Virtualmachine():
server = pysphere.VIServer()
try :
server.connect(EXSIIP, UserName, PassWord)
except pysphere.VIException, e :
print "Can not connect to exsi. Exit now!"
sys.exit(e.rc)
print "Connected to EXSI"
vm = server.get_vm_by_name("win2k3-R2-32bits")
vm.revert_to_named_snapshot(Snapshot)

if __name__ == '__main__':
Reset_Virtualmachine()
print "Enter to Staf install."
staf_install()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自动化 python STAF