Skip to content

Commit adc48c6

Browse files
authored
feat: update tests (#92)
1 parent 390b248 commit adc48c6

File tree

8 files changed

+69
-187
lines changed

8 files changed

+69
-187
lines changed

kvm-install-vm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,10 +889,12 @@ function create ()
889889
function attach-disk ()
890890
{
891891
# Set default variables
892-
FORMAT=qcow2
892+
local FORMAT=qcow2
893+
local TARGET=""
894+
local DISKSIZE=""
893895

894896
# Parse command line arguments
895-
while getopts ":d:f:ps:t:h" opt
897+
while getopts ":d:f:p:s:t:h" opt
896898
do
897899
case "$opt" in
898900
d ) DISKSIZE="${OPTARG}G" ;;

test.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
#!/bin/bash
2+
3+
if ! command -v bats >/dev/null 2>&1; then
4+
cat <<EOF >&2
5+
Error: The 'bats' testing framework is required but was not found.
6+
Please install it and try again:
7+
8+
# Debian/Ubuntu
9+
sudo apt-get update && sudo apt-get install bats
10+
11+
# Fedora
12+
sudo dnf install bats
13+
14+
EOF
15+
exit 1
16+
fi
17+
218
$(which bats) tests/

tests/check_destroy.bats

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/check_distributions.bats

Lines changed: 6 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -5,149 +5,21 @@ VMNAME=batstestvm
55
function create_test_vm ()
66
{
77
local -r var="$1"
8-
run kvm-install-vm create -t ${var} ${VMNAME}-${var}
8+
run ./kvm-install-vm create -t ${var} ${VMNAME}-${var}
99
[ "$status" -eq 0 ]
1010
}
1111

1212
function remove_test_vm ()
1313
{
1414
local -r var="$1"
15-
run kvm-install-vm remove ${VMNAME}-${var}
15+
run ./kvm-install-vm remove ${VMNAME}-${var}
1616
[ "$status" -eq 0 ]
1717
}
1818

19-
@test "Install VM (Amazon Linux 2) - $VMNAME-amazon2" {
20-
create_test_vm amazon2
19+
@test "Install VM (Ubuntu 24.04) - $VMNAME-ubuntu24.04" {
20+
create_test_vm ubuntu24.04
2121
}
2222

23-
@test "Delete VM (Amazon Linux 2) - $VMNAME-amazon2" {
24-
remove_test_vm amazon2
25-
}
26-
27-
@test "Install VM (CentOS 7 Atomic) - $VMNAME-centos7-atomic" {
28-
create_test_vm centos7-atomic
29-
}
30-
31-
@test "Delete VM (CentOS 7 Atomic) - $VMNAME-centos7-atomic" {
32-
remove_test_vm centos7-atomic
33-
}
34-
35-
@test "Install VM (CentOS 8) - $VMNAME-centos8" {
36-
create_test_vm centos8
37-
}
38-
39-
@test "Delete VM (CentOS 8) - $VMNAME-centos8" {
40-
remove_test_vm centos8
41-
}
42-
43-
@test "Install VM (Fedora 29) - $VMNAME-fedora29" {
44-
create_test_vm fedora27
45-
}
46-
47-
@test "Delete VM (Fedora 29) - $VMNAME-fedora29" {
48-
remove_test_vm fedora27
49-
}
50-
51-
@test "Install VM (Fedora 29 Atomic) - $VMNAME-fedora29-atomic" {
52-
create_test_vm fedora27-atomic
53-
}
54-
55-
@test "Delete VM (Fedora 29 Atomic) - $VMNAME-fedora29-atomic" {
56-
remove_test_vm fedora27-atomic
57-
}
58-
59-
@test "Install VM (Fedora 30) - $VMNAME-fedora30" {
60-
create_test_vm fedora28
61-
}
62-
63-
@test "Delete VM (Fedora 30) - $VMNAME-fedora30" {
64-
remove_test_vm fedora28
65-
}
66-
67-
@test "Install VM (Fedora 31) - $VMNAME-fedora31" {
68-
create_test_vm fedora31
69-
}
70-
71-
@test "Delete VM (Fedora 31) - $VMNAME-fedora31" {
72-
remove_test_vm fedora31
73-
}
74-
75-
@test "Install VM (Fedora 32) - $VMNAME-fedora32" {
76-
create_test_vm fedora32
77-
}
78-
79-
@test "Delete VM (Fedora 32) - $VMNAME-fedora32" {
80-
remove_test_vm fedora32
81-
}
82-
83-
@test "Install VM (Fedora 33) - $VMNAME-fedora33" {
84-
create_test_vm fedora33
85-
}
86-
87-
@test "Delete VM (Fedora 33) - $VMNAME-fedora33" {
88-
remove_test_vm fedora33
89-
}
90-
91-
@test "Install VM (Fedora 34) - $VMNAME-fedora34" {
92-
create_test_vm fedora34
93-
}
94-
95-
@test "Delete VM (Fedora 34) - $VMNAME-fedora34" {
96-
remove_test_vm fedora34
97-
}
98-
99-
@test "Install VM (Ubuntu 16.04) - $VMNAME-ubuntu1604" {
100-
create_test_vm ubuntu1604
101-
}
102-
103-
@test "Delete VM (Ubuntu 16.04) - $VMNAME-ubuntu1604" {
104-
remove_test_vm ubuntu1604
105-
}
106-
107-
@test "Install VM (Ubuntu 18.04) - $VMNAME-ubuntu1804" {
108-
create_test_vm ubuntu1804
109-
}
110-
111-
@test "Delete VM (Ubuntu 18.04) - $VMNAME-ubuntu1804" {
112-
remove_test_vm ubuntu1804
113-
}
114-
115-
@test "Install VM (Ubuntu 20.04) - $VMNAME-ubuntu2004" {
116-
create_test_vm ubuntu2004
117-
}
118-
119-
@test "Delete VM (Ubuntu 20.04) - $VMNAME-ubuntu2004" {
120-
remove_test_vm ubuntu2004
121-
}
122-
123-
@test "Install VM (Ubuntu 24.04) - $VMNAME-ubuntu2404" {
124-
create_test_vm ubuntu2404
125-
}
126-
127-
@test "Delete VM (Ubuntu 24.04) - $VMNAME-ubuntu2404" {
128-
remove_test_vm ubuntu2404
129-
}
130-
131-
@test "Install VM (Debian 9) - $VMNAME-debian9" {
132-
create_test_vm debian9
133-
}
134-
135-
@test "Delete VM (Debian 9) - $VMNAME-debian9" {
136-
remove_test_vm debian9
137-
}
138-
139-
@test "Install VM (Debian 10) - $VMNAME-debian10" {
140-
create_test_vm debian10
141-
}
142-
143-
@test "Delete VM (Debian 10) - $VMNAME-debian10" {
144-
remove_test_vm debian10
145-
}
146-
147-
@test "Install VM (openSUSE Leap 15) - $VMNAME-opensuse15" {
148-
create_test_vm opensuse15
149-
}
150-
151-
@test "Delete VM (openSUSE Leap 15) - $VMNAME-opensuse15" {
152-
remove_test_vm opensuse15
23+
@test "Delete VM (Ubuntu 24.04) - $VMNAME-ubuntu24.04" {
24+
remove_test_vm ubuntu24.04
15325
}

tests/check_lifecycle.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bats
2+
3+
VMPREFIX=batstestvm
4+
5+
@test "Install VM - $VMPREFIX-rocky9" {
6+
run ./kvm-install-vm create ${VMPREFIX}-rocky9
7+
[ "$status" -eq 0 ]
8+
}
9+
10+
@test "Shutdown/Destroy VM - $VMPREFIX-rocky9" {
11+
run virsh destroy $VMPREFIX-rocky9
12+
[ "$status" -eq 0 ]
13+
}
14+
15+
@test "Delete VM - $VMPREFIX-rocky9" {
16+
run ./kvm-install-vm remove ${VMPREFIX}-rocky9
17+
[[ "${lines[0]}" =~ "Domain is not running" ]]
18+
[ "$status" -eq 0 ]
19+
}

tests/check_prerequisites.bats

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#!/usr/bin/env bats
22

3-
@test "Check that genisoimage is available" {
4-
command -v genisoimage
5-
}
6-
73
@test "Check that virt-install is available" {
84
command -v virt-install
95
}

tests/check_script.bats

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,75 @@ load vmdir
44
load vmname
55

66
@test "Check for help usage message" {
7-
run kvm-install-vm
7+
run ./kvm-install-vm
88
[[ "${lines[0]}" =~ "NAME" ]]
99
}
1010

1111
@test "Test create with no hostname" {
12-
run kvm-install-vm create
12+
run ./kvm-install-vm create
1313
[[ "${lines[0]}" =~ "NAME" ]]
1414
}
1515

16-
@test "Test create with options and no hostname" {
17-
run kvm-install-vm create -t debian9 -d 20
16+
@test "Test create with options and no VM name" {
17+
run ./kvm-install-vm create -t debian12 -d 20
1818
[[ "${lines[0]}" =~ "Please specify a single host to create." ]]
1919
}
2020

2121
@test "Test remove with no hostname" {
22-
run kvm-install-vm remove
22+
run ./kvm-install-vm remove
2323
[[ "${lines[0]}" =~ "NAME" ]]
2424
}
2525

26-
@test "Install VM (CentOS8) - $VMNAME" {
27-
run kvm-install-vm create $VMNAME
26+
@test "Install VM (Rocky9) - $VMNAME" {
27+
run ./kvm-install-vm create "$VMNAME"
2828
[ "$status" -eq 0 ]
2929
}
3030

3131
@test "Check running VM state" {
32-
run bash -c "virsh -q domstate $VMNAME"
32+
run virsh -q domstate $VMNAME
3333
[ "$output" = "running" ]
3434
}
3535

36-
@test "Check libvirt-nss hostname resolution" {
37-
run sleep 45
38-
run ping -c 1 $VMNAME
39-
[ "$status" -eq 0 ]
40-
[[ "${lines[-2]}" =~ "1 packets transmitted, 1 received," ]]
41-
}
42-
43-
@test "Check cloud-init package is removed" {
44-
run ssh -o StrictHostKeyChecking=no centos@$VMNAME rpm -q cloud-init
45-
[[ "$output" =~ "package cloud-init is not installed" ]]
46-
}
36+
#@test "Check libvirt-nss hostname resolution" {
37+
# run sleep 45
38+
# run ping -c 1 "$VMNAME"
39+
# [ "$status" -eq 0 ]
40+
# [[ "${lines[-2]}" =~ "1 packets transmitted, 1 received," ]]
41+
#}
4742

4843
@test "Attach disk to VM without specifying target" {
49-
run bash -c "kvm-install-vm attach-disk -d 1 $VMNAME"
44+
run ./kvm-install-vm attach-disk -d 1 "$VMNAME"
5045
[ "$status" -eq 2 ]
5146
[[ "${lines[0]}" =~ "ERR: You must specify a target device" ]]
5247
}
5348

5449
@test "Attach disk to VM without specifying disk size" {
55-
run bash -c "kvm-install-vm attach-disk -t vdb $VMNAME"
50+
run ./kvm-install-vm attach-disk -t vdb "$VMNAME"
5651
[ "$status" -eq 2 ]
5752
[[ "${lines[0]}" =~ "You must specify a size" ]]
5853
}
5954

6055
@test "Attach disk to VM" {
61-
run bash -c "kvm-install-vm attach-disk -d 1 -t vdb $VMNAME"
56+
run ./kvm-install-vm attach-disk -d 1 -t vdb "$VMNAME"
6257
[ "$status" -eq 0 ]
6358
}
6459

6560
@test "Check block list for VM" {
66-
run bash -c "grep ^vdb <(virsh domblklist $VMNAME)"
61+
run grep " vdb " <(virsh domblklist "$VMNAME")
6762
[ "$status" -eq 0 ]
6863
}
6964

7065
@test "Delete VM - $VMNAME" {
71-
run bash -c "kvm-install-vm remove $VMNAME"
66+
run ./kvm-install-vm remove "$VMNAME"
7267
[ "$status" -eq 0 ]
7368
}
7469

7570
@test "Check destroyed VM state" {
76-
run bash -c "virsh -q domstate $VMNAME"
71+
run virsh -q domstate "$VMNAME"
7772
[[ "$output" =~ "error: failed to get domain '$VMNAME'" ]]
7873
}
7974

8075
@test "Check destroyed VM files" {
81-
run bash -c "ls ${VMDIR}/${VMNAME}"
76+
run ls "${VMDIR}/${VMNAME}"
8277
[[ "$output" =~ "No such file or directory" ]]
8378
}

tests/vmdir.bash

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
VMDIR=${HOME}/virt/vms
2-
if [ -f ~/.kivrc ]
2+
3+
if [[ -f .kivrc ]]
34
then
4-
source ${HOME}/.kivrc
5+
source .kivrc
56
fi

0 commit comments

Comments
 (0)