Skip to content

Commit 77822bd

Browse files
committed
controllers: simple GitRepository test
1 parent 5582d99 commit 77822bd

File tree

5 files changed

+223
-2
lines changed

5 files changed

+223
-2
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
})

controllers/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (s *Storage) RemoveAll(artifact sourcev1.Artifact) error {
9494
// RemoveAllButCurrent removes all files for the given artifact base dir excluding the current one
9595
func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) error {
9696
dir := filepath.Dir(artifact.Path)
97-
errors := []string{}
97+
var errors []string
9898
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
9999
if path != artifact.Path && !info.IsDir() && info.Mode()&os.ModeSymlink != os.ModeSymlink {
100100
if err := os.Remove(path); err != nil {

controllers/suite_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var _ = BeforeSuite(func(done Done) {
9494

9595
Expect(loadExampleKeys()).To(Succeed())
9696

97-
tmpStoragePath, err := ioutil.TempDir("", "helmrepository")
97+
tmpStoragePath, err := ioutil.TempDir("", "source-controller-storage-")
9898
Expect(err).NotTo(HaveOccurred(), "failed to create tmp storage dir")
9999

100100
storage, err = NewStorage(tmpStoragePath, "localhost", time.Second*30)
@@ -105,6 +105,14 @@ var _ = BeforeSuite(func(done Done) {
105105
})
106106
Expect(err).ToNot(HaveOccurred())
107107

108+
err = (&GitRepositoryReconciler{
109+
Client: k8sManager.GetClient(),
110+
Log: ctrl.Log.WithName("controllers").WithName("GitRepository"),
111+
Scheme: scheme.Scheme,
112+
Storage: storage,
113+
}).SetupWithManager(k8sManager)
114+
Expect(err).ToNot(HaveOccurred(), "failed to setup GtRepositoryReconciler")
115+
108116
err = (&HelmRepositoryReconciler{
109117
Client: k8sManager.GetClient(),
110118
Log: ctrl.Log.WithName("controllers").WithName("HelmRepository"),

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.13
44

55
require (
66
github.com/blang/semver v3.5.0+incompatible
7+
github.com/go-git/go-billy/v5 v5.0.0
78
github.com/go-git/go-git/v5 v5.0.0
89
github.com/go-logr/logr v0.1.0
910
github.com/onsi/ginkgo v1.11.0

internal/testserver/git.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ type GitServer struct {
5555
sshServer *gitkit.SSH
5656
}
5757

58+
// AutoCreate enables the automatic creation of a non-existing Git
59+
// repository on push.
60+
func (s *GitServer) AutoCreate() *GitServer {
61+
s.config.AutoCreate = true
62+
return s
63+
}
64+
5865
// StartHTTP starts a new HTTP git server with the current configuration.
5966
func (s *GitServer) StartHTTP() error {
6067
s.StopHTTP()

0 commit comments

Comments
 (0)