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 Sep 28, 2021
1 parent e3be50e commit f79a72a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 148 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"}))
}
167 changes: 64 additions & 103 deletions pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
}

0 comments on commit f79a72a

Please sign in to comment.