1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Tests for checking built image with VirtualBox.
"""
import os
import pathlib
import random
import string
import subprocess
import tempfile
import time
import pytest
@pytest.fixture(name='random_string')
def fixture_random_string():
"""Generate a random string."""
def _random_string():
return ''.join(
[random.choice(string.ascii_lowercase) for _ in range(8)])
return _random_string
@pytest.fixture(name='build_info')
def fixture_build_info(random_string):
"""Create basic build information."""
with tempfile.TemporaryDirectory() as output_dir:
yield {
'output_dir': output_dir,
'build_stamp': random_string(),
'current_target': 'virtualbox-amd64'
}
@pytest.fixture(name='invoke')
def fixture_invoke(build_info):
"""Return a helper that invokes freedom-maker."""
def _invoke(targets=None, **kwargs):
"""Invoke Freedom Maker."""
parameters = ['--build-dir', build_info['output_dir']]
if 'build_stamp' not in kwargs:
parameters += ['--build-stamp', build_info['build_stamp']]
command = ['python3', '-m', 'freedommaker'] + parameters + targets
subprocess.check_call(command)
return _invoke
def get_built_file(build_info):
"""Return the path of the expected built file."""
file_name = 'freedombox-unstable_{build_stamp}_all-amd64.vdi.xz' \
.format(build_stamp=build_info['build_stamp'])
return os.path.join(build_info['output_dir'], file_name)
def _run(*args, ignore_errors=False):
"""Execute a command."""
subprocess.run(*args, check=not ignore_errors)
@pytest.mark.skipif(os.environ.get('FM_RUN_VM_TESTS') != 'true',
reason='Not requested')
def test_basic_build(build_info, invoke):
"""Test booting and opening SSH shell.
Also:
- Output the plinth diagnostic log.
"""
invoke(['virtualbox-amd64'])
vm_name = 'freedom-maker-test'
test_ssh_port = 2222
first_run_wait_time = 120
compressed_built_file = get_built_file(build_info)
built_file = compressed_built_file.rsplit('.', maxsplit=1)[0]
if not os.path.isfile(built_file):
subprocess.check_call(
['unxz', '--keep', '--force', compressed_built_file])
passwd_tool = pathlib.Path(__file__).parent / '../passwd_in_image.py'
_run([
'sudo', 'python3', passwd_tool, built_file, 'fbx', '--password', 'frdm'
])
try:
_run([
'VBoxManage', 'createvm', '--name', vm_name, '--ostype', 'Debian',
'--register'
])
_run([
'VBoxManage', 'storagectl', vm_name, '--name', 'SATA Controller',
'--add', 'sata', '--controller', 'IntelAHCI'
])
_run([
'VBoxManage', 'storageattach', vm_name, '--storagectl',
'SATA Controller', '--port', '0', '--device', '0', '--type', 'hdd',
'--medium', built_file
])
_run([
'VBoxManage', 'modifyvm', vm_name, '--pae', 'on', '--memory',
'1024', '--vram', '128', '--firmware', 'efi64', '--nic1', 'nat',
'--natpf1', ',tcp,,{port},,22'.format(port=test_ssh_port)
])
_run(['VBoxManage', 'startvm', vm_name, '--type', 'headless'])
time.sleep(first_run_wait_time)
echo = subprocess.Popen(['echo', 'frdm'], stdout=subprocess.PIPE)
process = subprocess.Popen([
'sshpass', '-p', 'frdm', 'ssh', '-o',
'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no',
'-t', '-t', '-p',
str(test_ssh_port), 'fbx@127.0.0.1', 'sudo plinth --list-modules'
],
stdin=echo.stdout)
process.communicate()
finally:
echo = subprocess.Popen(['echo', 'frdm'], stdout=subprocess.PIPE)
process = subprocess.Popen([
'sshpass', '-p', 'frdm', 'ssh', '-o',
'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no',
'-t', '-t', '-p',
str(test_ssh_port), 'fbx@127.0.0.1', 'sudo shutdown now'
],
stdin=echo.stdout)
process.communicate()
time.sleep(30)
_run(['VBoxManage', 'modifyvm', vm_name, '--hda', 'none'],
ignore_errors=True)
_run(['VBoxManage', 'unregistervm', vm_name, '--delete'],
ignore_errors=True)
|