|
| 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 | +terraform_environment cleanup |
| 76 | +terraform_environment local |
| 77 | + |
| 78 | +# Check if the unit tests pass on the server |
| 79 | +MACHINECHECKMAXATTEMPTS=10 |
| 80 | +MACHINECHECKCOUNT=0 |
| 81 | +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 |
| 82 | + let MACHINECHECKCOUNT=MACHINECHECKCOUNT+1 |
| 83 | + echo "Checking if machine-check is passing yet" |
| 84 | + 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); |
| 85 | + sleep 15; |
| 86 | +done |
| 87 | + |
| 88 | +if [ "$MACHINECHECKCOUNT" -lt "$MACHINECHECKMAXATTEMPTS" ]; then |
| 89 | + echo "Tests passed!" |
| 90 | + SUCCESS=true |
| 91 | +else |
| 92 | + echo "Ran out of attempts! Test failed. The machine-check assertions did not pass, or failed to run." |
| 93 | +fi |
| 94 | + |
| 95 | +terraform_environment cleanup |
| 96 | + |
| 97 | +# Clean up after ourselves |
| 98 | +trap 'terraform_environment cleanup' EXIT |
| 99 | +trap kill_any_homelabmanager_api EXIT |
| 100 | +trap print_test_result EXIT |
0 commit comments