Docker setup for automated builds + environment for testing
authorMichael Prokop <mika@debian.org>
Wed, 8 Jul 2015 14:24:41 +0000 (16:24 +0200)
committerMichael Prokop <mika@grml.org>
Wed, 8 Jul 2015 23:28:12 +0000 (01:28 +0200)
It would be nice to automatically execute a wide range of tests
on each commit to the grml-debootstrap repository. We aren't
there yet, but a combination of docker and packer builds should
provide a decent starting point.

This change includes a Dockerfile to provide a public available
grml-debootstrap docker environment via the Docker Hub Registry
as well as scripts and tests for further exploration.

Please refer to the included README or visit
https://registry.hub.docker.com/u/mika/grml-debootstrap/ for
further information.

docker/.gitignore [new file with mode: 0644]
docker/Dockerfile [new file with mode: 0644]
docker/README.md [new file with mode: 0644]
docker/test_dirinstall.bats [new file with mode: 0644]
docker/test_dirinstall.sh [new file with mode: 0755]
docker/test_vminstall.bats [new file with mode: 0644]
docker/test_vminstall.sh [new file with mode: 0755]

diff --git a/docker/.gitignore b/docker/.gitignore
new file mode 100644 (file)
index 0000000..bac1555
--- /dev/null
@@ -0,0 +1,2 @@
+debian
+*.img
diff --git a/docker/Dockerfile b/docker/Dockerfile
new file mode 100644 (file)
index 0000000..5439785
--- /dev/null
@@ -0,0 +1,29 @@
+# Image to be used as a base, e.g. one of the official ones:
+# https://github.com/docker-library/official-images/blob/master/library/debian
+FROM debian:sid
+
+# install packages
+RUN apt-get update
+
+# main packages
+RUN apt-get install -y grml-debootstrap bats eatmydata
+
+# convenient packages
+RUN apt-get install -y curl less vim wget zsh
+
+# grml config
+RUN wget -O /root/.vimrc http://git.grml.org/f/grml-etc-core/etc/vim/vimrc
+RUN wget -O /root/.zshrc http://git.grml.org/f/grml-etc-core/etc/zsh/zshrc
+
+# nice defaults
+ENV LANG C.UTF-8
+ENV TERM xterm-256color
+
+# be verbose about package versions
+RUN echo 'APT::Get::Show-Versions "1";' > /etc/apt/apt.conf.d/verbose
+
+# cleanup
+RUN apt-get clean
+RUN rm -rf /var/lib/apt/lists/*
+
+ENTRYPOINT ["/bin/zsh"]
diff --git a/docker/README.md b/docker/README.md
new file mode 100644 (file)
index 0000000..a2ca38f
--- /dev/null
@@ -0,0 +1,61 @@
+Purpose
+-------
+
+Execute grml-debootstrap inside a docker container.
+This is useful e.g. for developing new features, reproducing problems and writing tests in a clean and reproducible environment.
+
+Files
+-----
+
+The Dockerfile is controlling the [automated builds at the Docker Hub Registry](https://registry.hub.docker.com/u/mika/grml-debootstrap/).
+
+The `*.bats` files are test scripts, used for verifying the Debian installations as generated by the `test*.sh` scripts.
+
+Usage instructions
+------------------
+
+Make sure the docker image generated by the Dockerfile is available on your system:
+
+````
+% docker pull mika/grml-debootstrap
+````
+
+Start a container instance and switch into it via e.g.:
+
+````
+% docker run -it --privileged=true mika/grml-debootstrap
+````
+
+*NOTE:* `--privileged=true` is required for usage with loop devices and mounting proc, sysfs etc inside the container
+
+*TIP:* add the `--rm` option to the command line to automatically remove the container when it exits
+
+Use grml-debootstrap inside the container, e.g.:
+
+* Install Debian inside a VM image:
+
+````
+# grml-debootstrap --vmfile --target /srv/debian.img --password grml --hostname docker [--force]
+````
+
+* Install Debian system in a directory:
+
+````
+# grml-debootstrap --target /srv/debian --password grml --hostname docker [--force]
+````
+
+*TIP:* to speed up the build prepend `eatmydata` in the the grml-debootstrap command line
+
+*TIP:* if you want to have access to the generated Debian systems after exiting the container make sure to share your working directory as `/srv` via adding `-v $PWD:/srv/` to your `docker run` command line
+
+Usage of test scripts
+---------------------
+
+This directory provides example scripts (`test*.sh`) and tests (`*.bats`) for building and verifying Debian installations, generated via grml-debootstrap.
+To use them make sure this docker directory is available inside the docker container at /srv:
+
+````
+% docker run -it --privileged=true -v $PWD:/srv/ --rm mika/grml-debootstrap
+````
+
+Then you can simply invoke `/srv/test_dirinstall.sh` or `/srv/test_vminstall.sh`, which will install Debian systems at `/srv/debian` or `/srv/debian.img` respectively and run some tests on the resulting systems.
diff --git a/docker/test_dirinstall.bats b/docker/test_dirinstall.bats
new file mode 100644 (file)
index 0000000..86bd5fa
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env bats
+
+mountpoint="/srv/debian"
+
+@test "ensure no grub configuration is present" {
+  run ls "${mountpoint}"/boot/grub/grub.cfg
+  [ "$status" -ne 0 ]
+}
+
+@test "kernel is absent" {
+  run ls "${mountpoint}"/boot/vmlinuz-*
+  [ "$status" -ne 0 ]
+}
+
+@test "ensure eatmydata package is present" {
+  run chroot "${mountpath}" dpkg --list eatmydata
+  [ "$status" -eq 0 ]
+}
+
+@test "debian_version exists and is valid version" {
+  run cat "${mountpoint}/etc/debian_version"
+  [ "$status" -eq 0 ]
+  [[ "$output" == [0-9].[0-9]* ]] || [[ "$output" == 'stretch/sid' ]]
+}
diff --git a/docker/test_dirinstall.sh b/docker/test_dirinstall.sh
new file mode 100755 (executable)
index 0000000..32f9030
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+set -e
+set -u
+
+if ! [ -r /.dockerenv ] ; then
+  echo "This doesn't look like a docker container, exiting to avoid data damage." >&2
+  exit 1
+fi
+
+echo eatmydata >> /etc/debootstrap/packages
+
+eatmydata grml-debootstrap --target /srv/debian --password grml --hostname docker --nokernel --force
+
+bats /srv/test_dirinstall.bats
diff --git a/docker/test_vminstall.bats b/docker/test_vminstall.bats
new file mode 100644 (file)
index 0000000..95254b4
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/env bats
+
+mountpath="/mnt"
+image="/srv/debian.img"
+
+setup() {
+  if ! mountpoint "${mountpath}" &>/dev/null ; then
+    partition="$(kpartx -asv ${image} | awk '/add/ {print $3}')"
+    mount "/dev/mapper/${partition}" "${mountpath}"
+  fi
+}
+
+teardown() {
+  if mountpoint "${mountpath}" &>/dev/null ; then
+    umount "${mountpath}"
+    kpartx -vd "${image}"
+  fi
+}
+
+@test "ensure grub configuration is present" {
+  run ls "${mountpath}"/boot/grub/grub.cfg
+  [ "$status" -eq 0 ]
+}
+
+@test "ensure eatmydata package is present" {
+  run chroot "${mountpath}" dpkg --list eatmydata
+  [ "$status" -eq 0 ]
+}
+
+@test "kernel is present" {
+  run ls "${mountpath}"/boot/vmlinuz-*
+  [ "$status" -eq 0 ]
+}
+
+@test "debian_version exists and is valid version" {
+  run cat "${mountpath}/etc/debian_version"
+  [ "$status" -eq 0 ]
+  [[ "$output" == [0-9].[0-9]* ]] || [[ "$output" == 'stretch/sid' ]]
+}
diff --git a/docker/test_vminstall.sh b/docker/test_vminstall.sh
new file mode 100755 (executable)
index 0000000..bf48735
--- /dev/null
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+set -e
+set -u
+
+# MOUNTPATH="/mnt"
+
+#bailout() {
+#  [ -n "${LOOP_DEVICE:-}" ] || exit 1
+#  [ -n "${1:-}" ] || EXIT_CODE=1
+#  umount "${MOUNTPATH}"
+#  kpartx -vd "${LOOP_DEVICE}"
+#  losetup -d "${LOOP_DEVICE}"
+#  exit $EXIT_CODE
+#}
+#
+#trap "bailout 1" ERR HUP INT QUIT TERM
+
+if ! [ -r /.dockerenv ] ; then
+  echo "This doesn't look like a docker container, exiting to avoid data damage." >&2
+  exit 1
+fi
+
+echo eatmydata >> /etc/debootstrap/packages
+eatmydata grml-debootstrap --vmfile --target /srv/debian.img --password grml --hostname docker --force
+
+# get access to inner file system
+#LOOP_DEVICE="$(losetup -fv /srv/debian.img)"
+#PARTITION="$(kpartx -asv ${LOOP_DEVICE} | awk '/add/ {print $3}')"
+#mount "${PARTITION}" "${MOUNTPATH}"
+
+bats /srv/test_vminstall.bats
+
+# cleanup
+#bailout 0