@@ -111,6 +111,157 @@ var _ = Describe("ImageUpdateAutomation", func() {
111111 Expect (initGitRepo (gitServer , "testdata/appconfig" , branch , repositoryPath )).To (Succeed ())
112112 })
113113
114+ Context ("commit message template" , func () {
115+
116+ var (
117+ localRepo * git.Repository
118+ commitMessage string
119+ )
120+
121+ const (
122+ commitTemplate = `Commit summary
123+
124+ Automation: {{ .AutomationObject }}
125+
126+ Files:
127+ {{ range $filename, $_ := .Updated.Files -}}
128+ - {{ $filename }}
129+ {{ end -}}
130+
131+ Objects:
132+ {{ range $resource, $_ := .Updated.Objects -}}
133+ - {{ $resource.Kind }} {{ $resource.Name }}
134+ {{ end -}}
135+
136+ Images:
137+ {{ range .Updated.Images -}}
138+ - {{.}}
139+ {{ end -}}
140+ `
141+ commitMessageFmt = `Commit summary
142+
143+ Automation: %s/update-test
144+
145+ Files:
146+ - deploy.yaml
147+ Objects:
148+ - Deployment test
149+ Images:
150+ - helloworld:v1.0.0
151+ `
152+ )
153+
154+ BeforeEach (func () {
155+ commitMessage = fmt .Sprintf (commitMessageFmt , namespace .Name )
156+
157+ Expect (initGitRepo (gitServer , "testdata/appconfig" , branch , repositoryPath )).To (Succeed ())
158+ repoURL := gitServer .HTTPAddressWithCredentials () + repositoryPath
159+ var err error
160+ localRepo , err = git .Clone (memory .NewStorage (), memfs .New (), & git.CloneOptions {
161+ URL : repoURL ,
162+ RemoteName : "origin" ,
163+ ReferenceName : plumbing .NewBranchReferenceName (branch ),
164+ })
165+ Expect (err ).ToNot (HaveOccurred ())
166+
167+ gitRepoKey := types.NamespacedName {
168+ Name : "image-auto-" + randStringRunes (5 ),
169+ Namespace : namespace .Name ,
170+ }
171+ gitRepo := & sourcev1.GitRepository {
172+ ObjectMeta : metav1.ObjectMeta {
173+ Name : gitRepoKey .Name ,
174+ Namespace : namespace .Name ,
175+ },
176+ Spec : sourcev1.GitRepositorySpec {
177+ URL : repoURL ,
178+ Interval : metav1.Duration {Duration : time .Minute },
179+ },
180+ }
181+ Expect (k8sClient .Create (context .Background (), gitRepo )).To (Succeed ())
182+ policyKey := types.NamespacedName {
183+ Name : "policy-" + randStringRunes (5 ),
184+ Namespace : namespace .Name ,
185+ }
186+ // NB not testing the image reflector controller; this
187+ // will make a "fully formed" ImagePolicy object.
188+ policy := & imagev1_reflect.ImagePolicy {
189+ ObjectMeta : metav1.ObjectMeta {
190+ Name : policyKey .Name ,
191+ Namespace : policyKey .Namespace ,
192+ },
193+ Spec : imagev1_reflect.ImagePolicySpec {
194+ ImageRepositoryRef : meta.LocalObjectReference {
195+ Name : "not-expected-to-exist" ,
196+ },
197+ Policy : imagev1_reflect.ImagePolicyChoice {
198+ SemVer : & imagev1_reflect.SemVerPolicy {
199+ Range : "1.x" ,
200+ },
201+ },
202+ },
203+ Status : imagev1_reflect.ImagePolicyStatus {
204+ LatestImage : "helloworld:v1.0.0" ,
205+ },
206+ }
207+ Expect (k8sClient .Create (context .Background (), policy )).To (Succeed ())
208+ Expect (k8sClient .Status ().Update (context .Background (), policy )).To (Succeed ())
209+
210+ // Insert a setter reference into the deployment file,
211+ // before creating the automation object itself.
212+ commitInRepo (repoURL , branch , "Install setter marker" , func (tmp string ) {
213+ replaceMarker (tmp , policyKey )
214+ })
215+
216+ // pull the head commit we just pushed, so it's not
217+ // considered a new commit when checking for a commit
218+ // made by automation.
219+ waitForNewHead (localRepo , branch )
220+
221+ // now create the automation object, and let it (one
222+ // hopes!) make a commit itself.
223+ updateKey := types.NamespacedName {
224+ Namespace : namespace .Name ,
225+ Name : "update-test" ,
226+ }
227+ updateBySetters := & imagev1.ImageUpdateAutomation {
228+ ObjectMeta : metav1.ObjectMeta {
229+ Name : updateKey .Name ,
230+ Namespace : updateKey .Namespace ,
231+ },
232+ Spec : imagev1.ImageUpdateAutomationSpec {
233+ Interval : metav1.Duration {Duration : 2 * time .Hour }, // this is to ensure any subsequent run should be outside the scope of the testing
234+ Checkout : imagev1.GitCheckoutSpec {
235+ GitRepositoryRef : meta.LocalObjectReference {
236+ Name : gitRepoKey .Name ,
237+ },
238+ Branch : branch ,
239+ },
240+ Update : & imagev1.UpdateStrategy {
241+ Strategy : imagev1 .UpdateStrategySetters ,
242+ },
243+ Commit : imagev1.CommitSpec {
244+ MessageTemplate : commitTemplate ,
245+ },
246+ },
247+ }
248+ Expect (k8sClient .Create (context .Background (), updateBySetters )).To (Succeed ())
249+ // wait for a new commit to be made by the controller
250+ waitForNewHead (localRepo , branch )
251+ })
252+
253+ AfterEach (func () {
254+ Expect (k8sClient .Delete (context .Background (), namespace )).To (Succeed ())
255+ })
256+
257+ It ("formats the commit message as in the template" , func () {
258+ head , _ := localRepo .Head ()
259+ commit , err := localRepo .CommitObject (head .Hash ())
260+ Expect (err ).ToNot (HaveOccurred ())
261+ Expect (commit .Message ).To (Equal (commitMessage ))
262+ })
263+ })
264+
114265 endToEnd := func (impl , proto string ) func () {
115266 return func () {
116267 var (
0 commit comments