Skip to content

Commit

Permalink
fix: error when setting gitlab settings in test (#2342)
Browse files Browse the repository at this point in the history
* test: fix error when setting gitlab settings

Signed-off-by: Matej Vašek <[email protected]>

* test: replace sleep with active condition check

Signed-off-by: Matej Vašek <[email protected]>

---------

Signed-off-by: Matej Vašek <[email protected]>
  • Loading branch information
matejvasek authored Jun 7, 2024
1 parent 056f3ff commit 80d0e21
Showing 1 changed file with 69 additions and 7 deletions.
76 changes: 69 additions & 7 deletions pkg/pipelines/tekton/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package tekton_test

import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
Expand All @@ -12,6 +13,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
Expand Down Expand Up @@ -212,7 +214,10 @@ func setupGitlabEnv(ctx context.Context, t *testing.T, baseURL, username, passwo
t.Fatal(err)
}

glabCli, err := gitlab.NewClient(rootToken, gitlab.WithBaseURL(baseURL))
// http client with hacky RoundTripper that removes problematic values from the JSON response
httpCli := &http.Client{Transport: rt{}}

glabCli, err := gitlab.NewClient(rootToken, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(httpCli))
if err != nil {
t.Fatal(err)
}
Expand All @@ -233,16 +238,28 @@ func setupGitlabEnv(ctx context.Context, t *testing.T, baseURL, username, passwo
}
_, _, err = glabCli.Settings.UpdateSettings(newSettings)
if err != nil {
// just log the error, it appears that despite the error the setting is updated
t.Log(err)
t.Fatal(err)
}
// For some reason the setting update does not kick in immediately.

select {
case <-ctx.Done():
t.Fatal("cancelled")
case <-time.After(time.Minute):
ticker := time.NewTicker(time.Second)
out:
for {
select {
case <-ctx.Done():
t.Fatal("cancelled")
case <-ticker.C:
s, _, e := glabCli.Settings.GetSettings()
if e != nil {
t.Fatal(e)
}
if s != nil && s.AllowLocalRequestsFromWebHooksAndServices {
ticker.Stop()
break out
}
}
}

//endregion

//region Create test user
Expand Down Expand Up @@ -353,6 +370,51 @@ func setupGitlabEnv(ctx context.Context, t *testing.T, baseURL, username, passwo
}
}

// RoundTripper which only purpose is to ensures that response JSON from the setting endpoint
// does not contain empty string value the key container_registry_import_created_before.
// Empty string for date/time causes serialization error.
type rt struct{}

func (r rt) RoundTrip(request *http.Request) (*http.Response, error) {
resp, err := http.DefaultTransport.RoundTrip(request)

if request.URL.Path != "/api/v4/application/settings" {
return resp, err
}
if resp.Header.Get("Content-Type") != "application/json" {
return resp, err
}
if err != nil {
return nil, err
}

origBody := resp.Body
defer origBody.Close()

var data any
dec := json.NewDecoder(origBody)
err = dec.Decode(&data)
if err != nil {
return nil, fmt.Errorf("roundtripper could not deserialize data: %v", err)
}

if m, ok := data.(map[string]any); ok {
if val, inMap := m["container_registry_import_created_before"]; inMap && val == "" {
delete(m, "container_registry_import_created_before")
}
}

var newBody bytes.Buffer
enc := json.NewEncoder(&newBody)
err = enc.Encode(&data)
if err != nil {
return nil, fmt.Errorf("roundtripper could not serialize data: %v", err)
}

resp.Body = io.NopCloser(&newBody)
return resp, nil
}

func getAPIToken(baseURL, username, password string) (string, error) {
jar, err := cookiejar.New(nil)
if err != nil {
Expand Down

0 comments on commit 80d0e21

Please sign in to comment.