diff --git a/controllers/testdata/appconfig-expected/deploy.yaml b/controllers/testdata/appconfig-expected/deploy.yaml new file mode 100644 index 00000000..121ddb1a --- /dev/null +++ b/controllers/testdata/appconfig-expected/deploy.yaml @@ -0,0 +1,10 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test +spec: + template: + spec: + containers: + - name: helll + image: helloworld:1.0.1 diff --git a/controllers/update_test.go b/controllers/update_test.go index cd39e94e..b8df91cd 100644 --- a/controllers/update_test.go +++ b/controllers/update_test.go @@ -39,6 +39,7 @@ import ( sourcev1alpha1 "github.com/fluxcd/source-controller/api/v1alpha1" imagev1alpha1 "github.com/squaremo/image-automation-controller/api/v1alpha1" + "github.com/squaremo/image-automation-controller/pkg/test" imagev1alpha1_reflect "github.com/squaremo/image-reflector-controller/api/v1alpha1" ) @@ -197,6 +198,16 @@ var _ = Describe("ImageUpdateAutomation", func() { commit, err := localRepo.CommitObject(head.Hash()) Expect(err).ToNot(HaveOccurred()) Expect(commit.Message).To(Equal(commitMessage)) + + tmp, err := ioutil.TempDir("", "gotest-imageauto") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(tmp) + + _, err = git.PlainClone(tmp, false, &git.CloneOptions{ + URL: repoURL, + }) + Expect(err).ToNot(HaveOccurred()) + test.ExpectMatchingDirectories(tmp, "testdata/appconfig-expected") }) }) }) diff --git a/pkg/test/files.go b/pkg/test/files.go new file mode 100644 index 00000000..87fae5ba --- /dev/null +++ b/pkg/test/files.go @@ -0,0 +1,67 @@ +/* +Copyright 2020 Michael Bridgen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package test + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + + . "github.com/onsi/gomega" +) + +// 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()) + filepath.Walk(expectedRoot, func(path string, info os.FileInfo, err error) error { + if err != nil { + return nil + } + // ignore emacs backups + if strings.HasSuffix(path, "~") { + return nil + } + relPath := path[len(expectedRoot):] + actualPath := filepath.Join(actualRoot, relPath) + if info.IsDir() { + if strings.HasPrefix(filepath.Base(path), ".") { + return filepath.SkipDir + } + Expect(actualPath).To(BeADirectory()) + return nil + } + Expect(actualPath).To(BeARegularFile()) + actualBytes, err := ioutil.ReadFile(actualPath) + expectedBytes, err := ioutil.ReadFile(path) + Expect(string(actualBytes)).To(Equal(string(expectedBytes))) + return nil + }) + filepath.Walk(actualRoot, func(path string, info os.FileInfo, err error) error { + p := path[len(actualRoot):] + // ignore emacs backups + if strings.HasSuffix(p, "~") { + return nil + } + if info.IsDir() && strings.HasPrefix(filepath.Base(p), ".") { + return filepath.SkipDir + } + Expect(filepath.Join(expectedRoot, p)).To(BeAnExistingFile()) + return nil + }) +} diff --git a/pkg/test/files_test.go b/pkg/test/files_test.go new file mode 100644 index 00000000..a52df93f --- /dev/null +++ b/pkg/test/files_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2020 Michael Bridgen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestFiles(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Files comparison helper") +} + +var _ = Describe("Test helper", 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") + }) +}) diff --git a/pkg/update/testdata/base/foo.yaml b/pkg/test/testdata/base/foo.yaml similarity index 100% rename from pkg/update/testdata/base/foo.yaml rename to pkg/test/testdata/base/foo.yaml diff --git a/pkg/update/testdata/equiv/foo.yaml b/pkg/test/testdata/equiv/foo.yaml similarity index 100% rename from pkg/update/testdata/equiv/foo.yaml rename to pkg/test/testdata/equiv/foo.yaml diff --git a/pkg/update/update_test.go b/pkg/update/update_test.go index 6d874232..1a1b096e 100644 --- a/pkg/update/update_test.go +++ b/pkg/update/update_test.go @@ -3,70 +3,26 @@ package update import ( "io/ioutil" "os" - "path/filepath" - "strings" "testing" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" -) -// 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()) - filepath.Walk(expectedRoot, func(path string, info os.FileInfo, err error) error { - if err != nil { - return nil - } - // ignore emacs backups - if strings.HasSuffix(path, "~") { - return nil - } - relPath := path[len(expectedRoot):] - actualPath := filepath.Join(actualRoot, relPath) - if info.IsDir() { - Expect(actualPath).To(BeADirectory()) - return nil - } - Expect(actualPath).To(BeARegularFile()) - actualBytes, err := ioutil.ReadFile(actualPath) - expectedBytes, err := ioutil.ReadFile(path) - Expect(string(actualBytes)).To(Equal(string(expectedBytes))) - return nil - }) - filepath.Walk(actualRoot, func(path string, info os.FileInfo, err error) error { - p := path[len(actualRoot):] - // ignore emacs backups - if strings.HasSuffix(p, "~") { - return nil - } - Expect(filepath.Join(expectedRoot, p)).To(BeAnExistingFile()) - return nil - }) -} + "github.com/squaremo/image-automation-controller/pkg/test" +) func TestUpdate(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "Update suite") } -var _ = Describe("Test helper", 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("Update image everywhere", func() { It("leaves a different image alone", func() { tmp, err := ioutil.TempDir("", "gotest") Expect(err).ToNot(HaveOccurred()) defer os.RemoveAll(tmp) Expect(UpdateImageEverywhere("testdata/leave/original", tmp, "notused", "notused:v1.0.1")).To(Succeed()) - expectMatchingDirectories("testdata/leave/expected", tmp) + test.ExpectMatchingDirectories("testdata/leave/expected", tmp) }) It("replaces the given image", func() { @@ -74,6 +30,6 @@ var _ = Describe("Update image everywhere", func() { Expect(err).ToNot(HaveOccurred()) defer os.RemoveAll(tmp) Expect(UpdateImageEverywhere("testdata/replace/original", tmp, "used", "used:v1.1.0")).To(Succeed()) - expectMatchingDirectories("testdata/replace/expected", tmp) + test.ExpectMatchingDirectories("testdata/replace/expected", tmp) }) })