Skip to content

Commit 52ff52b

Browse files
committed
add rudimentary integration test script
1 parent 3135e00 commit 52ff52b

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

fixtures/integrationtest.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[
2+
{
3+
"model": "resources.hypervisor",
4+
"pk": 1,
5+
"fields": {
6+
"name": "local",
7+
"interface": "eth0"
8+
}
9+
},
10+
{
11+
"model": "resources.profile",
12+
"pk": 1,
13+
"fields": {
14+
"name": "default",
15+
"enabled": true
16+
}
17+
},
18+
{
19+
"model": "resources.profile",
20+
"pk": 2,
21+
"fields": {
22+
"name": "offline",
23+
"enabled": false
24+
}
25+
},
26+
{
27+
"model": "resources.virtualmachine",
28+
"pk": 1,
29+
"fields": {
30+
"create": "2023-07-15T18:10:44.750Z",
31+
"name": "integrationtest",
32+
"ram_in_mb": 4096,
33+
"cpu": 8,
34+
"host": 1,
35+
"role": "shellserver",
36+
"profile": 1,
37+
"image": "arch-openstack-LATEST-image-bootstrap.qcow2",
38+
"static_ip": "192.168.1.123",
39+
"saltmaster_ip": "127.0.0.1",
40+
"enabled": true,
41+
"extra_storage_in_gb": 1,
42+
"network_interface_count": 1,
43+
"extra_storage_pool": "default"
44+
}
45+
}
46+
]

main.tf

Whitespace-only changes.

scripts/integration_test.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
SUCCESS=false
5+
HOMELABMANAGERPORT=$(shuf -i 4500-5500 -n 1)
6+
TESTVMIP="192.168.1.$(shuf -i 20-250 -n 1)"
7+
ROLETOTEST=${1-shellserver}
8+
echo "Testing role $ROLETOTEST on homelabmanager 127.0.0.1:$HOMELABMANAGERPORT with VM IP $TESTVMIP"
9+
10+
function terraform_environment()
11+
{
12+
# Ensure an environment
13+
export LIBVIRT_DEFAULT_URI="qemu:///system"
14+
mkdir -p /tmp/integration-test-environment/terraform
15+
cd /tmp/integration-test-environment/terraform
16+
touch checksum
17+
chronic wget "127.0.0.1:$HOMELABMANAGERPORT/terraform/?host=$1" -O main.tf
18+
chronic sudo terraform init || /bin/true
19+
chronic sudo terraform validate
20+
if ! cat checksum | md5sum --quiet -c; then
21+
md5sum main.tf > checksum
22+
sudo terraform destroy --auto-approve || /bin/true
23+
sudo virsh list --all | grep -v debian-10 | grep -v focal | grep -v ubuntu18 | grep -v arch-openstack | grep shut | awk '{print$2}' | xargs -I {} sh -c 'sudo virsh destroy {} || /bin/true; sudo virsh undefine {} || /bin/true' || /bin/true
24+
cd /var/lib/libvirt/images
25+
ls /var/lib/libvirt/images | grep -v focal | grep -v bionic-server | grep -v debian-10 | grep -v arch-openstack | xargs -I {} sudo rm -rf "{}"
26+
cd -
27+
sudo terraform apply --auto-approve || /bin/true
28+
sleep 1
29+
sudo terraform apply --auto-approve
30+
fi
31+
}
32+
33+
function kill_any_homelabmanager_api()
34+
{
35+
pkill -f "127.0.0.1:$HOMELABMANAGERPORT" || /bin/true
36+
}
37+
38+
function print_test_result()
39+
{
40+
if $SUCCESS; then
41+
echo "Tests passed!"
42+
exit 0
43+
else
44+
echo "Test failed!"
45+
/bin/false
46+
exit 1
47+
fi
48+
}
49+
50+
# Clean up from any previous attempt
51+
kill_any_homelabmanager_api
52+
sudo rm -rf /tmp/integration-test-environment
53+
54+
# Copy the repo to the test environment
55+
HOMELABDIR=$(dirname $(dirname $(realpath -s "$0")))
56+
mkdir -p /tmp/integration-test-environment
57+
cd /tmp/integration-test-environment
58+
cp -R $HOMELABDIR /tmp/integration-test-environment/
59+
60+
# Start the homelabmanager API
61+
cd /tmp/integration-test-environment/homelabmanager
62+
python3 -m venv venv
63+
. venv/bin/activate
64+
pip3 install -r requirements/dev.txt
65+
ACTIVE_INTERFACE=$(ip addr | awk '/state UP/ {print $2}' | cut -d ':' -f1 | tail -n 1)
66+
./manage.py migrate
67+
sed -i "s/eth0/$ACTIVE_INTERFACE/g" fixtures/integrationtest.json
68+
sed -i "s/192.168.1.123/$TESTVMIP/g" fixtures/integrationtest.json
69+
sed -i "s/shellserver/$ROLETOTEST/g" fixtures/integrationtest.json
70+
./manage.py loaddata fixtures/integrationtest.json
71+
export VM_SALTMASTER_IP=127.0.0.1
72+
./manage.py runserver 127.0.0.1:$HOMELABMANAGERPORT &
73+
sleep 5
74+
75+
if test -d /tmp/integration-test-environment/terraform; then
76+
terraform_environment cleanup
77+
fi
78+
terraform_environment local
79+
80+
echo "Giving things some time to get started"
81+
sleep 120
82+
83+
# Check if the unit tests pass on the server
84+
MACHINECHECKMAXATTEMPTS=10
85+
MACHINECHECKCOUNT=0
86+
while [ -z "$MACHINECHECKPID" ] && [ "$MACHINECHECKCOUNT" -lt "$MACHINECHECKMAXATTEMPTS" ] && ! sudo virsh -c qemu:///system qemu-agent-command integrationtest_on_local "{\"execute\": \"guest-exec-status\", \"arguments\": { \"pid\": $MACHINECHECKPID }}" | jq -r '.return.["out-data"]' | base64 --decode | grep 'All tests pass'; do
87+
let MACHINECHECKCOUNT=MACHINECHECKCOUNT+1
88+
echo "Checking if machine-check is passing yet"
89+
MACHINECHECKPID=$(sudo virsh -c qemu:///system qemu-agent-command integrationtest_on_local '{"execute": "guest-exec", "arguments": { "path": "/usr/bin/machine-check", "arg": [ ], "capture-output": true }}' | jq .return.pid);
90+
sleep 30;
91+
done
92+
93+
if [ "$MACHINECHECKCOUNT" -lt "$MACHINECHECKMAXATTEMPTS" ]; then
94+
echo "Tests passed!"
95+
SUCCESS=true
96+
else
97+
echo "Ran out of attempts! Test failed. The machine-check assertions did not pass, or failed to run."
98+
fi
99+
100+
terraform_environment cleanup
101+
102+
# Clean up after ourselves
103+
trap 'terraform_environment cleanup' EXIT
104+
trap kill_any_homelabmanager_api EXIT
105+
trap print_test_result EXIT

0 commit comments

Comments
 (0)