From 81bfb1154de434889c29a16b6af2b815f34b16e9 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 30 Jul 2021 18:01:45 +0200 Subject: [PATCH] test(web/file): for CreateFS --- web/file/createfs.go | 32 +++++++------ web/file/createfs_test.go | 62 ++++++++++++++++++++++++++ web/file/error.go | 10 +++++ web/file/fs/fs.go | 2 +- web/file/testdata/createfs_fs.toml | 2 + web/file/testdata/createfs_fsnone.toml | 2 + web/file/testdata/createfs_none.toml | 1 + web/file/testdata/createfs_s3.toml | 7 +++ 8 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 web/file/createfs_test.go create mode 100644 web/file/error.go create mode 100644 web/file/testdata/createfs_fs.toml create mode 100644 web/file/testdata/createfs_fsnone.toml create mode 100644 web/file/testdata/createfs_none.toml create mode 100644 web/file/testdata/createfs_s3.toml diff --git a/web/file/createfs.go b/web/file/createfs.go index ebb9802..6af0ef1 100644 --- a/web/file/createfs.go +++ b/web/file/createfs.go @@ -1,8 +1,6 @@ package file import ( - "errors" - "dev.sum7.eu/genofire/golang-lib/web/file/fs" "dev.sum7.eu/genofire/golang-lib/web/file/s3" ) @@ -23,7 +21,7 @@ var stringToType = map[string]fsType{ func (t *fsType) UnmarshalText(input []byte) error { val, ok := stringToType[string(input)] if !ok { - return errors.New("invalid file store type") + return ErrInvalidFSType } *t = val return nil @@ -31,29 +29,29 @@ func (t *fsType) UnmarshalText(input []byte) error { // FSInfo is a TOML structure storing access information about a file store. type FSInfo struct { - fstype fsType `toml:"type"` + FSType fsType `toml:"type"` // file system - root string `toml:",omitempty"` + Root string `toml:",omitempty"` // s3 - endpoint string `toml:",omitempty"` - secure bool `toml:",omitempty"` - id string `toml:",omitempty"` - secret string `toml:",omitempty"` - bucket string `toml:",omitempty"` - location string `toml:",omitempty"` + Endpoint string `toml:",omitempty"` + Secure bool `toml:",omitempty"` + ID string `toml:",omitempty"` + Secret string `toml:",omitempty"` + Bucket string `toml:",omitempty"` + Location string `toml:",omitempty"` } // Create creates a file store from the information provided. func (i *FSInfo) Create() (FS, error) { - switch i.fstype { + switch i.FSType { case typeFS: - if len(i.root) == 0 { - return nil, errors.New("no file store root") + if len(i.Root) == 0 { + return nil, ErrNoFSRoot } - return &fs.FS{Root: i.root}, nil + return &fs.FS{Root: i.Root}, nil case typeS3: - return s3.New(i.endpoint, i.secure, i.id, i.secret, i.bucket, i.location) + return s3.New(i.Endpoint, i.Secure, i.ID, i.Secret, i.Bucket, i.Location) default: - return nil, errors.New("FSInfo.Create not implemented for provided file store type") + return nil, ErrNotImplementedFSType } } diff --git a/web/file/createfs_test.go b/web/file/createfs_test.go new file mode 100644 index 0000000..67be9af --- /dev/null +++ b/web/file/createfs_test.go @@ -0,0 +1,62 @@ +package file_test + +import ( + "testing" + + fsfile "dev.sum7.eu/genofire/golang-lib/file" + "dev.sum7.eu/genofire/golang-lib/web/file" + "github.com/stretchr/testify/assert" +) + +func TestCreateFSOK(t *testing.T) { + assert := assert.New(t) + + config := file.FSInfo{} + err := fsfile.ReadTOML("testdata/createfs_fs.toml", &config) + assert.NoError(err) + + fs, err := config.Create() + assert.NoError(err) + assert.NoError(fs.Check()) +} + +func TestCreateS3(t *testing.T) { + assert := assert.New(t) + + config := file.FSInfo{} + err := fsfile.ReadTOML("testdata/createfs_s3.toml", &config) + assert.NoError(err) + + fs, err := config.Create() + assert.NoError(err) + assert.NoError(fs.Check()) +} + +func TestCreateFSNotOK(t *testing.T) { + assert := assert.New(t) + + config := file.FSInfo{} + err := fsfile.ReadTOML("testdata/createfs_fsnone.toml", &config) + assert.NoError(err) + + _, err = config.Create() + assert.ErrorIs(err, file.ErrNoFSRoot) +} + +func TestCreateFSNone(t *testing.T) { + assert := assert.New(t) + + config := file.FSInfo{} + err := fsfile.ReadTOML("testdata/createfs_none.toml", &config) + + // https://github.com/naoina/toml/pull/51 + assert.Contains(err.Error(), file.ErrInvalidFSType.Error()) +} + +func TestCreateFSInvalid(t *testing.T) { + assert := assert.New(t) + + config := file.FSInfo{} + _, err := config.Create() + assert.ErrorIs(err, file.ErrNoFSRoot) +} diff --git a/web/file/error.go b/web/file/error.go new file mode 100644 index 0000000..6dc6d67 --- /dev/null +++ b/web/file/error.go @@ -0,0 +1,10 @@ +package file + +import "errors" + +// errors +var ( + ErrInvalidFSType = errors.New("invalid file store type") + ErrNoFSRoot = errors.New("no file store root") + ErrNotImplementedFSType = errors.New("FSInfo.Create not implemented for provided file store type") +) diff --git a/web/file/fs/fs.go b/web/file/fs/fs.go index 858e38e..a68d455 100644 --- a/web/file/fs/fs.go +++ b/web/file/fs/fs.go @@ -85,7 +85,7 @@ func (fs *FS) OpenUUID(id uuid.UUID) (fs.File, error) { if err != nil { return nil, fmt.Errorf("open data: %w", err) } - return File{File: f, fs: fs, id: uuid.MustParse(id.String())}, nil + return File{File: f, fs: fs, id: id}, nil } // Open searches for and opens the file with the given name. diff --git a/web/file/testdata/createfs_fs.toml b/web/file/testdata/createfs_fs.toml new file mode 100644 index 0000000..83b2790 --- /dev/null +++ b/web/file/testdata/createfs_fs.toml @@ -0,0 +1,2 @@ +type = "fs" +root = "fs/testdata" diff --git a/web/file/testdata/createfs_fsnone.toml b/web/file/testdata/createfs_fsnone.toml new file mode 100644 index 0000000..f081110 --- /dev/null +++ b/web/file/testdata/createfs_fsnone.toml @@ -0,0 +1,2 @@ +type = "fs" +root = "" diff --git a/web/file/testdata/createfs_none.toml b/web/file/testdata/createfs_none.toml new file mode 100644 index 0000000..3d13e66 --- /dev/null +++ b/web/file/testdata/createfs_none.toml @@ -0,0 +1 @@ +type = "notsupported" diff --git a/web/file/testdata/createfs_s3.toml b/web/file/testdata/createfs_s3.toml new file mode 100644 index 0000000..a5db086 --- /dev/null +++ b/web/file/testdata/createfs_s3.toml @@ -0,0 +1,7 @@ +type = "s3" +endpoint = "play.min.io" +secure = true +id = "Q3AM3UQ867SPQQA43P2F" +secret = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" +bucket = "file-store" +location = ""