3
0

Extend store to accept other afero.Fs structs

This enables us to write better tests, without
poluting the FS
This commit is contained in:
Denis Arh
2019-09-09 03:01:45 +02:00
parent 73e0180600
commit 437127448a
4 changed files with 48 additions and 28 deletions
-1
View File
@@ -1 +0,0 @@
/test
+30 -17
View File
@@ -11,6 +11,8 @@ import (
type (
store struct {
fs afero.Fs
namespace string
originalFn func(id uint64, ext string) string
@@ -29,15 +31,27 @@ type (
}
)
func New(namespace string) (Store, error) {
var (
defPreviewFn = func(id uint64, ext string) string {
return fmt.Sprintf("%d_preview.%s", id, ext)
}
defOriginalFn = func(id uint64, ext string) string {
return fmt.Sprintf("%d.%s", id, ext)
}
)
func New(namespace string) (*store, error) {
return NewWithAfero(afero.NewOsFs(), namespace)
}
func NewWithAfero(fs afero.Fs, namespace string) (*store, error) {
return &store{
fs: fs,
namespace: namespace,
originalFn: func(id uint64, ext string) string {
return fmt.Sprintf("%d.%s", id, ext)
},
previewFn: func(id uint64, ext string) string {
return fmt.Sprintf("%d_preview.%s", id, ext)
},
originalFn: defOriginalFn,
previewFn: defPreviewFn,
}, nil
}
@@ -65,18 +79,19 @@ func (s *store) Preview(id uint64, ext string) string {
return path.Join(s.namespace, s.previewFn(id, ext))
}
func (s *store) Save(filename string, contents io.Reader) error {
func (s *store) Save(filename string, contents io.Reader) (err error) {
// check filename for validity
if err := s.check(filename); err != nil {
return err
if err = s.check(filename); err != nil {
return
}
folder := path.Dir(filename)
fs := afero.NewOsFs()
fs.MkdirAll(folder, 0755)
if err = s.fs.MkdirAll(folder, 0755); err != nil {
return
}
return afero.WriteReader(fs, filename, contents)
return afero.WriteReader(s.fs, filename, contents)
}
func (s *store) Remove(filename string) error {
@@ -85,8 +100,7 @@ func (s *store) Remove(filename string) error {
return err
}
fs := afero.NewOsFs()
return fs.Remove(filename)
return s.fs.Remove(filename)
}
func (s *store) Open(filename string) (afero.File, error) {
@@ -95,6 +109,5 @@ func (s *store) Open(filename string) (afero.File, error) {
return nil, err
}
fs := afero.NewOsFs()
return fs.Open(filename)
return s.fs.Open(filename)
}
+4 -1
View File
@@ -5,6 +5,8 @@ import (
"io"
"testing"
"github.com/spf13/afero"
"github.com/cortezaproject/corteza-server/internal/test"
)
@@ -15,7 +17,8 @@ func TestStore(t *testing.T) {
return b.String()
}
store, err := New("test")
store, err := NewWithAfero(afero.NewMemMapFs(), "test")
test.Assert(t, err == nil, "Unexpected error when creating store: %+v", err)
test.Assert(t, store != nil, "Expected non-nil return for new store")
test.Assert(t, store.Namespace() == "test", "Unexpected store namespace: test != %s", store.Namespace())
+14 -9
View File
@@ -29,6 +29,7 @@ type (
)
var (
DefaultStore store.Store
DefaultPermissions permissionServicer
DefaultLogger *zap.Logger
@@ -46,10 +47,12 @@ var (
func Init(ctx context.Context, log *zap.Logger, c Config) (err error) {
DefaultLogger = log.Named("service")
fs, err := store.New(c.Storage.Path)
log.Info("initializing store", zap.String("path", c.Storage.Path), zap.Error(err))
if err != nil {
return err
if DefaultStore == nil {
DefaultStore, err = store.New(c.Storage.Path)
log.Info("initializing store", zap.String("path", c.Storage.Path), zap.Error(err))
if err != nil {
return err
}
}
client, err := http.New(&http.Config{
@@ -59,15 +62,17 @@ func Init(ctx context.Context, log *zap.Logger, c Config) (err error) {
return err
}
DefaultPermissions = permissions.Service(
ctx,
DefaultLogger,
permissions.Repository(repository.DB(ctx), "messaging_permission_rules"))
if DefaultPermissions == nil {
// Do not override permissions service stored under DefaultPermissions
// to allow integration tests to inject own permission service
pRepo := permissions.Repository(repository.DB(ctx), "messaging_permission_rules")
DefaultPermissions = permissions.Service(ctx, DefaultLogger, pRepo)
}
DefaultAccessControl = AccessControl(DefaultPermissions)
DefaultEvent = Event(ctx)
DefaultChannel = Channel(ctx)
DefaultAttachment = Attachment(ctx, fs)
DefaultAttachment = Attachment(ctx, DefaultStore)
DefaultMessage = Message(ctx)
DefaultCommand = Command(ctx)
DefaultWebhook = Webhook(ctx, client)