Skip to content

Commit

Permalink
Add tests for getBucketSummary. (#2310)
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <sschrock@google.com>

Signed-off-by: Spencer Schrock <sschrock@google.com>
  • Loading branch information
spencerschrock authored Sep 28, 2022
1 parent ac55bf4 commit 347c2a8
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 0 deletions.
68 changes: 68 additions & 0 deletions cron/internal/bq/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
package main

import (
"context"
"path/filepath"
"reflect"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func TestIsCompleted(t *testing.T) {
Expand Down Expand Up @@ -60,3 +67,64 @@ func TestIsCompleted(t *testing.T) {
})
}
}

func TestGetBucketSummary(t *testing.T) {
t.Parallel()
//nolint:govet
testcases := []struct {
name string
blobPath string
want *bucketSummary
wantErr bool
}{
{
name: "basic",
blobPath: "testdata/basic",
want: &bucketSummary{
shards: map[time.Time]*shardSummary{
time.Date(2022, 9, 19, 2, 0, 1, 0, time.UTC): {
shardMetadata: []byte(`{"shardLoc":"test","numShard":3,"commitSha":"2231d1f722454c6c9aa6ad77377d2936803216ff"}`),
shardsExpected: 3,
shardsCreated: 2,
isTransferred: true,
},
time.Date(2022, 9, 26, 2, 0, 3, 0, time.UTC): {
shardMetadata: []byte(`{"shardLoc":"test","numShard":5,"commitSha":"2231d1f722454c6c9aa6ad77377d2936803216ff"}`),
shardsExpected: 5,
shardsCreated: 3,
isTransferred: false,
},
},
},
wantErr: false,
},
{
name: "invalid file present",
blobPath: "testdata/invalid",
want: nil,
wantErr: true,
},
}

exporter := func(t reflect.Type) bool { return strings.HasPrefix(t.PkgPath(), "github.com/ossf/scorecard") }

for i := range testcases {
tt := &testcases[i]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// convert local to absolute path, which is needed for the fileblob bucket
testdataPath, err := filepath.Abs(tt.blobPath)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
summary, err := getBucketSummary(context.Background(), "file:///"+testdataPath)
if (err != nil) != tt.wantErr {
t.Errorf("getBucketSummary() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(summary, tt.want, cmp.Exporter(exporter)) {
t.Errorf("Got diff: %s", cmp.Diff(summary, tt.want, cmp.Exporter(exporter)))
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"shardLoc":"test","numShard":3,"commitSha":"2231d1f722454c6c9aa6ad77377d2936803216ff"}
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"shardLoc":"test","numShard":5,"commitSha":"2231d1f722454c6c9aa6ad77377d2936803216ff"}
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions cron/internal/data/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"time"

"gocloud.dev/blob"
// Needed to read file:/// buckets. Intended primarily for testing, though needed here for tests outside the package.
_ "gocloud.dev/blob/fileblob"
// Needed to link in GCP drivers.
_ "gocloud.dev/blob/gcsblob"

Expand Down

0 comments on commit 347c2a8

Please sign in to comment.