diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 0000000..9abe84c --- /dev/null +++ b/tests/common.py @@ -0,0 +1,27 @@ +# Copyright 2024 Canonical Ltd. +# Authors: +# - Hector Cao +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 3, as published +# by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, +# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . +# + +import os +import Qemu + +script_path=os.path.dirname(os.path.realpath(__file__)) + '/' +# put in /var/tmp instead of /tmp to be persistent across reboots +guest_workdir='/var/tmp/tdxtest' + +def deploy_and_setup(m : Qemu.QemuSSH): + m.rsync_file(f'{script_path}', f'{guest_workdir}') + m.check_exec(f'cd {guest_workdir} && ./setup_guest.sh') diff --git a/tests/lib/Qemu.py b/tests/lib/Qemu.py index fa217aa..d1a52ac 100644 --- a/tests/lib/Qemu.py +++ b/tests/lib/Qemu.py @@ -439,7 +439,7 @@ def __init__(self, self._create_image() # TODO : WA for log, to be removed - print('\n\nQemuMachine created.') + print(f'\n\nQemuMachine created (debug={self.debug}).') self.qcmd = QemuCommand( self.workdir_name, diff --git a/tests/setup_guest.sh b/tests/setup_guest.sh new file mode 100755 index 0000000..21cff7d --- /dev/null +++ b/tests/setup_guest.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# this script is supposed to be executed under root + +DEBIAN_FRONTEND=noninteractive apt install -y python3 python3-pip + +cd lib/tdx-tools/ +python3 -m pip install --break-system-packages ./ diff --git a/tests/tests/test_boot_basic.py b/tests/tests/test_boot_basic.py index ff97c79..93de347 100644 --- a/tests/tests/test_boot_basic.py +++ b/tests/tests/test_boot_basic.py @@ -17,8 +17,7 @@ import os import Qemu - -script_path=os.path.dirname(os.path.realpath(__file__)) +from common import * def test_guest_boot(): """ @@ -28,8 +27,8 @@ def test_guest_boot(): qm.run() m = Qemu.QemuSSH(qm) - m.rsync_file(f'{script_path}/../lib', '/tmp/tdxtest/') - m.check_exec('cd /tmp/tdxtest/lib/tdx-tools/ && python3 -m pip install --break-system-packages ./') + + deploy_and_setup(m) # tdx guest device driver m.check_exec('ls -la /dev/tdx_guest') diff --git a/tests/tests/test_guest_eventlog.py b/tests/tests/test_guest_eventlog.py index 0a4c532..d2030b6 100644 --- a/tests/tests/test_guest_eventlog.py +++ b/tests/tests/test_guest_eventlog.py @@ -22,8 +22,7 @@ import Qemu import util - -script_path=os.path.dirname(os.path.realpath(__file__)) +from common import * def test_guest_eventlog(): """ @@ -34,8 +33,7 @@ def test_guest_eventlog(): m = Qemu.QemuSSH(qm) - m.rsync_file(f'{script_path}/../lib', '/tmp/tdxtest/') - m.check_exec('cd /tmp/tdxtest/lib/tdx-tools/ && python3 -m pip install --break-system-packages ./') + deploy_and_setup(m) stdout, stderr = m.check_exec('tdeventlog') for l in stderr.readlines(): @@ -52,8 +50,7 @@ def test_guest_eventlog_initrd(): m = Qemu.QemuSSH(qm) - m.rsync_file(f'{script_path}/../lib', '/tmp/tdxtest/') - m.check_exec('cd /tmp/tdxtest/lib/tdx-tools/ && python3 -m pip install --break-system-packages ./') + deploy_and_setup(m) stdout, stderr = m.check_exec('tdeventlog_check_initrd') for l in stderr.readlines(): diff --git a/tests/tests/test_guest_measurement.py b/tests/tests/test_guest_measurement.py index 291ad3b..29fd026 100644 --- a/tests/tests/test_guest_measurement.py +++ b/tests/tests/test_guest_measurement.py @@ -22,8 +22,7 @@ import Qemu import util - -script_path=os.path.dirname(os.path.realpath(__file__)) +from common import * def test_guest_measurement_check_rtmr(): """ @@ -33,8 +32,8 @@ def test_guest_measurement_check_rtmr(): qm.run() m = Qemu.QemuSSH(qm) - m.rsync_file(f'{script_path}/../lib', '/tmp/tdxtest/') - m.check_exec('cd /tmp/tdxtest/lib/tdx-tools/ && python3 -m pip install --break-system-packages ./') + + deploy_and_setup(m) m.check_exec('tdrtmrcheck') diff --git a/tests/tests/test_guest_reboot.py b/tests/tests/test_guest_reboot.py index 181e1e7..ccecce2 100644 --- a/tests/tests/test_guest_reboot.py +++ b/tests/tests/test_guest_reboot.py @@ -20,8 +20,6 @@ import os import Qemu -script_path=os.path.dirname(os.path.realpath(__file__)) - def test_guest_reboot(): """ Boot TD several times diff --git a/tests/tests/test_guest_report.py b/tests/tests/test_guest_report.py index 7aef1dc..b7cb8a0 100644 --- a/tests/tests/test_guest_report.py +++ b/tests/tests/test_guest_report.py @@ -22,8 +22,7 @@ import Qemu import util - -script_path=os.path.dirname(os.path.realpath(__file__)) +from common import * def test_guest_report(): """ @@ -33,10 +32,9 @@ def test_guest_report(): qm.run() m = Qemu.QemuSSH(qm) - m.rsync_file(f'{script_path}/../lib', '/tmp/tdxtest/') - m.rsync_file(f'{script_path}/guest', '/tmp/tdxtest/') - m.check_exec('cd /tmp/tdxtest/lib/tdx-tools/ && python3 -m pip install --break-system-packages ./') - m.check_exec('python3 /tmp/tdxtest/guest/test_tdreport.py') + deploy_and_setup(m) + + m.check_exec(f'python3 {guest_workdir}/tests/guest/test_tdreport.py') qm.stop() diff --git a/tests/tests/test_quote_configfs_tsm.py b/tests/tests/test_quote_configfs_tsm.py index fb4e2dc..4cf76ec 100644 --- a/tests/tests/test_quote_configfs_tsm.py +++ b/tests/tests/test_quote_configfs_tsm.py @@ -20,8 +20,7 @@ import Qemu import util - -script_path=os.path.dirname(os.path.realpath(__file__)) +from common import * def test_quote_check_configfs_tsm(): """ @@ -31,8 +30,9 @@ def test_quote_check_configfs_tsm(): qm.run() m = Qemu.QemuSSH(qm) - m.rsync_file(f'{script_path}/../lib', '/tmp/tdxtest/') - m.check_exec('cd /tmp/tdxtest/lib/tdx-tools/ && python3 -m pip install --break-system-packages ./') + + deploy_and_setup(m) + m.check_exec('tdtsmcheck') qm.stop()