-
-
Notifications
You must be signed in to change notification settings - Fork 21
148 lines (140 loc) · 5.7 KB
/
test_virtual_machine.yml
File metadata and controls
148 lines (140 loc) · 5.7 KB
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
140
141
142
143
144
145
146
147
148
name: Test in QEMU VM with Debian Image (Manual)
on:
pull_request:
branches: [ "main", "dev" ]
workflow_dispatch:
permissions:
contents: read
jobs:
test-in-vm:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
arch: ["amd64", "i386"]
cpu: ["host", "qemu64"]
name: Test in QEMU VM on ${{ matrix.arch }} with CPU ${{ matrix.cpu }}
steps:
- name: Install dependencies
run: |
# Install QEMU/KVM and utilities for handling the image and SSH
sudo apt-get update
sudo apt-get install -y --no-install-recommends qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils unzip wget sshpass
- name: Prepare Debian VM Image
run: |
echo "Downloading Debian image artifact..."
if [[ "${{ matrix.arch }}" == "i386" ]]; then
wget -O artifact.zip "https://people.debian.org/~gio/dqib/attic/i386-pc.zip"
else
wget -O artifact.zip "https://gitlab.com/api/v4/projects/giomasce%2Fdqib/jobs/artifacts/master/download?job=convert_${{ matrix.arch }}-pc"
fi
#
echo "Extracting artifact..."
unzip artifact.zip
- name: Create Test Script
id: test_script
run: |
# Create a script file to be executed inside the VM
cat << 'EOF' > run_tests.sh
#!/bin/bash
set -e
echo "--- Running inside VM ---"
export DEBIAN_FRONTEND=noninteractive
#
# Update package lists and install dependencies
apt-get update
if [[ "${{ matrix.arch }}" == "i386" ]]; then
apt-get install -y --no-install-recommends libdwarf-dev libelf-dev libiberty-dev curl libc6-dbg python3-pip python3-venv python3-dev git build-essential libssl-dev pkg-config
# We have a dependency which requires Rust which does not have prebuilt wheels for i386
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -q -y
. ./.cargo/env
# We create a symlink from /usr/include/libdwarf to /usr/include/libdwarf-1 because the Debian package is currently broken
ln -s /usr/include/libdwarf /usr/include/libdwarf-1
else
apt-get install -y --no-install-recommends libdwarf-dev libelf-dev libiberty-dev libc6-dbg python3-pip python3-venv python3-dev git build-essential
fi
#
# Create a virtual environment for Python dependencies
python3 -m venv /tmp/venv
#
# Activate the virtual environment
source /tmp/venv/bin/activate
#
# Install Python test dependencies
python -m pip install capstone
python -m pip install pwntools pytest objgraph
#
# Clone the libdebug repository
git clone https://github.com/libdebug/libdebug.git
#
# Change to the libdebug directory
cd libdebug
#
# Checkout the correct commit
git checkout ${{ github.event.pull_request.head.sha }}
#
# Install the library
python -m pip install --upgrade .
#
# Change to the test directory
cd test
#
# Run the tests with pytest
echo "--- Starting pytest ---"
PYTEST_ARGS=(
"--ignore=other_tests"
"--ignore" "scripts/atexit_handler_test.py"
"--ignore" "scripts/speed_test.py"
)
if [[ "${{ matrix.arch }}" == "i386" ]]; then
PYTEST_ARGS+=("-k" "not test_hw_bp_at_invalid_location")
fi
python -m pytest "${PYTEST_ARGS[@]}"
echo "--- Finished pytest ---"
EOF
- name: Run QEMU VM and Execute Script
env:
# IMPORTANT: Replace with the actual credentials for your Debian image
VM_USER: root
VM_PASS: root
run: |
# Start QEMU in the background, forwarding host port 2222 to the VM's SSH port 22
sudo qemu-system-x86_64 \
-m 4G \
-smp 2 \
-cpu ${{ matrix.cpu }} \
-enable-kvm \
-hda dqib_${{ matrix.arch }}-pc/image.qcow2 \
-kernel dqib_${{ matrix.arch }}-pc/kernel \
-initrd dqib_${{ matrix.arch }}-pc/initrd \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \
-device virtio-net-pci,netdev=net0 \
-append "root=LABEL=rootfs console=ttyS0" \
-display none \
-daemonize \
-pidfile vm.pid
#
# Wait for the VM to boot and the SSH server to be ready
echo "Waiting for SSH connection..."
for i in {1..30}; do
if sshpass -p "$VM_PASS" ssh -p 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$VM_USER@localhost" "echo 'SSH is up'"; then
break
fi
echo "Attempt $i failed, retrying in 10 seconds..."
sleep 10
done
#
# Copy the test script to the VM
echo "Copying test script to VM..."
sshpass -p "$VM_PASS" scp -P 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ./run_tests.sh "$VM_USER@localhost":~/
#
# Execute the test script inside the VM
echo "Executing test script in VM..."
sshpass -p "$VM_PASS" ssh -p 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$VM_USER@localhost" "chmod +x ~/run_tests.sh && ~/run_tests.sh"
- name: Shutdown VM
if: always() # Always run this step to ensure cleanup
run: |
echo "Shutting down VM..."
if [ -f vm.pid ]; then
sudo kill $(sudo cat vm.pid)
fi