Skip to content

Commit c46f20b

Browse files
committed
fixing tests
1 parent 9b43388 commit c46f20b

File tree

7 files changed

+72
-93
lines changed

7 files changed

+72
-93
lines changed

pkg/metrics/buffer_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,23 @@ func TestManager(t *testing.T) {
8383

8484
t.Run("concurrent access", func(_ *testing.T) {
8585
manager := NewManager(cfg, mockDB, logger.NewTestLogger())
86-
done := make(chan bool)
8786

88-
const goroutines = 10
87+
goroutines := 10
88+
iterations := 100
89+
if testing.Short() {
90+
goroutines = 4
91+
iterations = 10
92+
}
8993

90-
const iterations = 100
94+
done := make(chan struct{}, goroutines)
9195

9296
for i := 0; i < goroutines; i++ {
9397
go func(id int) {
9498
for j := 0; j < iterations; j++ {
9599
_ = manager.AddMetric("node1", time.Now(), int64(id*1000+j), "test", "device1", "partition1", "agent1")
96100
}
97101

98-
done <- true
102+
done <- struct{}{}
99103
}(i)
100104
}
101105

pkg/nats/accounts/account_manager_test.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ import (
2424
"github.com/nats-io/nkeys"
2525
)
2626

27-
// newTestOperator creates an operator for testing.
28-
func newTestOperator(t *testing.T) *Operator {
29-
t.Helper()
30-
31-
seed, _, err := GenerateOperatorKey()
32-
if err != nil {
33-
t.Fatalf("GenerateOperatorKey() error = %v", err)
34-
}
35-
36-
op, err := NewOperator(&OperatorConfig{
37-
Name: "test-operator",
38-
OperatorSeed: seed,
39-
})
40-
if err != nil {
41-
t.Fatalf("NewOperator() error = %v", err)
42-
}
43-
44-
return op
45-
}
46-
4727
func TestNewAccountSigner(t *testing.T) {
4828
op := newTestOperator(t)
4929
signer := NewAccountSigner(op)
@@ -344,6 +324,9 @@ func TestAccountSigner_MultipleAccounts(t *testing.T) {
344324

345325
// Create multiple accounts
346326
tenants := []string{"tenant-a", "tenant-b", "tenant-c"}
327+
if testing.Short() {
328+
tenants = tenants[:2]
329+
}
347330
results := make(map[string]*TenantAccountResult)
348331

349332
for _, tenant := range tenants {

pkg/nats/accounts/operator_test.go

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import (
2525
)
2626

2727
func TestGenerateOperatorKey(t *testing.T) {
28+
if testing.Short() {
29+
t.Skip("key generation is covered in long tests")
30+
}
31+
2832
seed, publicKey, err := GenerateOperatorKey()
2933
if err != nil {
3034
t.Fatalf("GenerateOperatorKey() error = %v", err)
@@ -62,6 +66,10 @@ func TestGenerateOperatorKey(t *testing.T) {
6266
}
6367

6468
func TestGenerateAccountKey(t *testing.T) {
69+
if testing.Short() {
70+
t.Skip("key generation is covered in long tests")
71+
}
72+
6573
seed, publicKey, err := GenerateAccountKey()
6674
if err != nil {
6775
t.Fatalf("GenerateAccountKey() error = %v", err)
@@ -84,6 +92,10 @@ func TestGenerateAccountKey(t *testing.T) {
8492
}
8593

8694
func TestGenerateUserKey(t *testing.T) {
95+
if testing.Short() {
96+
t.Skip("key generation is covered in long tests")
97+
}
98+
8799
seed, publicKey, err := GenerateUserKey()
88100
if err != nil {
89101
t.Fatalf("GenerateUserKey() error = %v", err)
@@ -107,10 +119,8 @@ func TestGenerateUserKey(t *testing.T) {
107119

108120
func TestNewOperator_DirectSeed(t *testing.T) {
109121
// Generate a test operator key
110-
seed, expectedPubKey, err := GenerateOperatorKey()
111-
if err != nil {
112-
t.Fatalf("GenerateOperatorKey() error = %v", err)
113-
}
122+
seed := testOperatorSeed
123+
expectedPubKey := testOperatorPublicKey(t)
114124

115125
cfg := &OperatorConfig{
116126
Name: "test-operator",
@@ -133,10 +143,8 @@ func TestNewOperator_DirectSeed(t *testing.T) {
133143

134144
func TestNewOperator_FromFile(t *testing.T) {
135145
// Generate a test operator key
136-
seed, expectedPubKey, err := GenerateOperatorKey()
137-
if err != nil {
138-
t.Fatalf("GenerateOperatorKey() error = %v", err)
139-
}
146+
seed := testOperatorSeed
147+
expectedPubKey := testOperatorPublicKey(t)
140148

141149
// Write seed to temp file
142150
tmpDir := t.TempDir()
@@ -162,10 +170,8 @@ func TestNewOperator_FromFile(t *testing.T) {
162170

163171
func TestNewOperator_FromEnv(t *testing.T) {
164172
// Generate a test operator key
165-
seed, expectedPubKey, err := GenerateOperatorKey()
166-
if err != nil {
167-
t.Fatalf("GenerateOperatorKey() error = %v", err)
168-
}
173+
seed := testOperatorSeed
174+
expectedPubKey := testOperatorPublicKey(t)
169175

170176
// Set environment variable
171177
envVar := "TEST_NATS_OPERATOR_SEED"
@@ -187,15 +193,8 @@ func TestNewOperator_FromEnv(t *testing.T) {
187193
}
188194

189195
func TestNewOperator_SystemAccountPublicKeyFromEnv(t *testing.T) {
190-
seed, _, err := GenerateOperatorKey()
191-
if err != nil {
192-
t.Fatalf("GenerateOperatorKey() error = %v", err)
193-
}
194-
195-
_, systemPubKey, err := GenerateAccountKey()
196-
if err != nil {
197-
t.Fatalf("GenerateAccountKey() error = %v", err)
198-
}
196+
seed := testOperatorSeed
197+
systemPubKey := testAccountPublicKey(t)
199198

200199
envVar := "TEST_NATS_SYSTEM_ACCOUNT_PUBLIC_KEY"
201200
t.Setenv(envVar, systemPubKey)
@@ -217,15 +216,8 @@ func TestNewOperator_SystemAccountPublicKeyFromEnv(t *testing.T) {
217216
}
218217

219218
func TestNewOperator_SystemAccountPublicKeyFromFile(t *testing.T) {
220-
seed, _, err := GenerateOperatorKey()
221-
if err != nil {
222-
t.Fatalf("GenerateOperatorKey() error = %v", err)
223-
}
224-
225-
_, systemPubKey, err := GenerateAccountKey()
226-
if err != nil {
227-
t.Fatalf("GenerateAccountKey() error = %v", err)
228-
}
219+
seed := testOperatorSeed
220+
systemPubKey := testAccountPublicKey(t)
229221

230222
tmpDir := t.TempDir()
231223
pubKeyFile := filepath.Join(tmpDir, "system_account.pub")
@@ -263,17 +255,14 @@ func TestNewOperator_InvalidSeed(t *testing.T) {
263255

264256
func TestNewOperator_WrongKeyType(t *testing.T) {
265257
// Generate an account key (not operator)
266-
seed, _, err := GenerateAccountKey()
267-
if err != nil {
268-
t.Fatalf("GenerateAccountKey() error = %v", err)
269-
}
258+
seed := testAccountSeed
270259

271260
cfg := &OperatorConfig{
272261
Name: "test-operator",
273262
OperatorSeed: seed, // Account seed, not operator seed
274263
}
275264

276-
_, err = NewOperator(cfg)
265+
_, err := NewOperator(cfg)
277266
if err == nil {
278267
t.Error("NewOperator() expected error for wrong key type, got nil")
279268
}
@@ -292,10 +281,7 @@ func TestNewOperator_NoSeed(t *testing.T) {
292281
}
293282

294283
func TestOperator_CreateOperatorJWT(t *testing.T) {
295-
seed, _, err := GenerateOperatorKey()
296-
if err != nil {
297-
t.Fatalf("GenerateOperatorKey() error = %v", err)
298-
}
284+
seed := testOperatorSeed
299285

300286
cfg := &OperatorConfig{
301287
Name: "test-operator",
@@ -329,10 +315,7 @@ func TestOperator_CreateOperatorJWT(t *testing.T) {
329315
}
330316

331317
func TestEncodeDecodeKeyForStorage(t *testing.T) {
332-
seed, _, err := GenerateOperatorKey()
333-
if err != nil {
334-
t.Fatalf("GenerateOperatorKey() error = %v", err)
335-
}
318+
seed := testOperatorSeed
336319

337320
encoded := EncodeKeyForStorage(seed)
338321
if encoded == seed {

pkg/nats/accounts/user_manager_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ import (
2828
// createTestAccount creates a test account and returns its seed.
2929
func createTestAccount(t *testing.T) string {
3030
t.Helper()
31-
32-
seed, _, err := GenerateAccountKey()
33-
if err != nil {
34-
t.Fatalf("GenerateAccountKey() error = %v", err)
35-
}
36-
37-
return seed
31+
return testAccountSeed
3832
}
3933

4034
func TestGenerateUserCredentials_Basic(t *testing.T) {
@@ -324,11 +318,9 @@ func TestGenerateUserCredentials_InvalidAccountSeed(t *testing.T) {
324318

325319
func TestGenerateUserCredentials_WrongKeyType(t *testing.T) {
326320
// Use operator seed instead of account seed
327-
operatorSeed, _, _ := GenerateOperatorKey()
328-
329321
_, err := GenerateUserCredentials(
330322
"test-tenant",
331-
operatorSeed,
323+
testOperatorSeed,
332324
"test-user",
333325
CredentialTypeCollector,
334326
nil,
@@ -374,7 +366,11 @@ func TestGenerateUserCredentials_UniqueKeys(t *testing.T) {
374366

375367
// Generate multiple user credentials
376368
var creds []*UserCredentials
377-
for i := 0; i < 5; i++ {
369+
iterations := 5
370+
if testing.Short() {
371+
iterations = 2
372+
}
373+
for i := 0; i < iterations; i++ {
378374
c, err := GenerateUserCredentials(
379375
"test-tenant",
380376
accountSeed,

pkg/registry/batch_optimization_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,26 @@ func TestSimplifiedRegistryBehavior(t *testing.T) {
6161
defer ctrl.Finish()
6262

6363
mockDB := db.NewMockService(ctrl)
64-
mockDB.EXPECT().WithTx(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, fn func(db.Service) error) error {
65-
return fn(mockDB)
66-
}).AnyTimes()
67-
mockDB.EXPECT().LockOCSFDevices(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
64+
mockDB.EXPECT().WithTx(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, fn func(db.Service) error) error {
65+
return fn(mockDB)
66+
}).AnyTimes()
67+
mockDB.EXPECT().LockOCSFDevices(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
6868
allowCanonicalizationQueries(mockDB)
6969
testLogger := logger.NewTestLogger()
7070
registry := NewDeviceRegistry(mockDB, testLogger)
7171

7272
// Create test device updates
73-
updates := createTestDeviceUpdates(tt.sightingCount)
73+
sightingCount := tt.sightingCount
74+
if testing.Short() && sightingCount > 10 {
75+
sightingCount = 10
76+
}
77+
updates := createTestDeviceUpdates(sightingCount)
7478

7579
ctx := context.Background()
7680

7781
// The registry calls ProcessBatchDeviceUpdates which then calls the database
7882
mockDB.EXPECT().
79-
PublishBatchDeviceUpdates(ctx, gomock.Len(tt.sightingCount)).
83+
PublishBatchDeviceUpdates(ctx, gomock.Len(sightingCount)).
8084
Return(nil).
8185
Times(1)
8286

@@ -87,7 +91,7 @@ func TestSimplifiedRegistryBehavior(t *testing.T) {
8791

8892
// Verify
8993
require.NoError(t, err)
90-
t.Logf("%s: Processed %d device updates in %v", tt.description, tt.sightingCount, duration)
94+
t.Logf("%s: Processed %d device updates in %v", tt.description, sightingCount, duration)
9195
})
9296
}
9397
}

pkg/registry/registry_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestProcessBatchDeviceUpdatesUpdatesStore(t *testing.T) {
8181
update := &models.DeviceUpdate{
8282
DeviceID: "default:10.1.0.1",
8383
IP: "10.1.0.1",
84-
GatewayID: "gateway-1",
84+
GatewayID: "gateway-1",
8585
AgentID: "agent-1",
8686
Source: models.DiscoverySourceSNMP,
8787
Timestamp: ts,
@@ -1629,8 +1629,12 @@ func TestProcessBatchDeviceUpdates_CanonicalDeviceIDMatchesForStatsAggregator(t
16291629
registry := NewDeviceRegistry(mockDB, testLogger, WithIdentityEngine(mockDB))
16301630

16311631
// Simulate a batch of devices like faker would generate
1632-
updates := make([]*models.DeviceUpdate, 100)
1633-
for i := 0; i < 100; i++ {
1632+
updateCount := 100
1633+
if testing.Short() {
1634+
updateCount = 10
1635+
}
1636+
updates := make([]*models.DeviceUpdate, updateCount)
1637+
for i := 0; i < updateCount; i++ {
16341638
updates[i] = &models.DeviceUpdate{
16351639
IP: fmt.Sprintf("10.0.%d.%d", i/256, i%256),
16361640
DeviceID: fmt.Sprintf("default:10.0.%d.%d", i/256, i%256),
@@ -1654,7 +1658,7 @@ func TestProcessBatchDeviceUpdates_CanonicalDeviceIDMatchesForStatsAggregator(t
16541658

16551659
err := registry.ProcessBatchDeviceUpdates(ctx, updates)
16561660
require.NoError(t, err)
1657-
require.Len(t, published, 100, "should publish all 100 updates")
1661+
require.Len(t, published, updateCount, "should publish all updates")
16581662

16591663
// Verify ALL published updates would pass isCanonicalRecord check
16601664
nonCanonicalCount := 0

pkg/registry/service_device_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,20 @@ func TestServiceDeviceRegistration_HighCardinalityCheckers(t *testing.T) {
315315
}).
316316
AnyTimes()
317317

318-
// Create 100 checker instances for a single agent
318+
// Create checker instances for a single agent
319319
agentID := "agent-123"
320320
gatewayID := "gateway-1"
321321
hostIP := "192.168.1.100"
322-
updates := make([]*models.DeviceUpdate, 0, 100)
323322

324323
checkerTypes := []string{"sysmon", "rperf", "snmp", "mapper"}
324+
checkerIterations := 25
325+
if testing.Short() {
326+
checkerIterations = 3
327+
}
328+
totalCheckers := checkerIterations * len(checkerTypes)
329+
updates := make([]*models.DeviceUpdate, 0, totalCheckers)
325330
checkerIndex := 0
326-
for i := 0; i < 25; i++ {
331+
for i := 0; i < checkerIterations; i++ {
327332
for _, checkerType := range checkerTypes {
328333
checkerID := fmt.Sprintf("%s-%d@%s", checkerType, checkerIndex, agentID)
329334
updates = append(updates, models.CreateCheckerDeviceUpdate(checkerID, checkerType, agentID, gatewayID, hostIP, "default", nil))
@@ -334,7 +339,7 @@ func TestServiceDeviceRegistration_HighCardinalityCheckers(t *testing.T) {
334339
err := registry.ProcessBatchDeviceUpdates(ctx, updates)
335340
require.NoError(t, err)
336341

337-
require.Len(t, publishedUpdates, 100, "All 100 checkers should be published")
342+
require.Len(t, publishedUpdates, totalCheckers, "All checkers should be published")
338343

339344
// Verify all have unique device IDs
340345
deviceIDs := make(map[string]bool)

0 commit comments

Comments
 (0)