chore: de-flake TestWorkspaceAgent_Metadata (round 2) (#7039)

This time, we keep the timing / "racey" tests, but avoid running
them in the harsher CI conditions.
This commit is contained in:
Ammar Bandukwala 2023-04-06 16:10:13 -05:00 committed by GitHub
parent e1149992d8
commit 24d8644c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 7 deletions

View File

@ -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")
}

View File

@ -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{

View File

@ -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")

16
testutil/ci.go Normal file
View File

@ -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"
}

View File

@ -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.