|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux CD contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net/url" |
| 22 | + "os" |
| 23 | + "path" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/go-git/go-billy/v5/memfs" |
| 28 | + "github.com/go-git/go-git/v5" |
| 29 | + "github.com/go-git/go-git/v5/config" |
| 30 | + "github.com/go-git/go-git/v5/plumbing/object" |
| 31 | + "github.com/go-git/go-git/v5/storage/memory" |
| 32 | + . "github.com/onsi/ginkgo" |
| 33 | + . "github.com/onsi/gomega" |
| 34 | + corev1 "k8s.io/api/core/v1" |
| 35 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 | + "k8s.io/apimachinery/pkg/types" |
| 37 | + |
| 38 | + sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" |
| 39 | + "github.com/fluxcd/source-controller/internal/testserver" |
| 40 | +) |
| 41 | + |
| 42 | +var _ = Describe("GitRepositoryReconciler", func() { |
| 43 | + |
| 44 | + const ( |
| 45 | + timeout = time.Second * 30 |
| 46 | + interval = time.Second * 1 |
| 47 | + indexInterval = time.Second * 1 |
| 48 | + ) |
| 49 | + |
| 50 | + Context("GitRepsoitory", func() { |
| 51 | + var ( |
| 52 | + namespace *corev1.Namespace |
| 53 | + gitServer *testserver.GitServer |
| 54 | + err error |
| 55 | + ) |
| 56 | + |
| 57 | + BeforeEach(func() { |
| 58 | + namespace = &corev1.Namespace{ |
| 59 | + ObjectMeta: metav1.ObjectMeta{Name: "git-repository-test" + randStringRunes(5)}, |
| 60 | + } |
| 61 | + err = k8sClient.Create(context.Background(), namespace) |
| 62 | + Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") |
| 63 | + |
| 64 | + gitServer, err = testserver.NewTempGitServer() |
| 65 | + Expect(err).NotTo(HaveOccurred()) |
| 66 | + gitServer.AutoCreate() |
| 67 | + }) |
| 68 | + |
| 69 | + AfterEach(func() { |
| 70 | + os.RemoveAll(gitServer.Root()) |
| 71 | + |
| 72 | + err = k8sClient.Delete(context.Background(), namespace) |
| 73 | + Expect(err).NotTo(HaveOccurred(), "failed to delete test namespace") |
| 74 | + }) |
| 75 | + |
| 76 | + It("Creates artifacts for", func() { |
| 77 | + err = gitServer.StartHTTP() |
| 78 | + Expect(err).NotTo(HaveOccurred()) |
| 79 | + |
| 80 | + By("Creating a new git repository with a single commit") |
| 81 | + u, err := url.Parse(gitServer.HTTPAddress()) |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + u.Path = path.Join(u.Path, "repository.git") |
| 84 | + |
| 85 | + fs := memfs.New() |
| 86 | + r, err := git.Init(memory.NewStorage(), fs) |
| 87 | + Expect(err).NotTo(HaveOccurred()) |
| 88 | + |
| 89 | + _, err = r.CreateRemote(&config.RemoteConfig{ |
| 90 | + Name: "origin", |
| 91 | + URLs: []string{u.String()}, |
| 92 | + }) |
| 93 | + Expect(err).NotTo(HaveOccurred()) |
| 94 | + |
| 95 | + ff, err := fs.Create("fixture") |
| 96 | + Expect(err).NotTo(HaveOccurred()) |
| 97 | + _ = ff.Close() |
| 98 | + |
| 99 | + wt, err := r.Worktree() |
| 100 | + Expect(err).NotTo(HaveOccurred()) |
| 101 | + |
| 102 | + _, err = wt.Add(fs.Join("fixture")) |
| 103 | + Expect(err).NotTo(HaveOccurred()) |
| 104 | + |
| 105 | + cHash, err := wt.Commit("Sample", &git.CommitOptions{Author: &object.Signature{ |
| 106 | + Name: "John Doe", |
| 107 | + |
| 108 | + When: time.Now(), |
| 109 | + }}) |
| 110 | + Expect(err).NotTo(HaveOccurred()) |
| 111 | + |
| 112 | + err = r.Push(&git.PushOptions{}) |
| 113 | + Expect(err).NotTo(HaveOccurred()) |
| 114 | + |
| 115 | + By("Creating a new resource for the repository") |
| 116 | + key := types.NamespacedName{ |
| 117 | + Name: "gitrepository-sample-" + randStringRunes(5), |
| 118 | + Namespace: namespace.Name, |
| 119 | + } |
| 120 | + created := &sourcev1.GitRepository{ |
| 121 | + ObjectMeta: metav1.ObjectMeta{ |
| 122 | + Name: key.Name, |
| 123 | + Namespace: key.Namespace, |
| 124 | + }, |
| 125 | + Spec: sourcev1.GitRepositorySpec{ |
| 126 | + URL: u.String(), |
| 127 | + Interval: metav1.Duration{Duration: indexInterval}, |
| 128 | + }, |
| 129 | + } |
| 130 | + Expect(k8sClient.Create(context.Background(), created)).Should(Succeed()) |
| 131 | + |
| 132 | + By("Expecting artifact and revision") |
| 133 | + got := &sourcev1.GitRepository{} |
| 134 | + Eventually(func() bool { |
| 135 | + _ = k8sClient.Get(context.Background(), key, got) |
| 136 | + return got.Status.Artifact != nil && storage.ArtifactExist(*got.Status.Artifact) |
| 137 | + }, timeout, interval).Should(BeTrue()) |
| 138 | + Expect(got.Status.Artifact.Revision).To(Equal("master/" + cHash.String())) |
| 139 | + |
| 140 | + By("Pushing a change to the repository") |
| 141 | + ff, err = fs.Create("fixture2") |
| 142 | + Expect(err).NotTo(HaveOccurred()) |
| 143 | + _ = ff.Close() |
| 144 | + |
| 145 | + _, err = wt.Add(fs.Join("fixture2")) |
| 146 | + Expect(err).NotTo(HaveOccurred()) |
| 147 | + |
| 148 | + cHash, err = wt.Commit("Sample", &git.CommitOptions{Author: &object.Signature{ |
| 149 | + Name: "John Doe", |
| 150 | + |
| 151 | + When: time.Now(), |
| 152 | + }}) |
| 153 | + Expect(err).NotTo(HaveOccurred()) |
| 154 | + |
| 155 | + err = r.Push(&git.PushOptions{}) |
| 156 | + Expect(err).NotTo(HaveOccurred()) |
| 157 | + |
| 158 | + By("Expecting new artifact revision and GC") |
| 159 | + Eventually(func() bool { |
| 160 | + now := &sourcev1.GitRepository{} |
| 161 | + _ = k8sClient.Get(context.Background(), key, now) |
| 162 | + return now.Status.Artifact.Revision != got.Status.Artifact.Revision && |
| 163 | + !storage.ArtifactExist(*got.Status.Artifact) |
| 164 | + }, timeout, interval).Should(BeTrue()) |
| 165 | + |
| 166 | + By("Expecting git clone error") |
| 167 | + updated := &sourcev1.GitRepository{} |
| 168 | + Expect(k8sClient.Get(context.Background(), key, updated)).Should(Succeed()) |
| 169 | + updated.Spec.URL = "https://invalid.com" |
| 170 | + Expect(k8sClient.Update(context.Background(), updated)).Should(Succeed()) |
| 171 | + Eventually(func() bool { |
| 172 | + _ = k8sClient.Get(context.Background(), key, updated) |
| 173 | + for _, c := range updated.Status.Conditions { |
| 174 | + if c.Reason == sourcev1.GitOperationFailedReason && |
| 175 | + strings.Contains(c.Message, "git clone error") { |
| 176 | + return true |
| 177 | + } |
| 178 | + } |
| 179 | + return false |
| 180 | + }, timeout, interval).Should(BeTrue()) |
| 181 | + Expect(updated.Status.Artifact).ToNot(BeNil()) |
| 182 | + |
| 183 | + By("Expecting to delete successfully") |
| 184 | + got = &sourcev1.GitRepository{} |
| 185 | + Eventually(func() error { |
| 186 | + _ = k8sClient.Get(context.Background(), key, got) |
| 187 | + return k8sClient.Delete(context.Background(), got) |
| 188 | + }, timeout, interval).Should(Succeed()) |
| 189 | + |
| 190 | + By("Expecting delete to finish") |
| 191 | + Eventually(func() error { |
| 192 | + return k8sClient.Get(context.Background(), key, &sourcev1.GitRepository{}) |
| 193 | + }).ShouldNot(Succeed()) |
| 194 | + |
| 195 | + By("Expecting GC on delete") |
| 196 | + exists := func(path string) bool { |
| 197 | + // wait for tmp sync on macOS |
| 198 | + time.Sleep(time.Second) |
| 199 | + _, err := os.Stat(path) |
| 200 | + return err == nil |
| 201 | + } |
| 202 | + Eventually(exists(got.Status.Artifact.Path), timeout, interval).ShouldNot(BeTrue()) |
| 203 | + }) |
| 204 | + }) |
| 205 | +}) |
0 commit comments