mirror of
https://github.com/drone-plugins/drone-docker.git
synced 2026-06-04 10:15:30 +08:00
feat: [CI-18951]: added unit tests and better comments for the changes
This commit is contained in:
@@ -597,7 +597,8 @@ func addProxyValue(build *Build, key string) {
|
||||
|
||||
// helper function to get a proxy value from the environment.
|
||||
//
|
||||
// assumes that the upper and lower case versions of are the same.
|
||||
// Checks in order: lowercase key, uppercase key, then HARNESS_<UPPERCASE_KEY>.
|
||||
// Assumes that the upper and lower case versions are the same value.
|
||||
func getProxyValue(key string) string {
|
||||
value := os.Getenv(key)
|
||||
|
||||
@@ -617,9 +618,10 @@ func getProxyValue(key string) string {
|
||||
// helper function that looks to see if a proxy value was set in the build args.
|
||||
func hasProxyBuildArg(build *Build, key string) bool {
|
||||
keyUpper := strings.ToUpper(key)
|
||||
harnessKey := "HARNESS_" + keyUpper
|
||||
|
||||
for _, s := range build.Args {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) || strings.HasPrefix(s, harnessKey) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -628,9 +630,10 @@ func hasProxyBuildArg(build *Build, key string) bool {
|
||||
}
|
||||
func hasProxyBuildArgNew(build *Build, key string) bool {
|
||||
keyUpper := strings.ToUpper(key)
|
||||
harnessKey := "HARNESS_" + keyUpper
|
||||
|
||||
for _, s := range build.ArgsNew {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) || strings.HasPrefix(s, harnessKey) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -179,3 +180,90 @@ func TestCommandBuild(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProxyValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
envVars map[string]string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "lowercase env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{"http_proxy": "http://proxy:8080"},
|
||||
expected: "http://proxy:8080",
|
||||
},
|
||||
{
|
||||
name: "uppercase env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{"HTTP_PROXY": "http://proxy:8080"},
|
||||
expected: "http://proxy:8080",
|
||||
},
|
||||
{
|
||||
name: "HARNESS prefixed env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{"HARNESS_HTTP_PROXY": "http://harness-proxy:8080"},
|
||||
expected: "http://harness-proxy:8080",
|
||||
},
|
||||
{
|
||||
name: "standard takes precedence over HARNESS",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{
|
||||
"HTTP_PROXY": "http://standard:8080",
|
||||
"HARNESS_HTTP_PROXY": "http://harness:8080",
|
||||
},
|
||||
expected: "http://standard:8080",
|
||||
},
|
||||
{
|
||||
name: "lowercase takes precedence over uppercase",
|
||||
key: "no_proxy",
|
||||
envVars: map[string]string{
|
||||
"no_proxy": "localhost,127.0.0.1",
|
||||
"NO_PROXY": "*.example.com",
|
||||
"HARNESS_NO_PROXY": "*.local",
|
||||
},
|
||||
expected: "localhost,127.0.0.1",
|
||||
},
|
||||
{
|
||||
name: "lowercase takes precedence over HARNESS",
|
||||
key: "https_proxy",
|
||||
envVars: map[string]string{
|
||||
"https_proxy": "https://standard:8080",
|
||||
"HARNESS_HTTPS_PROXY": "https://harness:8080",
|
||||
},
|
||||
expected: "https://standard:8080",
|
||||
},
|
||||
{
|
||||
name: "no env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{},
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Clean env
|
||||
lowercaseKey := tt.key
|
||||
uppercaseKey := strings.ToUpper(tt.key)
|
||||
harnessKey := "HARNESS_" + strings.ToUpper(tt.key)
|
||||
|
||||
os.Unsetenv(lowercaseKey)
|
||||
os.Unsetenv(uppercaseKey)
|
||||
os.Unsetenv(harnessKey)
|
||||
|
||||
// Set test environment variables
|
||||
for k, v := range tt.envVars {
|
||||
os.Setenv(k, v)
|
||||
defer os.Unsetenv(k)
|
||||
}
|
||||
|
||||
// Execute and verify
|
||||
result := getProxyValue(tt.key)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getProxyValue(%q) = %q, want %q", tt.key, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user