Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit cd40b32

Browse files
authored
Merge pull request #57 from r2d4/integration-tests
Use go integration tests
2 parents 828be57 + 784cf7c commit cd40b32

File tree

7 files changed

+183
-49
lines changed

7 files changed

+183
-49
lines changed

.container-diff-tests.sh

Lines changed: 0 additions & 30 deletions
This file was deleted.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
tests/*_actual.json
21
out/*

.travis.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
sudo: required
2-
dist: trusty
3-
41
language: go
2+
os: linux
53
go:
6-
- 1.8.1
4+
- 1.8.3
75
go_import_path: github.com/GoogleCloudPlatform/container-diff
86

97
script:
10-
- ./.gofmt.sh
11-
- travis_wait ./.container-diff-tests.sh
8+
- make test integration

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ORG := github.com/GoogleCloudPlatform
99
PROJECT := container-diff
1010
REPOPATH ?= $(ORG)/$(PROJECT)
1111

12-
SUPPORTED_PLATFORMS := linux-amd64 darwin-amd64
12+
SUPPORTED_PLATFORMS := linux-amd64 darwin-amd64 windows-amd64
1313
BUILD_PACKAGE = $(REPOPATH)
1414

1515
# These build tags are from the containers/image library.
@@ -33,7 +33,11 @@ cross: $(foreach platform, $(SUPPORTED_PLATFORMS), out/$(PROJECT)-$(platform))
3333

3434
.PHONY: test
3535
test: $(BUILD_DIR)/$(PROJECT)
36-
./.container-diff-tests.sh
36+
@ ./test.sh
37+
38+
.PHONY: integration
39+
integration: $(BUILD_DIR)/$(PROJECT)
40+
go test -v -tags integration $(REPOPATH)/tests
3741

3842
.PHONY: clean
3943
clean:

.gofmt.sh renamed to test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
set -e
1818

19+
echo "Running go tests..."
20+
go test `go list ./... | grep -v vendor`
21+
22+
echo "Checking gofmt..."
1923
files=$(find . -name "*.go" | grep -v vendor/ | xargs gofmt -l -s)
2024
if [[ $files ]]; then
2125
echo "Gofmt errors in files: $files"

tests/integration_test.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// +build integration
2+
3+
package tests
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"io/ioutil"
9+
"os/exec"
10+
"path/filepath"
11+
"strings"
12+
"testing"
13+
)
14+
15+
const (
16+
diffBase = "gcr.io/gcp-runtimes/diff-base"
17+
diffModified = "gcr.io/gcp-runtimes/diff-modified"
18+
19+
aptBase = "gcr.io/gcp-runtimes/apt-base"
20+
aptModified = "gcr.io/gcp-runtimes/apt-modified"
21+
22+
// Why is this node-modified:2.0?
23+
nodeBase = "gcr.io/gcp-runtimes/node-modified:2.0"
24+
nodeModified = "gcr.io/gcp-runtimes/node-modified"
25+
26+
pipModified = "gcr.io/gcp-runtimes/pip-modified"
27+
28+
multiBase = "gcr.io/gcp-runtimes/multi-base"
29+
multiModified = "gcr.io/gcp-runtimes/multi-modified"
30+
)
31+
32+
type ContainerDiffRunner struct {
33+
t *testing.T
34+
binaryPath string
35+
}
36+
37+
func (c *ContainerDiffRunner) Run(command ...string) (string, error) {
38+
path, err := filepath.Abs(c.binaryPath)
39+
if err != nil {
40+
c.t.Fatalf("Error finding container-diff binary: %s", err)
41+
}
42+
c.t.Logf("Running command: %s %s", path, command)
43+
cmd := exec.Command(path, command...)
44+
45+
var stdout, stderr bytes.Buffer
46+
cmd.Stdout = &stdout
47+
cmd.Stderr = &stderr
48+
if err := cmd.Run(); err != nil {
49+
return "", fmt.Errorf("Error running command %s: %s Stderr: %s", command, err, stderr.String())
50+
}
51+
return stdout.String(), nil
52+
}
53+
54+
func TestDiffAndAnalysis(t *testing.T) {
55+
runner := ContainerDiffRunner{
56+
t: t,
57+
binaryPath: "../out/container-diff",
58+
}
59+
60+
var tests = []struct {
61+
description string
62+
imageA string
63+
imageB string
64+
differFlag string
65+
subcommand string
66+
67+
//TODO: Don't consume a json file
68+
expectedFile string
69+
}{
70+
{
71+
description: "file differ",
72+
subcommand: "diff",
73+
imageA: diffBase,
74+
imageB: diffModified,
75+
differFlag: "-f",
76+
expectedFile: "file_diff_expected.json",
77+
},
78+
{
79+
description: "apt differ",
80+
subcommand: "diff",
81+
imageA: aptBase,
82+
imageB: aptModified,
83+
differFlag: "-a",
84+
expectedFile: "apt_diff_expected.json",
85+
},
86+
{
87+
description: "node differ",
88+
subcommand: "diff",
89+
imageA: nodeBase,
90+
imageB: nodeModified,
91+
differFlag: "-n",
92+
expectedFile: "node_diff_order_expected.json",
93+
},
94+
{
95+
description: "multi differ",
96+
subcommand: "diff",
97+
imageA: multiBase,
98+
imageB: multiModified,
99+
differFlag: "-npa",
100+
expectedFile: "multi_diff_expected.json",
101+
},
102+
{
103+
description: "history differ",
104+
subcommand: "diff",
105+
imageA: diffBase,
106+
imageB: diffModified,
107+
differFlag: "-d",
108+
expectedFile: "hist_diff_expected.json",
109+
},
110+
{
111+
description: "apt sorted differ",
112+
subcommand: "diff",
113+
imageA: aptBase,
114+
imageB: aptModified,
115+
differFlag: "-ao",
116+
expectedFile: "apt_sorted_diff_expected.json",
117+
},
118+
{
119+
description: "apt analysis",
120+
subcommand: "analyze",
121+
imageA: aptModified,
122+
differFlag: "-a",
123+
expectedFile: "apt_analysis_expected.json",
124+
},
125+
{
126+
description: "file sorted analysis",
127+
subcommand: "analyze",
128+
imageA: diffModified,
129+
differFlag: "-fo",
130+
expectedFile: "file_sorted_analysis_expected.json",
131+
},
132+
{
133+
description: "pip analysis",
134+
subcommand: "analyze",
135+
imageA: pipModified,
136+
differFlag: "-p",
137+
expectedFile: "pip_analysis_expected.json",
138+
},
139+
{
140+
description: "node analysis",
141+
subcommand: "analyze",
142+
imageA: nodeModified,
143+
differFlag: "-n",
144+
expectedFile: "node_analysis_expected.json",
145+
},
146+
}
147+
for _, test := range tests {
148+
t.Run(test.description, func(t *testing.T) {
149+
args := []string{test.subcommand, test.imageA}
150+
if test.imageB != "" {
151+
args = append(args, test.imageB)
152+
}
153+
args = append(args, test.differFlag)
154+
args = append(args, "-j")
155+
actual, err := runner.Run(args...)
156+
if err != nil {
157+
t.Fatalf("Error running command: %s", err)
158+
}
159+
e, err := ioutil.ReadFile(test.expectedFile)
160+
if err != nil {
161+
t.Fatalf("Error reading expected file output file: %s", err)
162+
}
163+
actual = strings.TrimSpace(actual)
164+
expected := strings.TrimSpace(string(e))
165+
if actual != expected {
166+
t.Errorf("Error actual output does not match expected. \n\nExpected: %s\n\n Actual: %s", expected, actual)
167+
}
168+
})
169+
}
170+
}

tests/test_run_comparisons.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)