diff --git a/coderd/coderdtest/coderdtest.go b/coderd/coderdtest/coderdtest.go index 4e02c4c4178d3..fe1e6bf8a26a2 100644 --- a/coderd/coderdtest/coderdtest.go +++ b/coderd/coderdtest/coderdtest.go @@ -269,7 +269,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can options.DeploymentValues = DeploymentValues(t) } // This value is not safe to run in parallel. - if options.DeploymentValues.DisableOwnerWorkspaceExec { + if options.DeploymentValues.DisableOwnerWorkspaceExec.Value() && testutil.IsParallel(t) { t.Logf("WARNING: DisableOwnerWorkspaceExec is set, this is not safe in parallel tests!") } diff --git a/testutil/parallel.go b/testutil/parallel.go new file mode 100644 index 0000000000000..3fb6cae467551 --- /dev/null +++ b/testutil/parallel.go @@ -0,0 +1,17 @@ +package testutil + +import "testing" + +// IsParallel determines whether the current test is running with parallel set. +// The Go standard library does not currently expose this. However, we can easily +// determine this using the fact that a call to t.Setenv() after t.Parallel() will panic. +func IsParallel(t testing.TB) (parallel bool) { + t.Helper() + defer func() { + if r := recover(); r != nil { + parallel = true + } + }() + t.Setenv(t.Name()+"_PARALLEL", "") + return parallel +} diff --git a/testutil/parallel_test.go b/testutil/parallel_test.go new file mode 100644 index 0000000000000..f37ddcccffc9a --- /dev/null +++ b/testutil/parallel_test.go @@ -0,0 +1,19 @@ +package testutil_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/coder/coder/v2/testutil" +) + +// nolint:paralleltest // this is the whole point +func Test_IsParallel_False(t *testing.T) { + require.False(t, testutil.IsParallel(t)) +} + +func Test_IsParallel_True(t *testing.T) { + t.Parallel() + require.True(t, testutil.IsParallel(t)) +}