diff --git a/pkg/test/files.go b/pkg/test/files.go index e6117588..e58e11d9 100644 --- a/pkg/test/files.go +++ b/pkg/test/files.go @@ -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 { @@ -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 { @@ -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()) } } diff --git a/pkg/test/files_test.go b/pkg/test/files_test.go index 1e270bb4..46ce50b2 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") - }) -}) - -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"})) +} diff --git a/pkg/update/update_test.go b/pkg/update/update_test.go index 03efe317..486d47c3 100644 --- a/pkg/update/update_test.go +++ b/pkg/update/update_test.go @@ -23,7 +23,6 @@ import ( "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" @@ -33,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") -} - -var _ = Describe("Update image via kyaml setters2", func() { +func TestUpdateWithSetters(t *testing.T) { + g := NewWithT(t) - 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 := ioutil.TempDir("", "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", - }, + policies := []imagev1_reflect.ImagePolicy{ + { + ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} + Namespace: "automation-ns", + Name: "policy", }, - { - 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", - }, + Status: imagev1_reflect.ImagePolicyStatus{ + LatestImage: "index.repo.fake/updated:v1.0.1", }, - } - - _, 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 := ioutil.TempDir("", "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", + }, + { + ObjectMeta: metav1.ObjectMeta{ // name matches marker used in testdata/setters/{original,expected} + Namespace: "automation-ns", + Name: "unchanged", }, - NameMeta: yaml.NameMeta{ - Namespace: "bar", - Name: "foo", + Status: imagev1_reflect.ImagePolicyStatus{ + LatestImage: "image:v1.0.0", }, - }} - 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, - }, + }, + } + + 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, - }, + }, + "marked.yaml": { + Objects: map[ObjectIdentifier][]ImageRef{ + markedResourceID: { + expectedImageRef, }, }, }, - } + }, + } - Expect(result).To(Equal(expectedResult)) - }) -}) + g.Expect(result).To(Equal(expectedResult)) +}