3 # VM testing base class
5 # Copyright 2017 Red Hat Inc.
8 # Fam Zheng <famz@redhat.com>
10 # This code is licensed under the GPL version 2 or later. See
11 # the COPYING file in the top-level directory.
14 from __future__
import print_function
20 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), "..", "..", "scripts"))
21 from qemu
import QEMUMachine
, kvm_available
28 import multiprocessing
31 SSH_KEY
= open(os
.path
.join(os
.path
.dirname(__file__
),
32 "..", "keys", "id_rsa")).read()
33 SSH_PUB_KEY
= open(os
.path
.join(os
.path
.dirname(__file__
),
34 "..", "keys", "id_rsa.pub")).read()
38 GUEST_PASS
= "qemupass"
39 ROOT_PASS
= "qemupass"
41 # The script to run in the guest that builds QEMU
43 # The guest name, to be overridden by subclasses
45 # The guest architecture, to be overridden by subclasses
47 def __init__(self
, debug
=False, vcpus
=None):
49 self
._tmpdir
= os
.path
.realpath(tempfile
.mkdtemp(prefix
="vm-test-",
52 atexit
.register(shutil
.rmtree
, self
._tmpdir
)
54 self
._ssh
_key
_file
= os
.path
.join(self
._tmpdir
, "id_rsa")
55 open(self
._ssh
_key
_file
, "w").write(SSH_KEY
)
56 subprocess
.check_call(["chmod", "600", self
._ssh
_key
_file
])
58 self
._ssh
_pub
_key
_file
= os
.path
.join(self
._tmpdir
, "id_rsa.pub")
59 open(self
._ssh
_pub
_key
_file
, "w").write(SSH_PUB_KEY
)
62 self
._stderr
= sys
.stderr
63 self
._devnull
= open(os
.devnull
, "w")
65 self
._stdout
= sys
.stdout
67 self
._stdout
= self
._devnull
69 "-nodefaults", "-m", "4G",
71 "-netdev", "user,id=vnet,hostfwd=:127.0.0.1:0-:22",
72 "-device", "virtio-net-pci,netdev=vnet",
73 "-vnc", "127.0.0.1:0,to=20",
74 "-serial", "file:%s" % os
.path
.join(self
._tmpdir
, "serial.out")]
75 if vcpus
and vcpus
> 1:
76 self
._args
+= ["-smp", str(vcpus
)]
77 if kvm_available(self
.arch
):
78 self
._args
+= ["-enable-kvm"]
80 logging
.info("KVM not available, not using -enable-kvm")
83 def _download_with_cache(self
, url
, sha256sum
=None):
84 def check_sha256sum(fname
):
87 checksum
= subprocess
.check_output(["sha256sum", fname
]).split()[0]
88 return sha256sum
== checksum
90 cache_dir
= os
.path
.expanduser("~/.cache/qemu-vm/download")
91 if not os
.path
.exists(cache_dir
):
92 os
.makedirs(cache_dir
)
93 fname
= os
.path
.join(cache_dir
, hashlib
.sha1(url
).hexdigest())
94 if os
.path
.exists(fname
) and check_sha256sum(fname
):
96 logging
.debug("Downloading %s to %s...", url
, fname
)
97 subprocess
.check_call(["wget", "-c", url
, "-O", fname
+ ".download"],
98 stdout
=self
._stdout
, stderr
=self
._stderr
)
99 os
.rename(fname
+ ".download", fname
)
102 def _ssh_do(self
, user
, cmd
, check
, interactive
=False):
103 ssh_cmd
= ["ssh", "-q",
104 "-o", "StrictHostKeyChecking=no",
105 "-o", "UserKnownHostsFile=" + os
.devnull
,
106 "-o", "ConnectTimeout=1",
107 "-p", self
.ssh_port
, "-i", self
._ssh
_key
_file
]
110 assert not isinstance(cmd
, str)
111 ssh_cmd
+= ["%s@127.0.0.1" % user
] + list(cmd
)
112 logging
.debug("ssh_cmd: %s", " ".join(ssh_cmd
))
113 r
= subprocess
.call(ssh_cmd
)
115 raise Exception("SSH command failed: %s" % cmd
)
119 return self
._ssh
_do
(self
.GUEST_USER
, cmd
, False)
121 def ssh_interactive(self
, *cmd
):
122 return self
._ssh
_do
(self
.GUEST_USER
, cmd
, False, True)
124 def ssh_root(self
, *cmd
):
125 return self
._ssh
_do
("root", cmd
, False)
127 def ssh_check(self
, *cmd
):
128 self
._ssh
_do
(self
.GUEST_USER
, cmd
, True)
130 def ssh_root_check(self
, *cmd
):
131 self
._ssh
_do
("root", cmd
, True)
133 def build_image(self
, img
):
134 raise NotImplementedError
136 def add_source_dir(self
, src_dir
):
137 name
= "data-" + hashlib
.sha1(src_dir
).hexdigest()[:5]
138 tarfile
= os
.path
.join(self
._tmpdir
, name
+ ".tar")
139 logging
.debug("Creating archive %s for src_dir dir: %s", tarfile
, src_dir
)
140 subprocess
.check_call(["./scripts/archive-source.sh", tarfile
],
141 cwd
=src_dir
, stdin
=self
._devnull
,
142 stdout
=self
._stdout
, stderr
=self
._stderr
)
143 self
._data
_args
+= ["-drive",
144 "file=%s,if=none,id=%s,cache=writeback,format=raw" % \
147 "virtio-blk,drive=%s,serial=%s,bootindex=1" % (name
, name
)]
149 def boot(self
, img
, extra_args
=[]):
150 args
= self
._args
+ [
152 "-drive", "file=%s,if=none,id=drive0,cache=writeback" % img
,
153 "-device", "virtio-blk,drive=drive0,bootindex=0"]
154 args
+= self
._data
_args
+ extra_args
155 logging
.debug("QEMU args: %s", " ".join(args
))
156 qemu_bin
= os
.environ
.get("QEMU", "qemu-system-" + self
.arch
)
157 guest
= QEMUMachine(binary
=qemu_bin
, args
=args
)
161 logging
.error("Failed to launch QEMU, command line:")
162 logging
.error(" ".join([qemu_bin
] + args
))
163 logging
.error("Log:")
164 logging
.error(guest
.get_log())
165 logging
.error("QEMU version >= 2.10 is required")
167 atexit
.register(self
.shutdown
)
169 usernet_info
= guest
.qmp("human-monitor-command",
170 command_line
="info usernet")
172 for l
in usernet_info
["return"].splitlines():
174 if "TCP[HOST_FORWARD]" in fields
and "22" in fields
:
175 self
.ssh_port
= l
.split()[3]
176 if not self
.ssh_port
:
177 raise Exception("Cannot find ssh port from 'info usernet':\n%s" % \
180 def wait_ssh(self
, seconds
=300):
181 starttime
= datetime
.datetime
.now()
182 endtime
= starttime
+ datetime
.timedelta(seconds
=seconds
)
184 while datetime
.datetime
.now() < endtime
:
185 if self
.ssh("exit 0") == 0:
188 seconds
= (endtime
- datetime
.datetime
.now()).total_seconds()
189 logging
.debug("%ds before timeout", seconds
)
192 raise Exception("Timeout while waiting for guest ssh")
195 self
._guest
.shutdown()
200 def qmp(self
, *args
, **kwargs
):
201 return self
._guest
.qmp(*args
, **kwargs
)
203 def parse_args(vmcls
):
205 def get_default_jobs():
206 if kvm_available(vmcls
.arch
):
207 return multiprocessing
.cpu_count() / 2
211 parser
= optparse
.OptionParser(
212 description
="VM test utility. Exit codes: "
214 "1 = command line error, "
215 "2 = environment initialization failed, "
216 "3 = test command failed")
217 parser
.add_option("--debug", "-D", action
="store_true",
218 help="enable debug output")
219 parser
.add_option("--image", "-i", default
="%s.img" % vmcls
.name
,
220 help="image file name")
221 parser
.add_option("--force", "-f", action
="store_true",
222 help="force build image even if image exists")
223 parser
.add_option("--jobs", type=int, default
=get_default_jobs(),
224 help="number of virtual CPUs")
225 parser
.add_option("--verbose", "-V", action
="store_true",
226 help="Pass V=1 to builds within the guest")
227 parser
.add_option("--build-image", "-b", action
="store_true",
229 parser
.add_option("--build-qemu",
230 help="build QEMU from source in guest")
231 parser
.add_option("--build-target",
232 help="QEMU build target", default
="check")
233 parser
.add_option("--interactive", "-I", action
="store_true",
234 help="Interactively run command")
235 parser
.add_option("--snapshot", "-s", action
="store_true",
236 help="run tests with a snapshot")
237 parser
.disable_interspersed_args()
238 return parser
.parse_args()
242 args
, argv
= parse_args(vmcls
)
243 if not argv
and not args
.build_qemu
and not args
.build_image
:
244 print("Nothing to do?")
246 logging
.basicConfig(level
=(logging
.DEBUG
if args
.debug
248 vm
= vmcls(debug
=args
.debug
, vcpus
=args
.jobs
)
250 if os
.path
.exists(args
.image
) and not args
.force
:
251 sys
.stderr
.writelines(["Image file exists: %s\n" % args
.image
,
252 "Use --force option to overwrite\n"])
254 return vm
.build_image(args
.image
)
256 vm
.add_source_dir(args
.build_qemu
)
257 cmd
= [vm
.BUILD_SCRIPT
.format(
258 configure_opts
= " ".join(argv
),
260 target
=args
.build_target
,
261 verbose
= "V=1" if args
.verbose
else "")]
266 img
+= ",snapshot=on"
269 except Exception as e
:
270 if isinstance(e
, SystemExit) and e
.code
== 0:
272 sys
.stderr
.write("Failed to prepare guest environment\n")
273 traceback
.print_exc()
277 if vm
.ssh_interactive(*cmd
) == 0:
282 if vm
.ssh(*cmd
) != 0: