2021-06-24 12:21:03 +00:00
|
|
|
package envproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-06-29 14:47:42 +00:00
|
|
|
func TestEnvProxy(t *testing.T) {
|
2021-06-24 12:21:03 +00:00
|
|
|
tests := []struct {
|
2021-06-29 14:47:42 +00:00
|
|
|
name string
|
|
|
|
proxyArgs []string
|
|
|
|
initialEnv []string
|
|
|
|
modifiedEnv []string
|
2021-06-24 12:21:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-06-29 14:47:42 +00:00
|
|
|
name: "Without args on SetProxy",
|
|
|
|
proxyArgs: []string{"", ""},
|
|
|
|
initialEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_aws_profile"},
|
|
|
|
modifiedEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_aws_profile"},
|
2021-06-24 12:21:03 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 14:47:42 +00:00
|
|
|
name: "With args on SetProxy",
|
|
|
|
proxyArgs: []string{"TEST_DCTL_S3_", "TEST_AWS_"},
|
|
|
|
initialEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_aws_profile"},
|
|
|
|
modifiedEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_dctl_s3_profile"},
|
2021-06-24 12:21:03 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 14:47:42 +00:00
|
|
|
name: "Without toPrefix on SetProxy",
|
|
|
|
proxyArgs: []string{"TEST_DCTL_S3_", ""},
|
|
|
|
initialEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_aws_profile"},
|
|
|
|
modifiedEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_aws_profile"},
|
2021-06-24 12:21:03 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 14:47:42 +00:00
|
|
|
name: "Without fromPrefix on SetProxy",
|
|
|
|
proxyArgs: []string{"", "TEST_AWS_"},
|
|
|
|
initialEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_aws_profile"},
|
|
|
|
modifiedEnv: []string{"TEST_DCTL_S3_PROFILE=test_dctl_s3_profile", "TEST_AWS_PROFILE=test_aws_profile"},
|
2021-06-24 12:21:03 +00:00
|
|
|
},
|
2021-07-01 15:47:03 +00:00
|
|
|
{
|
|
|
|
name: "Without initialEnv",
|
|
|
|
proxyArgs: []string{"TEST_DCTL_S3_", "TEST_AWS_"},
|
|
|
|
initialEnv: []string{},
|
|
|
|
modifiedEnv: []string{},
|
|
|
|
},
|
2021-06-24 12:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-29 14:47:42 +00:00
|
|
|
|
|
|
|
for _, value := range tt.initialEnv {
|
|
|
|
tmp := strings.SplitN(value, "=", 2)
|
|
|
|
os.Setenv(tmp[0], tmp[1])
|
|
|
|
}
|
2021-06-24 12:21:03 +00:00
|
|
|
|
2021-07-01 15:47:03 +00:00
|
|
|
envProxy := NewEnvProxy(tt.proxyArgs[0], tt.proxyArgs[1])
|
2021-06-24 12:21:03 +00:00
|
|
|
|
|
|
|
envProxy.Apply()
|
|
|
|
|
2021-06-29 14:47:42 +00:00
|
|
|
currentEnv := os.Environ()
|
|
|
|
if !compareEnv(currentEnv, tt.modifiedEnv) {
|
|
|
|
t.Errorf("Expected %v, got %v", tt.modifiedEnv, currentEnv)
|
2021-06-24 12:21:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 14:47:42 +00:00
|
|
|
envProxy.Restore()
|
|
|
|
|
|
|
|
currentEnv = os.Environ()
|
|
|
|
if !compareEnv(currentEnv, tt.initialEnv) {
|
|
|
|
t.Errorf("Expected %v, got %v", tt.initialEnv, currentEnv)
|
2021-06-24 12:21:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:47:42 +00:00
|
|
|
func compareEnv(currentEnv, testEnv []string) bool {
|
|
|
|
isValid := 0
|
|
|
|
for _, initialValue := range testEnv {
|
|
|
|
for _, value := range currentEnv {
|
|
|
|
if initialValue == value {
|
|
|
|
isValid++
|
2021-06-24 12:21:03 +00:00
|
|
|
}
|
2021-06-29 14:47:42 +00:00
|
|
|
}
|
2021-06-24 12:21:03 +00:00
|
|
|
}
|
2021-06-29 14:47:42 +00:00
|
|
|
return isValid == len(testEnv)
|
2021-06-24 12:21:03 +00:00
|
|
|
}
|