写完代码测试时重复的最多的步骤就是

  1. 编译,复制vmlinuz和xen.gz
  2. 重启VMware虚拟机
  3. 启动domainU xm create domU.conf 4. 开一个screen窗口attach到domainU的console xm console #domid 5. 在domainU中运行测试程序

于是写了个自动执行3 4 5的脚步,主要用到了熊熊推荐的pexpect,这东东很赞啊

为了提高用户体验,读取domainU的启动信息时我采用的方法是读一行输出一行,读到结尾登陆字符时通过超时设置退出循环,这样可能效率比较低,不过测试脚本也不care这个了

实际使用时碰到了另一个问题,domainU执行完自动命令后命令行会出现很严重的对齐问题,最后发现登陆后运行一次reset就可以了。

脚本如下

#!/usr/bin/python

# Automatic test script for Xen DomainU
# Author: zellux

import pexpect, os

conf = {
    'login_name'     : 'm2-vm2',
    'domainU_name'   : 'R900-DomU0',
    'domainU_conf'   : '/home/wyx/domU1',
    'domainU_id'     : '2',
    'domainU_user'   : 'wyx',
    'domainU_passwd' : 'wyx',
    }

# Command to be executed after domainU starts
cmd = """
cd m2
cd reg_test
./base_test -t affinity
"""

# Create domainU
print '[M2 Test] Starting domainU ...',
pexpect.run('xm create %(domainU_conf)s' % conf)
print 'done'

# Get domainU id
print '[M2 Test] SGetting domainU id ...',
ret = pexpect.run('xm list')
for line in ret.split('\n')[1:]:
    part = line.split()
    if part[0] == conf['domainU_name']:
        conf['domainU_id'] = part[1]
        break
print 'done'

# Run domainU commands
child = pexpect.spawn('xm console %(domainU_id)s' % conf)
print '[M2 Test] SReading from domainU console...'
try:
    while True:
        child.expect('\n', timeout=1, )
        print child.before.split['\n'][-1]
except:
    pass

child.expect('%(login_name)s login:' % conf)
child.sendline(conf['domainU_user'])
child.sendline(conf['domainU_passwd'])

for line in cmd.split('\n'):
    child.sendline(line)

try:
    child.expect(pexpect.EOF, timeout=1)
except:
    pass
print child.before
child.interact()