Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests - Additional sharding tests #1180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1
github.com/golang/mock v1.6.0
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-retryablehttp v0.7.2
golang.org/x/exp v0.0.0-20220823124025-807a23277127
Expand Down Expand Up @@ -139,7 +140,7 @@ require (
google.golang.org/api v0.108.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
301 changes: 241 additions & 60 deletions pkg/sharding/ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ package sharding

import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/google/trillian/testonly"

"github.com/google/trillian"
"google.golang.org/grpc"
"gopkg.in/yaml.v2"
)

func TestNewLogRanges(t *testing.T) {
Expand All @@ -34,7 +40,7 @@ func TestNewLogRanges(t *testing.T) {
- treeID: 0002
treeLength: 4`
file := filepath.Join(t.TempDir(), "sharding-config")
if err := os.WriteFile(file, []byte(contents), 0644); err != nil {
if err := os.WriteFile(file, []byte(contents), 0o644); err != nil {
t.Fatal(err)
}
treeID := uint(45)
Expand All @@ -48,7 +54,8 @@ func TestNewLogRanges(t *testing.T) {
}, {
TreeID: 2,
TreeLength: 4,
}},
},
},
active: int64(45),
}
ctx := context.Background()
Expand All @@ -65,69 +72,13 @@ func TestNewLogRanges(t *testing.T) {
}
}

func TestLogRangesFromPath(t *testing.T) {
contents := `
- treeID: 0001
treeLength: 3
encodedPublicKey: c2hhcmRpbmcK
- treeID: 0002
treeLength: 4`
file := filepath.Join(t.TempDir(), "sharding-config")
if err := os.WriteFile(file, []byte(contents), 0644); err != nil {
t.Fatal(err)
}
expected := Ranges{
{
TreeID: 1,
TreeLength: 3,
EncodedPublicKey: "c2hhcmRpbmcK",
}, {
TreeID: 2,
TreeLength: 4,
},
}

got, err := logRangesFromPath(file)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
t.Fatalf("expected %v got %v", expected, got)
}
}

func TestLogRangesFromPathJSON(t *testing.T) {
contents := `[{"treeID": 0001, "treeLength": 3, "encodedPublicKey":"c2hhcmRpbmcK"}, {"treeID": 0002, "treeLength": 4}]`
file := filepath.Join(t.TempDir(), "sharding-config")
if err := os.WriteFile(file, []byte(contents), 0644); err != nil {
t.Fatal(err)
}
expected := Ranges{
{
TreeID: 1,
TreeLength: 3,
EncodedPublicKey: "c2hhcmRpbmcK",
}, {
TreeID: 2,
TreeLength: 4,
},
}

got, err := logRangesFromPath(file)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
t.Fatalf("expected %v got %v", expected, got)
}
}

func TestLogRanges_ResolveVirtualIndex(t *testing.T) {
lrs := LogRanges{
inactive: []LogRange{
{TreeID: 1, TreeLength: 17},
{TreeID: 2, TreeLength: 1},
{TreeID: 3, TreeLength: 100}},
{TreeID: 3, TreeLength: 100},
},
active: 4,
}

Expand Down Expand Up @@ -388,3 +339,233 @@ func TestLogRanges_AllShards(t *testing.T) {
})
}
}

func TestLogRangesFromPath(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want Ranges
content string
wantJSON bool
wantYaml bool
wantInvalidJSON bool
wantErr bool
}{
{
name: "empty",
args: args{
path: "",
},
want: Ranges{},
wantErr: true,
},
{
name: "empty file",
args: args{
path: "one",
},
want: Ranges{},
wantErr: false,
},
{
name: "valid json",
args: args{
path: "one",
},
want: Ranges{
{
TreeID: 1,
TreeLength: 2,
},
},
wantJSON: true,
wantErr: false,
},
{
name: "valid yaml",
args: args{
path: "one",
},
want: Ranges{
{
TreeID: 1,
TreeLength: 2,
},
},
wantYaml: true,
wantErr: false,
},
{
name: "invalid json",
args: args{
path: "one",
},
want: Ranges{},
wantInvalidJSON: true,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.path != "" {
f, err := os.CreateTemp("", tt.args.path)
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
switch {
case tt.wantJSON:
if err := json.NewEncoder(f).Encode(tt.want); err != nil {
t.Fatalf("Failed to encode json: %v", err)
}
case tt.wantYaml:
if err := yaml.NewEncoder(f).Encode(tt.want); err != nil {
t.Fatalf("Failed to encode yaml: %v", err)
}
case tt.wantInvalidJSON:
if _, err := f.WriteString("invalid json"); err != nil {
t.Fatalf("Failed to write invalid json: %v", err)
}
}
if _, err := f.Write([]byte(tt.content)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
defer f.Close()
defer os.Remove(f.Name())
naveensrinivasan marked this conversation as resolved.
Show resolved Hide resolved
tt.args.path = f.Name()
}
got, err := logRangesFromPath(tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("logRangesFromPath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("logRangesFromPath() got = %v, want %v", got, tt.want)
}
})
}
}

func TestUpdateRange(t *testing.T) {
type args struct {
ctx context.Context
r LogRange
}
tests := []struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about testing a case where wantErr == false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what I could pass to this func not to return an error.

if err := root.UnmarshalBinary(resp.SignedLogRoot.LogRoot); err != nil {
			return LogRange{}, err
		}

I am mocking the call.

name string
args args
want LogRange
wantErr bool
rootResponse *trillian.GetLatestSignedLogRootResponse
signedLogError error
}{
{
name: "empty",
args: args{
ctx: context.Background(),
r: LogRange{},
},
want: LogRange{},
wantErr: true,
rootResponse: &trillian.GetLatestSignedLogRootResponse{
SignedLogRoot: &trillian.SignedLogRoot{},
},
signedLogError: nil,
},
{
name: "error in GetLatestSignedLogRoot",
args: args{
ctx: context.Background(),
r: LogRange{},
},
want: LogRange{},
wantErr: true,
rootResponse: &trillian.GetLatestSignedLogRootResponse{
SignedLogRoot: &trillian.SignedLogRoot{},
},
signedLogError: errors.New("error"),
},
}

mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, fakeServer, err := testonly.NewMockServer(mockCtl)
if err != nil {
t.Fatalf("Failed to create mock server: %v", err)
}
defer fakeServer()

s.Log.EXPECT().GetLatestSignedLogRoot(
gomock.Any(), gomock.Any()).Return(tt.rootResponse, tt.signedLogError).AnyTimes()
got, err := updateRange(tt.args.ctx, s.LogClient, tt.args.r)

if (err != nil) != tt.wantErr {
t.Errorf("updateRange() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("updateRange() got = %v, want %v", got, tt.want)
}
})
}
}

func TestNewLogRangesWithMock(t *testing.T) {
type args struct {
ctx context.Context
path string
treeID uint
}
tests := []struct {
name string
args args
want LogRanges
wantErr bool
}{
{
name: "empty path",
args: args{
ctx: context.Background(),
path: "",
treeID: 1,
},
want: LogRanges{},
wantErr: false,
},
{
name: "treeID 0",
args: args{
ctx: context.Background(),
path: "x",
treeID: 0,
},
want: LogRanges{},
wantErr: true,
},
}

mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

s, fakeServer, err := testonly.NewMockServer(mockCtl)
if err != nil {
t.Fatalf("Failed to create mock server: %v", err)
}
defer fakeServer()
got, err := NewLogRanges(tt.args.ctx, s.LogClient, tt.args.path, tt.args.treeID)
if (err != nil) != tt.wantErr {
t.Errorf("NewLogRanges() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewLogRanges() got = %v, want %v", got, tt.want)
}
})
}
}