From 2902af3e41caf9e28504923114abae171b8f6ffc Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Thu, 24 Oct 2024 12:51:00 -0700 Subject: [PATCH] use gitea NewClient to catch errors (#427) Signed-off-by: Manabu McCloskey --- pkg/controllers/localbuild/gitea.go | 10 ++++++---- pkg/controllers/localbuild/gitea_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/controllers/localbuild/gitea.go b/pkg/controllers/localbuild/gitea.go index 8d01a715..79f844c8 100644 --- a/pkg/controllers/localbuild/gitea.go +++ b/pkg/controllers/localbuild/gitea.go @@ -178,10 +178,12 @@ func (r *LocalbuildReconciler) setGiteaToken(ctx context.Context, secret corev1. } func getGiteaToken(ctx context.Context, baseUrl, username, password string) (string, error) { - - giteaClient := gitea.NewClientWithHTTP(baseUrl, util.GetHttpClient()) - giteaClient.SetBasicAuth(username, password) - giteaClient.SetContext(ctx) + giteaClient, err := gitea.NewClient(baseUrl, gitea.SetHTTPClient(util.GetHttpClient()), + gitea.SetBasicAuth(username, password), gitea.SetContext(ctx), + ) + if err != nil { + return "", fmt.Errorf("creating gitea client: %w", err) + } tokens, resp, err := giteaClient.ListAccessTokens(gitea.ListAccessTokensOptions{}) if err != nil { return "", fmt.Errorf("listing gitea access tokens. status: %s error : %w", resp.Status, err) diff --git a/pkg/controllers/localbuild/gitea_test.go b/pkg/controllers/localbuild/gitea_test.go index 6f874c62..b4d0c761 100644 --- a/pkg/controllers/localbuild/gitea_test.go +++ b/pkg/controllers/localbuild/gitea_test.go @@ -1,10 +1,15 @@ package localbuild import ( + "context" + "net/http" + "net/http/httptest" "testing" + "time" "github.com/cnoe-io/idpbuilder/api/v1alpha1" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGiteaInternalBaseUrl(t *testing.T) { @@ -21,3 +26,13 @@ func TestGiteaInternalBaseUrl(t *testing.T) { s = giteaInternalBaseUrl(c) assert.Equal(t, "http://cnoe.localtest.me:8080/gitea", s) } + +func TestGetGiteaToken(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(time.Second * 35) + })) + defer ts.Close() + ctx := context.Background() + _, err := getGiteaToken(ctx, ts.URL, "", "") + require.Error(t, err) +}