test(web/file): for CreateFS

This commit is contained in:
Martin 2021-07-30 18:01:45 +02:00
parent 6db99dd2bb
commit 81bfb1154d
No known key found for this signature in database
GPG Key ID: 88B64E3BE097CFAC
8 changed files with 100 additions and 18 deletions

View File

@ -1,8 +1,6 @@
package file package file
import ( import (
"errors"
"dev.sum7.eu/genofire/golang-lib/web/file/fs" "dev.sum7.eu/genofire/golang-lib/web/file/fs"
"dev.sum7.eu/genofire/golang-lib/web/file/s3" "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 { func (t *fsType) UnmarshalText(input []byte) error {
val, ok := stringToType[string(input)] val, ok := stringToType[string(input)]
if !ok { if !ok {
return errors.New("invalid file store type") return ErrInvalidFSType
} }
*t = val *t = val
return nil 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. // FSInfo is a TOML structure storing access information about a file store.
type FSInfo struct { type FSInfo struct {
fstype fsType `toml:"type"` FSType fsType `toml:"type"`
// file system // file system
root string `toml:",omitempty"` Root string `toml:",omitempty"`
// s3 // s3
endpoint string `toml:",omitempty"` Endpoint string `toml:",omitempty"`
secure bool `toml:",omitempty"` Secure bool `toml:",omitempty"`
id string `toml:",omitempty"` ID string `toml:",omitempty"`
secret string `toml:",omitempty"` Secret string `toml:",omitempty"`
bucket string `toml:",omitempty"` Bucket string `toml:",omitempty"`
location string `toml:",omitempty"` Location string `toml:",omitempty"`
} }
// Create creates a file store from the information provided. // Create creates a file store from the information provided.
func (i *FSInfo) Create() (FS, error) { func (i *FSInfo) Create() (FS, error) {
switch i.fstype { switch i.FSType {
case typeFS: case typeFS:
if len(i.root) == 0 { if len(i.Root) == 0 {
return nil, errors.New("no file store root") return nil, ErrNoFSRoot
} }
return &fs.FS{Root: i.root}, nil return &fs.FS{Root: i.Root}, nil
case typeS3: 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: default:
return nil, errors.New("FSInfo.Create not implemented for provided file store type") return nil, ErrNotImplementedFSType
} }
} }

62
web/file/createfs_test.go Normal file
View File

@ -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)
}

10
web/file/error.go Normal file
View File

@ -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")
)

View File

@ -85,7 +85,7 @@ func (fs *FS) OpenUUID(id uuid.UUID) (fs.File, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("open data: %w", err) 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. // Open searches for and opens the file with the given name.

2
web/file/testdata/createfs_fs.toml vendored Normal file
View File

@ -0,0 +1,2 @@
type = "fs"
root = "fs/testdata"

View File

@ -0,0 +1,2 @@
type = "fs"
root = ""

1
web/file/testdata/createfs_none.toml vendored Normal file
View File

@ -0,0 +1 @@
type = "notsupported"

7
web/file/testdata/createfs_s3.toml vendored Normal file
View File

@ -0,0 +1,7 @@
type = "s3"
endpoint = "play.min.io"
secure = true
id = "Q3AM3UQ867SPQQA43P2F"
secret = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
bucket = "file-store"
location = ""