Skip to content

Commit d3ed0bb

Browse files
committed
google/internal/externalaccount: fix flaky tests
It seems like these handful of test would fail because of issues related to timings. When running the tests there was sometimes a second scew from when the subjectToken got and want were calculated. Switched them all to use a static clock so the time portion will never differ. Fixes golang/go#49339 Change-Id: Ia2e4e140526ee1fc37fa9d24765cd8ec3033ea22 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/361354 Trust: Cody Oss <codyoss@google.com> Run-TryBot: Cody Oss <codyoss@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
1 parent ba495a6 commit d3ed0bb

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

‎google/internal/externalaccount/aws_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ func TestAwsCredential_BasicRequest(t *testing.T) {
540540
oldGetenv := getenv
541541
defer func() { getenv = oldGetenv }()
542542
getenv = setEnvironment(map[string]string{})
543+
oldNow := now
544+
defer func() { now = oldNow }()
545+
now = setTime(defaultTime)
543546

544547
base, err := tfc.parse(context.Background())
545548
if err != nil {
@@ -560,7 +563,7 @@ func TestAwsCredential_BasicRequest(t *testing.T) {
560563
)
561564

562565
if got, want := out, expected; !reflect.DeepEqual(got, want) {
563-
t.Errorf("subjectToken = %q, want %q", got, want)
566+
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
564567
}
565568
}
566569

@@ -575,6 +578,9 @@ func TestAwsCredential_BasicRequestWithoutSecurityToken(t *testing.T) {
575578
oldGetenv := getenv
576579
defer func() { getenv = oldGetenv }()
577580
getenv = setEnvironment(map[string]string{})
581+
oldNow := now
582+
defer func() { now = oldNow }()
583+
now = setTime(defaultTime)
578584

579585
base, err := tfc.parse(context.Background())
580586
if err != nil {
@@ -595,7 +601,7 @@ func TestAwsCredential_BasicRequestWithoutSecurityToken(t *testing.T) {
595601
)
596602

597603
if got, want := out, expected; !reflect.DeepEqual(got, want) {
598-
t.Errorf("subjectToken = %q, want %q", got, want)
604+
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
599605
}
600606
}
601607

@@ -613,6 +619,9 @@ func TestAwsCredential_BasicRequestWithEnv(t *testing.T) {
613619
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
614620
"AWS_REGION": "us-west-1",
615621
})
622+
oldNow := now
623+
defer func() { now = oldNow }()
624+
now = setTime(defaultTime)
616625

617626
base, err := tfc.parse(context.Background())
618627
if err != nil {
@@ -633,7 +642,7 @@ func TestAwsCredential_BasicRequestWithEnv(t *testing.T) {
633642
)
634643

635644
if got, want := out, expected; !reflect.DeepEqual(got, want) {
636-
t.Errorf("subjectToken = %q, want %q", got, want)
645+
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
637646
}
638647
}
639648

@@ -651,6 +660,9 @@ func TestAwsCredential_BasicRequestWithDefaultEnv(t *testing.T) {
651660
"AWS_SECRET_ACCESS_KEY": "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
652661
"AWS_DEFAULT_REGION": "us-west-1",
653662
})
663+
oldNow := now
664+
defer func() { now = oldNow }()
665+
now = setTime(defaultTime)
654666

655667
base, err := tfc.parse(context.Background())
656668
if err != nil {
@@ -670,7 +682,7 @@ func TestAwsCredential_BasicRequestWithDefaultEnv(t *testing.T) {
670682
)
671683

672684
if got, want := out, expected; !reflect.DeepEqual(got, want) {
673-
t.Errorf("subjectToken = %q, want %q", got, want)
685+
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
674686
}
675687
}
676688

@@ -689,6 +701,9 @@ func TestAwsCredential_BasicRequestWithTwoRegions(t *testing.T) {
689701
"AWS_REGION": "us-west-1",
690702
"AWS_DEFAULT_REGION": "us-east-1",
691703
})
704+
oldNow := now
705+
defer func() { now = oldNow }()
706+
now = setTime(defaultTime)
692707

693708
base, err := tfc.parse(context.Background())
694709
if err != nil {
@@ -708,7 +723,7 @@ func TestAwsCredential_BasicRequestWithTwoRegions(t *testing.T) {
708723
)
709724

710725
if got, want := out, expected; !reflect.DeepEqual(got, want) {
711-
t.Errorf("subjectToken = %q, want %q", got, want)
726+
t.Errorf("subjectToken = \n%q\n want \n%q", got, want)
712727
}
713728
}
714729

0 commit comments

Comments
 (0)