Skip to content

Commit

Permalink
test: integration tests with bats
Browse files Browse the repository at this point in the history
Add integration tests using Docker and bats, with a simple first test to
make sure the test framework works. I'm using my own openSUSE repo for
the packages (hopefully they'll be pushed to Leap soon).

Signed-off-by: Aleksa Sarai <asarai@suse.com>
  • Loading branch information
cyphar committed Nov 11, 2016
1 parent e928b06 commit 22401eb
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 11 deletions.
15 changes: 11 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
language: go

# `make ci` uses Docker.
sudo: required
services:
- docker

# We can't test Go 1.5, because golint requires >1.6 (which is annoying).
# Also note that this probably doesn't work with `make ci` right now.
go:
- 1.6
- 1.7
- master

env:
- DOCKER_IMAGE="opensuse/amd64:latest"

notifications:
email: false

before_script:
- make install-deps

script:
- make ci
- make umoci
- make DOCKER_IMAGE=$DOCKER_IMAGE ci
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# umoci: Umoci Modifies Open Containers' Images
# Copyright (C) 2016 SUSE LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM opensuse/amd64:42.2
MAINTAINER "Aleksa Sarai <asarai@suse.com>"

# Use my personal repo because currently Go is broken in openSUSE (will be
# fixed in https://build.opensuse.org/request/show/439834), and because skopeo
# still isn't in Virtualization:containers or openSUSE:Factory.
RUN zypper ar -f -p 10 -g obs://home:cyphar obs-home-cyphar && \
zypper --gpg-auto-import-keys -n ref && \
zypper -n up
RUN zypper -n in go git make skopeo go-mtree bats

ENV GOPATH /go
ENV PATH $GOPATH/bin:$PATH
RUN go get -u github.com/golang/lint/golint

ENV SOURCE_IMAGE=/opensuse SOURCE_TAG=latest
ARG DOCKER_IMAGE=opensuse/amd64:tumbleweed
RUN skopeo copy docker://$DOCKER_IMAGE oci:$SOURCE_IMAGE:$SOURCE_TAG

VOLUME ["/go/src/github.com/cyphar/umoci"]
WORKDIR /go/src/github.com/cyphar/umoci
COPY . /go/src/github.com/cyphar/umoci
39 changes: 32 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ GO ?= go

# Set up the ... lovely ... GOPATH hacks.
PROJECT := github.com/cyphar/umoci
UMOCI_LINK := vendor/$(PROJECT)

# We use Docker because Go is just horrific to deal with.
UMOCI_IMAGE := umoci_dev

# Version information.
VERSION := $(shell cat ./VERSION)
Expand All @@ -30,18 +32,20 @@ GO_SRC = $(shell find . -name \*.go)
umoci: $(GO_SRC)
$(GO) build -i -ldflags "-X main.gitCommit=${COMMIT} -X main.version=${VERSION}" -tags "$(BUILDTAGS)" -o umoci $(PROJECT)/cmd/umoci

.PHONY: update-deps
update-deps:
hack/vendor.sh

.PHONY: clean
clean:
rm -f umoci

install-deps:
go get -u github.com/golang/lint/golint
go get -u github.com/vbatts/git-validation
validate: umociimage
docker run -it -v $(PWD):/go/src/$(PROJECT) $(UMOCI_IMAGE) make local-validate

EPOCH_COMMIT ?= 97ecdbd53dcb72b7a0d62196df281f131dc9eb2f
validate:
.PHONY: local-validate
local-validate:
test -z "$$(gofmt -s -l . | grep -v '^vendor/' | grep -v '^third_party/' | tee /dev/stderr)"
out="$$(golint $(PROJECT)/... | grep -v '/vendor/' | grep -v '/third_party/' | grep -vE 'system/utils_linux.*ALL_CAPS|system/mknod_linux.*underscores')"; \
if [ -n "$$out" ]; then \
Expand All @@ -52,7 +56,28 @@ validate:
#@echo "git-validation"
#@git-validation -v -run DCO,short-subject,dangling-whitespace $(EPOCH_COMMIT)..HEAD

test:
# Used for tests.
DOCKER_IMAGE :=opensuse/amd64:tumbleweed

.PHONY: umociimage
umociimage:
docker build -t $(UMOCI_IMAGE) --build-arg DOCKER_IMAGE=$(DOCKER_IMAGE) .

.PHONY: test-unit
test-unit: umociimage
docker run -it -v $(PWD):/go/src/$(PROJECT) $(UMOCI_IMAGE) make local-test-unit

.PHONY: local-test-unit
local-test-unit: umoci
go test -v $(PROJECT)/...

ci: umoci validate test
.PHONY: test-integration
test-integration: umociimage
docker run -it -v $(PWD):/go/src/$(PROJECT) $(UMOCI_IMAGE) make local-test-integration

.PHONY: local-test-integration
local-test-integration: umoci
bats -t test/*.bats

.PHONY: ci
ci: umoci validate test-unit test-integration
55 changes: 55 additions & 0 deletions test/helpers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
# umoci: Umoci Modifies Open Containers' Images
# Copyright (C) 2016 SUSE LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Root directory of integration tests.
INTEGRATION_ROOT=$(dirname "$(readlink -f "$BASH_SOURCE")")
UMOCI="${INTEGRATION_ROOT}/../umoci"
GOMTREE="/usr/bin/gomtree" # For some reason $(whence ...) and $(where ...) are broken.

# The source OCI image path, which we will make a copy of for each test.
SOURCE_IMAGE="${SOURCE_IMAGE:-/image}"
SOURCE_TAG="${SOURCE_TAG:-latest}"

# Where we're going to copy the images and bundle to.
IMAGE="${BATS_TMPDIR}/image"
TAG="${SOURCE_TAG}"

function umoci() {
sane_run "$UMOCI" "$@"
}

function gomtree() {
sane_run "$GOMTREE" "$@"
}

function sane_run() {
local cmd="$1"
shift

run "$cmd" "$@"

# Some debug information to make life easier.
echo "$(basename "$cmd") $@ (status=$status)" >&2
echo "$output" >&2
}

function setup_image() {
cp -r "${SOURCE_IMAGE}" "${IMAGE}"
}

function teardown_image() {
rm -rf "${IMAGE}"
}
48 changes: 48 additions & 0 deletions test/unpack.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bats -t
# umoci: Umoci Modifies Open Containers' Images
# Copyright (C) 2016 SUSE LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load helpers

BUNDLE_A="$BATS_TMPDIR/bundle.a"
BUNDLE_B="$BATS_TMPDIR/bundle.b"

function setup() {
setup_image
}

function teardown() {
teardown_image
rm -rf "$BUNDLE_A"
rm -rf "$BUNDLE_B"
}

@test "unpack [consistent results]" {
# Unpack the image.
umoci unpack --image "$IMAGE" --from "$TAG" --bundle "$BUNDLE_A"
[ "$status" -eq 0 ]

# Wait a beat.
sleep 5s

# Unpack it again.
umoci unpack --image "$IMAGE" --from "$TAG" --bundle "$BUNDLE_B"
[ "$status" -eq 0 ]

# Ensure that gomtree suceeds on the new unpacked bundle.
gomtree -p "$BUNDLE_B/rootfs" -f "$BUNDLE_A"/sha256:*.mtree
[ "$status" -eq 0 ]
[ -z "$output" ]
}

0 comments on commit 22401eb

Please sign in to comment.