-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: boot generated VM and wait for ssh port
- Loading branch information
Showing
6 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import contextlib | ||
import subprocess | ||
from unittest.mock import call, patch | ||
|
||
import pytest | ||
|
||
from testutil import has_executable, get_free_port, wait_ssh_ready | ||
|
||
|
||
def test_get_free_port(): | ||
port_nr = get_free_port() | ||
assert port_nr > 1024 and port_nr < 65535 | ||
|
||
|
||
@pytest.mark.skipif(not has_executable("nc"), reason="needs nc") | ||
@patch("time.sleep") | ||
def test_wait_ssh_ready(mocked_sleep): | ||
port = get_free_port() | ||
with pytest.raises(ConnectionRefusedError): | ||
wait_ssh_ready(port, sleep=0.1, max_wait_sec=0.35) | ||
assert mocked_sleep.call_args_list == [call(0.1), call(0.1), call(0.1)] | ||
# now make port ready | ||
with contextlib.ExitStack() as cm: | ||
p = subprocess.Popen(f"echo OpenSSH | nc -l {port}", shell=True) | ||
cm.callback(p.kill) | ||
wait_ssh_ready(port, sleep=0.1, max_wait_sec=10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pathlib | ||
import subprocess | ||
import sys | ||
|
||
from testutil import get_free_port, wait_ssh_ready | ||
|
||
|
||
class VM: | ||
MEM = "2000" | ||
QEMU = "qemu-system-x86_64" | ||
|
||
def __init__(self, img, snapshot=True): | ||
self._img = pathlib.Path(img) | ||
self._qemu_p = None | ||
self._ssh_port = None | ||
self._snapshot = snapshot | ||
|
||
def __del__(self): | ||
self.force_stop() | ||
|
||
def start(self): | ||
if self._qemu_p is not None: | ||
return | ||
log_path = self._img.with_suffix(".serial-log") | ||
self._ssh_port = get_free_port() | ||
qemu_cmdline = [ | ||
self.QEMU, "-enable-kvm", | ||
"-m", self.MEM, | ||
# get "illegal instruction" inside the VM otherwise | ||
"-cpu", "host", | ||
"-nographic", | ||
"-serial", "stdio", | ||
"-monitor", "none", | ||
"-netdev", f"user,id=net.0,hostfwd=tcp::{self._ssh_port}-:22", | ||
"-device", "rtl8139,netdev=net.0", | ||
] | ||
if self._snapshot: | ||
qemu_cmdline.append("-snapshot") | ||
qemu_cmdline.append(self._img) | ||
self._log(f"vm starting, log available at {log_path}") | ||
|
||
# XXX: use systemd-run to ensure cleanup? | ||
self._qemu_p = subprocess.Popen( | ||
qemu_cmdline, stdout=sys.stdout, stderr=sys.stderr) | ||
# XXX: also check that qemu is working and did not crash | ||
self.wait_ssh_ready() | ||
self._log(f"vm ready at port {self._ssh_port}") | ||
|
||
def _log(self, msg): | ||
# XXX: use a proper logger | ||
sys.stdout.write(msg.rstrip("\n") + "\n") | ||
|
||
def wait_ssh_ready(self): | ||
wait_ssh_ready(self._ssh_port, sleep=1, max_wait_sec=600) | ||
|
||
def force_stop(self): | ||
if self._qemu_p: | ||
self._qemu_p.kill() | ||
self._qemu_p = None | ||
|
||
def __enter__(self): | ||
self.start() | ||
return self | ||
|
||
def __exit__(self, type, value, tb): | ||
self.force_stop() |