提交 f457d3c3 编写于 作者: L Lucas Meneghel Rodrigues

Merge pull request #752 from clebergnu/vm_snapshot_bug

VM plugin cleanup bug
......@@ -40,30 +40,25 @@ class RunVM(plugin.Plugin):
default_hypervisor_uri = 'qemu:///system'
msg = 'test execution on a Virtual Machine'
self.vm_parser = parser.runner.add_argument_group(msg)
self.vm_parser.add_argument('--vm-domain', dest='vm_domain',
self.vm_parser.add_argument('--vm-domain',
help=('Specify Libvirt Domain Name'))
self.vm_parser.add_argument('--vm-hypervisor-uri',
dest='vm_hypervisor_uri',
default=default_hypervisor_uri,
help=('Specify hypervisor URI driver '
'connection. Current: %s' %
default_hypervisor_uri))
self.vm_parser.add_argument('--vm-hostname', dest='vm_hostname',
self.vm_parser.add_argument('--vm-hostname',
help='Specify VM hostname to login')
self.vm_parser.add_argument('--vm-username', dest='vm_username',
default=username,
self.vm_parser.add_argument('--vm-username', default=username,
help='Specify the username to login on VM')
self.vm_parser.add_argument('--vm-password', dest='vm_password',
self.vm_parser.add_argument('--vm-password',
default=None,
help='Specify the password to login on VM')
self.vm_parser.add_argument('--vm-cleanup', dest='vm_cleanup',
action='store_true',
default=False,
self.vm_parser.add_argument('--vm-cleanup',
action='store_true', default=False,
help='Restore VM to a previous state, '
'before running tests')
self.vm_parser.add_argument('--vm-no-copy',
dest='vm_no_copy',
action='store_true',
self.vm_parser.add_argument('--vm-no-copy', action='store_true',
help="Don't copy tests and use the "
"exact uri on VM machine.")
self.configured = True
......
......@@ -145,5 +145,7 @@ class VMTestResult(RemoteTestResult):
def tear_down(self):
super(VMTestResult, self).tear_down()
if self.args.vm_cleanup is True and self.vm.snapshot is not None:
self.vm.restore_snapshot()
if self.args.vm_cleanup is True:
self.vm.stop()
if self.vm.snapshot is not None:
self.vm.restore_snapshot()
import os
import sys
import shutil
import tempfile
from avocado import Test
from avocado.utils import script
from avocado.utils import process
CLEAN_TEST = """import os
from avocado import Test
class Clean(Test):
def test(self):
path = os.path.expanduser("~/.avocado-has-not-touched-me")
self.assertFalse(os.path.exists(path))
"""
DIRTY_TEST = """import os
from avocado import Test
class Dirty(Test):
def test(self):
self.touch(os.path.expanduser("~/.avocado-has-not-touched-me"))
@staticmethod
def touch(name):
with open(name, 'w'):
os.utime(name, None)
"""
class VMCleanup(Test):
"""
Tests the Avocado VM plugin `--vm-cleanup` feature
The approach chosen here is to have a first run of Avocado making sure that
a flag file does not exist. A second run of Avocado, now executed with an
additional `--vm-cleanup` parameter runs a test that creates the flag file.
Finally, the third and last execution of re-runs the same test as the first
run, checking that the flag file does *not* exist, that is, the clean up of
the VM did work.
Because this test requires a libvirt domain and the hostname/address of
that virtual machine, these parameters assume no defaults for safety and/or
security causes. Please edit the vm-cleanup.yaml file with the appropriate
parameters.
"""
def setUp(self):
vm_domain = self.params.get("vm_domain", default=None)
vm_host = self.params.get("vm_host", default=None)
if vm_domain is None or vm_host is None:
self.skip('Either "vm_domain" or "vm_host" parameters have not '
'been given. Please edit the "vm-cleanup.yaml" file '
'with the appropriate parameters')
self.tmpdir = tempfile.mkdtemp()
clean_test = os.path.join(self.tmpdir, 'clean.py')
self.clean_test_path = script.make_script(clean_test, CLEAN_TEST)
dirty_test = os.path.join(self.tmpdir, 'dirty.py')
self.dirty_test_path = script.make_script(dirty_test, DIRTY_TEST)
def test(self):
vm_domain = self.params.get("vm_domain", default=None)
vm_host = self.params.get("vm_host", default=None)
vm_username = self.params.get("vm_username", default=None)
vm_password = self.params.get("vm_password", default=None)
cmd = ('avocado run --sysinfo=off --job-results-dir %s --vm-domain=%s '
'--vm-host=%s')
cmd %= (self.tmpdir, vm_domain, vm_host)
if vm_username:
cmd += ' --vm-username=%s' % vm_username
if vm_password:
cmd += ' --vm-password=%s' % vm_password
cmd_clean = '%s %s' % (cmd, self.clean_test_path)
cmd_dirty = '%s --vm-cleanup %s' % (cmd, self.dirty_test_path)
process.run(cmd_clean)
process.run(cmd_dirty)
process.run(cmd_clean)
def tearDown(self):
shutil.rmtree(self.tmpdir)
vm_domain: REPLACE_WITH_DOMAIN_NAME
vm_host: REPLACE_WITH_VM_DOMAIN_HOSTNAME_OR_IP_ADDRESS
vm_username: REPLACE_WITH_USERNAME_ON_VM
vm_password: BETTER_COMMENT_THIS_AND_USE_AN_SSH_KEY
......@@ -52,6 +52,7 @@ class VMTestResultTest(unittest.TestCase):
self.remote = VMTestResult(Stream, Args)
# vm.RemoteTestResult.tear_down()
RemoteTestResult.should_receive('tear_down').once().ordered()
mock_vm.should_receive('stop').once().ordered()
mock_vm.should_receive('restore_snapshot').once().ordered()
def tearDown(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册