Skip to content

Commit

Permalink
Only build images for tests are we are running
Browse files Browse the repository at this point in the history
If we are running only one individual test, we don't want to build
all of the images, so this commit creates a builder which tracks which
images it has built and can be used by a tests to check if it should
build an image before running, or it will use the images that have
already been built by a previous test.

The name of the context tarball has also been made unique (it includes
the unix timestamp) to avoid potential test flakes if two tests using
the same GCS bucket run simultaneously.
  • Loading branch information
bobcatfish committed Jul 27, 2018
1 parent 2a788a9 commit a7a357b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 229 deletions.
14 changes: 6 additions & 8 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ export IMAGE_REPO="gcr.io/somerepo"
make integration-test
```

If you want to run `make integration-test`, you must override the project using environment variables:

* `GCS_BUCKET` - The name of your GCS bucket
* `IMAGE_REPO` - The path to your docker image repo

You can also run tests with `go test`, for example to run tests individually:

```shell
export GCS_BUCKET="gs://<your bucket>"
export IMAGE_REPO="gcr.io/somerepo"
go test -v --bucket $GCS_BUCKET --repo $IMAGE_REPO -run TestLayers/test_layer_dockerfiles/Dockerfile_test_copy
go test -v --bucket $GCS_BUCKET --repo $IMAGE_REPO -run TestLayers/test_layer_Dockerfile_test_copy_bucket
```

Requirements:
Expand All @@ -92,11 +95,6 @@ Requirements:
the user currently logged into `gcloud`
* An image repo which you have write access to via the user currently logged into `gcloud`

If you want to run these tests yourself, you must override the project using environment variables:

* `GCS_BUCKET` - The name of your GCS bucket
* `IMAGE_REPO` - The path to your docker image repo

These tests will be kicked off by [reviewers](#reviews) for submitted PRs.

## Creating a PR
Expand Down
6 changes: 4 additions & 2 deletions integration/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import (
"os/signal"
)

func runOnInterrupt(f func()) {
// RunOnInterrupt will execute the function f if execution is interrupted with the
// interrupt signal.
func RunOnInterrupt(f func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
for range c {
log.Println("Interrupted, cleaning up.")
f()
os.Exit(1)
Expand Down
22 changes: 14 additions & 8 deletions integration/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import (
"os"
"os/exec"
"path/filepath"
"time"
)

func createIntegrationTarball() (string, error) {
// CreateIntegrationTarball will take the contents of the integration directory and write
// them to a tarball in a temmporary dir. It will return a path to the tarball.
func CreateIntegrationTarball() (string, error) {
log.Println("Creating tarball of integration test files to use as build context")
dir, err := os.Getwd()
if err != nil {
Expand All @@ -35,8 +38,7 @@ func createIntegrationTarball() (string, error) {
if err != nil {
return "", fmt.Errorf("Failed to create temporary directoy to hold tarball: %s", err)
}
uuid := randomString(32)
contextFile := fmt.Sprintf("%s/context_%s.tar.gz", tempDir, uuid)
contextFile := fmt.Sprintf("%s/context_%d.tar.gz", tempDir, time.Now().UnixNano())
cmd := exec.Command("tar", "-C", dir, "-zcvf", contextFile, ".")
_, err = RunCommandWithoutTest(cmd)
if err != nil {
Expand All @@ -45,19 +47,23 @@ func createIntegrationTarball() (string, error) {
return contextFile, err
}

func uploadBuildContext(gcsBucket string, contextFile string) (string, error) {
log.Printf("Uploading tarball at %s to GCS bucket at %s\n", contextFile, gcsBucket)
// UploadFileToBucket will upload the at filePath to gcsBucket. It will return the path
// of the file in gcsBucket.
func UploadFileToBucket(gcsBucket string, filePath string) (string, error) {
log.Printf("Uploading file at %s to GCS bucket at %s\n", filePath, gcsBucket)

cmd := exec.Command("gsutil", "cp", contextFile, gcsBucket)
cmd := exec.Command("gsutil", "cp", filePath, gcsBucket)
_, err := RunCommandWithoutTest(cmd)
if err != nil {
return "", fmt.Errorf("Failed to copy tarball to GCS bucket %s: %s", gcsBucket, err)
}

return filepath.Join(gcsBucket, contextFile), err
return filepath.Join(gcsBucket, filePath), err
}

func deleteFromGCS(path string) error {
// DeleteFromBucket will remove the content at path. path should be the full path
// to a file in GCS.
func DeleteFromBucket(path string) error {
cmd := exec.Command("gsutil", "rm", path)
_, err := RunCommandWithoutTest(cmd)
if err != nil {
Expand Down
15 changes: 11 additions & 4 deletions integration/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,22 @@ var singleSnapshotImages = map[string]bool{
var bucketContextTests = []string{"Dockerfile_test_copy_bucket"}
var reproducibleTests = []string{"Dockerfile_test_env"}

func getDockerImage(imageRepo, dockerfile string) string {
// GetDockerImage constructs the name of the docker image that would be built with
// dockerfile if it was tagged with imageRepo.
func GetDockerImage(imageRepo, dockerfile string) string {
return strings.ToLower(imageRepo + dockerPrefix + dockerfile)
}

func getKanikoImage(imageRepo, dockerfile string) string {
// GetKanikoImage constructs the name of the kaniko image that would be built with
// dockerfile if it was tagged with imageRepo.
func GetKanikoImage(imageRepo, dockerfile string) string {
return strings.ToLower(imageRepo + kanikoPrefix + dockerfile)
}

func findDockerFiles(dockerfilesPath string) ([]string, error) {
// FindDockerFiles will look for test docker files in the directory dockerfilesPath.
// These files must start with `Dockerfile_test`. If the file is one we are intentionally
// skipping, it will not be included in the returned list.
func FindDockerFiles(dockerfilesPath string) ([]string, error) {
// TODO: remove test_user_run from this when https://github.com/GoogleContainerTools/container-diff/issues/237 is fixed
testsToIgnore := map[string]bool{"Dockerfile_test_user_run": true}
allDockerfiles, err := filepath.Glob(path.Join(dockerfilesPath, "Dockerfile_test*"))
Expand Down Expand Up @@ -156,7 +163,7 @@ func (d *DockerFileBuilder) BuildImage(imageRepo, gcsBucket, dockerfilesPath, do
if singleSnapshotImages[dockerfile] {
buildArgs = append(buildArgs, singleSnapshotFlag)
}
kanikoImage := getKanikoImage(imageRepo, dockerfile)
kanikoImage := GetKanikoImage(imageRepo, dockerfile)
kanikoCmd := exec.Command("docker",
append([]string{"run",
"-v", os.Getenv("HOME") + "/.config/gcloud:/root/.config/gcloud",
Expand Down
Loading

0 comments on commit a7a357b

Please sign in to comment.