Skip to content

Commit

Permalink
Tests - Additional sharding tests
Browse files Browse the repository at this point in the history
Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
  • Loading branch information
naveensrinivasan committed Nov 6, 2022
1 parent 05d92d3 commit 66bbc31
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ require (
google.golang.org/api v0.100.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
)
123 changes: 118 additions & 5 deletions pkg/sharding/ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ package sharding

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

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

func TestNewLogRanges(t *testing.T) {
Expand All @@ -34,7 +36,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 +50,8 @@ func TestNewLogRanges(t *testing.T) {
}, {
TreeID: 2,
TreeLength: 4,
}},
},
},
active: int64(45),
}
ctx := context.Background()
Expand All @@ -73,7 +76,7 @@ func TestLogRangesFromPath(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)
}
expected := Ranges{
Expand All @@ -99,7 +102,7 @@ func TestLogRangesFromPath(t *testing.T) {
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 {
if err := os.WriteFile(file, []byte(contents), 0o644); err != nil {
t.Fatal(err)
}
expected := Ranges{
Expand Down Expand Up @@ -127,7 +130,8 @@ func TestLogRanges_ResolveVirtualIndex(t *testing.T) {
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 +392,112 @@ func TestLogRanges_AllShards(t *testing.T) {
})
}
}

func Test_logRangesFromPath(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)
}
defer os.Remove(f.Name())
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())
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)
}
})
}
}

0 comments on commit 66bbc31

Please sign in to comment.