From f32aa46ea8547699255a4c7ba5c83597ff7d6483 Mon Sep 17 00:00:00 2001 From: Chirag S Date: Wed, 18 Feb 2026 10:40:37 +0530 Subject: [PATCH] feat: [CI-18951]: added unit tests and better comments for the changes --- docker.go | 11 ++++--- docker_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/docker.go b/docker.go index efd448d..e29ad5d 100644 --- a/docker.go +++ b/docker.go @@ -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_. +// Assumes that the upper and lower case versions are the same value. func getProxyValue(key string) string { value := os.Getenv(key) @@ -606,7 +607,7 @@ func getProxyValue(key string) string { } value = os.Getenv(strings.ToUpper(key)) - + if len(value) > 0 { return value } @@ -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 } } diff --git a/docker_test.go b/docker_test.go index 0fe7028..0850d8b 100644 --- a/docker_test.go +++ b/docker_test.go @@ -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) + } + }) + } +}