Browse Source

feat: update tests

pull/92/head
Giovanni Torres 8 months ago
parent
commit
628907a498
  1. 6
      kvm-install-vm
  2. 16
      test.sh
  3. 19
      tests/check_destroy.bats
  4. 140
      tests/check_distributions.bats
  5. 19
      tests/check_lifecycle.bats
  6. 4
      tests/check_prerequisites.bats
  7. 47
      tests/check_script.bats
  8. 5
      tests/vmdir.bash

6
kvm-install-vm

@ -889,10 +889,12 @@ function create ()
function attach-disk ()
{
# Set default variables
FORMAT=qcow2
local FORMAT=qcow2
local TARGET=""
local DISKSIZE=""
# Parse command line arguments
while getopts ":d:f:ps:t:h" opt
while getopts ":d:f:p:s:t:h" opt
do
case "$opt" in
d ) DISKSIZE="${OPTARG}G" ;;

16
test.sh

@ -1,2 +1,18 @@
#!/bin/bash
if ! command -v bats >/dev/null 2>&1; then
cat <<EOF >&2
Error: The 'bats' testing framework is required but was not found.
Please install it and try again:
# Debian/Ubuntu
sudo apt-get update && sudo apt-get install bats
# Fedora
sudo dnf install bats
EOF
exit 1
fi
$(which bats) tests/

19
tests/check_destroy.bats

@ -1,19 +0,0 @@
#!/usr/bin/env bats
VMPREFIX=batstestvm
@test "Install VM - $VMPREFIX-centos7-destroy" {
run kvm-install-vm create ${VMPREFIX}-centos7-destroy
[ "$status" -eq 0 ]
}
@test "Shutdown/Destroy VM - $VMPREFIX-centos7-destroy" {
run virsh destroy $VMPREFIX-centos7-destroy
[ "$status" -eq 0 ]
}
@test "Delete VM - $VMPREFIX-centos7-destroy" {
run kvm-install-vm remove ${VMPREFIX}-centos7-destroy
[[ "${lines[0]}" =~ "Domain is not running" ]]
[ "$status" -eq 0 ]
}

140
tests/check_distributions.bats

@ -5,149 +5,21 @@ VMNAME=batstestvm
function create_test_vm ()
{
local -r var="$1"
run kvm-install-vm create -t ${var} ${VMNAME}-${var}
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}
run ./kvm-install-vm remove ${VMNAME}-${var}
[ "$status" -eq 0 ]
}
@test "Install VM (Amazon Linux 2) - $VMNAME-amazon2" {
create_test_vm amazon2
@test "Install VM (Ubuntu 24.04) - $VMNAME-ubuntu24.04" {
create_test_vm ubuntu24.04
}
@test "Delete VM (Amazon Linux 2) - $VMNAME-amazon2" {
remove_test_vm amazon2
}
@test "Install VM (CentOS 7 Atomic) - $VMNAME-centos7-atomic" {
create_test_vm centos7-atomic
}
@test "Delete VM (CentOS 7 Atomic) - $VMNAME-centos7-atomic" {
remove_test_vm centos7-atomic
}
@test "Install VM (CentOS 8) - $VMNAME-centos8" {
create_test_vm centos8
}
@test "Delete VM (CentOS 8) - $VMNAME-centos8" {
remove_test_vm centos8
}
@test "Install VM (Fedora 29) - $VMNAME-fedora29" {
create_test_vm fedora27
}
@test "Delete VM (Fedora 29) - $VMNAME-fedora29" {
remove_test_vm fedora27
}
@test "Install VM (Fedora 29 Atomic) - $VMNAME-fedora29-atomic" {
create_test_vm fedora27-atomic
}
@test "Delete VM (Fedora 29 Atomic) - $VMNAME-fedora29-atomic" {
remove_test_vm fedora27-atomic
}
@test "Install VM (Fedora 30) - $VMNAME-fedora30" {
create_test_vm fedora28
}
@test "Delete VM (Fedora 30) - $VMNAME-fedora30" {
remove_test_vm fedora28
}
@test "Install VM (Fedora 31) - $VMNAME-fedora31" {
create_test_vm fedora31
}
@test "Delete VM (Fedora 31) - $VMNAME-fedora31" {
remove_test_vm fedora31
}
@test "Install VM (Fedora 32) - $VMNAME-fedora32" {
create_test_vm fedora32
}
@test "Delete VM (Fedora 32) - $VMNAME-fedora32" {
remove_test_vm fedora32
}
@test "Install VM (Fedora 33) - $VMNAME-fedora33" {
create_test_vm fedora33
}
@test "Delete VM (Fedora 33) - $VMNAME-fedora33" {
remove_test_vm fedora33
}
@test "Install VM (Fedora 34) - $VMNAME-fedora34" {
create_test_vm fedora34
}
@test "Delete VM (Fedora 34) - $VMNAME-fedora34" {
remove_test_vm fedora34
}
@test "Install VM (Ubuntu 16.04) - $VMNAME-ubuntu1604" {
create_test_vm ubuntu1604
}
@test "Delete VM (Ubuntu 16.04) - $VMNAME-ubuntu1604" {
remove_test_vm ubuntu1604
}
@test "Install VM (Ubuntu 18.04) - $VMNAME-ubuntu1804" {
create_test_vm ubuntu1804
}
@test "Delete VM (Ubuntu 18.04) - $VMNAME-ubuntu1804" {
remove_test_vm ubuntu1804
}
@test "Install VM (Ubuntu 20.04) - $VMNAME-ubuntu2004" {
create_test_vm ubuntu2004
}
@test "Delete VM (Ubuntu 20.04) - $VMNAME-ubuntu2004" {
remove_test_vm ubuntu2004
}
@test "Install VM (Ubuntu 24.04) - $VMNAME-ubuntu2404" {
create_test_vm ubuntu2404
}
@test "Delete VM (Ubuntu 24.04) - $VMNAME-ubuntu2404" {
remove_test_vm ubuntu2404
}
@test "Install VM (Debian 9) - $VMNAME-debian9" {
create_test_vm debian9
}
@test "Delete VM (Debian 9) - $VMNAME-debian9" {
remove_test_vm debian9
}
@test "Install VM (Debian 10) - $VMNAME-debian10" {
create_test_vm debian10
}
@test "Delete VM (Debian 10) - $VMNAME-debian10" {
remove_test_vm debian10
}
@test "Install VM (openSUSE Leap 15) - $VMNAME-opensuse15" {
create_test_vm opensuse15
}
@test "Delete VM (openSUSE Leap 15) - $VMNAME-opensuse15" {
remove_test_vm opensuse15
@test "Delete VM (Ubuntu 24.04) - $VMNAME-ubuntu24.04" {
remove_test_vm ubuntu24.04
}

19
tests/check_lifecycle.bats

@ -0,0 +1,19 @@
#!/usr/bin/env bats
VMPREFIX=batstestvm
@test "Install VM - $VMPREFIX-rocky9" {
run ./kvm-install-vm create ${VMPREFIX}-rocky9
[ "$status" -eq 0 ]
}
@test "Shutdown/Destroy VM - $VMPREFIX-rocky9" {
run virsh destroy $VMPREFIX-rocky9
[ "$status" -eq 0 ]
}
@test "Delete VM - $VMPREFIX-rocky9" {
run ./kvm-install-vm remove ${VMPREFIX}-rocky9
[[ "${lines[0]}" =~ "Domain is not running" ]]
[ "$status" -eq 0 ]
}

4
tests/check_prerequisites.bats

@ -1,9 +1,5 @@
#!/usr/bin/env bats
@test "Check that genisoimage is available" {
command -v genisoimage
}
@test "Check that virt-install is available" {
command -v virt-install
}

47
tests/check_script.bats

@ -4,80 +4,75 @@ load vmdir
load vmname
@test "Check for help usage message" {
run kvm-install-vm
run ./kvm-install-vm
[[ "${lines[0]}" =~ "NAME" ]]
}
@test "Test create with no hostname" {
run kvm-install-vm create
run ./kvm-install-vm create
[[ "${lines[0]}" =~ "NAME" ]]
}
@test "Test create with options and no hostname" {
run kvm-install-vm create -t debian9 -d 20
@test "Test create with options and no VM name" {
run ./kvm-install-vm create -t debian12 -d 20
[[ "${lines[0]}" =~ "Please specify a single host to create." ]]
}
@test "Test remove with no hostname" {
run kvm-install-vm remove
run ./kvm-install-vm remove
[[ "${lines[0]}" =~ "NAME" ]]
}
@test "Install VM (CentOS8) - $VMNAME" {
run kvm-install-vm create $VMNAME
@test "Install VM (Rocky9) - $VMNAME" {
run ./kvm-install-vm create "$VMNAME"
[ "$status" -eq 0 ]
}
@test "Check running VM state" {
run bash -c "virsh -q domstate $VMNAME"
run virsh -q domstate $VMNAME
[ "$output" = "running" ]
}
@test "Check libvirt-nss hostname resolution" {
run sleep 45
run ping -c 1 $VMNAME
[ "$status" -eq 0 ]
[[ "${lines[-2]}" =~ "1 packets transmitted, 1 received," ]]
}
@test "Check cloud-init package is removed" {
run ssh -o StrictHostKeyChecking=no centos@$VMNAME rpm -q cloud-init
[[ "$output" =~ "package cloud-init is not installed" ]]
}
#@test "Check libvirt-nss hostname resolution" {
# run sleep 45
# run ping -c 1 "$VMNAME"
# [ "$status" -eq 0 ]
# [[ "${lines[-2]}" =~ "1 packets transmitted, 1 received," ]]
#}
@test "Attach disk to VM without specifying target" {
run bash -c "kvm-install-vm attach-disk -d 1 $VMNAME"
run ./kvm-install-vm attach-disk -d 1 "$VMNAME"
[ "$status" -eq 2 ]
[[ "${lines[0]}" =~ "ERR: You must specify a target device" ]]
}
@test "Attach disk to VM without specifying disk size" {
run bash -c "kvm-install-vm attach-disk -t vdb $VMNAME"
run ./kvm-install-vm attach-disk -t vdb "$VMNAME"
[ "$status" -eq 2 ]
[[ "${lines[0]}" =~ "You must specify a size" ]]
}
@test "Attach disk to VM" {
run bash -c "kvm-install-vm attach-disk -d 1 -t vdb $VMNAME"
run ./kvm-install-vm attach-disk -d 1 -t vdb "$VMNAME"
[ "$status" -eq 0 ]
}
@test "Check block list for VM" {
run bash -c "grep ^vdb <(virsh domblklist $VMNAME)"
run grep " vdb " <(virsh domblklist "$VMNAME")
[ "$status" -eq 0 ]
}
@test "Delete VM - $VMNAME" {
run bash -c "kvm-install-vm remove $VMNAME"
run ./kvm-install-vm remove "$VMNAME"
[ "$status" -eq 0 ]
}
@test "Check destroyed VM state" {
run bash -c "virsh -q domstate $VMNAME"
run virsh -q domstate "$VMNAME"
[[ "$output" =~ "error: failed to get domain '$VMNAME'" ]]
}
@test "Check destroyed VM files" {
run bash -c "ls ${VMDIR}/${VMNAME}"
run ls "${VMDIR}/${VMNAME}"
[[ "$output" =~ "No such file or directory" ]]
}

5
tests/vmdir.bash

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

Loading…
Cancel
Save