Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike: write integration tests with golang #407

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"github.com/aelsabbahy/goss"
"github.com/aelsabbahy/goss/outputs"
"github.com/urfave/cli"
//"time"
)

const gossName = "goss"

var version string

func main() {
startTime := time.Now()
app := cli.NewApp()
app.EnableBashCompletion = true
app.Version = version
app.Name = "goss"
app.Name = gossName
app.Usage = "Quick and Easy server validation"
app.Flags = []cli.Flag{
cli.StringFlag{
Expand Down Expand Up @@ -286,11 +287,11 @@ func main() {
Value: 5 * time.Second,
},
cli.StringFlag{
Name: "username, u",
Name: "username, u",
Usage: "Username for basic auth",
},
cli.StringFlag{
Name: "password, p",
Name: "password, p",
Usage: "Password for basic auth",
},
},
Expand Down
86 changes: 86 additions & 0 deletions cmd/goss/goss_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// +build integration

package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/rendon/testcli"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
err := os.Chdir("../..")
if err != nil {
fmt.Printf("could not change dir: %v", err)
os.Exit(1)
}
build := exec.Command("go", "build", "./cmd/goss")
err = build.Run()
if err != nil {
fmt.Printf("could not make binary for %s: %v", gossName, err)
os.Exit(1)
}
err = os.Chdir(filepath.Join("cmd", "goss"))
if err != nil {
fmt.Printf("could not cd back to cmd/goss to run test suite: %v", err)
}
os.Exit(m.Run())
}

func TestGossSuccess(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
args []string
}{
"no_args": {
args: []string{},
},
"help": {
args: []string{"--help"},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
c := command(tc.args)
c.Run()
assert.True(t, c.Success())
stdoutGolden := golden("goss", name, "stdout", true)
stderrGolden := golden("goss", name, "stderr", true)
if os.Getenv("UPDATE_GOLDEN") != "" {
ioutil.WriteFile(stdoutGolden, []byte(c.Stdout()), 0644)
ioutil.WriteFile(stderrGolden, []byte(c.Stderr()), 0644)
}
assert.Equal(t, read(t, stdoutGolden), c.Stdout())
assert.Equal(t, read(t, stderrGolden), c.Stderr())
})
}
}

func command(args []string) *testcli.Cmd {
return testcli.Command(filepath.Join("..", "../", "goss"), args...)
}

func golden(command string, caseName string, streamName string, isPass bool) string {
outcome := "fail"
if isPass {
outcome = "pass"
}
return filepath.Join("testdata", command, fmt.Sprintf("%v.%v.%v.golden", caseName, streamName, outcome))
}

func read(t *testing.T, golden string) string {
content, err := ioutil.ReadFile(golden)
if err != nil {
if os.IsNotExist(err) {
return "" // this is fine; failure will happen on assert.
}
t.Fatalf("Could not read golden from %v: %v", golden, err)
}
return string(content)
}
Empty file.
20 changes: 20 additions & 0 deletions cmd/goss/testdata/goss/help.stdout.pass.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
NAME:
goss - Quick and Easy server validation

USAGE:
goss [global options] command [command options] [arguments...]

COMMANDS:
validate, v Validate system
serve, s Serve a health endpoint
render, r render gossfile after imports
autoadd, aa automatically add all matching resource to the test suite
add, a add a resource to the test suite
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--gossfile value, -g value Goss file to read from / write to (default: "./goss.yaml") [$GOSS_FILE]
--vars value json/yaml file containing variables for template [$GOSS_VARS]
--package value Package type to use [rpm, deb, apk, pacman]
--help, -h show help
--version, -v print the version
Empty file.
20 changes: 20 additions & 0 deletions cmd/goss/testdata/goss/no_args.stdout.pass.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
NAME:
goss - Quick and Easy server validation

USAGE:
goss [global options] command [command options] [arguments...]

COMMANDS:
validate, v Validate system
serve, s Serve a health endpoint
render, r render gossfile after imports
autoadd, aa automatically add all matching resource to the test suite
add, a add a resource to the test suite
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--gossfile value, -g value Goss file to read from / write to (default: "./goss.yaml") [$GOSS_FILE]
--vars value json/yaml file containing variables for template [$GOSS_VARS]
--package value Package type to use [rpm, deb, apk, pacman]
--help, -h show help
--version, -v print the version
1 change: 1 addition & 0 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ If you want to keep your tests in separate files, the best way to obtain a singl
* [http](#http)
* [interface](#interface)
* [kernel-param](#kernel-param)
* [matching](#matching)
* [mount](#mount)
* [package](#package)
* [port](#port)
Expand Down
21 changes: 14 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ import:
- pkg/mount
- package: github.com/patrickmn/go-cache
- package: github.com/miekg/dns
- package: github.com/rendon/testcli
- package: github.com/stretchr/testify
version: ~1.2.2
subpackages:
- assert
- require
36 changes: 36 additions & 0 deletions integration-tests/Dockerfile_wheezy
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
# tester build stage runs the golang integration tests within the container
FROM debian:wheezy as tester

ARG arch=amd64
ARG golang_version=1.9.3

RUN apt-get update && \
apt-get install -y \
ca-certificates \
curl \
git \
unzip && \
apt-get remove -y vim-tiny && \
apt-get clean
RUN curl -L "https://dl.google.com/go/go${golang_version}.linux-${arch}.tar.gz" --output golang.tgz && \
tar zxvf golang.tgz --directory /usr/local >/dev/null && \
ln -s /usr/local/go/bin/go /usr/local/bin/go && \
rm -rf golang.tgz
COPY ./ /code/src/github.com/aelsabbahy/goss
RUN cd /code/src/github.com/aelsabbahy/goss && \
export GOPATH=/code && \
curl -L https://github.com/Masterminds/glide/releases/download/0.10.2/glide-0.10.2-linux-${arch}.zip --output glide.zip && \
rm -rf linux-${arch} && \
unzip glide.zip && \
linux-${arch}/glide install && \
go test -v -tags integration ./cmd/goss && \
rm -rf /code && \
rm /usr/local/bin/go && \
rm -rf /usr/local/bin/go && \
unset GOPATH && \
apt-get remove -y \
curl \
git \
unzip && \
apt-get clean

FROM debian:wheezy
MAINTAINER Ahmed

Expand Down
8 changes: 7 additions & 1 deletion integration-tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -xeu

os=$1
arch=$2
golang_version="${TRAVIS_GO_VERSION-1.9.3}"

seccomp_opts() {
local docker_ver minor_ver
Expand All @@ -17,7 +18,12 @@ seccomp_opts() {
cp "../release/goss-linux-$arch" "goss/$os/"
# Run build if Dockerfile has changed but hasn't been pushed to dockerhub
if ! md5sum -c "Dockerfile_${os}.md5"; then
docker build -t "aelsabbahy/goss_${os}:latest" - < "Dockerfile_$os"
docker build \
-t "aelsabbahy/goss_${os}:latest" \
--file "Dockerfile_${os}" \
--build-arg "golang_version=${golang_version}" \
--build-arg "arch=${arch}" \
".."
# Pull if image doesn't exist locally
elif ! docker images | grep "aelsabbahy/goss_$os";then
docker pull "aelsabbahy/goss_$os"
Expand Down