aboutsummaryrefslogtreecommitdiff
path: root/terraform/test
diff options
context:
space:
mode:
Diffstat (limited to 'terraform/test')
-rw-r--r--terraform/test/.gitignore1
-rw-r--r--terraform/test/Makefile62
-rw-r--r--terraform/test/README.md26
-rwxr-xr-xterraform/test/vm/customize.sh21
4 files changed, 110 insertions, 0 deletions
diff --git a/terraform/test/.gitignore b/terraform/test/.gitignore
new file mode 100644
index 0000000..2f7896d
--- /dev/null
+++ b/terraform/test/.gitignore
@@ -0,0 +1 @@
+target/
diff --git a/terraform/test/Makefile b/terraform/test/Makefile
new file mode 100644
index 0000000..7f484e0
--- /dev/null
+++ b/terraform/test/Makefile
@@ -0,0 +1,62 @@
+all: run
+
+# Create a base Debian image.
+#
+# The base image will configure apt to use apt-cacher-ng, which is
+# required to be installed on the host.
+target/base.img:
+ mkdir -p target
+ sudo vmdebootstrap \
+ --verbose \
+ --owner=$(shell whoami) \
+ --size=3G \
+ --mirror=http://127.0.0.1:3142/debian \
+ --apt-mirror=http://10.0.2.2:3142/debian \
+ --configure-apt \
+ --distribution=buster \
+ --sudo \
+ --grub \
+ --serial-console \
+ --customize=./vm/customize.sh \
+ --image $@
+
+# Create a copy-on-write snapshot of the base image.
+# VMs will use this image to enable quick testing and fast roll-back.
+target/snapshot.qcow2: target/base.img
+ mkdir -p target
+ rm -f $@
+ qemu-img create \
+ -f qcow2 \
+ -b $(notdir $<) $@
+
+# Start a VM for testing config packages
+run: target/snapshot.qcow2
+ qemu-system-x86_64 \
+ -enable-kvm \
+ -machine q35,accel=kvm,kernel-irqchip=split \
+ -m 1024 \
+ -smp 2 \
+ -device intel-iommu,intremap=on \
+ -netdev user,\
+ hostfwd=tcp::10022-:22,\
+ hostfwd=tcp::10080-:80,\
+ hostfwd=tcp::10443-:443,\
+ id=net0 \
+ -device e1000,netdev=net0 \
+ -virtfs local,\
+ path=../provision,\
+ mount_tag=host0,\
+ security_model=mapped,\
+ id=host0 \
+ -drive format=qcow2,file=$< \
+ -nographic \
+ -monitor none \
+ -serial stdio
+
+clean:
+ rm -rf target/snapshot.qcow2
+
+dist-clean:
+ rm -rf target
+
+.PHONY: all run clean dist-clean
diff --git a/terraform/test/README.md b/terraform/test/README.md
new file mode 100644
index 0000000..f418b36
--- /dev/null
+++ b/terraform/test/README.md
@@ -0,0 +1,26 @@
+# Test Utilities for Provisioning Scripts
+
+## Dependencies
+
+```bash
+apt install \
+ apt-cacher-ng \
+ build-essential \
+ qemu-kvm \
+ vmdebootstrap
+```
+## Example
+
+1. `make run`: starts a virtual machine and mounts provisioning
+ scripts. Note that running this the first time will build a base
+ virtual machine image, requiring significatnt time and
+ bandwidth. Any changes applied to the filesystem from within a
+ running VM will be contained in a copy-on-write snapshot image.
+
+2. login with `root` (no password)
+
+3. `/usr/local/share/provision/provision --force` to run provisioning
+ scripts
+
+4. back on the host, visit `https://ip.localhost:10443` to confirm the
+ service is running
diff --git a/terraform/test/vm/customize.sh b/terraform/test/vm/customize.sh
new file mode 100755
index 0000000..8d18de0
--- /dev/null
+++ b/terraform/test/vm/customize.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+abort() {
+ echo "$1" >&2
+ exit 1
+}
+
+rootdir="$1"
+
+# avoid messing with host system, in case this script is run by accident
+[[ -n $rootdir ]] || abort "root directory is not set"
+
+mkdir -p $rootdir/usr/local/share/provision
+# mount local provision script directory from host on startup
+echo 9p >> $rootdir/etc/initramfs-tools/modules
+echo 9pnet >> $rootdir/etc/initramfs-tools/modules
+echo 9pnet_virtio >> $rootdir/etc/initramfs-tools/modules
+echo "host0 /usr/local/share/provision 9p trans=virtio,version=9p2000.L 0 0" >> $rootdir/etc/fstab
+
+# boot immediately
+sed --in-place 's/GRUB_TIMEOUT=[0-9]\+/GRUB_TIMEOUT=0/g' $rootdir/etc/default/grub