diff --git a/pkg/test/files.go b/pkg/test/files.go index 152604e..a3d7d7c 100644 --- a/pkg/test/files.go +++ b/pkg/test/files.go @@ -26,21 +26,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 { @@ -52,8 +52,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 { @@ -65,11 +65,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()) } } diff --git a/pkg/test/files_test.go b/pkg/test/files_test.go index 1e270bb..46ce50b 100644 --- a/pkg/test/files_test.go +++ b/pkg/test/files_test.go @@ -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") - }) -}) +func TestDiffDirectories(t *testing.T) { + g := NewWithT(t) -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"})) - }) + // 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"})) - 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"})) - }) + // 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"})) - 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"})) - }) -}) + // 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"})) +} diff --git a/pkg/update/filereader_test.go b/pkg/update/filereader_test.go index e99b7c9..8df6c58 100644 --- a/pkg/update/filereader_test.go +++ b/pkg/update/filereader_test.go @@ -17,36 +17,37 @@ limitations under the License. package update import ( + "testing" + "github.com/go-logr/logr" - . "github.com/onsi/ginkgo" . "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", - Trace: logr.Discard(), - } - 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", + Trace: logr.Discard(), + } + 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": {}, + })) + +} diff --git a/pkg/update/result_test.go b/pkg/update/result_test.go index e96205d..0292470 100644 --- a/pkg/update/result_test.go +++ b/pkg/update/result_test.go @@ -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" @@ -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"), - }, - }, - }, - "bar.yaml": { - Objects: map[ObjectIdentifier][]ImageRef{ - objectNames[1]: { - 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"), + }, + }, + }, + }, + } - 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"), + }, + })) +} diff --git a/pkg/update/update_test.go b/pkg/update/update_test.go index 5be39f3..486d47c 100644 --- a/pkg/update/update_test.go +++ b/pkg/update/update_test.go @@ -17,12 +17,12 @@ limitations under the License. package update import ( + "io/ioutil" "os" "testing" "github.com/go-logr/logr" "github.com/google/go-containerregistry/pkg/name" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -32,116 +32,78 @@ import ( imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1beta1" ) -func TestUpdate(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Update suite") +func TestUpdateWithSetters(t *testing.T) { + g := NewWithT(t) + + policies := []imagev1_reflect.ImagePolicy{ + { + ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} + Namespace: "automation-ns", + Name: "policy", + }, + Status: imagev1_reflect.ImagePolicyStatus{ + LatestImage: "index.repo.fake/updated:v1.0.1", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} + Namespace: "automation-ns", + Name: "unchanged", + }, + Status: imagev1_reflect.ImagePolicyStatus{ + LatestImage: "image:v1.0.0", + }, + }, + } + + tmp, err := ioutil.TempDir("", "gotest") + g.Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(tmp) + + result, err := UpdateWithSetters(logr.Discard(), "testdata/setters/original", tmp, policies) + g.Expect(err).ToNot(HaveOccurred()) + test.ExpectMatchingDirectories(g, tmp, "testdata/setters/expected") + + kustomizeResourceID := ObjectIdentifier{yaml.ResourceIdentifier{ + TypeMeta: yaml.TypeMeta{ + APIVersion: "kustomize.config.k8s.io/v1beta1", + Kind: "Kustomization", + }, + }} + markedResourceID := ObjectIdentifier{yaml.ResourceIdentifier{ + TypeMeta: yaml.TypeMeta{ + APIVersion: "batch/v1beta1", + Kind: "CronJob", + }, + NameMeta: yaml.NameMeta{ + Namespace: "bar", + Name: "foo", + }, + }} + r, _ := name.ParseReference("index.repo.fake/updated:v1.0.1") + expectedImageRef := imageRef{r, types.NamespacedName{ + Name: "policy", + Namespace: "automation-ns", + }} + + expectedResult := Result{ + Files: map[string]FileResult{ + "kustomization.yaml": { + Objects: map[ObjectIdentifier][]ImageRef{ + kustomizeResourceID: { + expectedImageRef, + }, + }, + }, + "marked.yaml": { + Objects: map[ObjectIdentifier][]ImageRef{ + markedResourceID: { + expectedImageRef, + }, + }, + }, + }, + } + + g.Expect(result).To(Equal(expectedResult)) } - -var _ = Describe("Update image via kyaml setters2", func() { - - var ( - policies = []imagev1_reflect.ImagePolicy{ - { - ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} - Namespace: "automation-ns", - Name: "policy", - }, - Status: imagev1_reflect.ImagePolicyStatus{ - LatestImage: "index.repo.fake/updated:v1.0.1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} - Namespace: "automation-ns", - Name: "unchanged", - }, - Status: imagev1_reflect.ImagePolicyStatus{ - LatestImage: "image:v1.0.0", - }, - }, - } - ) - - It("updates the image marked with the image policy (setter) ref", func() { - tmp, err := os.MkdirTemp("", "gotest") - Expect(err).ToNot(HaveOccurred()) - defer os.RemoveAll(tmp) - - policies := []imagev1_reflect.ImagePolicy{ - { - ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} - Namespace: "automation-ns", - Name: "policy", - }, - Status: imagev1_reflect.ImagePolicyStatus{ - LatestImage: "index.repo.fake/updated:v1.0.1", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} - Namespace: "automation-ns", - Name: "unchanged", - }, - Status: imagev1_reflect.ImagePolicyStatus{ - LatestImage: "image:v1.0.0", - }, - }, - } - - _, err = UpdateWithSetters(logr.Discard(), "testdata/setters/original", tmp, policies) - Expect(err).ToNot(HaveOccurred()) - test.ExpectMatchingDirectories(tmp, "testdata/setters/expected") - }) - - It("gives the result of the updates", func() { - tmp, err := os.MkdirTemp("", "gotest") - Expect(err).ToNot(HaveOccurred()) - defer os.RemoveAll(tmp) - - result, err := UpdateWithSetters(logr.Discard(), "testdata/setters/original", tmp, policies) - Expect(err).ToNot(HaveOccurred()) - - kustomizeResourceID := ObjectIdentifier{yaml.ResourceIdentifier{ - TypeMeta: yaml.TypeMeta{ - APIVersion: "kustomize.config.k8s.io/v1beta1", - Kind: "Kustomization", - }, - }} - markedResourceID := ObjectIdentifier{yaml.ResourceIdentifier{ - TypeMeta: yaml.TypeMeta{ - APIVersion: "batch/v1beta1", - Kind: "CronJob", - }, - NameMeta: yaml.NameMeta{ - Namespace: "bar", - Name: "foo", - }, - }} - r, _ := name.ParseReference("index.repo.fake/updated:v1.0.1") - expectedImageRef := imageRef{r, types.NamespacedName{ - Name: "policy", - Namespace: "automation-ns", - }} - - expectedResult := Result{ - Files: map[string]FileResult{ - "kustomization.yaml": { - Objects: map[ObjectIdentifier][]ImageRef{ - kustomizeResourceID: { - expectedImageRef, - }, - }, - }, - "marked.yaml": { - Objects: map[ObjectIdentifier][]ImageRef{ - markedResourceID: { - expectedImageRef, - }, - }, - }, - }, - } - - Expect(result).To(Equal(expectedResult)) - }) -})