Test the result of the automated commit

This factors out the function that checks directories for equivalence,
and uses it to check that the upstream repo has the expected update
when the controller has pushed its commit.
This commit is contained in:
Michael Bridgen 2020-07-23 15:30:47 +01:00
parent 97393366da
commit 7c6e87d06a
7 changed files with 130 additions and 48 deletions

View File

@ -0,0 +1,10 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
template:
spec:
containers:
- name: helll
image: helloworld:1.0.1

View File

@ -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")
})
})
})

67
pkg/test/files.go Normal file
View File

@ -0,0 +1,67 @@
/*
Copyright 2020 Michael Bridgen <mikeb@squaremobius.net>
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
})
}

38
pkg/test/files_test.go Normal file
View File

@ -0,0 +1,38 @@
/*
Copyright 2020 Michael Bridgen <mikeb@squaremobius.net>
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")
})
})

View File

@ -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)
})
})