diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2025-04-08 16:27:49 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2025-04-10 10:37:46 +0000 |
| commit | c8f1c38aba5ba9a902386908635f6e69c85cfb01 (patch) | |
| tree | 9f0114b33f43b4cda7a2511f77b90d848a4e4ee1 | |
| parent | e63481ffe5d7a5688b7422c02445312b5e9046eb (diff) | |
syz-cluster/pkg/db: add a helper class
In the tests, we often spawn different dummy objects.
Add a separate helper class to avoid duplicating this code.
| -rw-r--r-- | syz-cluster/pkg/db/finding_repo_test.go | 17 | ||||
| -rw-r--r-- | syz-cluster/pkg/db/util_test.go | 67 |
2 files changed, 70 insertions, 14 deletions
diff --git a/syz-cluster/pkg/db/finding_repo_test.go b/syz-cluster/pkg/db/finding_repo_test.go index f4668ad22..40ca385ac 100644 --- a/syz-cluster/pkg/db/finding_repo_test.go +++ b/syz-cluster/pkg/db/finding_repo_test.go @@ -6,34 +6,23 @@ package db import ( "testing" - "github.com/google/syzkaller/syz-cluster/pkg/api" "github.com/stretchr/testify/assert" ) func TestFindingRepo(t *testing.T) { client, ctx := NewTransientDB(t) - sessionRepo := NewSessionRepository(client) seriesRepo := NewSeriesRepository(client) findingRepo := NewFindingRepository(client) - testsRepo := NewSessionTestRepository(client) + dtd := &dummyTestData{t, ctx, client} series := &Series{ExtID: "some-series"} err := seriesRepo.Insert(ctx, series, nil) assert.NoError(t, err) - session := &Session{SeriesID: series.ID} - err = sessionRepo.Insert(ctx, session) - assert.NoError(t, err) + session := dtd.dummySession(series) // Add test steps. - for _, name := range []string{"first", "second"} { - err = testsRepo.InsertOrUpdate(ctx, &SessionTest{ - SessionID: session.ID, - TestName: name, - Result: api.TestPassed, - }) - assert.NoError(t, err) - } + dtd.addSessionTest(session, "first", "second") // Add findings. toInsert := []*Finding{ diff --git a/syz-cluster/pkg/db/util_test.go b/syz-cluster/pkg/db/util_test.go new file mode 100644 index 000000000..f33f1e221 --- /dev/null +++ b/syz-cluster/pkg/db/util_test.go @@ -0,0 +1,67 @@ +// Copyright 2025 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package db + +import ( + "context" + "testing" + "time" + + "cloud.google.com/go/spanner" + "github.com/google/syzkaller/syz-cluster/pkg/api" + "github.com/stretchr/testify/assert" +) + +type dummyTestData struct { + t *testing.T + ctx context.Context + client *spanner.Client +} + +func (d *dummyTestData) addSessionTest(session *Session, names ...string) { + testsRepo := NewSessionTestRepository(d.client) + for _, name := range names { + err := testsRepo.InsertOrUpdate(d.ctx, &SessionTest{ + SessionID: session.ID, + TestName: name, + Result: api.TestPassed, + }) + assert.NoError(d.t, err) + } +} + +func (d *dummyTestData) dummySession(series *Series) *Session { + sessionRepo := NewSessionRepository(d.client) + session := &Session{ + SeriesID: series.ID, + CreatedAt: time.Now(), + } + err := sessionRepo.Insert(d.ctx, session) + assert.NoError(d.t, err) + return session +} + +func (d *dummyTestData) startSession(session *Session) { + sessionRepo := NewSessionRepository(d.client) + err := sessionRepo.Start(d.ctx, session.ID) + assert.NoError(d.t, err) +} + +func (d *dummyTestData) finishSession(session *Session) { + sessionRepo := NewSessionRepository(d.client) + err := sessionRepo.Update(d.ctx, session.ID, func(session *Session) error { + session.SetFinishedAt(time.Now()) + return nil + }) + assert.NoError(d.t, err) +} + +func (d *dummyTestData) addFinding(session *Session, title, test string) { + findingRepo := NewFindingRepository(d.client) + assert.NoError(d.t, findingRepo.Save(d.ctx, &Finding{ + SessionID: session.ID, + Title: title, + TestName: test, + })) +} |
