From 856b665d83ec0919bd0f2049525e29e9806668ba Mon Sep 17 00:00:00 2001 From: Jesse Peterson Date: Mon, 12 Jun 2023 09:43:45 -0700 Subject: [PATCH] move storage tests to be non-internal; hasher is required for both mysql and file storage --- cmd/kmfddm/storage.go | 6 ++++-- ddm/items.go | 7 +++++-- ddm/token.go | 6 +++++- storage/file/file.go | 9 +++++---- storage/file/file_test.go | 8 +++++--- storage/mysql/mysql.go | 17 +++++------------ storage/mysql/mysql_test.go | 6 ++++-- storage/{internal => }/test/basic.go | 0 storage/{internal => }/test/declarations.go | 0 storage/{internal => }/test/enrollments.go | 0 storage/{internal => }/test/sets.go | 0 storage/{internal => }/test/status.go | 0 .../test/testdata/status.1st.json | 0 .../test/testdata/status.D0.error.json | 0 14 files changed, 33 insertions(+), 26 deletions(-) rename storage/{internal => }/test/basic.go (100%) rename storage/{internal => }/test/declarations.go (100%) rename storage/{internal => }/test/enrollments.go (100%) rename storage/{internal => }/test/sets.go (100%) rename storage/{internal => }/test/status.go (100%) rename storage/{internal => }/test/testdata/status.1st.json (100%) rename storage/{internal => }/test/testdata/status.D0.error.json (100%) diff --git a/cmd/kmfddm/storage.go b/cmd/kmfddm/storage.go index 78bf7a0..28f355f 100644 --- a/cmd/kmfddm/storage.go +++ b/cmd/kmfddm/storage.go @@ -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) } diff --git a/ddm/items.go b/ddm/items.go index 1081e5b..76e1399 100644 --- a/ddm/items.go +++ b/ddm/items.go @@ -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. @@ -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) { diff --git a/ddm/token.go b/ddm/token.go index f297c29..5af8401 100644 --- a/ddm/token.go +++ b/ddm/token.go @@ -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) { diff --git a/storage/file/file.go b/storage/file/file.go index 82f665e..685de86 100644 --- a/storage/file/file.go +++ b/storage/file/file.go @@ -9,8 +9,6 @@ import ( "path" "strings" "sync" - - "github.com/cespare/xxhash" ) // File is a filesystem-based storage backend. @@ -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 } diff --git a/storage/file/file_test.go b/storage/file/file_test.go index aa91c7b..e026bcc 100644 --- a/storage/file/file_test.go +++ b/storage/file/file_test.go @@ -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) } diff --git a/storage/mysql/mysql.go b/storage/mysql/mysql.go index dbca47e..ec41d22 100644 --- a/storage/mysql/mysql.go +++ b/storage/mysql/mysql.go @@ -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) @@ -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) @@ -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") diff --git a/storage/mysql/mysql_test.go b/storage/mysql/mysql_test.go index cac6de7..08128eb 100644 --- a/storage/mysql/mysql_test.go +++ b/storage/mysql/mysql_test.go @@ -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" ) @@ -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) } diff --git a/storage/internal/test/basic.go b/storage/test/basic.go similarity index 100% rename from storage/internal/test/basic.go rename to storage/test/basic.go diff --git a/storage/internal/test/declarations.go b/storage/test/declarations.go similarity index 100% rename from storage/internal/test/declarations.go rename to storage/test/declarations.go diff --git a/storage/internal/test/enrollments.go b/storage/test/enrollments.go similarity index 100% rename from storage/internal/test/enrollments.go rename to storage/test/enrollments.go diff --git a/storage/internal/test/sets.go b/storage/test/sets.go similarity index 100% rename from storage/internal/test/sets.go rename to storage/test/sets.go diff --git a/storage/internal/test/status.go b/storage/test/status.go similarity index 100% rename from storage/internal/test/status.go rename to storage/test/status.go diff --git a/storage/internal/test/testdata/status.1st.json b/storage/test/testdata/status.1st.json similarity index 100% rename from storage/internal/test/testdata/status.1st.json rename to storage/test/testdata/status.1st.json diff --git a/storage/internal/test/testdata/status.D0.error.json b/storage/test/testdata/status.D0.error.json similarity index 100% rename from storage/internal/test/testdata/status.D0.error.json rename to storage/test/testdata/status.D0.error.json