Refactored to use config json isntead of go file

This commit is contained in:
Priya Wadhwa 2018-02-28 12:01:52 -08:00
parent 2dc8eb27d6
commit 4ebdfb63f2
No known key found for this signature in database
GPG Key ID: 0D0DAFD8F7AA73AE
5 changed files with 73 additions and 119 deletions

View File

@ -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
}
}
]

View File

@ -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
}
}

View File

@ -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))