Skip to content

Commit 7c5d7b8

Browse files
committed
Add update path test
Signed-off-by: Stefan Prodan <[email protected]>
1 parent 2e53745 commit 7c5d7b8

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

controllers/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ var _ = BeforeSuite(func(done Done) {
9090
Expect(imageAutoReconciler.SetupWithManager(k8sManager)).To(Succeed())
9191

9292
go func() {
93+
defer GinkgoRecover()
9394
err = k8sManager.Start(ctrl.SetupSignalHandler())
9495
Expect(err).ToNot(HaveOccurred())
9596
}()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: update-no
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: hello
10+
image: helloworld:1.0.0 # SETTER_SITE
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: update-yes
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: hello
10+
image: helloworld:1.0.0 # SETTER_SITE

controllers/update_test.go

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"math/rand"
2525
"net/url"
2626
"os"
27+
"path"
2728
"path/filepath"
2829
"strings"
2930
"time"
@@ -263,6 +264,130 @@ Images:
263264
})
264265
})
265266

267+
Context("update path", func() {
268+
269+
var localRepo *git.Repository
270+
const commitTemplate = `Commit summary
271+
272+
{{ range $resource, $_ := .Updated.Objects -}}
273+
- {{ $resource.Name }}
274+
{{ end -}}
275+
`
276+
277+
BeforeEach(func() {
278+
Expect(initGitRepo(gitServer, "testdata/pathconfig", branch, repositoryPath)).To(Succeed())
279+
repoURL := gitServer.HTTPAddressWithCredentials() + repositoryPath
280+
var err error
281+
localRepo, err = git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
282+
URL: repoURL,
283+
RemoteName: "origin",
284+
ReferenceName: plumbing.NewBranchReferenceName(branch),
285+
})
286+
Expect(err).ToNot(HaveOccurred())
287+
288+
gitRepoKey := types.NamespacedName{
289+
Name: "image-auto-" + randStringRunes(5),
290+
Namespace: namespace.Name,
291+
}
292+
gitRepo := &sourcev1.GitRepository{
293+
ObjectMeta: metav1.ObjectMeta{
294+
Name: gitRepoKey.Name,
295+
Namespace: namespace.Name,
296+
},
297+
Spec: sourcev1.GitRepositorySpec{
298+
URL: repoURL,
299+
Interval: metav1.Duration{Duration: time.Minute},
300+
},
301+
}
302+
Expect(k8sClient.Create(context.Background(), gitRepo)).To(Succeed())
303+
policyKey := types.NamespacedName{
304+
Name: "policy-" + randStringRunes(5),
305+
Namespace: namespace.Name,
306+
}
307+
// NB not testing the image reflector controller; this
308+
// will make a "fully formed" ImagePolicy object.
309+
policy := &imagev1_reflect.ImagePolicy{
310+
ObjectMeta: metav1.ObjectMeta{
311+
Name: policyKey.Name,
312+
Namespace: policyKey.Namespace,
313+
},
314+
Spec: imagev1_reflect.ImagePolicySpec{
315+
ImageRepositoryRef: meta.LocalObjectReference{
316+
Name: "not-expected-to-exist",
317+
},
318+
Policy: imagev1_reflect.ImagePolicyChoice{
319+
SemVer: &imagev1_reflect.SemVerPolicy{
320+
Range: "1.x",
321+
},
322+
},
323+
},
324+
Status: imagev1_reflect.ImagePolicyStatus{
325+
LatestImage: "helloworld:v1.0.0",
326+
},
327+
}
328+
Expect(k8sClient.Create(context.Background(), policy)).To(Succeed())
329+
Expect(k8sClient.Status().Update(context.Background(), policy)).To(Succeed())
330+
331+
// Insert a setter reference into the deployment file,
332+
// before creating the automation object itself.
333+
commitInRepo(repoURL, branch, "Install setter marker", func(tmp string) {
334+
replaceMarker(path.Join(tmp, "yes"), policyKey)
335+
})
336+
commitInRepo(repoURL, branch, "Install setter marker", func(tmp string) {
337+
replaceMarker(path.Join(tmp, "no"), policyKey)
338+
})
339+
340+
// pull the head commit we just pushed, so it's not
341+
// considered a new commit when checking for a commit
342+
// made by automation.
343+
waitForNewHead(localRepo, branch)
344+
345+
// now create the automation object, and let it (one
346+
// hopes!) make a commit itself.
347+
updateKey := types.NamespacedName{
348+
Namespace: namespace.Name,
349+
Name: "update-test",
350+
}
351+
updateBySetters := &imagev1.ImageUpdateAutomation{
352+
ObjectMeta: metav1.ObjectMeta{
353+
Name: updateKey.Name,
354+
Namespace: updateKey.Namespace,
355+
},
356+
Spec: imagev1.ImageUpdateAutomationSpec{
357+
Interval: metav1.Duration{Duration: 2 * time.Hour}, // this is to ensure any subsequent run should be outside the scope of the testing
358+
Checkout: imagev1.GitCheckoutSpec{
359+
GitRepositoryRef: meta.LocalObjectReference{
360+
Name: gitRepoKey.Name,
361+
},
362+
Branch: branch,
363+
},
364+
Update: &imagev1.UpdateStrategy{
365+
Strategy: imagev1.UpdateStrategySetters,
366+
Path: "./yes",
367+
},
368+
Commit: imagev1.CommitSpec{
369+
MessageTemplate: commitTemplate,
370+
},
371+
},
372+
}
373+
Expect(k8sClient.Create(context.Background(), updateBySetters)).To(Succeed())
374+
// wait for a new commit to be made by the controller
375+
waitForNewHead(localRepo, branch)
376+
})
377+
378+
AfterEach(func() {
379+
Expect(k8sClient.Delete(context.Background(), namespace)).To(Succeed())
380+
})
381+
382+
It("updates only the deployment in the specified path", func() {
383+
head, _ := localRepo.Head()
384+
commit, err := localRepo.CommitObject(head.Hash())
385+
Expect(err).ToNot(HaveOccurred())
386+
Expect(commit.Message).To(Not(ContainSubstring("update-no")))
387+
Expect(commit.Message).To(ContainSubstring("update-yes"))
388+
})
389+
})
390+
266391
endToEnd := func(impl, proto string) func() {
267392
return func() {
268393
var (
@@ -619,7 +744,6 @@ Images:
619744
Expect(fetchedAuto.Spec.Update).To(Equal(&imagev1.UpdateStrategy{Strategy: imagev1.UpdateStrategySetters}))
620745
})
621746
})
622-
623747
})
624748

625749
func expectCommittedAndPushed(conditions []metav1.Condition) {

0 commit comments

Comments
 (0)