Skip to content

Commit 8fe8ed8

Browse files
Merge pull request #76 from DataDog/fix/storage-test-parallel-race
fix(storage): remove t.Parallel() from tests that mutate shared state
2 parents 9aead01 + f61f1e0 commit 8fe8ed8

1 file changed

Lines changed: 10 additions & 20 deletions

File tree

pkg/auth/storage/factory_test.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import (
1010
"testing"
1111
)
1212

13+
// NOTE: Tests in this file must NOT use t.Parallel() because they mutate
14+
// shared global state: the singleton storage cache (ResetStorage/GetStorage)
15+
// and the DD_TOKEN_STORAGE environment variable (os.Setenv/t.Setenv).
16+
// Running them concurrently causes races where one test's env var change
17+
// or cache reset is visible to another test, leading to flaky failures.
18+
1319
func TestGetStorage(t *testing.T) {
14-
t.Parallel()
1520
tests := []struct {
1621
name string
1722
envValue string
@@ -89,7 +94,6 @@ func TestGetStorage(t *testing.T) {
8994

9095
for _, tt := range tests {
9196
t.Run(tt.name, func(t *testing.T) {
92-
t.Parallel()
9397
// Reset storage state
9498
ResetStorage()
9599

@@ -100,8 +104,7 @@ func TestGetStorage(t *testing.T) {
100104

101105
// Set environment variable
102106
if tt.envValue != "" {
103-
_ = os.Setenv(StorageEnvVar, tt.envValue)
104-
defer func() { _ = os.Unsetenv(StorageEnvVar) }()
107+
t.Setenv(StorageEnvVar, tt.envValue)
105108
}
106109

107110
// Mock keychain availability
@@ -149,7 +152,6 @@ func TestGetStorage(t *testing.T) {
149152
}
150153

151154
func TestGetActiveBackend(t *testing.T) {
152-
t.Parallel()
153155
// Reset storage state
154156
ResetStorage()
155157

@@ -174,7 +176,6 @@ func TestGetActiveBackend(t *testing.T) {
174176
}
175177

176178
func TestIsUsingSecureStorage(t *testing.T) {
177-
t.Parallel()
178179
tests := []struct {
179180
name string
180181
backend BackendType
@@ -194,7 +195,6 @@ func TestIsUsingSecureStorage(t *testing.T) {
194195

195196
for _, tt := range tests {
196197
t.Run(tt.name, func(t *testing.T) {
197-
t.Parallel()
198198
ResetStorage()
199199

200200
// Force the backend type we want to test
@@ -220,7 +220,6 @@ func TestIsUsingSecureStorage(t *testing.T) {
220220
}
221221

222222
func TestGetStorageDescription(t *testing.T) {
223-
t.Parallel()
224223
ResetStorage()
225224

226225
// Force file backend
@@ -246,7 +245,6 @@ func TestGetStorageDescription(t *testing.T) {
246245
}
247246

248247
func TestResetStorage(t *testing.T) {
249-
t.Parallel()
250248
// Get storage to initialize state
251249
_, err := GetStorage(&StorageOptions{ForceBackend: BackendFile})
252250
if err != nil {
@@ -268,7 +266,6 @@ func TestResetStorage(t *testing.T) {
268266
}
269267

270268
func TestGetStorageDescription_Keychain(t *testing.T) {
271-
t.Parallel()
272269
// Skip if keychain is not available
273270
if !IsKeychainAvailable() {
274271
t.Skip("Keychain not available in test environment")
@@ -295,7 +292,6 @@ func TestGetStorageDescription_Keychain(t *testing.T) {
295292
}
296293

297294
func TestGetStorageDescription_File(t *testing.T) {
298-
t.Parallel()
299295
ResetStorage()
300296

301297
// Force file backend
@@ -317,7 +313,6 @@ func TestGetStorageDescription_File(t *testing.T) {
317313
}
318314

319315
func TestDetectBackend_AutoFallback(t *testing.T) {
320-
t.Parallel()
321316
// This tests the auto-detect fallback warning path
322317
ResetStorage()
323318

@@ -338,16 +333,14 @@ func TestDetectBackend_AutoFallback(t *testing.T) {
338333
}
339334

340335
func TestDetectBackend_InvalidEnvValue(t *testing.T) {
341-
t.Parallel()
342336
ResetStorage()
343337

344338
// Set invalid environment value
345-
_ = os.Setenv(StorageEnvVar, "invalid-backend")
346-
defer func() { _ = os.Unsetenv(StorageEnvVar) }()
339+
t.Setenv(StorageEnvVar, "invalid-backend")
347340

348341
_, err := GetStorage(nil)
349342
if err == nil {
350-
t.Error("Expected error for invalid DD_TOKEN_STORAGE value")
343+
t.Fatal("Expected error for invalid DD_TOKEN_STORAGE value")
351344
}
352345

353346
if !contains(err.Error(), "invalid DD_TOKEN_STORAGE value") {
@@ -356,13 +349,12 @@ func TestDetectBackend_InvalidEnvValue(t *testing.T) {
356349
}
357350

358351
func TestCreateStorage_UnknownBackend(t *testing.T) {
359-
t.Parallel()
360352
ResetStorage()
361353

362354
// Try to create storage with unknown backend
363355
_, err := createStorage("unknown-backend")
364356
if err == nil {
365-
t.Error("Expected error for unknown backend type")
357+
t.Fatal("Expected error for unknown backend type")
366358
}
367359

368360
if !contains(err.Error(), "unknown backend type") {
@@ -371,7 +363,6 @@ func TestCreateStorage_UnknownBackend(t *testing.T) {
371363
}
372364

373365
func TestGetStorage_CachedInstance(t *testing.T) {
374-
t.Parallel()
375366
ResetStorage()
376367

377368
// Get storage first time
@@ -393,7 +384,6 @@ func TestGetStorage_CachedInstance(t *testing.T) {
393384
}
394385

395386
func TestGetStorage_ForceBackendOverridesCache(t *testing.T) {
396-
t.Parallel()
397387
ResetStorage()
398388

399389
// Get file storage first

0 commit comments

Comments
 (0)