As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Unverified Commit 952a18f6 authored by Igor Drozdov's avatar Igor Drozdov
Browse files

Stub retryable http values in tests

Currently, the default values are used for retryable http.
That's why a test waits 1 second minimun to retry a request.
Client test takes 25 seconds to execute as a result.
When we stub the value to 1 millisecond instead, we get 0.5s of
execution
parent 7750f56e
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -20,7 +20,10 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/testhelper"
)
var secret = "sssh, it's a secret"
var (
secret = "sssh, it's a secret"
defaultHttpOpts = []HTTPClientOpt{WithHTTPRetryOpts(time.Millisecond, time.Millisecond, 2)}
)
func TestClients(t *testing.T) {
testhelper.PrepareTestRootDir(t)
Loading
Loading
@@ -81,7 +84,7 @@ func TestClients(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
url := tc.server(t, buildRequests(t, tc.relativeURLRoot))
httpClient, err := NewHTTPClientWithOpts(url, tc.relativeURLRoot, tc.caFile, "", 1, nil)
httpClient, err := NewHTTPClientWithOpts(url, tc.relativeURLRoot, tc.caFile, "", 1, defaultHttpOpts)
require.NoError(t, err)
client, err := NewGitlabNetClient("", "", tc.secret, httpClient)
Loading
Loading
@@ -313,7 +316,7 @@ func TestRetryableHTTPFeatureToggle(t *testing.T) {
}))
defer srv.Close()
httpClient, err := NewHTTPClientWithOpts(srv.URL, "/", "", "", 1, nil)
httpClient, err := NewHTTPClientWithOpts(srv.URL, "/", "", "", 1, defaultHttpOpts)
require.NoError(t, err)
require.NotNil(t, httpClient.HTTPClient)
require.Nil(t, httpClient.RetryableHTTP)
Loading
Loading
@@ -334,7 +337,7 @@ func TestRetryableHTTPFeatureToggle(t *testing.T) {
}))
defer srv.Close()
httpClient, err := NewHTTPClientWithOpts(srv.URL, "/", "", "", 1, nil)
httpClient, err := NewHTTPClientWithOpts(srv.URL, "/", "", "", 1, defaultHttpOpts)
require.NoError(t, err)
require.Nil(t, httpClient.HTTPClient)
require.NotNil(t, httpClient.RetryableHTTP)
Loading
Loading
Loading
Loading
@@ -24,6 +24,9 @@ const (
httpProtocol = "http://"
httpsProtocol = "https://"
defaultReadTimeoutSeconds = 300
defaultRetryWaitMin = time.Second
defaultRetryWaitMax = 15 * time.Second
defaultRetryMax = 2
)
var ErrCafileNotFound = errors.New("cafile not found")
Loading
Loading
@@ -35,8 +38,10 @@ type HttpClient struct {
}
type httpClientCfg struct {
keyPath, certPath string
caFile, caPath string
keyPath, certPath string
caFile, caPath string
retryWaitMin, retryWaitMax time.Duration
retryMax int
}
func (hcc httpClientCfg) HaveCertAndKey() bool { return hcc.keyPath != "" && hcc.certPath != "" }
Loading
Loading
@@ -53,6 +58,14 @@ func WithClientCert(certPath, keyPath string) HTTPClientOpt {
}
}
func WithHTTPRetryOpts(waitMin, waitMax time.Duration, maxAttempts int) HTTPClientOpt {
return func(hcc *httpClientCfg) {
hcc.retryWaitMin = waitMin
hcc.retryWaitMax = waitMax
hcc.retryMax = maxAttempts
}
}
func validateCaFile(filename string) error {
if filename == "" {
return nil
Loading
Loading
@@ -71,6 +84,18 @@ func validateCaFile(filename string) error {
// NewHTTPClientWithOpts builds an HTTP client using the provided options
func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath string, readTimeoutSeconds uint64, opts []HTTPClientOpt) (*HttpClient, error) {
hcc := &httpClientCfg{
caFile: caFile,
caPath: caPath,
retryWaitMin: defaultRetryWaitMin,
retryWaitMax: defaultRetryWaitMax,
retryMax: defaultRetryMax,
}
for _, opt := range opts {
opt(hcc)
}
var transport *http.Transport
var host string
var err error
Loading
Loading
@@ -84,15 +109,6 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri
return nil, err
}
hcc := &httpClientCfg{
caFile: caFile,
caPath: caPath,
}
for _, opt := range opts {
opt(hcc)
}
transport, host, err = buildHttpsTransport(*hcc, gitlabURL)
if err != nil {
return nil, err
Loading
Loading
@@ -110,8 +126,9 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri
if os.Getenv("FF_GITLAB_SHELL_RETRYABLE_HTTP") == "1" {
c := retryablehttp.NewClient()
c.RetryMax = 2
c.RetryWaitMax = 15 * time.Second
c.RetryMax = hcc.retryMax
c.RetryWaitMax = hcc.retryWaitMax
c.RetryWaitMin = hcc.retryWaitMin
c.Logger = nil
c.HTTPClient.Transport = correlation.NewInstrumentedRoundTripper(tracing.NewRoundTripper(transport))
c.HTTPClient.Timeout = readTimeout(readTimeoutSeconds)
Loading
Loading
Loading
Loading
@@ -123,7 +123,7 @@ func setupWithRequests(t *testing.T, caFile, caPath, clientCAPath, clientCertPat
url := testserver.StartHttpsServer(t, requests, clientCAPath)
var opts []HTTPClientOpt
opts := defaultHttpOpts
if clientCertPath != "" && clientKeyPath != "" {
opts = append(opts, WithClientCert(clientCertPath, clientKeyPath))
}
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment