From 42fb5c9f8e2b5b0f2907002efc5256a956c00ca6 Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Fri, 15 Dec 2017 13:19:03 +0800 Subject: [PATCH] add proxy settings to the API client for the api-testing part --- .../api-testing/client/harbor_api_client.go | 9 +++++++++ tests/apitests/api-testing/envs/environment.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/apitests/api-testing/client/harbor_api_client.go b/tests/apitests/api-testing/client/harbor_api_client.go index 71c8ec1df..bc87576ba 100644 --- a/tests/apitests/api-testing/client/harbor_api_client.go +++ b/tests/apitests/api-testing/client/harbor_api_client.go @@ -6,6 +6,7 @@ import ( "errors" "io/ioutil" "net/http" + "net/url" "strings" ) @@ -22,6 +23,7 @@ type APIClientConfig struct { CaFile string CertFile string KeyFile string + Proxy string } //APIClient provided the http client for trigger http requests @@ -60,6 +62,13 @@ func NewAPIClient(config APIClientConfig) (*APIClient, error) { TLSClientConfig: tlsConfig, } + //If proxy should be set + if len(strings.TrimSpace(config.Proxy)) > 0 { + if proxyURL, err := url.Parse(config.Proxy); err == nil { + transport.Proxy = http.ProxyURL(proxyURL) + } + } + client := &http.Client{ Transport: transport, } diff --git a/tests/apitests/api-testing/envs/environment.go b/tests/apitests/api-testing/envs/environment.go index 1e25f0f48..3b8ab8061 100644 --- a/tests/apitests/api-testing/envs/environment.go +++ b/tests/apitests/api-testing/envs/environment.go @@ -22,6 +22,7 @@ type Environment struct { CAFile string //env var: CA_FILE_PATH CertFile string //env var: CERT_FILE_PATH KeyFile string //env var: KEY_FILE_PATH + ProxyURL string //env var: http_proxy, https_proxy, HTTP_PROXY, HTTPS_PROXY //API client HTTPClient *client.APIClient @@ -95,6 +96,18 @@ func (env *Environment) Load() error { env.CertFile = certFile } + proxyEnvVar := "https_proxy" + if env.Protocol == "http" { + proxyEnvVar = "http_proxy" + } + proxyURL := os.Getenv(proxyEnvVar) + if !isNotEmpty(proxyURL) { + proxyURL = os.Getenv(strings.ToUpper(proxyEnvVar)) + } + if isNotEmpty(proxyURL) { + env.ProxyURL = proxyURL + } + if !env.loaded { cfg := client.APIClientConfig{ Username: env.Admin, @@ -102,6 +115,7 @@ func (env *Environment) Load() error { CaFile: env.CAFile, CertFile: env.CertFile, KeyFile: env.KeyFile, + Proxy: env.ProxyURL, } httpClient, err := client.NewAPIClient(cfg)