From f92e1f12247bcf39449d1b984994aa67624a1c53 Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Thu, 6 Apr 2023 19:31:34 +0000 Subject: [PATCH] chore: de-flake TestWorkspaceAgent_Metadata (round 2) This time, we keep the timing / "racey" tests, but avoid running them in the harsher CI conditions. --- agent/reaper/reaper_test.go | 4 ++-- coderd/workspaceagents_test.go | 10 +++++++--- helm/tests/chart_test.go | 4 +++- testutil/ci.go | 16 ++++++++++++++++ testutil/duration.go | 4 +++- 5 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 testutil/ci.go diff --git a/agent/reaper/reaper_test.go b/agent/reaper/reaper_test.go index 946b9558f6dfa..528c029dc5f91 100644 --- a/agent/reaper/reaper_test.go +++ b/agent/reaper/reaper_test.go @@ -23,7 +23,7 @@ func TestReap(t *testing.T) { // Don't run the reaper test in CI. It does weird // things like forkexecing which may have unintended // consequences in CI. - if _, ok := os.LookupEnv("CI"); ok { + if testutil.InCI() { t.Skip("Detected CI, skipping reaper tests") } @@ -73,7 +73,7 @@ func TestReapInterrupt(t *testing.T) { // Don't run the reaper test in CI. It does weird // things like forkexecing which may have unintended // consequences in CI. - if _, ok := os.LookupEnv("CI"); ok { + if testutil.InCI() { t.Skip("Detected CI, skipping reaper tests") } diff --git a/coderd/workspaceagents_test.go b/coderd/workspaceagents_test.go index 72508a98e0e96..34a57b4181624 100644 --- a/coderd/workspaceagents_test.go +++ b/coderd/workspaceagents_test.go @@ -1353,16 +1353,20 @@ func TestWorkspaceAgent_Metadata(t *testing.T) { var update []codersdk.WorkspaceAgentMetadata check := func(want codersdk.WorkspaceAgentMetadataResult, got codersdk.WorkspaceAgentMetadata) { - require.Greater(t, got.Result.CollectedAt, want.CollectedAt) + require.Equal(t, want.Value, got.Result.Value) + require.Equal(t, want.Error, got.Result.Error) + if testutil.InCI() && (runtime.GOOS == "windows" || testutil.InRaceMode()) { + // Avoid testing timings when flake chance is high. + return + } + require.WithinDuration(t, got.Result.CollectedAt, want.CollectedAt, time.Second) ageImpliedNow := got.Result.CollectedAt.Add(time.Duration(got.Result.Age) * time.Second) // We use a long WithinDuration to tolerate slow CI, but we're still making sure // that Age is within the ballpark. require.WithinDuration( t, time.Now(), ageImpliedNow, time.Second*10, ) - require.Equal(t, want.Value, got.Result.Value) - require.Equal(t, want.Error, got.Result.Error) } wantMetadata1 := codersdk.WorkspaceAgentMetadataResult{ diff --git a/helm/tests/chart_test.go b/helm/tests/chart_test.go index 5b4ca465f87f7..8bcde97c4a46e 100644 --- a/helm/tests/chart_test.go +++ b/helm/tests/chart_test.go @@ -11,6 +11,8 @@ import ( "github.com/stretchr/testify/require" "golang.org/x/xerrors" + + "github.com/coder/coder/testutil" ) // These tests run `helm template` with the values file specified in each test @@ -54,7 +56,7 @@ func TestRenderChart(t *testing.T) { if *UpdateGoldenFiles { t.Skip("Golden files are being updated. Skipping test.") } - if _, runningInCI := os.LookupEnv("CI"); runningInCI { + if testutil.InCI() { switch runtime.GOOS { case "windows", "darwin": t.Skip("Skipping tests on Windows and macOS in CI") diff --git a/testutil/ci.go b/testutil/ci.go new file mode 100644 index 0000000000000..d11d8a22241d6 --- /dev/null +++ b/testutil/ci.go @@ -0,0 +1,16 @@ +package testutil + +import ( + "flag" + "os" +) + +func InCI() bool { + _, ok := os.LookupEnv("CI") + return ok +} + +func InRaceMode() bool { + fl := flag.Lookup("race") + return fl != nil && fl.Value.String() == "true" +} diff --git a/testutil/duration.go b/testutil/duration.go index 5480e5f788528..a45f425a6e1fb 100644 --- a/testutil/duration.go +++ b/testutil/duration.go @@ -2,7 +2,9 @@ package testutil -import "time" +import ( + "time" +) // Constants for timing out operations, usable for creating contexts // that timeout or in require.Eventually.