Skip to content

Commit ffbc825

Browse files
committed
update pkg/test and pkg/update with testenv
Signed-off-by: Sunny <[email protected]>
1 parent 985d879 commit ffbc825

File tree

5 files changed

+200
-229
lines changed

5 files changed

+200
-229
lines changed

pkg/test/files.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ import (
2626

2727
// TODO rewrite this as just doing the diff, so I can test that it
2828
// fails at the right times too.
29-
func ExpectMatchingDirectories(actualRoot, expectedRoot string) {
30-
Expect(actualRoot).To(BeADirectory())
31-
Expect(expectedRoot).To(BeADirectory())
29+
func ExpectMatchingDirectories(g *WithT, actualRoot, expectedRoot string) {
30+
g.Expect(actualRoot).To(BeADirectory())
31+
g.Expect(expectedRoot).To(BeADirectory())
3232
actualonly, expectedonly, different := DiffDirectories(actualRoot, expectedRoot)
33-
Expect(actualonly).To(BeEmpty(), "Expect no files in %s but not in %s", actualRoot, expectedRoot)
34-
Expect(expectedonly).To(BeEmpty(), "Expect no files in %s but not in %s", expectedRoot, actualRoot)
33+
g.Expect(actualonly).To(BeEmpty(), "Expect no files in %s but not in %s", actualRoot, expectedRoot)
34+
g.Expect(expectedonly).To(BeEmpty(), "Expect no files in %s but not in %s", expectedRoot, actualRoot)
3535
// these are enumerated, so that the output is the actual difference
3636
for _, diff := range different {
37-
diff.FailedExpectation()
37+
diff.FailedExpectation(g)
3838
}
3939
}
4040

4141
type Diff interface {
4242
Path() string
43-
FailedExpectation()
43+
FailedExpectation(g *WithT)
4444
}
4545

4646
type contentdiff struct {
@@ -52,8 +52,8 @@ func (d contentdiff) Path() string {
5252
}
5353

5454
// Run an expectation that will fail, giving an appropriate error
55-
func (d contentdiff) FailedExpectation() {
56-
Expect(d.actual).To(Equal(d.expected))
55+
func (d contentdiff) FailedExpectation(g *WithT) {
56+
g.Expect(d.actual).To(Equal(d.expected))
5757
}
5858

5959
type dirfile struct {
@@ -65,11 +65,11 @@ func (d dirfile) Path() string {
6565
return d.path
6666
}
6767

68-
func (d dirfile) FailedExpectation() {
68+
func (d dirfile) FailedExpectation(g *WithT) {
6969
if d.expectedRegularFile {
70-
Expect(d.path).To(BeARegularFile())
70+
g.Expect(d.path).To(BeARegularFile())
7171
} else {
72-
Expect(d.path).To(BeADirectory())
72+
g.Expect(d.path).To(BeADirectory())
7373
}
7474
}
7575

pkg/test/files_test.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,51 @@ package test
1919
import (
2020
"testing"
2121

22-
. "github.com/onsi/ginkgo"
2322
. "github.com/onsi/gomega"
2423
)
2524

26-
func TestFiles(t *testing.T) {
27-
RegisterFailHandler(Fail)
28-
RunSpecs(t, "Files comparison helper")
25+
func TestExpectMatchingDirectories(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
actualRoot string
29+
expectedRoot string
30+
}{
31+
{
32+
name: "same directory",
33+
actualRoot: "testdata/base",
34+
expectedRoot: "testdata/base",
35+
},
36+
{
37+
name: "different equivalent directories",
38+
actualRoot: "testdata/base",
39+
expectedRoot: "testdata/equiv",
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
g := NewWithT(t)
46+
ExpectMatchingDirectories(g, tt.actualRoot, tt.expectedRoot)
47+
})
48+
}
2949
}
3050

31-
var _ = Describe("when no differences", func() {
32-
It("matches when given the same directory", func() {
33-
ExpectMatchingDirectories("testdata/base", "testdata/base")
34-
})
35-
It("matches when given equivalent directories", func() {
36-
ExpectMatchingDirectories("testdata/base", "testdata/equiv")
37-
})
38-
})
39-
40-
var _ = Describe("with differences", func() {
41-
It("finds files in expected from a/ but not in actual b/", func() {
42-
aonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b")
43-
Expect(aonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
44-
})
45-
46-
It("finds files in actual a/ that weren't expected from b/", func() {
47-
bonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b") // change in order
48-
Expect(bonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
49-
})
50-
51-
It("finds files that are different in a and b", func() {
52-
_, _, diffs := DiffDirectories("testdata/diff/a", "testdata/diff/b")
53-
var diffpaths []string
54-
for _, d := range diffs {
55-
diffpaths = append(diffpaths, d.Path())
56-
}
57-
Expect(diffpaths).To(Equal([]string{"/different/content.yaml", "/dirfile"}))
58-
})
59-
})
51+
func TestDiffDirectories(t *testing.T) {
52+
g := NewWithT(t)
53+
54+
// Finds files in expected from a/ but not in actual b/.
55+
aonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b")
56+
g.Expect(aonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
57+
58+
// Finds files in actual a/ that weren't expected from b/.
59+
bonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b") // change in order
60+
g.Expect(bonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
61+
62+
// Finds files that are different in a and b.
63+
_, _, diffs := DiffDirectories("testdata/diff/a", "testdata/diff/b")
64+
var diffpaths []string
65+
for _, d := range diffs {
66+
diffpaths = append(diffpaths, d.Path())
67+
}
68+
g.Expect(diffpaths).To(Equal([]string{"/different/content.yaml", "/dirfile"}))
69+
}

pkg/update/filereader_test.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,37 @@ limitations under the License.
1717
package update
1818

1919
import (
20+
"testing"
21+
2022
"github.com/go-logr/logr"
21-
. "github.com/onsi/ginkgo"
2223
. "github.com/onsi/gomega"
2324
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
2425
)
2526

26-
var _ = Describe("load YAMLs with ScreeningLocalReader", func() {
27-
It("loads only the YAMLs containing the token", func() {
28-
r := ScreeningLocalReader{
29-
Path: "testdata/setters/original",
30-
Token: "$imagepolicy",
31-
Trace: logr.Discard(),
32-
}
33-
nodes, err := r.Read()
34-
Expect(err).ToNot(HaveOccurred())
35-
// the test fixture has three files that contain the marker:
36-
// - otherns.yaml
37-
// - marked.yaml
38-
// - kustomization.yaml
39-
Expect(len(nodes)).To(Equal(3))
40-
filesSeen := map[string]struct{}{}
41-
for i := range nodes {
42-
path, _, err := kioutil.GetFileAnnotations(nodes[i])
43-
Expect(err).ToNot(HaveOccurred())
44-
filesSeen[path] = struct{}{}
45-
}
46-
Expect(filesSeen).To(Equal(map[string]struct{}{
47-
"marked.yaml": struct{}{},
48-
"kustomization.yaml": struct{}{},
49-
"otherns.yaml": struct{}{},
50-
}))
51-
})
52-
})
27+
func TestScreeningLocalReader(t *testing.T) {
28+
g := NewWithT(t)
29+
r := ScreeningLocalReader{
30+
Path: "testdata/setters/original",
31+
Token: "$imagepolicy",
32+
Trace: logr.Discard(),
33+
}
34+
nodes, err := r.Read()
35+
g.Expect(err).ToNot(HaveOccurred())
36+
// the test fixture has three files that contain the marker:
37+
// - otherns.yaml
38+
// - marked.yaml
39+
// - kustomization.yaml
40+
g.Expect(len(nodes)).To(Equal(3))
41+
filesSeen := map[string]struct{}{}
42+
for i := range nodes {
43+
path, _, err := kioutil.GetFileAnnotations(nodes[i])
44+
g.Expect(err).ToNot(HaveOccurred())
45+
filesSeen[path] = struct{}{}
46+
}
47+
g.Expect(filesSeen).To(Equal(map[string]struct{}{
48+
"marked.yaml": {},
49+
"kustomization.yaml": {},
50+
"otherns.yaml": {},
51+
}))
52+
53+
}

pkg/update/result_test.go

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package update
22

33
import (
4+
"testing"
5+
46
"github.com/google/go-containerregistry/pkg/name"
5-
. "github.com/onsi/ginkgo"
67
. "github.com/onsi/gomega"
78
"k8s.io/apimachinery/pkg/types"
89
"sigs.k8s.io/kustomize/kyaml/yaml"
@@ -18,79 +19,76 @@ func mustRef(ref string) imageRef {
1819
return imageRef{r, types.NamespacedName{}}
1920
}
2021

21-
var _ = Describe("image ref", func() {
22-
It("gives each component of an image ref", func() {
22+
func TestMustRef(t *testing.T) {
23+
g := NewWithT(t)
24+
25+
t.Run("gives each component of an image ref", func(t *testing.T) {
2326
ref := mustRef("helloworld:v1.0.1")
24-
Expect(ref.String()).To(Equal("helloworld:v1.0.1"))
25-
Expect(ref.Identifier()).To(Equal("v1.0.1"))
26-
Expect(ref.Repository()).To(Equal("library/helloworld"))
27-
Expect(ref.Registry()).To(Equal("index.docker.io"))
28-
Expect(ref.Name()).To(Equal("index.docker.io/library/helloworld:v1.0.1"))
27+
g.Expect(ref.String()).To(Equal("helloworld:v1.0.1"))
28+
g.Expect(ref.Identifier()).To(Equal("v1.0.1"))
29+
g.Expect(ref.Repository()).To(Equal("library/helloworld"))
30+
g.Expect(ref.Registry()).To(Equal("index.docker.io"))
31+
g.Expect(ref.Name()).To(Equal("index.docker.io/library/helloworld:v1.0.1"))
2932
})
3033

31-
It("deals with hostnames and digests", func() {
34+
t.Run("deals with hostnames and digests", func(t *testing.T) {
3235
image := "localhost:5000/org/helloworld@sha256:6745aaad46d795c9836632e1fb62f24b7e7f4c843144da8e47a5465c411a14be"
3336
ref := mustRef(image)
34-
Expect(ref.String()).To(Equal(image))
35-
Expect(ref.Identifier()).To(Equal("sha256:6745aaad46d795c9836632e1fb62f24b7e7f4c843144da8e47a5465c411a14be"))
36-
Expect(ref.Repository()).To(Equal("org/helloworld"))
37-
Expect(ref.Registry()).To(Equal("localhost:5000"))
38-
Expect(ref.Name()).To(Equal(image))
37+
g.Expect(ref.String()).To(Equal(image))
38+
g.Expect(ref.Identifier()).To(Equal("sha256:6745aaad46d795c9836632e1fb62f24b7e7f4c843144da8e47a5465c411a14be"))
39+
g.Expect(ref.Repository()).To(Equal("org/helloworld"))
40+
g.Expect(ref.Registry()).To(Equal("localhost:5000"))
41+
g.Expect(ref.Name()).To(Equal(image))
3942
})
40-
})
43+
}
4144

42-
var _ = Describe("update results", func() {
45+
func TestUpdateResults(t *testing.T) {
46+
g := NewWithT(t)
4347

4448
var result Result
4549
objectNames := []ObjectIdentifier{
46-
ObjectIdentifier{yaml.ResourceIdentifier{
50+
{yaml.ResourceIdentifier{
4751
NameMeta: yaml.NameMeta{Namespace: "ns", Name: "foo"},
4852
}},
49-
ObjectIdentifier{yaml.ResourceIdentifier{
53+
{yaml.ResourceIdentifier{
5054
NameMeta: yaml.NameMeta{Namespace: "ns", Name: "bar"},
5155
}},
5256
}
5357

54-
BeforeEach(func() {
55-
result = Result{
56-
Files: map[string]FileResult{
57-
"foo.yaml": {
58-
Objects: map[ObjectIdentifier][]ImageRef{
59-
objectNames[0]: {
60-
mustRef("image:v1.0"),
61-
mustRef("other:v2.0"),
62-
},
58+
result = Result{
59+
Files: map[string]FileResult{
60+
"foo.yaml": {
61+
Objects: map[ObjectIdentifier][]ImageRef{
62+
objectNames[0]: {
63+
mustRef("image:v1.0"),
64+
mustRef("other:v2.0"),
6365
},
6466
},
65-
"bar.yaml": {
66-
Objects: map[ObjectIdentifier][]ImageRef{
67-
objectNames[1]: {
68-
mustRef("image:v1.0"),
69-
mustRef("other:v2.0"),
70-
},
67+
},
68+
"bar.yaml": {
69+
Objects: map[ObjectIdentifier][]ImageRef{
70+
objectNames[1]: {
71+
mustRef("image:v1.0"),
72+
mustRef("other:v2.0"),
7173
},
7274
},
7375
},
74-
}
75-
})
76+
},
77+
}
7678

77-
It("deduplicates images", func() {
78-
Expect(result.Images()).To(Equal([]ImageRef{
79+
g.Expect(result.Images()).To(Equal([]ImageRef{
80+
mustRef("image:v1.0"),
81+
mustRef("other:v2.0"),
82+
}))
83+
84+
g.Expect(result.Objects()).To(Equal(map[ObjectIdentifier][]ImageRef{
85+
objectNames[0]: {
7986
mustRef("image:v1.0"),
8087
mustRef("other:v2.0"),
81-
}))
82-
})
83-
84-
It("collects images by object", func() {
85-
Expect(result.Objects()).To(Equal(map[ObjectIdentifier][]ImageRef{
86-
objectNames[0]: {
87-
mustRef("image:v1.0"),
88-
mustRef("other:v2.0"),
89-
},
90-
objectNames[1]: {
91-
mustRef("image:v1.0"),
92-
mustRef("other:v2.0"),
93-
},
94-
}))
95-
})
96-
})
88+
},
89+
objectNames[1]: {
90+
mustRef("image:v1.0"),
91+
mustRef("other:v2.0"),
92+
},
93+
}))
94+
}

0 commit comments

Comments
 (0)