feat: [CI-18951]: added unit tests and better comments for the changes

This commit is contained in:
Chirag S
2026-02-18 10:40:37 +05:30
parent 5810bf8a5a
commit f32aa46ea8
2 changed files with 95 additions and 4 deletions
+7 -4
View File
@@ -597,7 +597,8 @@ func addProxyValue(build *Build, key string) {
// helper function to get a proxy value from the environment. // 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 { func getProxyValue(key string) string {
value := os.Getenv(key) value := os.Getenv(key)
@@ -606,7 +607,7 @@ func getProxyValue(key string) string {
} }
value = os.Getenv(strings.ToUpper(key)) value = os.Getenv(strings.ToUpper(key))
if len(value) > 0 { if len(value) > 0 {
return value 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. // helper function that looks to see if a proxy value was set in the build args.
func hasProxyBuildArg(build *Build, key string) bool { func hasProxyBuildArg(build *Build, key string) bool {
keyUpper := strings.ToUpper(key) keyUpper := strings.ToUpper(key)
harnessKey := "HARNESS_" + keyUpper
for _, s := range build.Args { 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 return true
} }
} }
@@ -628,9 +630,10 @@ func hasProxyBuildArg(build *Build, key string) bool {
} }
func hasProxyBuildArgNew(build *Build, key string) bool { func hasProxyBuildArgNew(build *Build, key string) bool {
keyUpper := strings.ToUpper(key) keyUpper := strings.ToUpper(key)
harnessKey := "HARNESS_" + keyUpper
for _, s := range build.ArgsNew { 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 return true
} }
} }
+88
View File
@@ -1,6 +1,7 @@
package docker package docker
import ( import (
"os"
"os/exec" "os/exec"
"reflect" "reflect"
"strings" "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)
}
})
}
}