aboutsummaryrefslogtreecommitdiffstats
path: root/dashboard/app
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2025-02-07 09:48:29 +0100
committerTaras Madan <tarasmadan@google.com>2025-02-07 09:48:29 +0100
commita4f327c25a415765c6cca57d7d9fb572861fe158 (patch)
treecd2e05bc3121b8b547c421415e33f794c2119c89 /dashboard/app
parentaf3a292efbd8ef2f7432090ef699e9ea2a2453eb (diff)
dashboard/app: test jsonl coverage generation
Diffstat (limited to 'dashboard/app')
-rw-r--r--dashboard/app/coverage_test.go4
-rw-r--r--dashboard/app/public_json_api_test.go82
2 files changed, 84 insertions, 2 deletions
diff --git a/dashboard/app/coverage_test.go b/dashboard/app/coverage_test.go
index c4945ab09..516409228 100644
--- a/dashboard/app/coverage_test.go
+++ b/dashboard/app/coverage_test.go
@@ -148,7 +148,7 @@ func multiManagerCovDBFixture(t *testing.T) spannerclient.SpannerClient {
return m
}
-func newRowIteratorMock(t *testing.T, cov []*coveragedb.LinesCoverage,
+func newRowIteratorMock[K any](t *testing.T, cov []*K,
) *mocks.RowIterator {
m := mocks.NewRowIterator(t)
m.On("Stop").Once().Return()
@@ -156,7 +156,7 @@ func newRowIteratorMock(t *testing.T, cov []*coveragedb.LinesCoverage,
mRow := mocks.NewRow(t)
mRow.On("ToStruct", mock.Anything).
Run(func(args mock.Arguments) {
- arg := args.Get(0).(*coveragedb.LinesCoverage)
+ arg := args.Get(0).(*K)
*arg = *item
}).
Return(nil).Once()
diff --git a/dashboard/app/public_json_api_test.go b/dashboard/app/public_json_api_test.go
index 82cf9c320..6cf9b6afe 100644
--- a/dashboard/app/public_json_api_test.go
+++ b/dashboard/app/public_json_api_test.go
@@ -4,11 +4,18 @@
package main
import (
+ "bytes"
+ "context"
"fmt"
"testing"
"github.com/google/syzkaller/dashboard/api"
"github.com/google/syzkaller/dashboard/dashapi"
+ "github.com/google/syzkaller/pkg/coveragedb"
+ "github.com/google/syzkaller/pkg/coveragedb/mocks"
+ "github.com/google/syzkaller/pkg/coveragedb/spannerclient"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/mock"
)
func TestJSONAPIIntegration(t *testing.T) {
@@ -250,3 +257,78 @@ func TestPublicJSONAPI(t *testing.T) {
c.expectOK(err)
c.expectEQ(config, []byte("config1"))
}
+
+func TestWriteExtAPICoverageFor(t *testing.T) {
+ ctx := SetCoverageDBClient(context.Background(), fileFuncLinesDBFixture(t,
+ []*coveragedb.FuncLines{
+ {
+ FilePath: "/file",
+ FuncName: "func_name",
+ Lines: []int64{1, 2, 3},
+ },
+ },
+ []*coveragedb.FileCoverageWithLineInfo{
+ {
+ FileCoverageWithDetails: coveragedb.FileCoverageWithDetails{
+ Filepath: "/file",
+ },
+ LinesInstrumented: []int64{1, 2, 3},
+ HitCounts: []int64{10, 20, 30},
+ },
+ },
+ ))
+
+ var buf bytes.Buffer
+ err := writeExtAPICoverageFor(ctx, &buf, "test-ns", "test-repo")
+ assert.NoError(t, err)
+ assert.Equal(t, `{
+ "repo": "test-repo",
+ "commit": "",
+ "file_path": "/file",
+ "functions": [
+ {
+ "func_name": "func_name",
+ "total_blocks": 3,
+ "covered_blocks": [
+ {
+ "from_line": 1,
+ "from_column": 0,
+ "to_line": 1,
+ "to_column": -1
+ },
+ {
+ "from_line": 2,
+ "from_column": 0,
+ "to_line": 2,
+ "to_column": -1
+ },
+ {
+ "from_line": 3,
+ "from_column": 0,
+ "to_line": 3,
+ "to_column": -1
+ }
+ ]
+ }
+ ]
+}
+`, buf.String())
+}
+
+func fileFuncLinesDBFixture(t *testing.T, funcLines []*coveragedb.FuncLines,
+ fileCovWithLineInfo []*coveragedb.FileCoverageWithLineInfo) spannerclient.SpannerClient {
+ mPartialTran := mocks.NewReadOnlyTransaction(t)
+ mPartialTran.On("Query", mock.Anything, mock.Anything).
+ Return(newRowIteratorMock(t, funcLines)).Once()
+
+ mFullTran := mocks.NewReadOnlyTransaction(t)
+ mFullTran.On("Query", mock.Anything, mock.Anything).
+ Return(newRowIteratorMock(t, fileCovWithLineInfo)).Once()
+
+ m := mocks.NewSpannerClient(t)
+ m.On("Single").
+ Return(mPartialTran).Once()
+ m.On("Single").
+ Return(mFullTran).Once()
+ return m
+}