Skip to content

Commit

Permalink
Test the result of the automated commit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
squaremo committed Jul 23, 2020
1 parent 84fe3fb commit 884ed25
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 48 deletions.
10 changes: 10 additions & 0 deletions controllers/testdata/appconfig-expected/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
template:
spec:
containers:
- name: helll
image: helloworld:1.0.1
11 changes: 11 additions & 0 deletions controllers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
})
})
})
Expand Down
67 changes: 67 additions & 0 deletions pkg/test/files.go
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions pkg/test/files_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
})
File renamed without changes.
File renamed without changes.
52 changes: 4 additions & 48 deletions pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,33 @@ 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() {
tmp, err := ioutil.TempDir("", "gotest")
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)
})
})

0 comments on commit 884ed25

Please sign in to comment.