From 8b52742dbc848eb0975e62ae00fbfa4f8108e835 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Wed, 2 Aug 2023 22:41:37 +0530 Subject: [PATCH 1/3] git/gogit: add proxy support for pushing to remote Signed-off-by: Sanskar Jaiswal --- git/gogit/client.go | 13 +++++++------ git/gogit/clone_test.go | 23 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/git/gogit/client.go b/git/gogit/client.go index 9fc0b86e..62da4a31 100644 --- a/git/gogit/client.go +++ b/git/gogit/client.go @@ -409,12 +409,13 @@ func (g *Client) Push(ctx context.Context, cfg repository.PushConfig) error { } err = g.repository.PushContext(ctx, &extgogit.PushOptions{ - RefSpecs: refspecs, - Force: cfg.Force, - RemoteName: extgogit.DefaultRemoteName, - Auth: authMethod, - Progress: nil, - CABundle: caBundle(g.authOpts), + RefSpecs: refspecs, + Force: cfg.Force, + RemoteName: extgogit.DefaultRemoteName, + Auth: authMethod, + Progress: nil, + CABundle: caBundle(g.authOpts), + ProxyOptions: g.proxy, }) return goGitError(err) } diff --git a/git/gogit/clone_test.go b/git/gogit/clone_test.go index 31b95ee9..f5078d63 100644 --- a/git/gogit/clone_test.go +++ b/git/gogit/clone_test.go @@ -1128,7 +1128,7 @@ func Test_ssh_HostKeyAlgos(t *testing.T) { } } -func TestClone_WithProxy(t *testing.T) { +func TestCloneAndPush_WithProxy(t *testing.T) { g := NewWithT(t) server, err := gittestserver.NewTempGitServer() @@ -1178,6 +1178,27 @@ func TestClone_WithProxy(t *testing.T) { }) g.Expect(err).ToNot(HaveOccurred()) g.Expect(proxiedRequests).To(BeNumerically(">", 0)) + + // reset proxy requests counter + proxiedRequests = 0 + // make a commit on master and push it. + cc1, err := commitFile(ggc.repository, "test", "testing gogit push", time.Now()) + g.Expect(err).ToNot(HaveOccurred()) + err = ggc.Push(context.TODO(), repository.PushConfig{}) + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(proxiedRequests).To(BeNumerically(">", 0)) + + // check if we indeed pushed the commit to remote. + ggc, err = NewClient(t.TempDir(), authOpts, WithDiskStorage(), WithProxy(proxyOpts)) + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(ggc.proxy.URL).ToNot(BeEmpty()) + cc2, err := ggc.Clone(context.TODO(), repoURL, repository.CloneConfig{ + CheckoutStrategy: repository.CheckoutStrategy{ + Branch: git.DefaultBranch, + }, + }) + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(cc1.String()).To(Equal(cc2.Hash.String())) } func Test_getRemoteHEAD(t *testing.T) { From 11525516bd55152ce68848bb14680aad43f18479 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Thu, 3 Aug 2023 12:15:23 +0530 Subject: [PATCH 2/3] git/e2e: disable CGO while running e2e tests Disable CGO for Git e2e tests as it was originially required because of our libgit2 client. Since we no longer maintain a libgit2 client, there is no need to run the tests with CGO enabled. Signed-off-by: Sanskar Jaiswal --- git/internal/e2e/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/internal/e2e/run.sh b/git/internal/e2e/run.sh index 6f69227f..48ab17f2 100755 --- a/git/internal/e2e/run.sh +++ b/git/internal/e2e/run.sh @@ -17,4 +17,4 @@ if [[ "${GO_TEST_PREFIX}" = "" ]] || [[ "${GO_TEST_PREFIX}" = *"TestGitLabCEE2E" fi cd "${DIR}" -CGO_ENABLED=1 go test -v -tags 'netgo,osusergo,static_build,e2e' -race -run "^${GO_TEST_PREFIX}.*" ./... +go test -v -tags 'netgo,osusergo,static_build,e2e' -race -run "^${GO_TEST_PREFIX}.*" ./... From 926f56f12d6f10d2b1f1456715d8abec9383c37b Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Thu, 3 Aug 2023 19:17:03 +0530 Subject: [PATCH 3/3] git/e2e: run initial push operations inside `Eventually()` GitHub sometimes fails to push things using SSH due to weird and mysterious deploy key errors, like "unknown error: ERROR: Unknown public SSH key". As a workaround, the intial push operation for each test case is retried on failure for 20 seconds. Signed-off-by: Sanskar Jaiswal --- git/internal/e2e/utils.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/git/internal/e2e/utils.go b/git/internal/e2e/utils.go index 125acdfd..02174fc4 100644 --- a/git/internal/e2e/utils.go +++ b/git/internal/e2e/utils.go @@ -44,6 +44,8 @@ import ( var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890") +const timeout = time.Second * 20 + func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstreamRepo upstreamRepoInfo) { // clone repo _, err := client.Clone(context.TODO(), repoURL.String(), repository.CloneConfig{ @@ -62,8 +64,16 @@ func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstre ) g.Expect(err).ToNot(HaveOccurred(), "first commit") - err = client.Push(context.TODO(), repository.PushConfig{}) - g.Expect(err).ToNot(HaveOccurred()) + // GitHub sometimes takes a long time to propogate its deploy key and this leads + // to mysterious push errors like "unknown error: ERROR: Unknown public SSH key". + // This helps us get around that by retrying for a fixed amount of time. + g.Eventually(func() bool { + err = client.Push(context.TODO(), repository.PushConfig{}) + if err != nil { + return false + } + return true + }, timeout).Should(BeTrue()) headCommit, _, err := headCommitWithBranch(upstreamRepo.url, "main", upstreamRepo.username, upstreamRepo.password) g.Expect(err).ToNot(HaveOccurred()) @@ -120,8 +130,13 @@ func testUsingInit(g *WithT, client repository.Client, repoURL *url.URL, upstrea ) g.Expect(err).ToNot(HaveOccurred(), "first commit") - err = client.Push(context.TODO(), repository.PushConfig{}) - g.Expect(err).ToNot(HaveOccurred()) + g.Eventually(func() bool { + err = client.Push(context.TODO(), repository.PushConfig{}) + if err != nil { + return false + } + return true + }, timeout).Should(BeTrue()) headCommit, _, err := headCommitWithBranch(upstreamRepo.url, "main", upstreamRepo.username, upstreamRepo.password) g.Expect(err).ToNot(HaveOccurred()) @@ -157,6 +172,7 @@ func testUsingInit(g *WithT, client repository.Client, repoURL *url.URL, upstrea g.Expect(err).ToNot(HaveOccurred(), "third commit") err = client.Push(context.TODO(), repository.PushConfig{}) g.Expect(err).ToNot(HaveOccurred()) + headCommit, _, err = headCommitWithBranch(upstreamRepo.url, "new", upstreamRepo.username, upstreamRepo.password) g.Expect(err).ToNot(HaveOccurred()) g.Expect(headCommit).To(Equal(cc))