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

Commit 4640581

Browse files
committed
Use go integration tests
1 parent 8c7d517 commit 4640581

File tree

7 files changed

+171
-48
lines changed

7 files changed

+171
-48
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
66+
//TODO: Don't consume a json file
67+
expectedFile string
68+
}{
69+
{
70+
description: "file differ",
71+
imageA: diffBase,
72+
imageB: diffModified,
73+
differFlag: "-f",
74+
expectedFile: "file_diff_expected.json",
75+
},
76+
{
77+
description: "apt differ",
78+
imageA: aptBase,
79+
imageB: aptModified,
80+
differFlag: "-a",
81+
expectedFile: "apt_diff_expected.json",
82+
},
83+
{
84+
description: "node differ",
85+
imageA: nodeBase,
86+
imageB: nodeModified,
87+
differFlag: "-n",
88+
expectedFile: "node_diff_order_expected.json",
89+
},
90+
{
91+
description: "multi differ",
92+
imageA: multiBase,
93+
imageB: multiModified,
94+
differFlag: "-npa",
95+
expectedFile: "multi_diff_expected.json",
96+
},
97+
{
98+
description: "history differ",
99+
imageA: diffBase,
100+
imageB: diffModified,
101+
differFlag: "-d",
102+
expectedFile: "hist_diff_expected.json",
103+
},
104+
{
105+
description: "apt sorted differ",
106+
imageA: aptBase,
107+
imageB: aptModified,
108+
differFlag: "-ao",
109+
expectedFile: "apt_sorted_diff_expected.json",
110+
},
111+
{
112+
description: "apt analysis",
113+
imageA: aptModified,
114+
differFlag: "-a",
115+
expectedFile: "apt_analysis_expected.json",
116+
},
117+
{
118+
description: "file sorted analysis",
119+
imageA: diffModified,
120+
differFlag: "-fo",
121+
expectedFile: "file_sorted_analysis_expected.json",
122+
},
123+
{
124+
description: "pip analysis",
125+
imageA: pipModified,
126+
differFlag: "-p",
127+
expectedFile: "pip_analysis_expected.json",
128+
},
129+
{
130+
description: "node analysis",
131+
imageA: nodeModified,
132+
differFlag: "-n",
133+
expectedFile: "node_analysis_expected.json",
134+
},
135+
}
136+
for _, test := range tests {
137+
t.Run(test.description, func(t *testing.T) {
138+
args := []string{test.imageA}
139+
if test.imageB != "" {
140+
args = append(args, test.imageB)
141+
}
142+
args = append(args, test.differFlag)
143+
args = append(args, "-j")
144+
actual, err := runner.Run(args...)
145+
if err != nil {
146+
t.Fatalf("Error running command: %s", err)
147+
}
148+
e, err := ioutil.ReadFile(test.expectedFile)
149+
if err != nil {
150+
t.Fatalf("Error reading expected file output file: %s", err)
151+
}
152+
actual = strings.TrimSpace(actual)
153+
expected := strings.TrimSpace(string(e))
154+
if actual != expected {
155+
t.Errorf("Error actual output does not match expected. \n\nExpected: %s\n\n Actual: %s", expected, actual)
156+
}
157+
})
158+
}
159+
}

tests/test_run_comparisons.txt

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

0 commit comments

Comments
 (0)