working tests
This commit is contained in:
parent
b396b1648b
commit
df609f80af
|
@ -31,7 +31,7 @@ FROM stagex/core-libunwind:sx2025.02.0@sha256:99e2574ace4f7dfa3d8bfc93ab69e1fd5f
|
|||
FROM stagex/user-libusb:sx2025.02.0@sha256:b78ca9194fdb8dfb7b7177d16a156fac21e6c9822a0c35a17841400bc1a27f68 AS user-libusb
|
||||
FROM stagex/user-libzbar:sx2025.02.0@sha256:44ad89a661bc395d6b49d89a0367846f7bee40b198780777c5c7b1b3c0d49a0e AS user-libzbar
|
||||
FROM stagex/core-libzstd:sx2025.02.0@sha256:23cd975a27e218c5398efd17e1f8c491d31969ab674d3468dbf8b75ba40611ad AS core-libzstd
|
||||
FROM stagex/user-linux-airgap:sx2025.02.0@sha256:a2dbeace3ce085ba487e88b3968fea1ec29ce392f691d28c4b183e1ed9c0df4d AS user-linux-airgap
|
||||
FROM local-airgap AS user-linux-airgap
|
||||
FROM stagex/user-lzo:sx2025.02.0@sha256:b71c2944073f3fbc1fe543b9e4dfc4f59ec013a763a6209ded77b8f8bd0a33b4 AS user-lzo
|
||||
FROM stagex/user-mtools:sx2025.02.0@sha256:ea76e5f82f9833274a4438e9706779afd9b1c0b197c984c9d54c9887163ffb42 AS user-mtools
|
||||
FROM stagex/core-musl:sx2025.02.0@sha256:23d0614f60449015add2369959c89a6ea08e208302773b9a0811ce1195afc3a4 AS core-musl
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
#!/bin/sh
|
||||
### BEGIN INIT INFO
|
||||
# Provides: qemu-ga
|
||||
# Required-Start: $remote_fs $syslog
|
||||
# Required-Stop:
|
||||
# Default-Start: 2 3 4 5
|
||||
# Default-Stop:
|
||||
# Short-Description: Start QEMU Guest Agent
|
||||
### END INIT INFO
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
echo "Starting QEMU Guest Agent"
|
||||
qemu-ga -m virtio-serial -p /dev/virtio-ports/org.qemu.guest_agent.0 &
|
||||
;;
|
||||
stop)
|
||||
echo "Stopping QEMU Guest Agent"
|
||||
killall qemu-ga
|
||||
;;
|
||||
restart)
|
||||
"$0" stop
|
||||
"$0" start
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|restart}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,161 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
QGA_SOCKET="/tmp/qga.sock"
|
||||
|
||||
setup() {
|
||||
if [ ! -S "$QGA_SOCKET" ]; then
|
||||
echo "QGA socket not found at $QGA_SOCKET"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
teardown() {
|
||||
true
|
||||
}
|
||||
|
||||
qga_send() {
|
||||
local json_cmd="$1"
|
||||
local tmp_out
|
||||
tmp_out=$(mktemp)
|
||||
|
||||
{
|
||||
echo "$json_cmd"
|
||||
sleep 0.1
|
||||
} | socat - UNIX-CONNECT:"$QGA_SOCKET" > "$tmp_out"
|
||||
|
||||
cat "$tmp_out"
|
||||
rm -f "$tmp_out"
|
||||
}
|
||||
|
||||
qga_guest_exec() {
|
||||
local cmd="$1"
|
||||
shift
|
||||
|
||||
local args_json="[]"
|
||||
if [[ "$#" -gt 0 ]]; then
|
||||
args_json=$(printf '%s\n' "$@" | jq -R . | jq -s .)
|
||||
fi
|
||||
|
||||
local json
|
||||
json=$(jq -n \
|
||||
--arg path "$cmd" \
|
||||
--argjson args "$args_json" \
|
||||
'{
|
||||
execute: "guest-exec",
|
||||
arguments: {
|
||||
path: $path,
|
||||
arg: $args,
|
||||
"capture-output": true
|
||||
}
|
||||
}')
|
||||
|
||||
local response
|
||||
response=$(qga_send "$json")
|
||||
|
||||
echo "guest-exec JSON sent:" >&2
|
||||
echo "$json" >&2
|
||||
echo "guest-exec response:" >&2
|
||||
echo "$response" >&2
|
||||
|
||||
local first_line
|
||||
first_line=$(echo "$response" | head -n1)
|
||||
|
||||
local pid
|
||||
pid=$(echo "$first_line" | jq -r '.return.pid // empty')
|
||||
|
||||
if [[ ! "$pid" =~ ^[0-9]+$ ]]; then
|
||||
echo "ERROR: Failed to parse valid PID from response." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "$pid"
|
||||
}
|
||||
|
||||
qga_guest_exec_status() {
|
||||
local pid="$1"
|
||||
|
||||
local json
|
||||
json=$(jq -n --argjson pid "$pid" \
|
||||
'{execute: "guest-exec-status", arguments: {pid: $pid}}')
|
||||
|
||||
qga_send "$json"
|
||||
}
|
||||
|
||||
qga_decode_output() {
|
||||
local b64_data="$1"
|
||||
echo "$b64_data" | base64 -d
|
||||
}
|
||||
|
||||
qga_run_cmd_and_get_output() {
|
||||
local pid
|
||||
pid=$(qga_guest_exec "$@")
|
||||
if [[ -z "$pid" ]]; then
|
||||
echo "Failed to get PID from guest-exec" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local status_resp
|
||||
local exited="false"
|
||||
local retries=5
|
||||
|
||||
while [[ "$exited" != "true" && $retries -gt 0 ]]; do
|
||||
sleep 0.5
|
||||
status_resp=$(qga_guest_exec_status "$pid")
|
||||
exited=$(echo "$status_resp" | jq -r '.return.exited // empty')
|
||||
retries=$((retries - 1))
|
||||
done
|
||||
|
||||
if [[ "$exited" != "true" ]]; then
|
||||
echo "Command did not finish after retries." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local out_data
|
||||
out_data=$(echo "$status_resp" | jq -r '.return."out-data" // empty')
|
||||
local err_data
|
||||
err_data=$(echo "$status_resp" | jq -r '.return."err-data" // empty')
|
||||
|
||||
if [[ -n "$err_data" ]]; then
|
||||
echo "STDERR:" >&2
|
||||
qga_decode_output "$err_data" >&2
|
||||
fi
|
||||
|
||||
qga_decode_output "$out_data"
|
||||
}
|
||||
|
||||
qga_read_guest_file() {
|
||||
local file_path="$1"
|
||||
local pid
|
||||
pid=$(qga_guest_exec "/bin/cat" "$file_path")
|
||||
if [[ -z "$pid" ]]; then
|
||||
echo "Failed to get PID to read file" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local status_resp
|
||||
local exited="false"
|
||||
local retries=5
|
||||
|
||||
while [[ "$exited" != "true" && $retries -gt 0 ]]; do
|
||||
sleep 0.5
|
||||
status_resp=$(qga_guest_exec_status "$pid")
|
||||
exited=$(echo "$status_resp" | jq -r '.return.exited // empty')
|
||||
retries=$((retries - 1))
|
||||
done
|
||||
|
||||
if [[ "$exited" != "true" ]]; then
|
||||
echo "Failed to read file or command did not finish execution." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local out_data
|
||||
out_data=$(echo "$status_resp" | jq -r '.return."out-data"')
|
||||
local decoded_data
|
||||
decoded_data=$(qga_decode_output "$out_data")
|
||||
|
||||
if echo "$decoded_data" | base64 --decode &>/dev/null; then
|
||||
echo "$decoded_data" | base64 --decode
|
||||
else
|
||||
echo "$decoded_data"
|
||||
fi
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env bats
|
||||
|
||||
load 'helpers/qga_helper'
|
||||
|
||||
@test "echo in guest returns hello" {
|
||||
result="$(qga_run_cmd_and_get_output /bin/echo hello)"
|
||||
echo "Guest output: $result"
|
||||
[ "$result" = "hello" ]
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env bats
|
||||
|
||||
load 'helpers/qga_helper'
|
||||
|
||||
@test "keyfork --version returns version" {
|
||||
result="$(qga_run_cmd_and_get_output /bin/keyfork --version)"
|
||||
echo "Guest output: $result"
|
||||
[[ "$result" == *"keyfork"* ]]
|
||||
}
|
Loading…
Reference in New Issue