-
Notifications
You must be signed in to change notification settings - Fork 72
/
update_test.go
1136 lines (991 loc) · 37 KB
/
update_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020 The Flux authors
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 controllers
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/rand"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/go-logr/logr"
. "github.com/onsi/gomega"
"github.com/otiai10/copy"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/gittestserver"
"github.com/fluxcd/pkg/ssh"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
imagev1 "github.com/fluxcd/image-automation-controller/api/v1beta1"
"github.com/fluxcd/image-automation-controller/pkg/test"
"github.com/fluxcd/image-automation-controller/pkg/update"
)
const (
timeout = 10 * time.Second
testAuthorName = "Flux B Ot"
testAuthorEmail = "fluxbot@example.com"
testCommitTemplate = `Commit summary
Automation: {{ .AutomationObject }}
Files:
{{ range $filename, $_ := .Updated.Files -}}
- {{ $filename }}
{{ end -}}
Objects:
{{ range $resource, $_ := .Updated.Objects -}}
- {{ $resource.Kind }} {{ $resource.Name }}
{{ end -}}
Images:
{{ range .Updated.Images -}}
- {{.}} ({{.Policy.Name}})
{{ end -}}
`
testCommitMessageFmt = `Commit summary
Automation: %s/update-test
Files:
- deploy.yaml
Objects:
- Deployment test
Images:
- helloworld:v1.0.0 (%s)
`
)
// Copied from
// https://github.com/fluxcd/source-controller/blob/master/controllers/suite_test.go
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
func randStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func TestImageUpdateAutomation_commit_message(t *testing.T) {
g := NewWithT(t)
namespace := "image-auto-test-" + randStringRunes(5)
branch := randStringRunes(8)
repositoryPath := "/config-" + randStringRunes(6) + ".git"
gitRepoName := "image-auto-" + randStringRunes(5)
imagePolicyName := "policy-" + randStringRunes(5)
// Create test git server.
gitServer, err := setupGitTestServer()
g.Expect(err).ToNot(HaveOccurred(), "failed to create test git server")
defer os.RemoveAll(gitServer.Root())
defer gitServer.StopHTTP()
// Create test namespace.
nsCleanup, err := createNamespace(namespace)
g.Expect(err).ToNot(HaveOccurred(), "failed to create test namespace")
defer func() {
g.Expect(nsCleanup()).To(Succeed())
}()
// Create a git repo.
g.Expect(initGitRepo(gitServer, "testdata/appconfig", branch, repositoryPath)).To(Succeed())
// Clone the repo.
repoURL := gitServer.HTTPAddressWithCredentials() + repositoryPath
localRepo, err := cloneRepo(repoURL, branch)
g.Expect(err).ToNot(HaveOccurred(), "failed to clone git repo")
// Create GitRepository resource for the above repo.
err = createGitRepository(gitRepoName, namespace, "", repoURL, "")
g.Expect(err).ToNot(HaveOccurred(), "failed to create GitRepository resource")
// Create ImagePolicy with populated latest image in the status.
err = createImagePolicyWithLatestImage(imagePolicyName, namespace, "not-expected-to-exist", "1.x", "helloworld:v1.0.0")
g.Expect(err).ToNot(HaveOccurred(), "failed to create ImagePolicy resource")
t.Run("update with commit template", func(t *testing.T) {
commitMessage := fmt.Sprintf(testCommitMessageFmt, namespace, imagePolicyName)
// Update the setter marker in the repo.
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}
commitInRepo(g, repoURL, branch, "Install setter marker", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})
// Pull the head commit that was just pushed, so it's not considered a new
// commit when checking for a commit made by automation.
waitForNewHead(g, localRepo, branch)
// Create the automation object and let it make a commit itself.
updateStrategy := &imagev1.UpdateStrategy{
Strategy: imagev1.UpdateStrategySetters,
}
err = createImageUpdateAutomation("update-test", namespace, gitRepoName, branch, "", testCommitTemplate, "", updateStrategy)
g.Expect(err).ToNot(HaveOccurred())
// Wait for a new commit to be made by the controller.
waitForNewHead(g, localRepo, branch)
head, _ := localRepo.Head()
commit, err := localRepo.CommitObject(head.Hash())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(commit.Author).NotTo(BeNil())
g.Expect(commit.Author.Name).To(Equal(testAuthorName))
g.Expect(commit.Author.Email).To(Equal(testAuthorEmail))
g.Expect(commit.Message).To(Equal(commitMessage))
})
}
func TestImageUpdateAutomation_update_path(t *testing.T) {
g := NewWithT(t)
namespace := "image-auto-test-" + randStringRunes(5)
branch := randStringRunes(8)
repositoryPath := "/config-" + randStringRunes(6) + ".git"
gitRepoName := "image-auto-" + randStringRunes(5)
imagePolicyName := "policy-" + randStringRunes(5)
// Create test git server.
gitServer, err := setupGitTestServer()
g.Expect(err).ToNot(HaveOccurred(), "failed to create test git server")
defer os.RemoveAll(gitServer.Root())
defer gitServer.StopHTTP()
// Create test namespace.
nsCleanup, err := createNamespace(namespace)
g.Expect(err).ToNot(HaveOccurred(), "failed to create test namespace")
defer func() {
g.Expect(nsCleanup()).To(Succeed())
}()
// Create a git repo.
g.Expect(initGitRepo(gitServer, "testdata/pathconfig", branch, repositoryPath)).To(Succeed())
// Clone the repo.
repoURL := gitServer.HTTPAddressWithCredentials() + repositoryPath
localRepo, err := cloneRepo(repoURL, branch)
g.Expect(err).ToNot(HaveOccurred(), "failed to clone git repo")
// Create GitRepository resource for the above repo.
err = createGitRepository(gitRepoName, namespace, "", repoURL, "")
g.Expect(err).ToNot(HaveOccurred(), "failed to create GitRepository resource")
// Create ImagePolicy with populated latest image in the status.
err = createImagePolicyWithLatestImage(imagePolicyName, namespace, "not-expected-to-exist", "1.x", "helloworld:v1.0.0")
g.Expect(err).ToNot(HaveOccurred(), "failed to create ImagePolicy resource")
t.Run("update only under the update path", func(t *testing.T) {
// Update the setter marker in the repo.
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}
commitInRepo(g, repoURL, branch, "Install setter marker", func(tmp string) {
g.Expect(replaceMarker(path.Join(tmp, "yes"), policyKey)).To(Succeed())
})
commitInRepo(g, repoURL, branch, "Install setter marker", func(tmp string) {
g.Expect(replaceMarker(path.Join(tmp, "no"), policyKey)).To(Succeed())
})
// Pull the head commit that was just pushed, so it's not considered a new
// commit when checking for a commit made by automation.
waitForNewHead(g, localRepo, branch)
// Create the automation object and let it make a commit itself.
updateStrategy := &imagev1.UpdateStrategy{
Strategy: imagev1.UpdateStrategySetters,
Path: "./yes",
}
err = createImageUpdateAutomation("update-test", namespace, gitRepoName, branch, "", testCommitTemplate, "", updateStrategy)
g.Expect(err).ToNot(HaveOccurred())
// Wait for a new commit to be made by the controller.
waitForNewHead(g, localRepo, branch)
head, _ := localRepo.Head()
commit, err := localRepo.CommitObject(head.Hash())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(commit.Message).ToNot(ContainSubstring("update-no"))
g.Expect(commit.Message).To(ContainSubstring("update-yes"))
})
}
func TestImageUpdateAutomation_signed_commit(t *testing.T) {
g := NewWithT(t)
namespace := "image-auto-test-" + randStringRunes(5)
branch := randStringRunes(8)
repositoryPath := "/config-" + randStringRunes(6) + ".git"
gitRepoName := "image-auto-" + randStringRunes(5)
imagePolicyName := "policy-" + randStringRunes(5)
signingKeySecretName := "signing-key-secret-" + randStringRunes(5)
// Create test git server.
gitServer, err := setupGitTestServer()
g.Expect(err).ToNot(HaveOccurred(), "failed to create test git server")
defer os.RemoveAll(gitServer.Root())
defer gitServer.StopHTTP()
// Create test namespace.
nsCleanup, err := createNamespace(namespace)
g.Expect(err).ToNot(HaveOccurred(), "failed to create test namespace")
defer func() {
g.Expect(nsCleanup()).To(Succeed())
}()
// Create a git repo.
g.Expect(initGitRepo(gitServer, "testdata/appconfig", branch, repositoryPath)).To(Succeed())
// Clone the repo.
repoURL := gitServer.HTTPAddressWithCredentials() + repositoryPath
localRepo, err := cloneRepo(repoURL, branch)
g.Expect(err).ToNot(HaveOccurred(), "failed to clone git repo")
// Create GitRepository resource for the above repo.
err = createGitRepository(gitRepoName, namespace, "", repoURL, "")
g.Expect(err).ToNot(HaveOccurred(), "failed to create GitRepository resource")
// Create ImagePolicy with populated latest image in the status.
err = createImagePolicyWithLatestImage(imagePolicyName, namespace, "not-expected-to-exist", "1.x", "helloworld:v1.0.0")
g.Expect(err).ToNot(HaveOccurred(), "failed to create ImagePolicy resource")
t.Run("update with signed commit", func(t *testing.T) {
// Update the setter marker in the repo.
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}
commitInRepo(g, repoURL, branch, "Install setter marker", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})
// Pull the head commit that was just pushed, so it's not considered a new
// commit when checking for a commit made by automation.
waitForNewHead(g, localRepo, branch)
pgpEntity, err := createSigningKeyPair(signingKeySecretName, namespace)
g.Expect(err).ToNot(HaveOccurred(), "failed to create signing key pair")
// Create the automation object and let it make a commit itself.
updateStrategy := &imagev1.UpdateStrategy{
Strategy: imagev1.UpdateStrategySetters,
}
err = createImageUpdateAutomation("update-test", namespace, gitRepoName, branch, "", testCommitTemplate, signingKeySecretName, updateStrategy)
g.Expect(err).ToNot(HaveOccurred())
// Wait for a new commit to be made by the controller.
waitForNewHead(g, localRepo, branch)
head, _ := localRepo.Head()
commit, err := localRepo.CommitObject(head.Hash())
g.Expect(err).ToNot(HaveOccurred())
// Configure OpenPGP armor encoder.
b := bytes.NewBuffer(nil)
w, err := armor.Encode(b, openpgp.PrivateKeyType, nil)
g.Expect(err).ToNot(HaveOccurred())
// Serialize public key.
err = pgpEntity.Serialize(w)
g.Expect(err).ToNot(HaveOccurred())
err = w.Close()
g.Expect(err).ToNot(HaveOccurred())
// Verify commit.
ent, err := commit.Verify(b.String())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(ent.PrimaryKey.Fingerprint).To(Equal(pgpEntity.PrimaryKey.Fingerprint))
})
}
func TestImageUpdateAutomation_e2e(t *testing.T) {
tests := []struct {
name string
proto string
impl string
}{
{
name: "go-git with HTTP",
proto: "http",
impl: sourcev1.GoGitImplementation,
},
{
name: "go-git with SSH",
proto: "ssh",
impl: sourcev1.GoGitImplementation,
},
{
name: "libgit2 with HTTP",
proto: "http",
impl: sourcev1.LibGit2Implementation,
},
{
name: "libgit2 with SSH",
proto: "ssh",
impl: sourcev1.LibGit2Implementation,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
const latestImage = "helloworld:1.0.1"
namespace := "image-auto-test-" + randStringRunes(5)
branch := randStringRunes(8)
repositoryPath := "/config-" + randStringRunes(6) + ".git"
gitRepoName := "image-auto-" + randStringRunes(5)
gitSecretName := "git-secret-" + randStringRunes(5)
imagePolicyName := "policy-" + randStringRunes(5)
updateStrategy := &imagev1.UpdateStrategy{
Strategy: imagev1.UpdateStrategySetters,
}
// Create a test namespace.
nsCleanup, err := createNamespace(namespace)
g.Expect(err).ToNot(HaveOccurred(), "failed to create test namespace")
defer func() {
g.Expect(nsCleanup()).To(Succeed())
}()
// Create git server.
gitServer, err := setupGitTestServer()
g.Expect(err).ToNot(HaveOccurred(), "failed to create test git server")
defer os.RemoveAll(gitServer.Root())
defer gitServer.StopHTTP()
cloneLocalRepoURL := gitServer.HTTPAddressWithCredentials() + repositoryPath
repoURL, err := getRepoURL(gitServer, repositoryPath, tt.proto)
g.Expect(err).ToNot(HaveOccurred())
// Start the ssh server if needed.
if tt.proto == "ssh" {
// NOTE: Check how this is done in source-controller.
go func() {
gitServer.StartSSH()
}()
defer func() {
g.Expect(gitServer.StopSSH()).To(Succeed())
}()
}
commitMessage := "Commit a difference " + randStringRunes(5)
// Initialize a git repo.
g.Expect(initGitRepo(gitServer, "testdata/appconfig", branch, repositoryPath)).To(Succeed())
// Clone the repo locally.
localRepo, err := cloneRepo(cloneLocalRepoURL, branch)
g.Expect(err).ToNot(HaveOccurred(), "failed to clone git repo")
// Create GitRepository resource for the above repo.
if tt.proto == "ssh" {
// SSH requires an identity (private key) and known_hosts file
// in a secret.
err = createSSHIdentitySecret(gitSecretName, namespace, repoURL)
g.Expect(err).ToNot(HaveOccurred())
err = createGitRepository(gitRepoName, namespace, tt.impl, repoURL, gitSecretName)
g.Expect(err).ToNot(HaveOccurred())
} else {
err = createGitRepository(gitRepoName, namespace, tt.impl, repoURL, "")
g.Expect(err).ToNot(HaveOccurred())
}
// Create an image policy.
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}
// NB not testing the image reflector controller; this
// will make a "fully formed" ImagePolicy object.
err = createImagePolicyWithLatestImage(imagePolicyName, namespace, "not-expected-to-exist", "1.x", latestImage)
g.Expect(err).ToNot(HaveOccurred(), "failed to create ImagePolicy resource")
// Create ImageUpdateAutomation resource for each of the test cases
// and cleanup at the end.
t.Run("PushSpec", func(t *testing.T) {
imageUpdateAutomationName := "update-" + randStringRunes(5)
pushBranch := "pr-" + randStringRunes(5)
t.Run("update with PushSpec", func(t *testing.T) {
commitInRepo(g, cloneLocalRepoURL, branch, "Install setter marker", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})
// Pull the head commit we just pushed, so it's not
// considered a new commit when checking for a commit
// made by automation.
waitForNewHead(g, localRepo, branch)
// Now create the automation object, and let it (one
// hopes!) make a commit itself.
err = createImageUpdateAutomation(imageUpdateAutomationName, namespace, gitRepoName, branch, pushBranch, commitMessage, "", updateStrategy)
g.Expect(err).ToNot(HaveOccurred())
// Wait for a new commit to be made by the controller.
waitForNewHead(g, localRepo, pushBranch)
head, err := localRepo.Reference(plumbing.NewRemoteReferenceName(originRemote, pushBranch), true)
g.Expect(err).NotTo(HaveOccurred())
commit, err := localRepo.CommitObject(head.Hash())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(commit.Message).To(Equal(commitMessage))
})
t.Run("push branch gets updated", func(t *testing.T) {
// Get the head hash before update.
head, err := localRepo.Reference(plumbing.NewRemoteReferenceName(originRemote, pushBranch), true)
headHash := head.String()
g.Expect(err).NotTo(HaveOccurred())
// Update the policy and expect another commit in the push
// branch.
err = updateImagePolicyWithLatestImage(imagePolicyName, namespace, "helloworld:v1.3.0")
g.Expect(err).ToNot(HaveOccurred())
waitForNewHead(g, localRepo, pushBranch)
head, err = localRepo.Reference(plumbing.NewRemoteReferenceName(originRemote, pushBranch), true)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(head.String()).NotTo(Equal(headHash))
})
t.Run("still pushes to the push branch after it's merged", func(t *testing.T) {
// Get the head hash before.
head, err := localRepo.Reference(plumbing.NewRemoteReferenceName(originRemote, pushBranch), true)
headHash := head.String()
g.Expect(err).NotTo(HaveOccurred())
// Merge the push branch into checkout branch, and push the merge commit
// upstream.
// waitForNewHead() leaves the repo at the head of the branch given, i.e., the
// push branch), so we have to check out the "main" branch first.
g.Expect(checkoutBranch(localRepo, branch)).To(Succeed())
mergeBranchIntoHead(g, localRepo, pushBranch)
// Update the policy and expect another commit in the push
// branch.
err = updateImagePolicyWithLatestImage(imagePolicyName, namespace, "helloworld:v1.3.1")
g.Expect(err).ToNot(HaveOccurred())
waitForNewHead(g, localRepo, pushBranch)
head, err = localRepo.Reference(plumbing.NewRemoteReferenceName(originRemote, pushBranch), true)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(head.String()).NotTo(Equal(headHash))
})
// Cleanup the image update automation used above.
g.Expect(deleteImageUpdateAutomation(imageUpdateAutomationName, namespace)).To(Succeed())
})
t.Run("with update strategy setters", func(t *testing.T) {
// Insert a setter reference into the deployment file,
// before creating the automation object itself.
commitInRepo(g, cloneLocalRepoURL, branch, "Install setter marker", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})
// Pull the head commit we just pushed, so it's not
// considered a new commit when checking for a commit
// made by automation.
waitForNewHead(g, localRepo, branch)
// Now create the automation object, and let it (one
// hopes!) make a commit itself.
updateKey := types.NamespacedName{
Namespace: namespace,
Name: "update-" + randStringRunes(5),
}
err = createImageUpdateAutomation(updateKey.Name, namespace, gitRepoName, branch, "", commitMessage, "", updateStrategy)
g.Expect(err).ToNot(HaveOccurred())
defer func() {
g.Expect(deleteImageUpdateAutomation(updateKey.Name, namespace)).To(Succeed())
}()
// wait for a new commit to be made by the controller
waitForNewHead(g, localRepo, branch)
// Get the head.
head, err := localRepo.Head()
g.Expect(err).ToNot(HaveOccurred())
var newObj imagev1.ImageUpdateAutomation
g.Expect(testEnv.Get(context.Background(), updateKey, &newObj)).To(Succeed())
g.Expect(newObj.Status.LastPushCommit).To(Equal(head.Hash().String()))
g.Expect(newObj.Status.LastPushTime).ToNot(BeNil())
compareRepoWithExpected(g, cloneLocalRepoURL, branch, "testdata/appconfig-setters-expected", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})
})
t.Run("no reconciliation when object is suspended", func(t *testing.T) {
// Create the automation object.
updateKey := types.NamespacedName{
Namespace: namespace,
Name: "update-" + randStringRunes(5),
}
err = createImageUpdateAutomation(updateKey.Name, namespace, gitRepoName, branch, "", commitMessage, "", updateStrategy)
g.Expect(err).ToNot(HaveOccurred())
defer func() {
g.Expect(deleteImageUpdateAutomation(updateKey.Name, namespace)).To(Succeed())
}()
// Wait for the object to be available in the cache before
// attempting update.
g.Eventually(func() bool {
obj := &imagev1.ImageUpdateAutomation{}
if err := testEnv.Get(context.Background(), updateKey, obj); err != nil {
return false
}
return true
}, timeout, time.Second).Should(BeTrue())
// Suspend the automation object.
var updatePatch imagev1.ImageUpdateAutomation
g.Expect(testEnv.Get(context.TODO(), updateKey, &updatePatch)).To(Succeed())
updatePatch.Spec.Suspend = true
g.Expect(testEnv.Patch(context.Background(), &updatePatch, client.Merge)).To(Succeed())
// Create a new image automation reconciler and run it
// explicitly.
imageAutoReconciler := &ImageUpdateAutomationReconciler{
Client: testEnv,
Scheme: scheme.Scheme,
}
// Wait for the suspension to reach the cache
var newUpdate imagev1.ImageUpdateAutomation
g.Eventually(func() bool {
if err := imageAutoReconciler.Get(context.Background(), updateKey, &newUpdate); err != nil {
return false
}
return newUpdate.Spec.Suspend
}, timeout, time.Second).Should(BeTrue())
// Run the reconciliation explicitly, and make sure it
// doesn't do anything
result, err := imageAutoReconciler.Reconcile(logr.NewContext(context.TODO(), ctrl.Log), ctrl.Request{
NamespacedName: updateKey,
})
g.Expect(err).To(BeNil())
// This ought to fail if suspend is not working, since the item would be requeued;
// but if not, additional checks lie below.
g.Expect(result).To(Equal(ctrl.Result{}))
var checkUpdate imagev1.ImageUpdateAutomation
g.Expect(testEnv.Get(context.Background(), updateKey, &checkUpdate)).To(Succeed())
g.Expect(checkUpdate.Status.ObservedGeneration).NotTo(Equal(checkUpdate.ObjectMeta.Generation))
})
t.Run("reconciles with reconcile request annotation", func(t *testing.T) {
// The automation has run, and is not expected to run
// again for 2 hours. Make a commit to the git repo
// which needs to be undone by automation, then add
// the annotation and make sure it runs again.
// TODO: Implement adding request annotation.
// Refer: https://github.com/fluxcd/image-automation-controller/pull/82/commits/4fde199362b42fa37068f2e6c6885cfea474a3d1#diff-1168fadffa18bd096582ae7f8b6db744fd896bd5600ee1d1ac6ac4474af251b9L292-L334
})
})
}
}
func TestImageUpdateAutomation_defaulting(t *testing.T) {
g := NewWithT(t)
branch := randStringRunes(8)
namespace := &corev1.Namespace{}
namespace.Name = "image-auto-test-" + randStringRunes(5)
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
defer cancel()
// Create a test namespace.
g.Expect(testEnv.Create(ctx, namespace)).To(Succeed())
defer func() {
g.Expect(testEnv.Delete(ctx, namespace)).To(Succeed())
}()
// Create an instance of ImageUpdateAutomation.
key := types.NamespacedName{
Name: "update-" + randStringRunes(5),
Namespace: namespace.Name,
}
auto := &imagev1.ImageUpdateAutomation{
ObjectMeta: metav1.ObjectMeta{
Name: key.Name,
Namespace: key.Namespace,
},
Spec: imagev1.ImageUpdateAutomationSpec{
SourceRef: imagev1.SourceReference{
Kind: "GitRepository",
Name: "garbage",
},
Interval: metav1.Duration{Duration: 2 * time.Hour}, // this is to ensure any subsequent run should be outside the scope of the testing
GitSpec: &imagev1.GitSpec{
Checkout: &imagev1.GitCheckoutSpec{
Reference: sourcev1.GitRepositoryRef{
Branch: branch,
},
},
// leave Update field out
Commit: imagev1.CommitSpec{
Author: imagev1.CommitUser{
Email: "fluxbot@example.com",
},
MessageTemplate: "nothing",
},
},
},
}
g.Expect(testEnv.Create(ctx, auto)).To(Succeed())
defer func() {
g.Expect(testEnv.Delete(ctx, auto)).To(Succeed())
}()
// Should default .spec.update to {strategy: Setters}.
var fetchedAuto imagev1.ImageUpdateAutomation
g.Eventually(func() bool {
err := testEnv.Get(ctx, key, &fetchedAuto)
return err == nil
}, timeout, time.Second).Should(BeTrue())
g.Expect(fetchedAuto.Spec.Update).
To(Equal(&imagev1.UpdateStrategy{Strategy: imagev1.UpdateStrategySetters}))
}
func expectCommittedAndPushed(conditions []metav1.Condition) {
rc := apimeta.FindStatusCondition(conditions, meta.ReadyCondition)
Expect(rc).ToNot(BeNil())
Expect(rc.Message).To(ContainSubstring("committed and pushed"))
}
func replaceMarker(path string, policyKey types.NamespacedName) error {
// NB this requires knowledge of what's in the git repo, so a little brittle
deployment := filepath.Join(path, "deploy.yaml")
filebytes, err := ioutil.ReadFile(deployment)
if err != nil {
return err
}
newfilebytes := bytes.ReplaceAll(filebytes, []byte("SETTER_SITE"), []byte(setterRef(policyKey)))
if err = ioutil.WriteFile(deployment, newfilebytes, os.FileMode(0666)); err != nil {
return err
}
return nil
}
func setterRef(name types.NamespacedName) string {
return fmt.Sprintf(`{"%s": "%s:%s"}`, update.SetterShortHand, name.Namespace, name.Name)
}
// waitForHead fetches the remote branch given until it differs from
// the remote ref locally (or if there's no ref locally, until it has
// fetched the remote branch). It resets the working tree head to the
// remote branch ref.
func waitForNewHead(g *WithT, repo *git.Repository, branch string) {
working, err := repo.Worktree()
g.Expect(err).ToNot(HaveOccurred())
// Try to find the remote branch in the repo locally; this will
// fail if we're on a branch that didn't exist when we cloned the
// repo (e.g., if the automation is pushing to another branch).
remoteHeadHash := ""
remoteBranch := plumbing.NewRemoteReferenceName(originRemote, branch)
remoteHead, err := repo.Reference(remoteBranch, false)
if err != plumbing.ErrReferenceNotFound {
g.Expect(err).ToNot(HaveOccurred())
}
if err == nil {
remoteHeadHash = remoteHead.Hash().String()
} // otherwise, any reference fetched will do.
// Now try to fetch new commits from that remote branch
g.Eventually(func() bool {
if err := repo.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{
config.RefSpec("refs/heads/" + branch + ":refs/remotes/origin/" + branch),
},
}); err != nil {
return false
}
remoteHead, err = repo.Reference(remoteBranch, false)
if err != nil {
return false
}
return remoteHead.Hash().String() != remoteHeadHash
}, timeout, time.Second).Should(BeTrue())
// New commits in the remote branch -- reset the working tree head
// to that. Note this does not create a local branch tracking the
// remote, so it is a detached head.
g.Expect(working.Reset(&git.ResetOptions{
Commit: remoteHead.Hash(),
Mode: git.HardReset,
})).To(Succeed())
}
func compareRepoWithExpected(g *WithT, repoURL, branch, fixture string, changeFixture func(tmp string)) {
expected, err := ioutil.TempDir("", "gotest-imageauto-expected")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(expected)
copy.Copy(fixture, expected)
changeFixture(expected)
tmp, err := ioutil.TempDir("", "gotest-imageauto")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmp)
_, err = git.PlainClone(tmp, false, &git.CloneOptions{
URL: repoURL,
ReferenceName: plumbing.NewBranchReferenceName(branch),
})
g.Expect(err).ToNot(HaveOccurred())
test.ExpectMatchingDirectories(g, tmp, expected)
}
func commitInRepo(g *WithT, repoURL, branch, msg string, changeFiles func(path string)) {
tmp, err := ioutil.TempDir("", "gotest-imageauto")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmp)
repo, err := git.PlainClone(tmp, false, &git.CloneOptions{
URL: repoURL,
ReferenceName: plumbing.NewBranchReferenceName(branch),
})
g.Expect(err).ToNot(HaveOccurred())
changeFiles(tmp)
worktree, err := repo.Worktree()
g.Expect(err).ToNot(HaveOccurred())
_, err = worktree.Add(".")
g.Expect(err).ToNot(HaveOccurred())
_, err = worktree.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: "Testbot",
Email: "test@example.com",
When: time.Now(),
},
})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(repo.Push(&git.PushOptions{RemoteName: "origin"})).To(Succeed())
}
// Initialise a git server with a repo including the files in dir.
func initGitRepo(gitServer *gittestserver.GitServer, fixture, branch, repositoryPath string) error {
fs := memfs.New()
repo, err := git.Init(memory.NewStorage(), fs)
if err != nil {
return err
}
err = populateRepoFromFixture(repo, fixture)
if err != nil {
return err
}
working, err := repo.Worktree()
if err != nil {
return err
}
if err = working.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
Create: true,
}); err != nil {
return err
}
remote, err := repo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{gitServer.HTTPAddressWithCredentials() + repositoryPath},
})
if err != nil {
return err
}
return remote.Push(&git.PushOptions{
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", branch, branch)),
},
})
}
func checkoutBranch(repo *git.Repository, branch string) error {
working, err := repo.Worktree()
if err != nil {
return err
}
// check that there's no local changes, as a sanity check
status, err := working.Status()
if err != nil {
return err
}
if len(status) > 0 {
for path := range status {
println(path, "is changed")
}
} // the checkout next will fail if there are changed files
if err = working.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
Create: false,
}); err != nil {
return err
}
return nil
}
// This merges the push branch into HEAD, and pushes upstream. This is
// to simulate e.g., a PR being merged.
func mergeBranchIntoHead(g *WithT, repo *git.Repository, pushBranch string) {
// hash of head
headRef, err := repo.Head()
g.Expect(err).NotTo(HaveOccurred())
pushBranchRef, err := repo.Reference(plumbing.NewRemoteReferenceName(originRemote, pushBranch), false)
g.Expect(err).NotTo(HaveOccurred())
// You need the worktree to be able to create a commit
worktree, err := repo.Worktree()
g.Expect(err).NotTo(HaveOccurred())
_, err = worktree.Commit(fmt.Sprintf("Merge %s", pushBranch), &git.CommitOptions{
Author: &object.Signature{
Name: "Testbot",
Email: "test@example.com",
When: time.Now(),
},
Parents: []plumbing.Hash{headRef.Hash(), pushBranchRef.Hash()},
})
g.Expect(err).NotTo(HaveOccurred())
// push upstream
err = repo.Push(&git.PushOptions{
RemoteName: originRemote,
})
g.Expect(err).NotTo(HaveOccurred())
}
// setupGitTestServer creates and returns a git test server. The caller must
// ensure it's stopped and cleaned up.
func setupGitTestServer() (*gittestserver.GitServer, error) {
gitServer, err := gittestserver.NewTempGitServer()
if err != nil {
return nil, err
}
username := randStringRunes(5)
password := randStringRunes(5)
// Using authentication makes using the server more fiddly in
// general, but is required for testing SSH.
gitServer.Auth(username, password)
gitServer.AutoCreate()
if err := gitServer.StartHTTP(); err != nil {
return nil, err
}
gitServer.KeyDir(filepath.Join(gitServer.Root(), "keys"))
if err := gitServer.ListenSSH(); err != nil {
return nil, err
}
return gitServer, nil
}
// cleanup is used to return closures for cleaning up.
type cleanup func() error
// createNamespace creates a namespace and returns a closure for deleting the
// namespace.
func createNamespace(name string) (cleanup, error) {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: name},
}
if err := testEnv.Create(context.Background(), namespace); err != nil {
return nil, err
}
cleanup := func() error {
return testEnv.Delete(context.Background(), namespace)
}
return cleanup, nil
}
func cloneRepo(repoURL, branch string) (*git.Repository, error) {
return git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
URL: repoURL,
RemoteName: "origin",
ReferenceName: plumbing.NewBranchReferenceName(branch),
})
}
func createGitRepository(name, namespace, impl, repoURL, secretRef string) error {
gitRepo := &sourcev1.GitRepository{
Spec: sourcev1.GitRepositorySpec{
URL: repoURL,
Interval: metav1.Duration{Duration: time.Minute},
},
}
gitRepo.Name = name
gitRepo.Namespace = namespace
if secretRef != "" {
gitRepo.Spec.SecretRef = &meta.LocalObjectReference{Name: secretRef}
}
if impl != "" {
gitRepo.Spec.GitImplementation = impl
}
return testEnv.Create(context.Background(), gitRepo)
}
func createImagePolicyWithLatestImage(name, namespace, repoRef, semverRange, latest string) error {
policy := &imagev1_reflect.ImagePolicy{
Spec: imagev1_reflect.ImagePolicySpec{
ImageRepositoryRef: meta.NamespacedObjectReference{
Name: repoRef,
},
Policy: imagev1_reflect.ImagePolicyChoice{
SemVer: &imagev1_reflect.SemVerPolicy{
Range: semverRange,
},
},
},
}
policy.Name = name
policy.Namespace = namespace
err := testEnv.Create(context.Background(), policy)
if err != nil {
return err
}
policy.Status.LatestImage = latest
return testEnv.Status().Update(context.Background(), policy)
}