Skip to content

Commit

Permalink
Merge pull request #243 from mihalicyn/qemu-ext-snap-tests
Browse files Browse the repository at this point in the history
Qemu external snap test
  • Loading branch information
tomponline authored Jul 16, 2024
2 parents 82b1783 + ad92d26 commit 42da814
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ jobs:
PURGE_LXD: "1"
name: ${{ matrix.test }} (${{ matrix.track }} - ${{ matrix.os }})
runs-on: ubuntu-${{ matrix.os }}
permissions:
# need that to manipulate caches
actions: write
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -107,6 +110,9 @@ jobs:
- test: cluster # cluster requires fan to be working so use 22.04 with latest/edge
track: "latest/edge"
os: "22.04"
- test: qemu-external-vm
track: "latest/edge"
os: "24.04"
exclude:
- test: cluster # fan is not yet working on 24.04 kernel: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2064508
os: "24.04"
Expand Down Expand Up @@ -211,6 +217,22 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

# needed for cache key
- name: Get Date
id: get-date
run: |
echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
shell: bash

# for simplicity, just use one cache directory
# and make it valid for one day
- uses: actions/cache/restore@v3
id: cache-restore
if: ${{ matrix.test == 'qemu-external-vm' }}
with:
path: /home/runner/work/cache
key: cache-${{ steps.get-date.outputs.date }}

- name: ${{ matrix.test }} (${{ matrix.track }})
run: |
set -eux
Expand All @@ -228,6 +250,22 @@ jobs:
fi
sudo --preserve-env=PURGE_LXD,TEST_IMG ./bin/local-run "tests/${TEST_SCRIPT}" ${{ matrix.track }} ${EXTRA_ARGS:-}
# always update cache as we have our own logic of
# cache invalidation and updates in addition to a date check
- name: Delete previous cache
if: ${{ steps.cache-restore.outputs.cache-hit }}
continue-on-error: true
run: |
gh extension install actions/gh-actions-cache
gh actions-cache delete "cache-${{ steps.get-date.outputs.date }}" --confirm
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/cache/save@v4
if: ${{ matrix.test == 'qemu-external-vm' }}
with:
path: /home/runner/work/cache
key: cache-${{ steps.get-date.outputs.date }}

- name: Tmate debugging session (self-hosted)
if: ${{ failure() && inputs.tmate-debug && inputs.self-hosted-runner }}
uses: canonical/action-tmate@main
Expand Down
86 changes: 86 additions & 0 deletions tests/qemu-external-vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/sh
set -eux

architecture="$(uname -m)"
if [ "${architecture}" != "x86_64" ]; then
echo "Skipping test on ${architecture}"
# shellcheck disable=SC2034
FAIL=0
exit 0
fi

if ! echo "${LXD_SNAP_CHANNEL}" | grep -q '^latest/edge'; then
echo "Skipping test as should only run on latest/edge"
# shellcheck disable=SC2034
FAIL=0
exit 0
fi

# Install LXD
install_lxd

# Configure LXD
lxd init --auto

# Get the source
git clone --depth=1 "https://github.com/canonical/lxd-pkg-snap.git" -b "latest-edge"

# Build qemu-for-lxd snap
cd lxd-pkg-snap/lxd-qemu-snap

CACHE_PATH="/home/runner/work/cache"
mkdir "${CACHE_PATH}" || true

# debug
ls -la "${CACHE_PATH}/"

LXD_QEMU_SNAP_FILE_NAME="qemu-for-lxd_1_amd64.snap"
LXD_QEMU_SNAP_HASHSUM_FILE_PATH="${CACHE_PATH}/${LXD_QEMU_SNAP_FILE_NAME}.source-hash"
LXD_QEMU_SNAP_CACHED_HASH_SUM=""
[ -e "${LXD_QEMU_SNAP_HASHSUM_FILE_PATH}" ] && LXD_QEMU_SNAP_CACHED_HASH_SUM=$(cat "${LXD_QEMU_SNAP_HASHSUM_FILE_PATH}")
LXD_QEMU_SNAP_HASH_SUM=$(find . -type f -print0 | sort -z | xargs -0 sha256sum | sha256sum | awk '{ print $1 }')

if [ "${LXD_QEMU_SNAP_CACHED_HASH_SUM}" != "${LXD_QEMU_SNAP_HASH_SUM}" ]; then
echo "==> Hash is changed (${LXD_QEMU_SNAP_CACHED_HASH_SUM} != ${LXD_QEMU_SNAP_HASH_SUM}). Rebuild is needed."

# Install snapcraft
snap install snapcraft --classic

snapcraft # this should produce a qemu-for-lxd_1_amd64.snap file

# save to cache
mv "${LXD_QEMU_SNAP_FILE_NAME}" "${CACHE_PATH}/"
echo "${LXD_QEMU_SNAP_HASH_SUM}" > "${LXD_QEMU_SNAP_HASHSUM_FILE_PATH}"
fi

SNAP_TO_INSTALL_PATH="${CACHE_PATH}/${LXD_QEMU_SNAP_FILE_NAME}"

# debug
ls -la "${CACHE_PATH}/"

snap install "${SNAP_TO_INSTALL_PATH}" --dangerous

# install gpu-2404 interface snap
snap install mesa-2404

# connect snaps
snap connect lxd:gpu-2404 mesa-2404:gpu-2404
snap connect lxd:qemu-external qemu-for-lxd:qemu-external

# let LXD to reload
sleep 5
lxd waitready --timeout=300

# check that qemu-external connect hook works properly
journalctl --quiet --no-hostname --no-pager --boot=0 --unit=snap.lxd.daemon.service | grep "Setting up external QEMU snap integration"

# check that qemu instance driver is activated and is in "external" mode
lxc query /1.0 | jq '.environment.driver' | grep "qemu"
lxc query /1.0 | jq '.environment.driver_version' | grep "(external)"

# check that connection with mesa-2404 snap works
serverPID="$(lxc query /1.0 | jq .environment.server_pid)"
grep -aF LIBGL_DRIVERS "/proc/${serverPID}/environ"

# shellcheck disable=SC2034
FAIL=0

0 comments on commit 42da814

Please sign in to comment.