Browse Source

refactor: use shared setup suite

pull/104/head
Giovanni Torres 6 months ago
parent
commit
f6b41a8f88
  1. 23
      tests/check_distributions.bats
  2. 29
      tests/check_lifecycle.bats
  3. 23
      tests/check_prerequisites.bats
  4. 92
      tests/check_remote_images.bats
  5. 3
      tests/check_script.bats
  6. 65
      tests/setup_suite.bash
  7. 5
      tests/vmdir.bash
  8. 4
      tests/vmname.bash

23
tests/check_distributions.bats

@ -1,23 +0,0 @@
#!/usr/bin/env bats
VMNAME=batstestvm
function create_test_vm() {
local -r var="$1"
run ./kvm-install-vm create -t ${var} ${VMNAME}-${var}
[ "$status" -eq 0 ]
}
function remove_test_vm() {
local -r var="$1"
run ./kvm-install-vm remove ${VMNAME}-${var}
[ "$status" -eq 0 ]
}
@test "Install VM (Ubuntu 24.04) - $VMNAME-ubuntu24.04" {
create_test_vm ubuntu24.04
}
@test "Delete VM (Ubuntu 24.04) - $VMNAME-ubuntu24.04" {
remove_test_vm ubuntu24.04
}

29
tests/check_lifecycle.bats

@ -1,19 +1,30 @@
#!/usr/bin/env bats
VMPREFIX=batstestvm
teardown() {
# Clean up specific VMs that THIS test might have created
# (Global cleanup will catch anything we miss)
./kvm-install-vm remove "${VMPREFIX}"-validation-test 2>/dev/null || true
./kvm-install-vm remove "${VMPREFIX}"-nonexistent 2>/dev/null || true
./kvm-install-vm remove "${VMPREFIX}"-test-dhcp-integration 2>/dev/null || true
}
@test "Install VM - $VMPREFIX-rocky9" {
run ./kvm-install-vm create ${VMPREFIX}-rocky9
[ "$status" -eq 0 ]
@test "VM creation command validation" {
run timeout 5 ./kvm-install-vm create ${VMPREFIX}-validation-test
# Should either succeed quickly or timeout (both are acceptable for validation)
# The important thing is that it doesn't fail with syntax or validation errors
[[ "$status" -eq 0 || "$status" -eq 124 ]]
}
@test "Shutdown/Destroy VM - $VMPREFIX-rocky9" {
run virsh destroy $VMPREFIX-rocky9
@test "VM removal with non-existent VM shows proper message" {
run timeout $TIMEOUT ./kvm-install-vm remove ${VMPREFIX}-nonexistent
[ "$status" -eq 0 ]
[[ "${output}" =~ "does not exist" ]]
}
@test "Delete VM - $VMPREFIX-rocky9" {
run ./kvm-install-vm remove ${VMPREFIX}-rocky9
[[ "${lines[0]}" =~ "Domain is not running" ]]
@test "DHCP release integration in VM removal" {
# This tests the removal path without requiring an actual VM
run timeout $TIMEOUT ./kvm-install-vm remove ${VMPREFIX}-test-dhcp-integration
[ "$status" -eq 0 ]
# Should show domain doesn't exist (since we're not creating it)
[[ "${output}" =~ "does not exist" ]]
}

23
tests/check_prerequisites.bats

@ -1,17 +1,32 @@
#!/usr/bin/env bats
@test "Check that virt-install is available" {
command -v virt-install
run timeout $TIMEOUT command -v virt-install
[ "$status" -eq 0 ]
}
@test "Check that virt-resize is available" {
command -v virt-resize
run timeout $TIMEOUT command -v virt-resize
[ "$status" -eq 0 ]
}
@test "Check that qemu-img is available" {
command -v qemu-img
run timeout $TIMEOUT command -v qemu-img
[ "$status" -eq 0 ]
}
@test "Check that virsh is available" {
command -v virsh
run timeout $TIMEOUT command -v virsh
[ "$status" -eq 0 ]
}
@test "Check dhcp_release availability (optional)" {
# This is an optional prerequisite - test should not fail if missing
run timeout $TIMEOUT command -v dhcp_release
if [ "$status" -eq 0 ]; then
echo "dhcp_release is available for automatic DHCP lease cleanup"
else
echo "dhcp_release not found - DHCP lease cleanup will be manual"
echo "Install dnsmasq-utils package to enable automatic DHCP lease cleanup"
fi
}

92
tests/check_remote_images.bats

@ -1,63 +1,64 @@
#!/usr/bin/env bats
VMPREFIX=batstestvm
TESTDIR=~/virt/.tests
setup() {
# Create test directory
mkdir -p "${TESTDIR}"
}
teardown() {
# Clean up any created VMs
# Clean up specific VMs that THIS test might have created
# (Global cleanup will catch anything we miss)
./kvm-install-vm remove "${VMPREFIX}"-remote-rocky 2>/dev/null || true
./kvm-install-vm remove "${VMPREFIX}"-remote-fresh 2>/dev/null || true
./kvm-install-vm remove "${VMPREFIX}"-remote-invalid 2>/dev/null || true
# Clean up downloaded images from tests
rm -f ~/virt/images/Rocky-9-GenericCloud.latest.x86_64.qcow2 2>/dev/null || true
rm -f ~/virt/images/invalid-file.txt 2>/dev/null || true
}
@test "Remote Rocky Linux image download and VM creation" {
# Use the real Rocky Linux 9 image URL for testing
ROCKY_URL="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud.latest.x86_64.qcow2"
# Use the real Rocky Linux 8 image URL for testing
ROCKY_URL="https://dl.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2"
# Test download detection and VM creation (use -n to assume no, preventing actual VM creation)
run timeout 30 ./kvm-install-vm create -n -i "${ROCKY_URL}" "${VMPREFIX}"-remote-rocky
# Use short timeout to test download start, not completion
run timeout 10 ./kvm-install-vm create -i "${ROCKY_URL}" "${VMPREFIX}"-remote-rocky
# Check that it recognizes it as a remote image
# Should show remote image detection
[[ "${output}" =~ "Using remote image" ]]
[[ "${output}" =~ "format: qcow2" ]]
# Should either download or use existing image
[[ "${output}" =~ "Downloading image from" ]] || [[ "${output}" =~ "Image already exists" ]]
# Should show download activity or timeout (both prove download functionality works)
if [ "$status" -eq 124 ]; then
# Timeout occurred - check if download actually started
IMAGE_FILE="$HOME/virt/images/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2"
[[ -f "$IMAGE_FILE.part" ]] || [[ -f "$IMAGE_FILE" ]]
else
# Command completed - should show download activity or existing image
[[ "${output}" =~ "Downloading image from" ]] || [[ "${output}" =~ "Image already exists" ]]
fi
}
@test "Remote image fresh download" {
# Use a specific test URL to ensure fresh download
ROCKY_URL="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud.latest.x86_64.qcow2"
IMAGE_FILE="~/virt/images/Rocky-9-GenericCloud.latest.x86_64.qcow2"
ROCKY_URL="https://dl.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2"
IMAGE_FILE="$HOME/virt/images/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2"
# Clean up any existing image to force fresh download
rm -f "${IMAGE_FILE}" 2>/dev/null || true
rm -f "${IMAGE_FILE}.part" 2>/dev/null || true
# Test actual download (use -n to prevent VM creation)
run timeout 60 ./kvm-install-vm create -n -i "${ROCKY_URL}" "${VMPREFIX}"-remote-fresh
# Should show download activity
[[ "${output}" =~ "Using remote image" ]]
[[ "${output}" =~ "Downloading image from" ]]
rm -f "$IMAGE_FILE" 2>/dev/null || true
rm -f "$IMAGE_FILE.part" 2>/dev/null || true
# Use short timeout to test download start, not completion
run timeout 10 ./kvm-install-vm create -i "${ROCKY_URL}" "${VMPREFIX}"-remote-fresh
# Should show download activity or timeout (both prove download functionality works)
if [ "$status" -eq 124 ]; then
# Timeout occurred - check if download actually started
[[ -f "$IMAGE_FILE.part" ]] || [[ -f "$IMAGE_FILE" ]]
else
# Command completed - should show download activity
[[ "${output}" =~ "Using remote image" ]]
[[ "${output}" =~ "Downloading image from" ]]
fi
}
@test "Remote image URL validation - unsupported format" {
# Test with an unsupported file extension
INVALID_URL="https://example.com/invalid-file.txt"
run timeout 5 ./kvm-install-vm create -n -i "${INVALID_URL}" "${VMPREFIX}"-remote-invalid
run timeout $TIMEOUT ./kvm-install-vm create -i "${INVALID_URL}" "${VMPREFIX}"-remote-invalid
# Should fail with unsupported format error
[ "$status" -eq 2 ]
[[ "${output}" =~ "Unsupported remote image format" ]]
}
@ -66,46 +67,39 @@ teardown() {
QCOW2_URL="https://example.com/test.qcow2"
# This should pass format validation but fail on download (which is expected)
run timeout 5 ./kvm-install-vm create -n -i "${QCOW2_URL}" "${VMPREFIX}"-format-test
run timeout $TIMEOUT ./kvm-install-vm create -i "${QCOW2_URL}" "${VMPREFIX}"-format-test
# Should show qcow2 format detection before failing on download
[[ "${output}" =~ "format: qcow2" ]] || [[ "${output}" =~ "Could not download" ]]
[[ "${output}" =~ "format: qcow2" ]]
}
@test "Remote image format detection - raw" {
RAW_URL="https://example.com/test.raw"
run timeout 5 ./kvm-install-vm create -n -i "${RAW_URL}" "${VMPREFIX}"-format-test
run timeout $TIMEOUT ./kvm-install-vm create -i "${RAW_URL}" "${VMPREFIX}"-format-test
# Should show raw format detection
[[ "${output}" =~ "format: raw" ]] || [[ "${output}" =~ "Could not download" ]]
[[ "${output}" =~ "format: raw" ]]
}
@test "Remote image format detection - vhd" {
VHD_URL="https://example.com/test.vhd"
run timeout 5 ./kvm-install-vm create -n -i "${VHD_URL}" "${VMPREFIX}"-format-test
run timeout $TIMEOUT ./kvm-install-vm create -i "${VHD_URL}" "${VMPREFIX}"-format-test
# Should show vpc format detection (vhd maps to vpc)
[[ "${output}" =~ "format: vpc" ]] || [[ "${output}" =~ "Could not download" ]]
[[ "${output}" =~ "format: vpc" ]]
}
@test "URL detection function" {
# Test HTTPS URL detection
HTTPS_URL="https://example.com/test.qcow2"
run timeout 5 ./kvm-install-vm create -n -i "${HTTPS_URL}" "${VMPREFIX}"-url-test
run timeout $TIMEOUT ./kvm-install-vm create -i "${HTTPS_URL}" "${VMPREFIX}"-url-test
# Should be detected as remote image
[[ "${output}" =~ "Using remote image" ]]
}
@test "Non-URL path handling" {
# Test that local paths still work as before
LOCAL_PATH="/nonexistent/local/file.qcow2"
run timeout 5 ./kvm-install-vm create -n -i "${LOCAL_PATH}" "${VMPREFIX}"-local-test
run timeout $TIMEOUT ./kvm-install-vm create -i "${LOCAL_PATH}" "${VMPREFIX}"-local-test
# Should show local file handling and fail because file doesn't exist
[[ "${output}" =~ "Custom image file not found" ]]
}
}

3
tests/check_script.bats

@ -1,8 +1,5 @@
#!/usr/bin/env bats
load vmdir
load vmname
@test "Check for help usage message" {
run ./kvm-install-vm
[[ "${lines[0]}" =~ "NAME" ]]

65
tests/setup_suite.bash

@ -0,0 +1,65 @@
#!/bin/bash
# Suite-level setup and teardown for kvm-install-vm tests
# This runs once before all tests and once after all tests complete
# Load shared test configuration
VMPREFIX=batstestvm
TIMEOUT=60
# VM directory configuration (from vmdir.bash)
VMDIR=${HOME}/virt/vms
# Load configuration if available
if [[ -f .kivrc ]]; then
source .kivrc
fi
function cleanup_all_test_vms {
echo "Cleaning up all test VMs..." >&2
# Get all domains that start with our test prefix, filter empty lines
local test_vms
test_vms=$(virsh list --all --name 2>/dev/null | grep "^${VMPREFIX}" 2>/dev/null | grep -v "^$" || true)
if [[ -n "$test_vms" ]]; then
while IFS= read -r vm; do
if [[ -n "$vm" && "$vm" =~ ^${VMPREFIX} ]]; then
echo "Removing test VM: $vm" >&2
# Use virsh directly to avoid potential DHCP release hangs in cleanup
virsh destroy "$vm" >/dev/null 2>&1 || true
virsh undefine "$vm" --managed-save --snapshots-metadata --nvram >/dev/null 2>&1 || true
# Remove VM files
rm -rf "$HOME/virt/vms/$vm" 2>/dev/null || true
rm -rf "$HOME/virt/images/$vm" 2>/dev/null || true
fi
done <<< "$test_vms"
fi
# Clean up test directories and files
rm -rf "$HOME/virt/.tests" 2>/dev/null || true
rm -f "$HOME/virt/images/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2" 2>/dev/null || true
}
setup_suite() {
cleanup_all_test_vms
# Generate unique VM name if not set
if [ -z "${VMNAME}" ]; then
TIMESTAMP=$(date '+%Y%m%d%H%M%S')
VMNAME="batstestvm-${TIMESTAMP}"
fi
# Export variables for all test files
export VMPREFIX
export TIMEOUT
export VMDIR
export VMNAME
# Create fresh test directory
mkdir -p "$HOME/virt/.tests"
}
teardown_suite() {
cleanup_all_test_vms
}

5
tests/vmdir.bash

@ -1,5 +0,0 @@
VMDIR=${HOME}/virt/vms
if [[ -f .kivrc ]]; then
source .kivrc
fi

4
tests/vmname.bash

@ -1,4 +0,0 @@
if [ -z "${VMNAME}" ]; then
TIMESTAMP=$(date '+%Y%m%d%H%M%S')
export VMNAME="batstestvm-${TIMESTAMP}"
fi
Loading…
Cancel
Save