@@ -18,6 +18,7 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
"net/url"
22
23
"os"
23
24
"path"
@@ -49,7 +50,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
49
50
indexInterval = time .Second * 1
50
51
)
51
52
52
- Context ("GitRepsoitory " , func () {
53
+ Context ("GitRepository " , func () {
53
54
var (
54
55
namespace * corev1.Namespace
55
56
gitServer * testserver.GitServer
@@ -75,205 +76,68 @@ var _ = Describe("GitRepositoryReconciler", func() {
75
76
Expect (err ).NotTo (HaveOccurred (), "failed to delete test namespace" )
76
77
})
77
78
78
- It ("Creates artifacts for" , func () {
79
- err = gitServer .StartHTTP ()
80
- Expect (err ).NotTo (HaveOccurred ())
81
-
82
- By ("Creating a new git repository with a single commit" )
83
- u , err := url .Parse (gitServer .HTTPAddress ())
84
- Expect (err ).NotTo (HaveOccurred ())
85
- u .Path = path .Join (u .Path , "repository.git" )
86
-
87
- fs := memfs .New ()
88
- r , err := git .Init (memory .NewStorage (), fs )
89
- Expect (err ).NotTo (HaveOccurred ())
90
-
91
- _ , err = r .CreateRemote (& config.RemoteConfig {
92
- Name : "origin" ,
93
- URLs : []string {u .String ()},
94
- })
95
- Expect (err ).NotTo (HaveOccurred ())
96
-
97
- ff , err := fs .Create ("fixture" )
98
- Expect (err ).NotTo (HaveOccurred ())
99
- _ = ff .Close ()
100
-
101
- wt , err := r .Worktree ()
102
- Expect (err ).NotTo (HaveOccurred ())
103
-
104
- _ , err = wt .Add (fs .Join ("fixture" ))
105
- Expect (err ).NotTo (HaveOccurred ())
106
-
107
- cHash , err := wt .Commit ("Sample" , & git.CommitOptions {Author : & object.Signature {
108
- Name : "John Doe" ,
109
-
110
- When : time .Now (),
111
- }})
112
- Expect (err ).NotTo (HaveOccurred ())
113
-
114
- err = r .Push (& git.PushOptions {})
115
- Expect (err ).NotTo (HaveOccurred ())
116
-
117
- By ("Creating a new resource for the repository" )
118
- key := types.NamespacedName {
119
- Name : "gitrepository-sample-" + randStringRunes (5 ),
120
- Namespace : namespace .Name ,
121
- }
122
- created := & sourcev1.GitRepository {
123
- ObjectMeta : metav1.ObjectMeta {
124
- Name : key .Name ,
125
- Namespace : key .Namespace ,
126
- },
127
- Spec : sourcev1.GitRepositorySpec {
128
- URL : u .String (),
129
- Interval : metav1.Duration {Duration : indexInterval },
130
- },
131
- }
132
- Expect (k8sClient .Create (context .Background (), created )).Should (Succeed ())
133
-
134
- By ("Expecting artifact and revision" )
135
- got := & sourcev1.GitRepository {}
136
- Eventually (func () bool {
137
- _ = k8sClient .Get (context .Background (), key , got )
138
- return got .Status .Artifact != nil && storage .ArtifactExist (* got .Status .Artifact )
139
- }, timeout , interval ).Should (BeTrue ())
140
- Expect (got .Status .Artifact .Revision ).To (Equal ("master/" + cHash .String ()))
141
-
142
- By ("Pushing a change to the repository" )
143
- ff , err = fs .Create ("fixture2" )
144
- Expect (err ).NotTo (HaveOccurred ())
145
- _ = ff .Close ()
146
-
147
- _ , err = wt .Add (fs .Join ("fixture2" ))
148
- Expect (err ).NotTo (HaveOccurred ())
149
-
150
- cHash , err = wt .Commit ("Sample" , & git.CommitOptions {Author : & object.Signature {
151
- Name : "John Doe" ,
152
-
153
- When : time .Now (),
154
- }})
155
- Expect (err ).NotTo (HaveOccurred ())
156
-
157
- err = r .Push (& git.PushOptions {})
158
- Expect (err ).NotTo (HaveOccurred ())
159
-
160
- By ("Expecting new artifact revision and GC" )
161
- Eventually (func () bool {
162
- now := & sourcev1.GitRepository {}
163
- _ = k8sClient .Get (context .Background (), key , now )
164
- return now .Status .Artifact .Revision != got .Status .Artifact .Revision &&
165
- ! storage .ArtifactExist (* got .Status .Artifact )
166
- }, timeout , interval ).Should (BeTrue ())
167
-
168
- By ("Expecting git clone error" )
169
- updated := & sourcev1.GitRepository {}
170
- Expect (k8sClient .Get (context .Background (), key , updated )).Should (Succeed ())
171
- updated .Spec .URL = "https://invalid.com"
172
- Expect (k8sClient .Update (context .Background (), updated )).Should (Succeed ())
173
- Eventually (func () bool {
174
- _ = k8sClient .Get (context .Background (), key , updated )
175
- for _ , c := range updated .Status .Conditions {
176
- if c .Reason == sourcev1 .GitOperationFailedReason &&
177
- strings .Contains (c .Message , "git clone error" ) {
178
- return true
179
- }
180
- }
181
- return false
182
- }, timeout , interval ).Should (BeTrue ())
183
- Expect (updated .Status .Artifact ).ToNot (BeNil ())
184
-
185
- By ("Expecting to delete successfully" )
186
- got = & sourcev1.GitRepository {}
187
- Eventually (func () error {
188
- _ = k8sClient .Get (context .Background (), key , got )
189
- return k8sClient .Delete (context .Background (), got )
190
- }, timeout , interval ).Should (Succeed ())
191
-
192
- By ("Expecting delete to finish" )
193
- Eventually (func () error {
194
- return k8sClient .Get (context .Background (), key , & sourcev1.GitRepository {})
195
- }).ShouldNot (Succeed ())
196
-
197
- By ("Expecting GC on delete" )
198
- exists := func (path string ) bool {
199
- // wait for tmp sync on macOS
200
- time .Sleep (time .Second )
201
- _ , err := os .Stat (path )
202
- return err == nil
203
- }
204
- Eventually (exists (got .Status .Artifact .Path ), timeout , interval ).ShouldNot (BeTrue ())
205
- })
206
-
207
79
type refTestCase struct {
208
- reference * sourcev1.GitRepositoryRef
209
- createBranches []string
210
- createTags []string
80
+ reference * sourcev1.GitRepositoryRef
81
+ createRefs []string
82
+
83
+ waitForReason string
211
84
212
- waitForReason string
213
85
expectStatus corev1.ConditionStatus
214
86
expectMessage string
215
87
expectRevision string
216
88
}
217
89
218
- DescribeTable ("Reference test configuration " , func (t refTestCase ) {
90
+ DescribeTable ("Git references tests " , func (t refTestCase ) {
219
91
err = gitServer .StartHTTP ()
220
- defer os .RemoveAll (gitServer .Root ())
221
92
defer gitServer .StopHTTP ()
222
93
Expect (err ).NotTo (HaveOccurred ())
223
94
224
95
u , err := url .Parse (gitServer .HTTPAddress ())
225
96
Expect (err ).NotTo (HaveOccurred ())
226
- u .Path = path .Join (u .Path , "repository.git" )
97
+ u .Path = path .Join (u .Path , fmt . Sprintf ( "repository-%s .git" , randStringRunes ( 5 )) )
227
98
228
99
fs := memfs .New ()
229
100
gitrepo , err := git .Init (memory .NewStorage (), fs )
230
101
Expect (err ).NotTo (HaveOccurred ())
231
102
232
- remote , err := gitrepo .CreateRemote (& config.RemoteConfig {
233
- Name : "origin" ,
234
- URLs : []string {u .String ()},
235
- })
236
- Expect (err ).NotTo (HaveOccurred ())
237
-
238
- ff , err := fs .Create ("fixture" )
239
- Expect (err ).NotTo (HaveOccurred ())
240
- _ = ff .Close ()
241
-
242
103
wt , err := gitrepo .Worktree ()
243
104
Expect (err ).NotTo (HaveOccurred ())
244
105
106
+ ff , _ := fs .Create ("fixture" )
107
+ _ = ff .Close ()
245
108
_ , err = wt .Add (fs .Join ("fixture" ))
246
109
Expect (err ).NotTo (HaveOccurred ())
247
110
248
- cHash , err := wt .Commit ("Sample" , & git.CommitOptions {Author : & object.Signature {
111
+ commit , err := wt .Commit ("Sample" , & git.CommitOptions {Author : & object.Signature {
249
112
Name : "John Doe" ,
250
113
251
114
When : time .Now (),
252
115
}})
253
116
Expect (err ).NotTo (HaveOccurred ())
254
- err = remote .Push (& git.PushOptions {})
255
- Expect (err ).NotTo (HaveOccurred ())
256
117
257
- for _ , branch := range t .createBranches {
258
- ref := plumbing .NewHashReference (plumbing .ReferenceName ("refs/heads/" + branch ), cHash )
259
- err = gitrepo .Storer .SetReference (ref )
260
- Expect (err ).NotTo (HaveOccurred ())
261
- err = remote .Push (& git.PushOptions {})
262
- Expect (err ).NotTo (HaveOccurred ())
263
- }
118
+ gitrepo .Worktree ()
264
119
265
- for _ , tag := range t .createTags {
266
- ref := plumbing .NewHashReference (plumbing .ReferenceName ("refs/tags/" + tag ), cHash )
267
- err = gitrepo .Storer .SetReference (ref )
268
- Expect (err ).NotTo (HaveOccurred ())
269
- err = remote .Push (& git.PushOptions {
270
- RefSpecs : []config.RefSpec {"refs/tags/*:refs/tags/*" },
271
- })
120
+ for _ , ref := range t .createRefs {
121
+ hRef := plumbing .NewHashReference (plumbing .ReferenceName (ref ), commit )
122
+ err = gitrepo .Storer .SetReference (hRef )
272
123
Expect (err ).NotTo (HaveOccurred ())
273
124
}
274
125
126
+ remote , err := gitrepo .CreateRemote (& config.RemoteConfig {
127
+ Name : "origin" ,
128
+ URLs : []string {u .String ()},
129
+ })
130
+ Expect (err ).NotTo (HaveOccurred ())
131
+
132
+ err = remote .Push (& git.PushOptions {
133
+ RefSpecs : []config.RefSpec {"refs/heads/*:refs/heads/*" , "refs/tags/*:refs/tags/*" },
134
+ })
135
+ Expect (err ).NotTo (HaveOccurred ())
136
+
137
+ t .reference .Commit = strings .Replace (t .reference .Commit , "<commit>" , commit .String (), 1 )
138
+
275
139
key := types.NamespacedName {
276
- Name : "gitrepository-sample-" + randStringRunes (5 ),
140
+ Name : fmt . Sprintf ( "git-ref-test-%s" , randStringRunes (5 ) ),
277
141
Namespace : namespace .Name ,
278
142
}
279
143
created := & sourcev1.GitRepository {
@@ -307,12 +171,12 @@ var _ = Describe("GitRepositoryReconciler", func() {
307
171
Expect (cond .Message ).To (ContainSubstring (t .expectMessage ))
308
172
Expect (got .Status .Artifact == nil ).To (Equal (t .expectRevision == "" ))
309
173
if t .expectRevision != "" {
310
- Expect (got .Status .Artifact .Revision ).To (Equal (t .expectRevision + "/" + cHash .String ()))
174
+ Expect (got .Status .Artifact .Revision ).To (Equal (t .expectRevision + "/" + commit .String ()))
311
175
}
312
176
},
313
177
Entry ("branch" , refTestCase {
314
178
reference : & sourcev1.GitRepositoryRef {Branch : "some-branch" },
315
- createBranches : []string {"some-branch" },
179
+ createRefs : []string {"refs/heads/ some-branch" },
316
180
waitForReason : sourcev1 .GitOperationSucceedReason ,
317
181
expectStatus : corev1 .ConditionTrue ,
318
182
expectRevision : "some-branch" ,
@@ -325,7 +189,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
325
189
}),
326
190
Entry ("tag" , refTestCase {
327
191
reference : & sourcev1.GitRepositoryRef {Tag : "some-tag" },
328
- createTags : []string {"some-tag" },
192
+ createRefs : []string {"refs/tags/ some-tag" },
329
193
waitForReason : sourcev1 .GitOperationSucceedReason ,
330
194
expectStatus : corev1 .ConditionTrue ,
331
195
expectRevision : "some-tag" ,
@@ -338,14 +202,14 @@ var _ = Describe("GitRepositoryReconciler", func() {
338
202
}),
339
203
Entry ("semver" , refTestCase {
340
204
reference : & sourcev1.GitRepositoryRef {SemVer : "1.0.0" },
341
- createTags : []string {"v1.0.0" },
205
+ createRefs : []string {"refs/tags/ v1.0.0" },
342
206
waitForReason : sourcev1 .GitOperationSucceedReason ,
343
207
expectStatus : corev1 .ConditionTrue ,
344
208
expectRevision : "v1.0.0" ,
345
209
}),
346
210
Entry ("semver range" , refTestCase {
347
211
reference : & sourcev1.GitRepositoryRef {SemVer : ">=0.1.0 <1.0.0" },
348
- createTags : []string {"0.1.0" , "0.1.1" , "0.2.0" , "1.0.0" },
212
+ createRefs : []string {"refs/tags/ 0.1.0" , "refs/tags/ 0.1.1" , "refs/tags/ 0.2.0" , "refs/tags/ 1.0.0" },
349
213
waitForReason : sourcev1 .GitOperationSucceedReason ,
350
214
expectStatus : corev1 .ConditionTrue ,
351
215
expectRevision : "0.2.0" ,
@@ -362,6 +226,33 @@ var _ = Describe("GitRepositoryReconciler", func() {
362
226
expectStatus : corev1 .ConditionFalse ,
363
227
expectMessage : "no match found for semver: 1.0.0" ,
364
228
}),
229
+ Entry ("commit" , refTestCase {
230
+ reference : & sourcev1.GitRepositoryRef {
231
+ Commit : "<commit>" ,
232
+ },
233
+ waitForReason : sourcev1 .GitOperationSucceedReason ,
234
+ expectStatus : corev1 .ConditionTrue ,
235
+ expectRevision : "master" ,
236
+ }),
237
+ Entry ("commit in branch" , refTestCase {
238
+ reference : & sourcev1.GitRepositoryRef {
239
+ Branch : "some-branch" ,
240
+ Commit : "<commit>" ,
241
+ },
242
+ createRefs : []string {"refs/heads/some-branch" },
243
+ waitForReason : sourcev1 .GitOperationSucceedReason ,
244
+ expectStatus : corev1 .ConditionTrue ,
245
+ expectRevision : "some-branch" ,
246
+ }),
247
+ Entry ("invalid commit" , refTestCase {
248
+ reference : & sourcev1.GitRepositoryRef {
249
+ Branch : "master" ,
250
+ Commit : "invalid" ,
251
+ },
252
+ waitForReason : sourcev1 .GitOperationFailedReason ,
253
+ expectStatus : corev1 .ConditionFalse ,
254
+ expectMessage : "git commit 'invalid' not found: object not found" ,
255
+ }),
365
256
)
366
257
})
367
258
})
0 commit comments