From 4ebdfb63f256a63324b180d65bcbf4410d4bcfd5 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Feb 2018 12:01:52 -0800 Subject: [PATCH] Refactored to use config json isntead of go file --- ...{Dockerfile => Dockerfile_test_extract_fs} | 0 .../dockerfiles/config_test_extract_fs.json | 46 ++++++++ .../executor/Dockerfile | 0 integration_tests/integration_test.go | 105 ------------------ integration_tests/integration_test_yaml.go | 41 ++++--- 5 files changed, 73 insertions(+), 119 deletions(-) rename integration_tests/dockerfiles/{Dockerfile => Dockerfile_test_extract_fs} (100%) create mode 100644 integration_tests/dockerfiles/config_test_extract_fs.json rename Dockerfile => integration_tests/executor/Dockerfile (100%) delete mode 100644 integration_tests/integration_test.go diff --git a/integration_tests/dockerfiles/Dockerfile b/integration_tests/dockerfiles/Dockerfile_test_extract_fs similarity index 100% rename from integration_tests/dockerfiles/Dockerfile rename to integration_tests/dockerfiles/Dockerfile_test_extract_fs diff --git a/integration_tests/dockerfiles/config_test_extract_fs.json b/integration_tests/dockerfiles/config_test_extract_fs.json new file mode 100644 index 000000000..848cd489d --- /dev/null +++ b/integration_tests/dockerfiles/config_test_extract_fs.json @@ -0,0 +1,46 @@ +[ + { + "Image1": "gcr.io/kbuild-test/docker-extract-filesystem:latest", + "Image2": "gcr.io/kbuild-test/kbuild-extract-filesystem:latest", + "DiffType": "File", + "Diff": { + "Adds": [ + { + "Name": "/workspace", + "Size": 16271436 + }, + { + "Name": "/workspace/Dockerfile", + "Size": 0 + }, + { + "Name": "/workspace/executor", + "Size": 16271436 + } + ], + "Dels": [ + { + "Name": "/dev", + "Size": 0 + }, + { + "Name": "/etc/hosts", + "Size": 109 + }, + { + "Name": "/etc/resolv.conf", + "Size": 38 + }, + { + "Name": "/proc", + "Size": 0 + }, + { + "Name": "/sys", + "Size": 0 + } + ], + "Mods": null + } + } +] \ No newline at end of file diff --git a/Dockerfile b/integration_tests/executor/Dockerfile similarity index 100% rename from Dockerfile rename to integration_tests/executor/Dockerfile diff --git a/integration_tests/integration_test.go b/integration_tests/integration_test.go deleted file mode 100644 index 884c9ca3d..000000000 --- a/integration_tests/integration_test.go +++ /dev/null @@ -1,105 +0,0 @@ -// +build integration - -/* -Copyright 2018 Google 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. -*/ -package main - -import ( - "encoding/json" - "fmt" - "os/exec" - "reflect" - "sort" - "testing" -) - -var imageTests = []struct { - description string - repo string - added []string - deleted []string - modified []string -}{ - { - description: "test extract filesystem", - repo: "extract-filesystem", - added: []string{"/workspace", "/workspace/executor", "/workspace/Dockerfile"}, - deleted: []string{"/proc", "/sys", "/dev", "/etc/hosts", "/etc/resolv.conf"}, - }, -} - -func Test_images(t *testing.T) { - daemonPrefix := "daemon://" - testRepo := "gcr.io/kbuild-test/" - dockerPrefix := "docker-" - kbuildPrefix := "kbuild-" - - for _, test := range imageTests { - dockerImage := daemonPrefix + testRepo + dockerPrefix + test.repo - kbuildImage := daemonPrefix + testRepo + kbuildPrefix + test.repo - - cmdOut, err := exec.Command("container-diff-linux-amd64", "diff", dockerImage, kbuildImage, "--type=file", "-j").Output() - - if err != nil { - t.Fatal(err) - } - - fmt.Println(string(cmdOut)) - - var f interface{} - err = json.Unmarshal(cmdOut, &f) - - if err != nil { - t.Fatal(err) - } - adds, dels, mods := parseDiffOutput(f) - checkEqual(t, test.added, adds) - checkEqual(t, test.deleted, dels) - checkEqual(t, test.modified, mods) - } -} - -func parseDiffOutput(f interface{}) ([]string, []string, []string) { - diff := (f.([]interface{})[0]).(map[string]interface{})["Diff"] - diffs := diff.(map[string]interface{}) - var adds = getFilenames(diffs, "Adds") - var dels = getFilenames(diffs, "Dels") - var mods = getFilenames(diffs, "Mods") - return adds, dels, mods -} - -func getFilenames(diffs map[string]interface{}, key string) []string { - array := diffs[key] - if array == nil { - return nil - } - arr := array.([]interface{}) - var filenames []string - for _, a := range arr { - filename := a.(map[string]interface{})["Name"] - filenames = append(filenames, filename.(string)) - } - return filenames -} - -func checkEqual(t *testing.T, actual, expected []string) { - sort.Strings(actual) - sort.Strings(expected) - if !reflect.DeepEqual(expected, actual) { - t.Errorf("%T differ.\nExpected\n%+v\nActual\n%+v", expected, expected, actual) - return - } -} diff --git a/integration_tests/integration_test_yaml.go b/integration_tests/integration_test_yaml.go index ff36a6424..8b896a44d 100644 --- a/integration_tests/integration_test_yaml.go +++ b/integration_tests/integration_test_yaml.go @@ -19,18 +19,19 @@ package main import ( "fmt" "gopkg.in/yaml.v2" - "path/filepath" ) var tests = []struct { description string dockerfilePath string + configPath string context string repo string }{ { description: "test extract filesystem", - dockerfilePath: "dockerfiles/Dockerfile", + dockerfilePath: "/workspace/integration_tests/dockerfiles/Dockerfile_test_extract_fs", + configPath: "/workspace/integration_tests/dockerfiles/config_test_extract_fs.json", context: "integration_tests/dockerfiles/", repo: "extract-filesystem", }, @@ -49,9 +50,12 @@ type testyaml struct { var executorImage = "executor-image" var executorCommand = "/workspace/executor" var dockerImage = "gcr.io/cloud-builders/docker" +var ubuntuImage = "ubuntu" var testRepo = "gcr.io/kbuild-test/" var dockerPrefix = "docker-" var kbuildPrefix = "kbuild-" +var daemonPrefix = "daemon://" +var containerDiffOutputFile = "container-diff.json" func main() { @@ -61,13 +65,13 @@ func main() { Args: []string{"cp", "gs://container-diff/latest/container-diff-linux-amd64", "."}, } containerDiffPermissions := step{ - Name: "ubuntu", + Name: ubuntuImage, Args: []string{"chmod", "+x", "container-diff-linux-amd64"}, } // Build executor image buildExecutorImage := step{ Name: dockerImage, - Args: []string{"build", "-t", executorImage, "."}, + Args: []string{"build", "-t", executorImage, "-f", "integration_tests/executor/Dockerfile", "."}, } y := testyaml{ @@ -75,32 +79,41 @@ func main() { } for _, test := range tests { // First, build the image with docker - var dockerfilePath = filepath.Join("/workspace/integration_tests", test.dockerfilePath) dockerBuild := step{ Name: dockerImage, - Args: []string{"build", "-t", testRepo + dockerPrefix + test.repo, "-f", dockerfilePath, test.context}, + Args: []string{"build", "-t", testRepo + dockerPrefix + test.repo, "-f", test.dockerfilePath, test.context}, } // Then, buld the image with kbuild and commit it var commitID = "test" kbuild := step{ Name: dockerImage, - Args: []string{"run", "-v", dockerfilePath + ":/workspace/Dockerfile", "--name", commitID, executorImage, executorCommand}, + Args: []string{"run", "-v", test.dockerfilePath + ":/workspace/Dockerfile", "--name", commitID, executorImage, executorCommand}, } commit := step{ Name: dockerImage, Args: []string{"commit", commitID, testRepo + kbuildPrefix + test.repo}, } - y.Steps = append(y.Steps, dockerBuild, kbuild, commit) - } - integrationTests := step{ - Name: "gcr.io/cloud-builders/go:debian", - Args: []string{"test", "integration_tests/integration_test.go"}, - Env: []string{"GOPATH=/", "PATH=/builder/bin:/go/bin:/usr/local/go/bin:/workspace"}, + dockerImage := daemonPrefix + testRepo + dockerPrefix + test.repo + kbuildImage := daemonPrefix + testRepo + kbuildPrefix + test.repo + // Run container diff on the images + args := "container-diff-linux-amd64 diff " + dockerImage + " " + kbuildImage + " --type=file -j > " + containerDiffOutputFile + containerDiff := step{ + Name: ubuntuImage, + Args: []string{"sh", "-c", args}, + Env: []string{"PATH=/workspace:/bin"}, + } + + // Compare output files + compareOutputs := step{ + Name: ubuntuImage, + Args: []string{"cmp", test.configPath, containerDiffOutputFile}, + } + + y.Steps = append(y.Steps, dockerBuild, kbuild, commit, containerDiff, compareOutputs) } - y.Steps = append(y.Steps, integrationTests) d, _ := yaml.Marshal(&y) fmt.Println(string(d))