Skip to content

Commit

Permalink
move storage tests to be non-internal; hasher is required for both my…
Browse files Browse the repository at this point in the history
…sql and file storage
  • Loading branch information
jessepeterson committed Jun 12, 2023
1 parent b3ca165 commit 856b665
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 26 deletions.
6 changes: 4 additions & 2 deletions cmd/kmfddm/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ type allStorage interface {
api.StatusAPIStorage
}

var hasher func() hash.Hash = func() hash.Hash { return xxhash.New() }

func storage(name, dsn string) (allStorage, error) {
switch name {
case "mysql":
return mysql.New(
hasher,
mysql.WithDSN(dsn),
mysql.WithNewHash(func() hash.Hash { return xxhash.New() }),
)
case "file":
if dsn == "" {
dsn = "db"
}
return file.New(dsn)
return file.New(dsn, hasher)
default:
return nil, fmt.Errorf("unknown storage name: %s", name)
}
Expand Down
7 changes: 5 additions & 2 deletions ddm/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ type DIBuilder struct {
}

func NewDIBuilder(newHash func() hash.Hash) *DIBuilder {
return &DIBuilder{
Hash: newHash(),
b := &DIBuilder{
DeclarationItems: DeclarationItems{
Declarations: ManifestDeclarationItems{
// init slices so they're non-nil for the JSON encoder.
Expand All @@ -60,6 +59,10 @@ func NewDIBuilder(newHash func() hash.Hash) *DIBuilder {
},
},
}
if newHash != nil {
b.Hash = newHash()
}
return b
}

func tokenHashWrite(h hash.Hash, d *Declaration) {
Expand Down
6 changes: 5 additions & 1 deletion ddm/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ type TokensBuilder struct {
}

func NewTokensBuilder(newHash func() hash.Hash) *TokensBuilder {
return &TokensBuilder{Hash: newHash()}
t := &TokensBuilder{}
if newHash != nil {
t.Hash = newHash()
}
return t
}

func (b *TokensBuilder) AddDeclarationData(d *Declaration) {
Expand Down
9 changes: 5 additions & 4 deletions storage/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"path"
"strings"
"sync"

"github.com/cespare/xxhash"
)

// File is a filesystem-based storage backend.
Expand All @@ -20,13 +18,16 @@ type File struct {
newHash func() hash.Hash
}

func New(path string) (*File, error) {
func New(path string, newHash func() hash.Hash) (*File, error) {
if err := os.Mkdir(path, 0755); err != nil && !errors.Is(err, os.ErrExist) {
return nil, err
}
if newHash == nil {
panic("newHash must not be nil")
}
return &File{
path: path,
newHash: func() hash.Hash { return xxhash.New() },
newHash: newHash,
}, nil
}

Expand Down
8 changes: 5 additions & 3 deletions storage/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ package file

import (
"context"
"hash"
"os"
"reflect"
"testing"

"github.com/jessepeterson/kmfddm/storage/internal/test"
"github.com/cespare/xxhash"
"github.com/jessepeterson/kmfddm/storage/test"
)

const testPath = "teststor"

func TestFile(t *testing.T) {
s, err := New(testPath)
s, err := New(testPath, func() hash.Hash { return xxhash.New() })
if err != nil {
t.Fatal(err)
}

test.TestBasic(t, s, context.Background())
test.TestBasicStatus(t, "../internal/test", s, context.Background())
test.TestBasicStatus(t, "../test", s, context.Background())

os.RemoveAll(testPath)
}
Expand Down
17 changes: 5 additions & 12 deletions storage/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ type MySQLStorage struct {
}

type config struct {
driver string
dsn string
db *sql.DB
newHash func() hash.Hash
driver string
dsn string
db *sql.DB
}

type Option func(*config)
Expand All @@ -41,13 +40,7 @@ func WithDB(db *sql.DB) Option {
}
}

func WithNewHash(newHash func() hash.Hash) Option {
return func(c *config) {
c.newHash = newHash
}
}

func New(opts ...Option) (*MySQLStorage, error) {
func New(newHash func() hash.Hash, opts ...Option) (*MySQLStorage, error) {
cfg := &config{driver: "mysql"}
for _, opt := range opts {
opt(cfg)
Expand All @@ -62,7 +55,7 @@ func New(opts ...Option) (*MySQLStorage, error) {
if err = cfg.db.Ping(); err != nil {
return nil, err
}
return &MySQLStorage{db: cfg.db, newHash: cfg.newHash}, nil
return &MySQLStorage{db: cfg.db, newHash: newHash}, nil
}

var ErrNotImplemented = errors.New("not implemented")
Expand Down
6 changes: 4 additions & 2 deletions storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package mysql
import (
"context"
"flag"
"hash"
"testing"

"github.com/jessepeterson/kmfddm/storage/internal/test"
"github.com/cespare/xxhash"
"github.com/jessepeterson/kmfddm/storage/test"

_ "github.com/go-sql-driver/mysql"
)
Expand All @@ -20,7 +22,7 @@ func TestMySQL(t *testing.T) {
t.Fatal("MySQL DSN flag not provided to test")
}

storage, err := New(WithDSN(*flDSN))
storage, err := New(func() hash.Hash { return xxhash.New() }, WithDSN(*flDSN))
if err != nil {
t.Fatal(err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 856b665

Please sign in to comment.