Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Integration tests #78

Merged
merged 17 commits into from
Mar 14, 2016
Merged
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ gocyclo:
test: testdeps fmtcheck vet lint
GO15VENDOREXPERIMENT=1 go test ./src/... $(TESTARGS)

integ:
GO15VENDOREXPERIMENT=1 go test ./src/... -tags="integration" -run TestInteg_
test_integration:
GO15VENDOREXPERIMENT=1 go test ./test/...

.PHONY: clean test fmtcheck lint vet gocyclo default
31 changes: 15 additions & 16 deletions Rockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.5.1
FROM golang:1.6-alpine

{{ $commit := (or .Env.GIT_COMMIT "") }}
{{ $branch := (or .Env.GIT_BRANCH "none") }}
Expand All @@ -9,27 +9,26 @@ WORKDIR /go/src/github.com/grammarly/rocker

ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO15VENDOREXPERIMENT=1

RUN go install \
-a -v -installsuffix cgo \
-ldflags "-X main.Version={{$version}} -X main.GitCommit={{$commit}} -X main.GitBranch={{$branch}} -X main.BuildTime=$(TZ=GMT date '+%Y-%m-%d_%H:%M_GMT')"

{{ if .test }}
MOUNT /var/run/docker.sock:/var/run/docker.sock
{{ if .TestArgs }}
ENV TESTARGS="{{ .TestArgs }}"
{{ end }}
ATTACH ["bash"]
RUN make test
{{ end }}
MOUNT /var/run/docker.sock:/var/run/docker.sock
RUN apk --update add make git bash docker

ATTACH ["bash"]
{{ if .TestArgs }}
ENV TESTARGS="{{ .TestArgs }}"
{{ end }}

RUN \
go build \
-a -installsuffix cgo \
-ldflags "-X main.Version={{$version}} -X main.GitCommit={{$commit}} -X main.GitBranch={{$branch}} -X main.BuildTime=$(TZ=GMT date '+%Y-%m-%d_%H:%M_GMT')" \
-v -o /bin/rocker
ATTACH ["bash"]
RUN make test
RUN make test_integration
{{ end }}

EXPORT /bin/rocker
EXPORT /go/bin/rocker

#========

FROM alpine:3.2

RUN apk --update add git bash
Expand Down
111 changes: 111 additions & 0 deletions test/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package tests

import (
"testing"
)

func TestCacheWithEnvVariables(t *testing.T) {
tag := "rocker-integratin-test:1.2.3"
defer removeImage(tag)

err := runRockerBuild(`
FROM alpine
RUN touch /tmp/foo
TAG ` + tag)

if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha1, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

err = runRockerBuild(`
FROM alpine
RUN ENV_VAR=foo touch /tmp/foo
TAG ` + tag)
if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha2, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

if sha1 == sha2 {
t.Fatal("Env variable should invalidate cache")
}
}
func TestCacheWorksByDefault(t *testing.T) {
tag := "rocker-integratin-test:1.2.3"
defer removeImage(tag)

err := runRockerBuild(`
FROM alpine
RUN touch /tmp/foo
TAG ` + tag)

if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha1, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

err = runRockerBuild(`
FROM alpine
RUN touch /tmp/foo
TAG ` + tag)
if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha2, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

if sha1 != sha2 {
t.Fail()
}
}

func TestNoCache(t *testing.T) {
tag := "rocker-integratin-test:1.2.3"
defer removeImage(tag)

err := runRockerBuild(`
FROM alpine
RUN touch /tmp/foo
TAG ` + tag)
if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha1, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

err = runRockerBuildWithOptions(`
FROM alpine
RUN touch /tmp/foo
TAG `+tag, "--no-cache")
if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha2, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

if sha1 == sha2 {
t.Fatalf("Sha of images are equal but shouldn't. sha1: %s, sha2: %s", sha1, sha2)
}
}
33 changes: 33 additions & 0 deletions test/mount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tests

import (
"io/ioutil"
"os"
"testing"
)

func TestMountLocal(t *testing.T) {
t.Skip("skipping till bug will be fixed")
dir, err := ioutil.TempDir("/tmp", "rocker_integration_test_mount_dir")
if err != nil {
t.Fatalf("Can't create temp dir, err : %v", err)
}
defer os.Remove(dir)

err = runRockerBuildWithOptions("FROM alpine:latest\n"+
"MOUNT "+dir+":/datadir\n"+
"RUN echo -n foobar > /datadir/foo", "--no-cache")

if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

content, err := ioutil.ReadFile(dir + "/foo")
if err != nil {
t.Fatalf("Can't read temp file. Error: %v", err)
}

if "foobar" != string(content) {
t.Fatalf("Content doesn't match, expected: 'foobar', got: '%s'", string(content))
}
}
52 changes: 52 additions & 0 deletions test/tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tests

import (
"testing"
)

func TestTagsDifferent(t *testing.T) {
tag := "rocker_integration_test_tag:latest"
defer removeImage(tag)

err := runRockerBuildWithOptions("FROM alpine:latest\nRUN touch /foobar\nTAG "+tag, "--no-cache")
if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha1, err := getImageShaByName("alpine:latest")
if err != nil {
t.Fatal(err)
}

sha2, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

if sha1 == sha2 {
t.Fatalf("Sha's of source image and tag are identical but shouldn't. sha1: '%s', sha2: '%s'", sha1, sha2)
}
}
func TestTagsTheSame(t *testing.T) {
tag := "rocker_integration_test_tag:latest"
defer removeImage(tag)

err := runRockerBuildWithOptions("FROM alpine:latest\nTAG "+tag, "--no-cache")
if err != nil {
t.Fatalf("Test fail: %v\n", err)
}

sha1, err := getImageShaByName("alpine:latest")
if err != nil {
t.Fatal(err)
}

sha2, err := getImageShaByName(tag)
if err != nil {
t.Fatal(err)
}

if sha1 != sha2 {
t.Fatalf("Sha's of source image and tag mismatch. sha1: '%s', sha2: '%s'", sha1, sha2)
}
}
113 changes: 113 additions & 0 deletions test/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package tests

import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
)

func getGOPATH() string {
gopath := os.Getenv("GOPATH")
if gopath == "" {
panic("$GOPATH is not defined")
}

return gopath
}

func runCmd(executable string, stdoutWriter io.Writer /* stderr io.Writer,*/, params ...string) error {
cmd := exec.Command(executable, params...)
fmt.Printf("Running: %v\n", strings.Join(cmd.Args, " "))
if stdoutWriter != nil {
cmd.Stdout = stdoutWriter
}
//cmd.Stderr = stderr

if err := cmd.Run(); err != nil {
//fmt.Printf("Failed to run '%v' with arguments '%v'\n", executable, params)
return err
}

return nil
}

func removeImage(imageName string) error {
return runCmd("docker", nil, "rmi", imageName)
}

func getImageShaByName(imageName string) (string, error) {
var b bytes.Buffer

if err := runCmd("docker", bufio.NewWriter(&b), "images", "-q", imageName); err != nil {
fmt.Println("Can't execute command:", err)
return "", err
}

sha := strings.Trim(b.String(), "\n")

if len(sha) < 12 {
return "", errors.New("Too short sha")
}

//fmt.Printf("Image: %v, size: %d\n", sha, len(sha))

return sha, nil
}

func runRockerWithFile(filename string) error {
gopath := os.Getenv("GOPATH")
if gopath == "" {
panic("$GOPATH is not defined")
}

if err := runCmd(gopath+"/bin/rocker", nil, "build", "--no-cache", "-f", filename); err != nil {
//fmt.Errorf("Failed to run rocker with filename '%v'", filename)
return err
}

return nil
}

func createTempFile(content string) (string, error) {
tmpfile, err := ioutil.TempFile("/tmp/", "rocker_integration_test_")
if err != nil {
return "", err
}

if _, err := tmpfile.Write([]byte(content)); err != nil {
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}
return tmpfile.Name(), nil
}

func runRockerBuildWithOptions(content string, opts ...string) error {
filename, err := createTempFile(content)
if err != nil {
return err
}
//defer os.Remove(filename)

gopath := getGOPATH()

p := []string{"build", "-f", filename}
params := append(p, opts...)
if err := runCmd(gopath+"/bin/rocker", nil, params...); err != nil {
//fmt.Printf("Failed to run rocker with filename '%v'", filename)
return err
}

return nil
}

func runRockerBuild(content string) error {
return runRockerBuildWithOptions(content)
}