Skip to content

Commit 68a3ea2

Browse files
committed
Add tests for libgit2 remote callbacks
- Adds tests for the libgit2 remote callbacks - Adds tests for CheckoutStrategyForImplementation with context timeout and verify timeout is respected by both the git implementations. Signed-off-by: Sunny <[email protected]>
1 parent 65e5c6a commit 68a3ea2

File tree

4 files changed

+248
-5
lines changed

4 files changed

+248
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7
1212
github.com/cyphar/filepath-securejoin v0.2.2
1313
github.com/fluxcd/pkg/apis/meta v0.10.0
14-
github.com/fluxcd/pkg/gittestserver v0.4.1
14+
github.com/fluxcd/pkg/gittestserver v0.4.2
1515
github.com/fluxcd/pkg/gitutil v0.1.0
1616
github.com/fluxcd/pkg/helmtestserver v0.2.0
1717
github.com/fluxcd/pkg/lockedfile v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
266266
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
267267
github.com/fluxcd/pkg/apis/meta v0.10.0 h1:N7wVGHC1cyPdT87hrDC7UwCwRwnZdQM46PBSLjG2rlE=
268268
github.com/fluxcd/pkg/apis/meta v0.10.0/go.mod h1:CW9X9ijMTpNe7BwnokiUOrLl/h13miwVr/3abEQLbKE=
269-
github.com/fluxcd/pkg/gittestserver v0.4.1 h1:knghRrVEEPnpO0VJYjoz0H2YMc4fnKAVt5hDGsB1IHc=
270-
github.com/fluxcd/pkg/gittestserver v0.4.1/go.mod h1:hUPx21fe/6oox336Wih/XF1fnmzLmptNMOvATbTZXNY=
269+
github.com/fluxcd/pkg/gittestserver v0.4.2 h1:XqoiemTnnUNldnOw8N7OTdalu2iZp1FTRhp9uUauDJQ=
270+
github.com/fluxcd/pkg/gittestserver v0.4.2/go.mod h1:hUPx21fe/6oox336Wih/XF1fnmzLmptNMOvATbTZXNY=
271271
github.com/fluxcd/pkg/gitutil v0.1.0 h1:VO3kJY/CKOCO4ysDNqfdpTg04icAKBOSb3lbR5uE/IE=
272272
github.com/fluxcd/pkg/gitutil v0.1.0/go.mod h1:Ybz50Ck5gkcnvF0TagaMwtlRy3X3wXuiri1HVsK5id4=
273273
github.com/fluxcd/pkg/helmtestserver v0.2.0 h1:cE7YHDmrWI0hr9QpaaeQ0vQ16Z0IiqZKiINDpqdY610=

pkg/git/libgit2/transport_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package libgit2
1818

1919
import (
2020
"bytes"
21+
"context"
2122
"crypto/x509"
2223
"encoding/base64"
2324
"encoding/pem"
@@ -346,6 +347,155 @@ gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nO
346347
}
347348
}
348349

350+
func Test_transferProgressCallback(t *testing.T) {
351+
tests := []struct {
352+
name string
353+
progress git2go.TransferProgress
354+
cancelFunc func(context.CancelFunc)
355+
wantErr git2go.ErrorCode
356+
}{
357+
{
358+
name: "ok - in progress",
359+
progress: git2go.TransferProgress{
360+
TotalObjects: 30,
361+
ReceivedObjects: 21,
362+
},
363+
cancelFunc: func(cf context.CancelFunc) {},
364+
wantErr: git2go.ErrorCodeOK,
365+
},
366+
{
367+
name: "ok - transfer complete",
368+
progress: git2go.TransferProgress{
369+
TotalObjects: 30,
370+
ReceivedObjects: 30,
371+
},
372+
cancelFunc: func(cf context.CancelFunc) {},
373+
wantErr: git2go.ErrorCodeOK,
374+
},
375+
{
376+
name: "ok - transfer complete, context cancelled",
377+
progress: git2go.TransferProgress{
378+
TotalObjects: 30,
379+
ReceivedObjects: 30,
380+
},
381+
cancelFunc: func(cf context.CancelFunc) { cf() },
382+
wantErr: git2go.ErrorCodeOK,
383+
},
384+
{
385+
name: "error - context cancelled",
386+
progress: git2go.TransferProgress{
387+
TotalObjects: 30,
388+
ReceivedObjects: 21,
389+
},
390+
cancelFunc: func(cf context.CancelFunc) { cf() },
391+
wantErr: git2go.ErrorCodeUser,
392+
},
393+
}
394+
395+
for _, tt := range tests {
396+
t.Run(tt.name, func(t *testing.T) {
397+
g := NewWithT(t)
398+
399+
ctx, cancel := context.WithCancel(context.TODO())
400+
defer cancel()
401+
402+
tpcb := transferProgressCallback(ctx)
403+
404+
tt.cancelFunc(cancel)
405+
406+
g.Expect(tpcb(tt.progress)).To(Equal(tt.wantErr))
407+
})
408+
}
409+
}
410+
411+
func Test_transportMessageCallback(t *testing.T) {
412+
tests := []struct {
413+
name string
414+
cancelFunc func(context.CancelFunc)
415+
wantErr git2go.ErrorCode
416+
}{
417+
{
418+
name: "ok - transport open",
419+
cancelFunc: func(cf context.CancelFunc) {},
420+
wantErr: git2go.ErrorCodeOK,
421+
},
422+
{
423+
name: "error - transport closed",
424+
cancelFunc: func(cf context.CancelFunc) { cf() },
425+
wantErr: git2go.ErrorCodeUser,
426+
},
427+
}
428+
429+
for _, tt := range tests {
430+
t.Run(tt.name, func(t *testing.T) {
431+
g := NewWithT(t)
432+
433+
ctx, cancel := context.WithCancel(context.TODO())
434+
defer cancel()
435+
436+
tmcb := transportMessageCallback(ctx)
437+
438+
tt.cancelFunc(cancel)
439+
440+
g.Expect(tmcb("")).To(Equal(tt.wantErr))
441+
})
442+
}
443+
}
444+
445+
func Test_pushTransferProgressCallback(t *testing.T) {
446+
type pushProgress struct {
447+
current uint32
448+
total uint32
449+
bytes uint
450+
}
451+
tests := []struct {
452+
name string
453+
progress pushProgress
454+
cancelFunc func(context.CancelFunc)
455+
wantErr git2go.ErrorCode
456+
}{
457+
{
458+
name: "ok - in progress",
459+
progress: pushProgress{current: 20, total: 25},
460+
cancelFunc: func(cf context.CancelFunc) {},
461+
wantErr: git2go.ErrorCodeOK,
462+
},
463+
{
464+
name: "ok - transfer complete",
465+
progress: pushProgress{current: 25, total: 25},
466+
cancelFunc: func(cf context.CancelFunc) {},
467+
wantErr: git2go.ErrorCodeOK,
468+
},
469+
{
470+
name: "ok - transfer complete, context cancelled",
471+
progress: pushProgress{current: 25, total: 25},
472+
cancelFunc: func(cf context.CancelFunc) { cf() },
473+
wantErr: git2go.ErrorCodeOK,
474+
},
475+
{
476+
name: "error - context cancelled",
477+
progress: pushProgress{current: 20, total: 25},
478+
cancelFunc: func(cf context.CancelFunc) { cf() },
479+
wantErr: git2go.ErrorCodeUser,
480+
},
481+
}
482+
483+
for _, tt := range tests {
484+
t.Run(tt.name, func(t *testing.T) {
485+
g := NewWithT(t)
486+
487+
ctx, cancel := context.WithCancel(context.TODO())
488+
defer cancel()
489+
490+
ptpcb := pushTransferProgressCallback(ctx)
491+
492+
tt.cancelFunc(cancel)
493+
494+
g.Expect(ptpcb(tt.progress.current, tt.progress.total, tt.progress.bytes)).To(Equal(tt.wantErr))
495+
})
496+
}
497+
}
498+
349499
func md5Fingerprint(in string) [16]byte {
350500
var out [16]byte
351501
copy(out[:], in)

pkg/git/strategy/strategy_test.go

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package strategy
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
23+
"net/http"
2224
"net/url"
2325
"os"
2426
"path/filepath"
@@ -189,7 +191,7 @@ func TestCheckoutStrategyForImplementation_Auth(t *testing.T) {
189191
// Run the test cases against the git implementations.
190192
for _, gitImpl := range gitImpls {
191193
for _, tt := range cases {
192-
t.Run(string(gitImpl)+"_"+tt.name, testFunc(tt, gitImpl))
194+
t.Run(fmt.Sprintf("%s_%s", gitImpl, tt.name), testFunc(tt, gitImpl))
193195
}
194196
}
195197
}
@@ -351,7 +353,98 @@ func TestCheckoutStrategyForImplementation_SemVerCheckout(t *testing.T) {
351353
// Run the test cases against the git implementations.
352354
for _, gitImpl := range gitImpls {
353355
for _, tt := range tests {
354-
t.Run(string(gitImpl)+"_"+tt.name, testFunc(tt, gitImpl))
356+
t.Run(fmt.Sprintf("%s_%s", gitImpl, tt.name), testFunc(tt, gitImpl))
357+
}
358+
}
359+
}
360+
361+
func TestCheckoutStrategyForImplementation_WithCtxTimeout(t *testing.T) {
362+
gitImpls := []git.Implementation{gogit.Implementation, libgit2.Implementation}
363+
364+
type testCase struct {
365+
name string
366+
timeout time.Duration
367+
wantErr bool
368+
}
369+
370+
cases := []testCase{
371+
{
372+
name: "fails with short timeout",
373+
timeout: 100 * time.Millisecond,
374+
wantErr: true,
375+
},
376+
{
377+
name: "succeeds with sufficient timeout",
378+
timeout: 5 * time.Second,
379+
wantErr: false,
380+
},
381+
}
382+
383+
// Keeping it low to keep the test run time low.
384+
serverDelay := 500 * time.Millisecond
385+
386+
testFunc := func(tt testCase, impl git.Implementation) func(t *testing.T) {
387+
return func(*testing.T) {
388+
g := NewWithT(t)
389+
390+
gitServer, err := gittestserver.NewTempGitServer()
391+
g.Expect(err).ToNot(HaveOccurred())
392+
defer os.RemoveAll(gitServer.Root())
393+
username := "test-user"
394+
password := "test-password"
395+
gitServer.Auth(username, password)
396+
gitServer.KeyDir(gitServer.Root())
397+
398+
middleware := func(next http.Handler) http.Handler {
399+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
400+
time.Sleep(serverDelay)
401+
next.ServeHTTP(w, r)
402+
})
403+
}
404+
gitServer.AddHTTPMiddlewares(middleware)
405+
406+
g.Expect(gitServer.StartHTTP()).ToNot(HaveOccurred())
407+
defer gitServer.StopHTTP()
408+
409+
branch := "main"
410+
repoPath := "bar/test-reponame"
411+
err = gitServer.InitRepo("testdata/repo1", branch, repoPath)
412+
g.Expect(err).ToNot(HaveOccurred())
413+
414+
repoURL := gitServer.HTTPAddressWithCredentials() + "/" + repoPath
415+
416+
authOpts := &git.AuthOptions{
417+
Transport: git.HTTP,
418+
Username: username,
419+
Password: password,
420+
}
421+
422+
checkoutOpts := git.CheckoutOptions{
423+
Branch: branch,
424+
}
425+
checkoutStrategy, err := CheckoutStrategyForImplementation(context.TODO(), impl, checkoutOpts)
426+
g.Expect(err).ToNot(HaveOccurred())
427+
428+
tmpDir, err := os.MkdirTemp("", "test-checkout")
429+
g.Expect(err).ToNot(HaveOccurred())
430+
defer os.RemoveAll(tmpDir)
431+
432+
checkoutCtx, cancel := context.WithTimeout(context.TODO(), tt.timeout)
433+
defer cancel()
434+
435+
_, gotErr := checkoutStrategy.Checkout(checkoutCtx, tmpDir, repoURL, authOpts)
436+
if tt.wantErr {
437+
g.Expect(gotErr).To(HaveOccurred())
438+
} else {
439+
g.Expect(gotErr).ToNot(HaveOccurred())
440+
}
441+
}
442+
}
443+
444+
// Run the test cases against the git implementations.
445+
for _, gitImpl := range gitImpls {
446+
for _, tt := range cases {
447+
t.Run(fmt.Sprintf("%s_%s", gitImpl, tt.name), testFunc(tt, gitImpl))
355448
}
356449
}
357450
}

0 commit comments

Comments
 (0)