From c7b679e686f25da086d89c1dc1ab460a5af138e1 Mon Sep 17 00:00:00 2001 From: Madhu Venugopal Date: Sat, 25 Apr 2015 04:54:51 -0700 Subject: [PATCH] Initial bats based integration tests for testing daemon network configs Pre-reqs : * docker machine (https://github.com/docker/machine) * bats (https://github.com/sstephenson/bats) * virtualbox Signed-off-by: Madhu Venugopal --- test/integration/README.md | 34 +++++++++ test/integration/daemon-configs.bats | 104 +++++++++++++++++++++++++++ test/integration/daemon.cfg | 4 ++ test/integration/helpers.bash | 50 +++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 test/integration/README.md create mode 100644 test/integration/daemon-configs.bats create mode 100644 test/integration/daemon.cfg create mode 100644 test/integration/helpers.bash diff --git a/test/integration/README.md b/test/integration/README.md new file mode 100644 index 0000000000..777b1cfa46 --- /dev/null +++ b/test/integration/README.md @@ -0,0 +1,34 @@ +# LibNetwork Integration Tests + +Integration tests provide end-to-end testing of LibNetwork and Drivers. + +While unit tests verify the code is working as expected by relying on mocks and +artificially created fixtures, integration tests actually use real docker +engines and communicate to it through the CLI. + +Note that integration tests do **not** replace unit tests and Docker is used as a good use-case. + +As a rule of thumb, code should be tested thoroughly with unit tests. +Integration tests on the other hand are meant to test a specific feature end to end. + +Integration tests are written in *bash* using the +[bats](https://github.com/sstephenson/bats) framework. + +## Pre-Requisites + +1. Bats (https://github.com/sstephenson/bats#installing-bats-from-source) +2. Docker Machine (https://github.com/docker/machine) +3. Virtualbox (as a Docker machine driver) + +## Running integration tests + +* Start by [installing] (https://github.com/sstephenson/bats#installing-bats-from-source) *bats* on your system. +* If not done already, [install](https://docs.docker.com/machine/) *docker-machine* into /usr/bin +* Make sure Virtualbox is installed as well, which will be used by docker-machine as a driver to launch VMs + +In order to run all integration tests, pass *bats* the test path: +``` +$ bats test/integration/daemon-configs.bats +``` + + diff --git a/test/integration/daemon-configs.bats b/test/integration/daemon-configs.bats new file mode 100644 index 0000000000..fd48fbe199 --- /dev/null +++ b/test/integration/daemon-configs.bats @@ -0,0 +1,104 @@ +#!/usr/bin/env bats + +load helpers + +export DRIVER=virtualbox +export NAME="bats-$DRIVER-daemon-configs" +export MACHINE_STORAGE_PATH=/tmp/machine-bats-daemon-test-$DRIVER +# Default memsize is 1024MB and disksize is 20000MB +# These values are defined in drivers/virtualbox/virtualbox.go +export DEFAULT_MEMSIZE=1024 +export DEFAULT_DISKSIZE=20000 +export CUSTOM_MEMSIZE=1536 +export CUSTOM_DISKSIZE=10000 +export CUSTOM_CPUCOUNT=1 +export BAD_URL="http://dev.null:9111/bad.iso" + +function setup() { + # add sleep because vbox; ugh + sleep 1 +} + +findDiskSize() { + # SATA-0-0 is usually the boot2disk.iso image + # We assume that SATA 1-0 is root disk VMDK and grab this UUID + # e.g. "SATA-ImageUUID-1-0"="fb5f33a7-e4e3-4cb9-877c-f9415ae2adea" + # TODO(slashk): does this work on Windows ? + run bash -c "VBoxManage showvminfo --machinereadable $NAME | grep SATA-ImageUUID-1-0 | cut -d'=' -f2" + run bash -c "VBoxManage showhdinfo $output | grep "Capacity:" | awk -F' ' '{ print $2 }'" +} + +findMemorySize() { + run bash -c "VBoxManage showvminfo --machinereadable $NAME | grep memory= | cut -d'=' -f2" +} + +findCPUCount() { + run bash -c "VBoxManage showvminfo --machinereadable $NAME | grep cpus= | cut -d'=' -f2" +} + +buildMachineWithOldIsoCheckUpgrade() { + run wget https://github.com/boot2docker/boot2docker/releases/download/v1.4.1/boot2docker.iso -O $MACHINE_STORAGE_PATH/cache/boot2docker.iso + run machine create -d virtualbox $NAME + run machine upgrade $NAME +} + +@test "$DRIVER: machine should not exist" { + run machine active $NAME + [ "$status" -eq 1 ] +} + +@test "$DRIVER: VM should not exist" { + run VBoxManage showvminfo $NAME + [ "$status" -eq 1 ] +} + +@test "$DRIVER: create" { + run machine create -d $DRIVER $NAME + [ "$status" -eq 0 ] +} + +@test "$DRIVER: active" { + run machine active $NAME + [ "$status" -eq 0 ] +} + +@test "$DRIVER: check default machine memory size" { + findMemorySize + [[ ${output} == "${DEFAULT_MEMSIZE}" ]] +} + +@test "$DRIVER: check default machine disksize" { + findDiskSize + [[ ${output} == *"$DEFAULT_DISKSIZE"* ]] +} + +@test "$DRIVER: test bridge-ip" { + run machine ssh $NAME sudo /etc/init.d/docker stop + run machine ssh $NAME sudo ifconfig docker0 down + run machine ssh $NAME sudo ip link delete docker0 + BIP='--bip=172.168.45.1/24' + set_extra_config $BIP + cat ${TMP_EXTRA_ARGS_FILE} | machine ssh $NAME sudo tee /var/lib/boot2docker/profile + cat ${DAEMON_CFG_FILE} | machine ssh $NAME "sudo tee -a /var/lib/boot2docker/profile" + run machine ssh $NAME sudo /etc/init.d/docker start + run machine ssh $NAME ifconfig docker0 + [ "$status" -eq 0 ] + [[ ${lines[1]} =~ "172.168.45.1" ]] +} + +@test "$DRIVER: run busybox container" { + run machine ssh $NAME sudo cat /var/lib/boot2docker/profile + run docker $(machine config $NAME) run busybox echo hello world + [ "$status" -eq 0 ] +} + +@test "$DRIVER: remove machine" { + run machine rm -f $NAME +} + +# Cleanup of machine store should always be the last 'test' +@test "$DRIVER: cleanup" { + run rm -rf $MACHINE_STORAGE_PATH + [ "$status" -eq 0 ] +} + diff --git a/test/integration/daemon.cfg b/test/integration/daemon.cfg new file mode 100644 index 0000000000..fc93dbd604 --- /dev/null +++ b/test/integration/daemon.cfg @@ -0,0 +1,4 @@ +CACERT=/var/lib/boot2docker/ca.pem +SERVERCERT=/var/lib/boot2docker/server-key.pem +SERVERKEY=/var/lib/boot2docker/server.pem +DOCKER_TLS=no diff --git a/test/integration/helpers.bash b/test/integration/helpers.bash new file mode 100644 index 0000000000..ec18e5d4eb --- /dev/null +++ b/test/integration/helpers.bash @@ -0,0 +1,50 @@ +#!/bin/bash + +# Root directory of the repository. +MACHINE_ROOT=/usr/bin + +PLATFORM=`uname -s | tr '[:upper:]' '[:lower:]'` +ARCH=`uname -m` + +if [ "$ARCH" = "x86_64" ]; then + ARCH="amd64" +else + ARCH="386" +fi +MACHINE_BIN_NAME=docker-machine_$PLATFORM-$ARCH +BATS_LOG=/tmp/bats.log + +touch ${BATS_LOG} +rm ${BATS_LOG} + +teardown() { + echo "$BATS_TEST_NAME +---------- +$output +---------- + +" >> ${BATS_LOG} +} + +EXTRA_ARGS_CFG='EXTRA_ARGS' +EXTRA_ARGS='--tlsverify --tlscacert=/var/lib/boot2docker/ca.pem --tlskey=/var/lib/boot2docker/server-key.pem --tlscert=/var/lib/boot2docker/server.pem --label=provider=virtualbox -H tcp://0.0.0.0:2376' +TMP_EXTRA_ARGS_FILE=/tmp/tmp_extra_args +DAEMON_CFG_FILE=${BATS_TEST_DIRNAME}/daemon.cfg +set_extra_config() { + if [ -f ${TMP_EXTRA_ARGS_FILE} ]; + then + rm ${TMP_EXTRA_ARGS_FILE} + fi + echo -n "${EXTRA_ARGS_CFG}='" > ${TMP_EXTRA_ARGS_FILE} + echo -n "$1 " >> ${TMP_EXTRA_ARGS_FILE} + echo "${EXTRA_ARGS}'" >> ${TMP_EXTRA_ARGS_FILE} +} + +if [ ! -e $MACHINE_ROOT/$MACHINE_BIN_NAME ]; then + echo "${MACHINE_ROOT}/${MACHINE_BIN_NAME} not found" + exit 1 +fi + +function machine() { + ${MACHINE_ROOT}/$MACHINE_BIN_NAME "$@" +}