Skip to content

Commit

Permalink
update pkg/test and pkg/update with testenv
Browse files Browse the repository at this point in the history
Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Nov 11, 2021
1 parent 3b131ba commit 3a03fad
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 228 deletions.
24 changes: 12 additions & 12 deletions pkg/test/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ import (

// TODO rewrite this as just doing the diff, so I can test that it
// fails at the right times too.
func ExpectMatchingDirectories(actualRoot, expectedRoot string) {
Expect(actualRoot).To(BeADirectory())
Expect(expectedRoot).To(BeADirectory())
func ExpectMatchingDirectories(g *WithT, actualRoot, expectedRoot string) {
g.Expect(actualRoot).To(BeADirectory())
g.Expect(expectedRoot).To(BeADirectory())
actualonly, expectedonly, different := DiffDirectories(actualRoot, expectedRoot)
Expect(actualonly).To(BeEmpty(), "Expect no files in %s but not in %s", actualRoot, expectedRoot)
Expect(expectedonly).To(BeEmpty(), "Expect no files in %s but not in %s", expectedRoot, actualRoot)
g.Expect(actualonly).To(BeEmpty(), "Expect no files in %s but not in %s", actualRoot, expectedRoot)
g.Expect(expectedonly).To(BeEmpty(), "Expect no files in %s but not in %s", expectedRoot, actualRoot)
// these are enumerated, so that the output is the actual difference
for _, diff := range different {
diff.FailedExpectation()
diff.FailedExpectation(g)
}
}

type Diff interface {
Path() string
FailedExpectation()
FailedExpectation(g *WithT)
}

type contentdiff struct {
Expand All @@ -53,8 +53,8 @@ func (d contentdiff) Path() string {
}

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

type dirfile struct {
Expand All @@ -66,11 +66,11 @@ func (d dirfile) Path() string {
return d.path
}

func (d dirfile) FailedExpectation() {
func (d dirfile) FailedExpectation(g *WithT) {
if d.expectedRegularFile {
Expect(d.path).To(BeARegularFile())
g.Expect(d.path).To(BeARegularFile())
} else {
Expect(d.path).To(BeADirectory())
g.Expect(d.path).To(BeADirectory())
}
}

Expand Down
76 changes: 43 additions & 33 deletions pkg/test/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,51 @@ package test
import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestFiles(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Files comparison helper")
func TestExpectMatchingDirectories(t *testing.T) {
tests := []struct {
name string
actualRoot string
expectedRoot string
}{
{
name: "same directory",
actualRoot: "testdata/base",
expectedRoot: "testdata/base",
},
{
name: "different equivalent directories",
actualRoot: "testdata/base",
expectedRoot: "testdata/equiv",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
ExpectMatchingDirectories(g, tt.actualRoot, tt.expectedRoot)
})
}
}

var _ = Describe("when no differences", func() {
It("matches when given the same directory", func() {
ExpectMatchingDirectories("testdata/base", "testdata/base")
})
It("matches when given equivalent directories", func() {
ExpectMatchingDirectories("testdata/base", "testdata/equiv")
})
})

var _ = Describe("with differences", func() {
It("finds files in expected from a/ but not in actual b/", func() {
aonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b")
Expect(aonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
})

It("finds files in actual a/ that weren't expected from b/", func() {
bonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b") // change in order
Expect(bonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))
})

It("finds files that are different in a and b", func() {
_, _, diffs := DiffDirectories("testdata/diff/a", "testdata/diff/b")
var diffpaths []string
for _, d := range diffs {
diffpaths = append(diffpaths, d.Path())
}
Expect(diffpaths).To(Equal([]string{"/different/content.yaml", "/dirfile"}))
})
})
func TestDiffDirectories(t *testing.T) {
g := NewWithT(t)

// Finds files in expected from a/ but not in actual b/.
aonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b")
g.Expect(aonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))

// Finds files in actual a/ that weren't expected from b/.
bonly, _, _ := DiffDirectories("testdata/diff/a", "testdata/diff/b") // change in order
g.Expect(bonly).To(Equal([]string{"/only", "/only/here.yaml", "/onlyhere.yaml"}))

// Finds files that are different in a and b.
_, _, diffs := DiffDirectories("testdata/diff/a", "testdata/diff/b")
var diffpaths []string
for _, d := range diffs {
diffpaths = append(diffpaths, d.Path())
}
g.Expect(diffpaths).To(Equal([]string{"/different/content.yaml", "/dirfile"}))
}
55 changes: 28 additions & 27 deletions pkg/update/filereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,35 @@ limitations under the License.
package update

import (
. "github.com/onsi/ginkgo"
"testing"

. "github.com/onsi/gomega"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
)

var _ = Describe("load YAMLs with ScreeningLocalReader", func() {
It("loads only the YAMLs containing the token", func() {
r := ScreeningLocalReader{
Path: "testdata/setters/original",
Token: "$imagepolicy",
}
nodes, err := r.Read()
Expect(err).ToNot(HaveOccurred())
// the test fixture has three files that contain the marker:
// - otherns.yaml
// - marked.yaml
// - kustomization.yaml
Expect(len(nodes)).To(Equal(3))
filesSeen := map[string]struct{}{}
for i := range nodes {
path, _, err := kioutil.GetFileAnnotations(nodes[i])
Expect(err).ToNot(HaveOccurred())
filesSeen[path] = struct{}{}
}
Expect(filesSeen).To(Equal(map[string]struct{}{
"marked.yaml": struct{}{},
"kustomization.yaml": struct{}{},
"otherns.yaml": struct{}{},
}))
})
})
func TestScreeningLocalReader(t *testing.T) {
g := NewWithT(t)
r := ScreeningLocalReader{
Path: "testdata/setters/original",
Token: "$imagepolicy",
}
nodes, err := r.Read()
g.Expect(err).ToNot(HaveOccurred())
// the test fixture has three files that contain the marker:
// - otherns.yaml
// - marked.yaml
// - kustomization.yaml
g.Expect(len(nodes)).To(Equal(3))
filesSeen := map[string]struct{}{}
for i := range nodes {
path, _, err := kioutil.GetFileAnnotations(nodes[i])
g.Expect(err).ToNot(HaveOccurred())
filesSeen[path] = struct{}{}
}
g.Expect(filesSeen).To(Equal(map[string]struct{}{
"marked.yaml": {},
"kustomization.yaml": {},
"otherns.yaml": {},
}))

}
104 changes: 51 additions & 53 deletions pkg/update/result_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package update

import (
"testing"

"github.com/google/go-containerregistry/pkg/name"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
Expand All @@ -18,79 +19,76 @@ func mustRef(ref string) imageRef {
return imageRef{r, types.NamespacedName{}}
}

var _ = Describe("image ref", func() {
It("gives each component of an image ref", func() {
func TestMustRef(t *testing.T) {
g := NewWithT(t)

t.Run("gives each component of an image ref", func(t *testing.T) {
ref := mustRef("helloworld:v1.0.1")
Expect(ref.String()).To(Equal("helloworld:v1.0.1"))
Expect(ref.Identifier()).To(Equal("v1.0.1"))
Expect(ref.Repository()).To(Equal("library/helloworld"))
Expect(ref.Registry()).To(Equal("index.docker.io"))
Expect(ref.Name()).To(Equal("index.docker.io/library/helloworld:v1.0.1"))
g.Expect(ref.String()).To(Equal("helloworld:v1.0.1"))
g.Expect(ref.Identifier()).To(Equal("v1.0.1"))
g.Expect(ref.Repository()).To(Equal("library/helloworld"))
g.Expect(ref.Registry()).To(Equal("index.docker.io"))
g.Expect(ref.Name()).To(Equal("index.docker.io/library/helloworld:v1.0.1"))
})

It("deals with hostnames and digests", func() {
t.Run("deals with hostnames and digests", func(t *testing.T) {
image := "localhost:5000/org/helloworld@sha256:6745aaad46d795c9836632e1fb62f24b7e7f4c843144da8e47a5465c411a14be"
ref := mustRef(image)
Expect(ref.String()).To(Equal(image))
Expect(ref.Identifier()).To(Equal("sha256:6745aaad46d795c9836632e1fb62f24b7e7f4c843144da8e47a5465c411a14be"))
Expect(ref.Repository()).To(Equal("org/helloworld"))
Expect(ref.Registry()).To(Equal("localhost:5000"))
Expect(ref.Name()).To(Equal(image))
g.Expect(ref.String()).To(Equal(image))
g.Expect(ref.Identifier()).To(Equal("sha256:6745aaad46d795c9836632e1fb62f24b7e7f4c843144da8e47a5465c411a14be"))
g.Expect(ref.Repository()).To(Equal("org/helloworld"))
g.Expect(ref.Registry()).To(Equal("localhost:5000"))
g.Expect(ref.Name()).To(Equal(image))
})
})
}

var _ = Describe("update results", func() {
func TestUpdateResults(t *testing.T) {
g := NewWithT(t)

var result Result
objectNames := []ObjectIdentifier{
ObjectIdentifier{yaml.ResourceIdentifier{
{yaml.ResourceIdentifier{
NameMeta: yaml.NameMeta{Namespace: "ns", Name: "foo"},
}},
ObjectIdentifier{yaml.ResourceIdentifier{
{yaml.ResourceIdentifier{
NameMeta: yaml.NameMeta{Namespace: "ns", Name: "bar"},
}},
}

BeforeEach(func() {
result = Result{
Files: map[string]FileResult{
"foo.yaml": {
Objects: map[ObjectIdentifier][]ImageRef{
objectNames[0]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
},
result = Result{
Files: map[string]FileResult{
"foo.yaml": {
Objects: map[ObjectIdentifier][]ImageRef{
objectNames[0]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
},
},
"bar.yaml": {
Objects: map[ObjectIdentifier][]ImageRef{
objectNames[1]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
},
},
"bar.yaml": {
Objects: map[ObjectIdentifier][]ImageRef{
objectNames[1]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
},
},
},
}
})
},
}

It("deduplicates images", func() {
Expect(result.Images()).To(Equal([]ImageRef{
g.Expect(result.Images()).To(Equal([]ImageRef{
mustRef("image:v1.0"),
mustRef("other:v2.0"),
}))

g.Expect(result.Objects()).To(Equal(map[ObjectIdentifier][]ImageRef{
objectNames[0]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
}))
})

It("collects images by object", func() {
Expect(result.Objects()).To(Equal(map[ObjectIdentifier][]ImageRef{
objectNames[0]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
},
objectNames[1]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
},
}))
})
})
},
objectNames[1]: {
mustRef("image:v1.0"),
mustRef("other:v2.0"),
},
}))
}
Loading

0 comments on commit 3a03fad

Please sign in to comment.