Repository navigation
Expand file tree
/
Copy pathvulnerability_test.go
More file actions
683 lines (591 loc) · 18.8 KB
/
Copy pathvulnerability_test.go
File metadata and controls
683 lines (591 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
package datastore
import (
"bytes"
"context"
"errors"
"strings"
"sync"
"testing"
"time"
"cloud.google.com/go/datastore"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/osv.dev/go/internal/models"
"github.com/google/osv.dev/go/testutils"
"github.com/ossf/osv-schema/bindings/go/osvschema"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestVulnerabilityStore_ListBySource(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: testutils.NewMockStorage(),
})
vulns := []Vulnerability{
{
SourceID: "source-a:path/1.json",
IsWithdrawn: false,
},
{
SourceID: "source-a:path/2.json",
IsWithdrawn: true,
},
{
SourceID: "source-b:path/3.json",
IsWithdrawn: false,
},
}
keys := []*datastore.Key{
datastore.NameKey("Vulnerability", "ID-1", nil),
datastore.NameKey("Vulnerability", "ID-2", nil),
datastore.NameKey("Vulnerability", "ID-3", nil),
}
if _, err := dsClient.PutMulti(ctx, keys, vulns); err != nil {
t.Fatalf("Failed to setup test data: %v", err)
}
tests := []struct {
name string
source string
skipWithdrawn bool
want []*models.VulnSourceRef
}{
{
name: "Source A - Skip Withdrawn",
source: "source-a",
skipWithdrawn: true,
want: []*models.VulnSourceRef{
{ID: "ID-1", Source: "source-a", Path: "path/1.json"},
},
},
{
name: "Source A - Include Withdrawn",
source: "source-a",
skipWithdrawn: false,
want: []*models.VulnSourceRef{
{ID: "ID-1", Source: "source-a", Path: "path/1.json"},
{ID: "ID-2", Source: "source-a", Path: "path/2.json"},
},
},
{
name: "Source B",
source: "source-b",
skipWithdrawn: true,
want: []*models.VulnSourceRef{
{ID: "ID-3", Source: "source-b", Path: "path/3.json"},
},
},
{
name: "Non-existent Source",
source: "source-c",
skipWithdrawn: true,
want: []*models.VulnSourceRef{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got []*models.VulnSourceRef
for entry, err := range store.ListBySource(ctx, tt.source, tt.skipWithdrawn) {
if err != nil {
t.Fatalf("ListBySource() error: %v", err)
}
got = append(got, entry)
}
sortOpt := cmpopts.SortSlices(func(a, b *models.VulnSourceRef) bool {
return a.ID < b.ID
})
if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty(), sortOpt); diff != "" {
t.Errorf("ListBySource() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestVulnerabilityStore_Get(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
gcsStore := testutils.NewMockStorage()
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: gcsStore,
})
v := &osvschema.Vulnerability{
Id: "GHSA-xxxx-yyyy-zzzz",
}
data, err := proto.Marshal(v)
if err != nil {
t.Fatalf("failed to marshal vulnerability: %v", err)
}
if err := gcsStore.WriteObject(ctx, "all/pb/GHSA-xxxx-yyyy-zzzz.pb", data, nil); err != nil {
t.Fatalf("failed to write mock GCS object: %v", err)
}
got, err := store.GetFull(ctx, "GHSA-xxxx-yyyy-zzzz")
if err != nil {
t.Fatalf("Get() error = %v", err)
}
if got.GetId() != v.GetId() {
t.Errorf("Get() id mismatch, got %s, want %s", got.GetId(), v.GetId())
}
}
func TestVulnerabilityStore_Get_NotFound(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
gcsStore := testutils.NewMockStorage()
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: gcsStore,
})
_, err := store.GetFull(ctx, "GHSA-not-exists")
if !errors.Is(err, models.ErrNotFound) {
t.Errorf("want ErrNotFound, got %v", err)
}
}
func TestVulnerabilityStore_GetWithMetadata(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
gcsStore := testutils.NewMockStorage()
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: gcsStore,
})
v := &osvschema.Vulnerability{
Id: "GHSA-xxxx-yyyy-zzzz",
}
data, err := proto.Marshal(v)
if err != nil {
t.Fatalf("failed to marshal vulnerability: %v", err)
}
if err := gcsStore.WriteObject(ctx, "all/pb/GHSA-xxxx-yyyy-zzzz.pb", data, nil); err != nil {
t.Fatalf("failed to write mock GCS object: %v", err)
}
key := datastore.NameKey("Vulnerability", "GHSA-xxxx-yyyy-zzzz", nil)
dv := Vulnerability{
SourceID: "source-x:path/1.json",
}
if _, err := dsClient.Put(ctx, key, &dv); err != nil {
t.Fatalf("failed to setup datastore test data: %v", err)
}
gotV, gotRef, err := store.GetWithMetadata(ctx, "GHSA-xxxx-yyyy-zzzz")
if err != nil {
t.Fatalf("GetWithMetadata() error = %v", err)
}
if gotV.GetId() != v.GetId() {
t.Errorf("GetWithMetadata() id mismatch, got %s, want %s", gotV.GetId(), v.GetId())
}
if gotRef.Source != "source-x" {
t.Errorf("gotRef.Source mismatch, got %s, want source-x", gotRef.Source)
}
if gotRef.Path != "path/1.json" {
t.Errorf("gotRef.Path mismatch, got %s, want path/1.json", gotRef.Path)
}
}
func TestVulnerabilityStore_GetWithMetadata_NotFound(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
gcsStore := testutils.NewMockStorage()
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: gcsStore,
})
_, _, err := store.GetWithMetadata(ctx, "GHSA-not-exists")
if !errors.Is(err, models.ErrNotFound) {
t.Errorf("want ErrNotFound, got %v", err)
}
}
func TestVulnerabilityStore_Write(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
gcsStore := testutils.NewMockStorage()
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: gcsStore,
})
v := &osvschema.Vulnerability{
Id: "TEST-WRITE-123",
Published: timestamppb.Now(),
Affected: []*osvschema.Affected{
{
Package: &osvschema.Package{
Name: "test-pkg",
Ecosystem: "npm",
},
Ranges: []*osvschema.Range{
{
Type: osvschema.Range_ECOSYSTEM,
Events: []*osvschema.Event{
{Introduced: "1.0.0"},
{Fixed: "2.0.0"},
},
},
},
},
},
}
req := models.WriteRequest{
ID: "TEST-WRITE-123",
Source: "test-source",
Path: "TEST-WRITE-123.json",
Raw: v,
Enriched: v,
AffectedCommits: models.AffectedCommitsResult{
Skip: false,
Commits: [][]byte{[]byte("test-commit")},
},
}
if err := store.Write(ctx, req); err != nil {
t.Fatalf("Write() failed: %v", err)
}
// 1. Verify Vulnerability was written
var dv Vulnerability
vKey := datastore.NameKey("Vulnerability", req.ID, nil)
if err := dsClient.Get(ctx, vKey, &dv); err != nil {
t.Fatalf("failed to fetch Vulnerability after Write: %v", err)
}
if dv.SourceID != "test-source:TEST-WRITE-123.json" {
t.Errorf("Vulnerability.SourceID mismatch, got %s", dv.SourceID)
}
// 2. Verify ListedVulnerability was written
var lv ListedVulnerability
lvKey := datastore.NameKey("ListedVulnerability", req.ID, nil)
if err := dsClient.Get(ctx, lvKey, &lv); err != nil {
t.Fatalf("failed to fetch ListedVulnerability after Write: %v", err)
}
if lv.Summary != v.GetSummary() {
t.Errorf("ListedVulnerability Summary mismatch")
}
// 3. Verify AffectedCommits was written
var ac AffectedCommits
acKey := datastore.NameKey("AffectedCommits", "TEST-WRITE-123-0", nil)
if err := dsClient.Get(ctx, acKey, &ac); err != nil {
t.Fatalf("failed to fetch AffectedCommits after Write: %v", err)
}
if len(ac.Commits) != 1 || string(ac.Commits[0]) != "test-commit" {
t.Errorf("AffectedCommits mismatch, got %v", ac.Commits)
}
// 4. Verify AffectedVersions was written
var avs []AffectedVersions
q := datastore.NewQuery("AffectedVersions").FilterField("vuln_id", "=", req.ID)
if _, err := dsClient.GetAll(ctx, q, &avs); err != nil {
t.Fatalf("failed to query AffectedVersions after Write: %v", err)
}
if len(avs) == 0 {
t.Errorf("Expected AffectedVersions to be written, got 0")
}
// 5. Verify GCS object was written
path := "all/pb/TEST-WRITE-123.pb"
gcsData, err := gcsStore.ReadObject(ctx, path)
if err != nil {
t.Fatalf("failed to read GCS object after Write: %v", err)
}
gotV := new(osvschema.Vulnerability)
if err := proto.Unmarshal(gcsData, gotV); err != nil {
t.Fatalf("failed to unmarshal vulnerability from GCS: %v", err)
}
if gotV.GetId() != v.GetId() {
t.Errorf("GCS Vulnerability ID mismatch, got %s, want %s", gotV.GetId(), v.GetId())
}
}
func TestVulnerabilityStore_Write_GCSFailure_Retry(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
gcsStore := testutils.NewMockStorage()
mockPublisher := &testutils.MockPublisher{}
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: gcsStore,
FailedWritePublisher: mockPublisher,
})
// Make GCS write fail
gcsStore.WriteError = errors.New("GCS write failed")
v := &osvschema.Vulnerability{
Id: "TEST-WRITE-RETRY",
}
req := models.WriteRequest{
ID: "TEST-WRITE-RETRY",
Source: "test-source",
Path: "TEST-WRITE-RETRY.json",
Enriched: v,
}
if err := store.Write(ctx, req); err != nil {
t.Fatalf("Write() failed: %v", err)
}
// Verify that it attempted to publish to the retry topic
if len(mockPublisher.Messages) != 1 {
t.Fatalf("Expected 1 message published, got %d", len(mockPublisher.Messages))
}
msg := mockPublisher.Messages[0]
if msg.Attributes["type"] != "gcs_retry" {
t.Errorf("Expected message type 'gcs_retry', got %q", msg.Attributes["type"])
}
expectedData, _ := proto.Marshal(v)
if !bytes.Equal(msg.Data, expectedData) {
t.Errorf("Message data mismatch")
}
}
func TestVulnerabilityStore_GetModified(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
})
now := time.Now().UTC().Truncate(time.Millisecond) // Datastore might lose precision, truncate to ms
vuln := Vulnerability{
Modified: now,
}
key := datastore.NameKey("Vulnerability", "TEST-MOD-1", nil)
if _, err := dsClient.Put(ctx, key, &vuln); err != nil {
t.Fatalf("Failed to setup test data: %v", err)
}
got, err := store.GetModified(ctx, "TEST-MOD-1")
if err != nil {
t.Fatalf("GetModified() error = %v", err)
}
if !got.Equal(now) {
t.Errorf("GetModified() got = %v, want %v", got, now)
}
}
func TestVulnerabilityStore_GetModified_NotFound(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
})
_, err := store.GetModified(ctx, "TEST-MOD-NOT-EXISTS")
if !errors.Is(err, models.ErrNotFound) {
t.Errorf("want ErrNotFound, got %v", err)
}
}
func TestVulnerabilityStore_GetModified_Batch(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
// Set a slightly longer timeout to ensure they all get batched
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
BatchTimeout: 10 * time.Millisecond,
})
now := time.Now().UTC().Truncate(time.Millisecond)
vulns := []Vulnerability{
{Modified: now},
{Modified: now.Add(time.Hour)},
}
keys := []*datastore.Key{
datastore.NameKey("Vulnerability", "TEST-BATCH-1", nil),
datastore.NameKey("Vulnerability", "TEST-BATCH-2", nil),
}
if _, err := dsClient.PutMulti(ctx, keys, vulns); err != nil {
t.Fatalf("Failed to setup test data: %v", err)
}
var wg sync.WaitGroup
wg.Add(3) // 2 exists, 1 not exists
var got1, got2 time.Time
var err1, err2, err3 error
go func() {
defer wg.Done()
got1, err1 = store.GetModified(ctx, "TEST-BATCH-1")
}()
go func() {
defer wg.Done()
got2, err2 = store.GetModified(ctx, "TEST-BATCH-2")
}()
go func() {
defer wg.Done()
_, err3 = store.GetModified(ctx, "TEST-BATCH-3") // Should be NotFound
}()
wg.Wait()
if err1 != nil {
t.Errorf("Request 1 failed: %v", err1)
} else if !got1.Equal(now) {
t.Errorf("Request 1 got %v, want %v", got1, now)
}
want2 := now.Add(time.Hour)
if err2 != nil {
t.Errorf("Request 2 failed: %v", err2)
} else if !got2.Equal(want2) {
t.Errorf("Request 2 got %v, want %v", got2, want2)
}
if !errors.Is(err3, models.ErrNotFound) {
t.Errorf("Request 3 expected ErrNotFound, got %v", err3)
}
}
func TestVulnerabilityStore_GetModified_BatchTriggerEarly(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
// Set a very long timeout but small max elements
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
BatchTimeout: 10 * time.Second, // Long timeout
BatchMaxElements: 2, // Trigger immediately on 2 elements
})
now := time.Now().UTC().Truncate(time.Millisecond)
vulns := []Vulnerability{
{Modified: now},
{Modified: now.Add(time.Hour)},
}
keys := []*datastore.Key{
datastore.NameKey("Vulnerability", "TEST-TRIGGER-1", nil),
datastore.NameKey("Vulnerability", "TEST-TRIGGER-2", nil),
}
if _, err := dsClient.PutMulti(ctx, keys, vulns); err != nil {
t.Fatalf("Failed to setup test data: %v", err)
}
var wg sync.WaitGroup
wg.Add(2)
var got1, got2 time.Time
var err1, err2 error
start := time.Now()
go func() {
defer wg.Done()
got1, err1 = store.GetModified(ctx, "TEST-TRIGGER-1")
}()
// Give it a tiny bit of time to make sure the first request is registered as leader
time.Sleep(10 * time.Millisecond)
go func() {
defer wg.Done()
got2, err2 = store.GetModified(ctx, "TEST-TRIGGER-2") // This should trigger the batch early
}()
wg.Wait()
duration := time.Since(start)
if duration > 2*time.Second {
t.Errorf("Batch took too long to complete (%v), expected early trigger", duration)
}
if err1 != nil {
t.Errorf("Request 1 failed: %v", err1)
} else if !got1.Equal(now) {
t.Errorf("Request 1 got %v, want %v", got1, now)
}
want2 := now.Add(time.Hour)
if err2 != nil {
t.Errorf("Request 2 failed: %v", err2)
} else if !got2.Equal(want2) {
t.Errorf("Request 2 got %v, want %v", got2, want2)
}
}
func TestVulnerabilityStore_Ping(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
mockGCS := testutils.NewMockStorage()
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: mockGCS,
})
// 1. Test Happy Path: Both GCS and Datastore are healthy (but empty).
// Ping should return nil because it correctly filters out ErrNotFound.
if err := store.Ping(ctx); err != nil {
t.Errorf("Expected Ping to succeed on healthy stores, got: %v", err)
}
// 2. Test GCS Outage: Mock GCS returning a real connection error.
mockGCS.ReadError = errors.New("GCS connection timeout")
if err := store.Ping(ctx); err == nil {
t.Errorf("Expected Ping to fail when GCS is unreachable, got nil")
} else if !strings.Contains(err.Error(), "gcs unreachable") {
t.Errorf("Expected GCS unreachable error, got: %v", err)
}
// 3. Test Datastore Outage: If we pass a nil client, Ping should fail
// cleanly without panicking (due to our batcher panic-safety).
brokenStore := NewVulnerabilityStore(VulnStoreConfig{
Client: nil,
GCS: mockGCS,
})
// Reset the GCS error so only Datastore is failing
mockGCS.ReadError = nil
if err := brokenStore.Ping(ctx); err == nil {
t.Errorf("Expected Ping to fail when Datastore client is nil, got nil")
} else if !strings.Contains(err.Error(), "datastore/batcher unreachable") {
t.Errorf("Expected Datastore/Batcher unreachable error, got: %v", err)
}
}
func TestVulnerabilityStore_ListModifiedSinceAndMutate(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
mockGCS := testutils.NewMockStorage()
store := NewVulnerabilityStore(VulnStoreConfig{
Client: dsClient,
GCS: mockGCS,
})
baseTime := time.Date(2026, 9, 23, 1, 0, 0, 0, time.UTC)
v1 := &osvschema.Vulnerability{
Id: "VULN-OLD",
Modified: timestamppb.New(baseTime),
Summary: "Old vuln",
}
v2 := &osvschema.Vulnerability{
Id: "VULN-NEW",
Modified: timestamppb.New(baseTime.Add(2 * time.Hour)),
Summary: "New vuln",
}
for _, v := range []*osvschema.Vulnerability{v1, v2} {
err := store.Write(ctx, models.WriteRequest{
ID: v.GetId(),
Source: "test-source",
Path: v.GetId() + ".json",
Raw: v,
Enriched: v,
AffectedCommits: models.AffectedCommitsResult{
Skip: true,
},
})
if err != nil {
t.Fatalf("store.Write(%s) failed: %v", v.GetId(), err)
}
}
since := baseTime.Add(1 * time.Hour)
var gotIDs []string
for v, err := range store.ListModifiedSince(ctx, &since) {
if err != nil {
t.Fatalf("ListModifiedSince failed: %v", err)
}
gotIDs = append(gotIDs, v.GetId())
}
if diff := cmp.Diff([]string{"VULN-NEW"}, gotIDs); diff != "" {
t.Errorf("ListModifiedSince mismatch (-want +got):\n%s", diff)
}
mutatedTime := baseTime.Add(3 * time.Hour)
updated, err := store.Mutate(ctx, "VULN-NEW", func(v *osvschema.Vulnerability) (bool, error) {
v.Aliases = []string{"CVE-2026-9999"}
v.Modified = timestamppb.New(mutatedTime)
return true, nil
})
if err != nil {
t.Fatalf("store.Mutate failed: %v", err)
}
if !updated {
t.Fatalf("expected updated=true from store.Mutate")
}
full, err := store.GetFull(ctx, "VULN-NEW")
if err != nil {
t.Fatalf("store.GetFull failed: %v", err)
}
if diff := cmp.Diff([]string{"CVE-2026-9999"}, full.GetAliases()); diff != "" {
t.Errorf("Aliases mismatch after Mutate (-want +got):\n%s", diff)
}
if !full.GetModified().AsTime().Equal(mutatedTime) {
t.Errorf("Modified after Mutate = %v, want %v", full.GetModified().AsTime(), mutatedTime)
}
}
func TestJobDataStore(t *testing.T) {
ctx := context.Background()
dsClient := testutils.MustNewDatastoreClientForTesting(t)
jobStore := NewJobDataStore(dsClient)
_, err := models.GetJobData[time.Time](ctx, jobStore, "test_last_run")
if !errors.Is(err, models.ErrNotFound) {
t.Fatalf("GetJobData on missing key err = %v, want models.ErrNotFound", err)
}
now := time.Date(2026, 9, 23, 4, 0, 0, 0, time.UTC)
if err := jobStore.Set(ctx, "test_last_run", now); err != nil {
t.Fatalf("Set failed: %v", err)
}
lr, err := models.GetJobData[time.Time](ctx, jobStore, "test_last_run")
if err != nil || !lr.Equal(now) {
t.Fatalf("GetJobData[time.Time] = (%v, %v), want %v", lr, err, now)
}
if err := jobStore.Set(ctx, "test_retry_list", []string{"A", "B"}); err != nil {
t.Fatalf("Set failed: %v", err)
}
list, err := models.GetJobData[[]string](ctx, jobStore, "test_retry_list")
if err != nil {
t.Fatalf("GetJobData[[]string] failed: %v", err)
}
if diff := cmp.Diff([]string{"A", "B"}, list); diff != "" {
t.Errorf("GetJobData[[]string] mismatch (-want +got):\n%s", diff)
}
}