Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit de02e46

Browse files
committed
Fix credential process tests based on review
1 parent 8771fbc commit de02e46

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

aws/credentials/processcreds/provider_test.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"os"
8+
"os/exec"
89
"runtime"
910
"strings"
1011
"testing"
@@ -305,13 +306,13 @@ func TestProcessProviderTimeout(t *testing.T) {
305306
command := "/bin/sleep 2"
306307
if runtime.GOOS == "windows" {
307308
// "timeout" command does not work due to pipe redirection
308-
command = "C:\\Windows\\system32\\ping -n 2 127.0.0.1>nul"
309+
command = "ping -n 2 127.0.0.1>nul"
309310
}
310311

311312
creds := processcreds.NewCredentialsTimeout(
312313
command,
313314
time.Duration(1)*time.Second)
314-
if _, err := creds.Get(); err == nil || err.(awserr.Error).Code() != processcreds.ErrCodeProcessProviderExecution || err.(awserr.Error).Message() != processcreds.ErrMsgProcessProviderTimeout {
315+
if _, err := creds.Get(); err == nil || err.(awserr.Error).Code() != processcreds.ErrCodeProcessProviderExecution || err.(awserr.Error).Message() != "credential process timed out" {
315316
t.Errorf("expected %v, got %v", processcreds.ErrCodeProcessProviderExecution, err)
316317
}
317318

@@ -474,6 +475,30 @@ func TestProcessProviderForceExpire(t *testing.T) {
474475

475476
}
476477

478+
func TestProcessProviderAltConstruct(t *testing.T) {
479+
oldEnv := preserveImportantStashEnv()
480+
defer awstesting.PopEnv(oldEnv)
481+
482+
// constructing with exec.Cmd instead of string
483+
myCommand := exec.Command(
484+
fmt.Sprintf(
485+
"%s %s",
486+
getOSCat(),
487+
strings.Join(
488+
[]string{"testdata", "static.json"},
489+
string(os.PathSeparator))))
490+
creds := processcreds.NewCredentialsCommand(myCommand, func(opt *processcreds.ProcessProvider) {
491+
opt.Timeout = time.Duration(1) * time.Second
492+
})
493+
_, err := creds.Get()
494+
if err != nil {
495+
t.Errorf("expected %v, got %v", "no error", err)
496+
}
497+
if creds.IsExpired() {
498+
t.Errorf("expected %v, got %v", "static credentials/not expired", "expired")
499+
}
500+
}
501+
477502
func BenchmarkProcessProvider(b *testing.B) {
478503
oldEnv := preserveImportantStashEnv()
479504
defer awstesting.PopEnv(oldEnv)
@@ -500,18 +525,14 @@ func BenchmarkProcessProvider(b *testing.B) {
500525
}
501526

502527
func preserveImportantStashEnv() []string {
503-
extraEnv := make(map[string]string)
528+
envsToKeep := []string{"PATH"}
529+
504530
if runtime.GOOS == "windows" {
505-
key := "ComSpec"
506-
if val, ok := os.LookupEnv(key); ok && len(val) > 0 {
507-
extraEnv[key] = val
508-
}
531+
envsToKeep = append(envsToKeep, "ComSpec")
532+
envsToKeep = append(envsToKeep, "SYSTEM32")
509533
}
510534

511-
key := "PATH"
512-
if val, ok := os.LookupEnv(key); ok && len(val) > 0 {
513-
extraEnv[key] = val
514-
}
535+
extraEnv := getEnvs(envsToKeep)
515536

516537
oldEnv := awstesting.StashEnv() //clear env
517538

@@ -522,6 +543,16 @@ func preserveImportantStashEnv() []string {
522543
return oldEnv
523544
}
524545

546+
func getEnvs(envs []string) map[string]string {
547+
extraEnvs := make(map[string]string)
548+
for _, env := range envs {
549+
if val, ok := os.LookupEnv(env); ok && len(val) > 0 {
550+
extraEnvs[env] = val
551+
}
552+
}
553+
return extraEnvs
554+
}
555+
525556
func getOSCat() string {
526557
if runtime.GOOS == "windows" {
527558
return "type"

0 commit comments

Comments
 (0)