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

Commit

Permalink
Add more integration tests for ARG caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Bogdanov committed Jul 16, 2016
1 parent 474102d commit cbab002
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
70 changes: 68 additions & 2 deletions test/buildarg_test.go
@@ -1,6 +1,10 @@
package tests

import "testing"
import (
"testing"

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

func TestBuildArg_Simple(t *testing.T) {
rockerfileContent := `
Expand All @@ -9,10 +13,72 @@ RUN export | grep proxy`

err := runRockerBuildWithOptions(rockerBuildOptions{
rockerfileContent: rockerfileContent,
buildParams: []string{"--no-cache", "--build-arg", "http_proxy=http://host:3128"},
buildParams: []string{"--build-arg", "http_proxy=http://host:3128", "--no-cache", "--no-garbage"},
testLines: []string{"export http_proxy='http://host:3128'"},
})
if err != nil {
t.Fatal(err)
}
}

func TestBuildArg_CacheHit(t *testing.T) {
var sha1, sha2 string

rockerfileContent := `
FROM alpine
ENV test_cache=` + randomString() + `
RUN export | grep proxy`

err := runRockerBuildWithOptions(rockerBuildOptions{
rockerfileContent: rockerfileContent,
buildParams: []string{"--build-arg", "http_proxy=http://host:3128"},
sha: &sha1,
})
if err != nil {
t.Fatal(err)
}
defer removeImage(sha1)

err = runRockerBuildWithOptions(rockerBuildOptions{
rockerfileContent: rockerfileContent,
buildParams: []string{"--build-arg", "http_proxy=http://host:3128"},
sha: &sha2,
})
if err != nil {
t.Fatal(err)
}
defer removeImage(sha2)

assert.Equal(t, sha1, sha2, "same build-arg should not invalidate cache")
}

func TestBuildArg_CacheInvalidate(t *testing.T) {
var sha1, sha2 string

rockerfileContent := `
FROM alpine
ENV test_cache=` + randomString() + `
RUN export | grep proxy`

err := runRockerBuildWithOptions(rockerBuildOptions{
rockerfileContent: rockerfileContent,
buildParams: []string{"--build-arg", "http_proxy=http://host:3128"},
sha: &sha1,
})
if err != nil {
t.Fatal(err)
}
defer removeImage(sha1)

err = runRockerBuildWithOptions(rockerBuildOptions{
rockerfileContent: rockerfileContent,
buildParams: []string{"--build-arg", "http_proxy=http://host:3129"}, // note different port
sha: &sha2,
})
if err != nil {
t.Fatal(err)
}
defer removeImage(sha2)

assert.NotEqual(t, sha1, sha2, "different build-arg values should invalidate cache")
}
3 changes: 1 addition & 2 deletions test/pull_test.go
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"fmt"
"strconv"
"testing"
"time"

Expand All @@ -22,7 +21,7 @@ func TestPull_Simple(t *testing.T) {
}

func TestPull_PushAndPull(t *testing.T) {
randomData := strconv.Itoa(int(time.Now().UnixNano() % int64(100000001)))
randomData := randomString()

auth, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions test/utils.go
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -19,6 +21,10 @@ import (
"github.com/mitchellh/go-homedir"
)

var (
outputShaRe = regexp.MustCompile("Successfully built (sha256:[a-f0-9]+)")
)

func runCmd(command string, args ...string) (string, error) {
return runCmdWithOptions(cmdOptions{
command: command,
Expand Down Expand Up @@ -136,6 +142,7 @@ type rockerBuildOptions struct {
testLines []string
workdir string
stdout io.Writer
sha *string
}

func runRockerBuildWithOptions(opts rockerBuildOptions) error {
Expand Down Expand Up @@ -187,6 +194,14 @@ func runRockerBuildWithOptions(opts rockerBuildOptions) error {
return fmt.Errorf("Failed to run rocker build, error: %s", err)
}

if opts.sha != nil {
if match := outputShaRe.FindStringSubmatch(output); match != nil {
*opts.sha = match[1]
} else {
return fmt.Errorf("Expected rocker build to return image SHA, got nothing.\n\nRocker build output:\n%s", output)
}
}

if len(opts.testLines) > 0 {
linesMap := map[string]int{}
for _, l := range strings.Split(output, "\n") {
Expand Down Expand Up @@ -260,6 +275,10 @@ func makeTempDir(t *testing.T, prefix string, files map[string]string) string {
return tmpDir
}

func randomString() string {
return strconv.Itoa(int(time.Now().UnixNano() % int64(100000001)))
}

func debugf(format string, args ...interface{}) {
if *verbosityLevel >= 2 {
fmt.Printf(format, args...)
Expand Down

0 comments on commit cbab002

Please sign in to comment.