Fam Zheng writes:
> This is the common code to implement a "VM test" to
>
> 1) Download and initialize a pre-defined VM that has necessary
> dependencies to build QEMU and SSH access.
>
> 2) Archive $SRC_PATH to a .tar file.
>
> 3) Boot the VM, and pass the source tar file to the guest.
>
> 4) SSH into the VM, untar the source tarball, build from the source.
>
> Signed-off-by: Fam Zheng
> ---
> tests/vm/basevm.py | 256
> +
> 1 file changed, 256 insertions(+)
> create mode 100755 tests/vm/basevm.py
>
> diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py
> new file mode 100755
> index 00..e4603f3fba
> --- /dev/null
> +++ b/tests/vm/basevm.py
> @@ -0,0 +1,256 @@
> +#!/usr/bin/env python
> +#
> +# VM testing base class
> +#
> +# Copyright 2017 Red Hat Inc.
> +#
> +# Authors:
> +# Fam Zheng
> +#
> +# This code is licensed under the GPL version 2 or later. See
> +# the COPYING file in the top-level directory.
> +#
> +
> +import os
> +import sys
> +import logging
> +import time
> +import datetime
> +sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..",
> "scripts"))
> +from qemu import QEMUMachine
> +import subprocess
> +import hashlib
> +import optparse
> +import atexit
> +import tempfile
> +import shutil
> +import multiprocessing
> +import traceback
> +
> +SSH_KEY = open(os.path.join(od.path.dirname(__file__),
> + "..", "keys", "id_rsa")).read()
> +SSH_PUB_KEY = open(os.path.join(od.path.dirname(__file__),
> + "..", "keys", "id_rsa.pub")).read()
Hmm I don't think this was tested:
12:35:56 [alex@zen:~/l/q/qemu.git] review/fam-vm-build-test-v7(+2/-2) ± make
vm-build-netbsd
CHK version_gen.h
VM-IMAGE netbsd
Traceback (most recent call last):
File "/home/alex/lsrc/qemu/qemu.git/tests/vm/netbsd", line 17, in
import basevm
File "/home/alex/lsrc/qemu/qemu.git/tests/vm/basevm.py", line 30, in
SSH_KEY = open(os.path.join(od.path.dirname(__file__),
NameError: name 'od' is not defined
/home/alex/lsrc/qemu/qemu.git/tests/vm/Makefile.include:23: recipe for target
'tests/vm/netbsd.img' failed
> +
> +class BaseVM(object):
> +GUEST_USER = "qemu"
> +GUEST_PASS = "qemupass"
> +ROOT_PASS = "qemupass"
> +
> +# The script to run in the guest that builds QEMU
> +BUILD_SCRIPT = ""
> +# The guest name, to be overridden by subclasses
> +name = "#base"
> +def __init__(self, debug=False, vcpus=None):
> +self._guest = None
> +self._tmpdir = tempfile.mkdtemp(prefix="vm-test-", suffix=".tmp",
> dir=".")
> +atexit.register(shutil.rmtree, self._tmpdir)
> +
> +self._ssh_key_file = os.path.join(self._tmpdir, "id_rsa")
> +open(self._ssh_key_file, "w").write(SSH_KEY)
> +subprocess.check_call(["chmod", "600", self._ssh_key_file])
> +
> +self._ssh_pub_key_file = os.path.join(self._tmpdir, "id_rsa.pub")
> +open(self._ssh_pub_key_file, "w").write(SSH_PUB_KEY)
> +
> +self.debug = debug
> +self._stderr = sys.stderr
> +self._devnull = open(os.devnull, "w")
> +if self.debug:
> +self._stdout = sys.stdout
> +else:
> +self._stdout = self._devnull
> +self._args = [ \
> +"-nodefaults", "-m", "2G",
> +"-cpu", "host",
> +"-netdev", "user,id=vnet,hostfwd=:0.0.0.0:0-:22",
> +"-device", "virtio-net-pci,netdev=vnet",
> +"-vnc", ":0,to=20",
> +"-serial", "file:%s" % os.path.join(self._tmpdir, "serial.out")]
> +if vcpus:
> +self._args += ["-smp", str(vcpus)]
> +if os.access("/dev/kvm", os.R_OK | os.W_OK):
> +self._args += ["-enable-kvm"]
> +else:
> +logging.info("KVM not available, not using -enable-kvm")
> +self._data_args = []
> +
> +def _download_with_cache(self, url, sha256sum=None):
> +def check_sha256sum(fname):
> +if not sha256sum:
> +return True
> +checksum = subprocess.check_output(["sha256sum",
> fname]).split()[0]
> +return sha256sum == checksum
> +
> +cache_dir = os.path.expanduser("~/.cache/qemu-vm/download")
> +if not os.path.exists(cache_dir):
> +os.makedirs(cache_dir)
> +fname = os.path.join(cache_dir, hashlib.sha1(url).hexdigest())
> +if os.path.exists(fname) and check_sha256sum(fname):
> +return fname
> +logging.debug("Downloading %s to %s...", url, fname)
> +subprocess.check_call(["wget", "-c", url, "-O", fname + ".download"],
> + stdout=self._stdout, stderr=self._stderr)
> +os.rename(fname + ".download", fname)
> +return fname
> +
> +def _ssh_do(self, user, cmd, check, interactive=False):
> +ssh_cmd = ["ssh", "-q",
> + "-o", "StrictHostKeyChecking=no",
> + "-o"