Skip to content

Commit b80c772

Browse files
author
Graham Forest
authored
Segregate optionally based on bucket, fix tests (#99)
- If we have a bucket, use it in the path so we don't stomp other buckets' files - Also make localfs tests parallel compatible - Use require more
1 parent 9a03fef commit b80c772

2 files changed

Lines changed: 57 additions & 36 deletions

File tree

localfs/store.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func init() {
2626
cloudstorage.Register(StoreType, localProvider)
2727
}
2828
func localProvider(conf *cloudstorage.Config) (cloudstorage.Store, error) {
29-
store, err := NewLocalStore(conf.LocalFS, conf.TmpDir)
29+
store, err := NewLocalStore(conf.Bucket, conf.LocalFS, conf.TmpDir)
3030
if err != nil {
3131
return nil, err
3232
}
@@ -48,14 +48,13 @@ const (
4848

4949
// LocalStore is client to local-filesystem store.
5050
type LocalStore struct {
51-
storepath string // possibly is relative ./tables
52-
pathCleaned string // cleaned removing ./ = "tables"
53-
cachepath string
54-
Id string
51+
storepath string // possibly is relative ./tables
52+
cachepath string
53+
Id string
5554
}
5655

5756
// NewLocalStore create local store from storage path on local filesystem, and cachepath.
58-
func NewLocalStore(storepath, cachepath string) (*LocalStore, error) {
57+
func NewLocalStore(bucket, storepath, cachepath string) (*LocalStore, error) {
5958

6059
if storepath == "" {
6160
return nil, fmt.Errorf("storepath=%q cannot be empty", storepath)
@@ -65,7 +64,7 @@ func NewLocalStore(storepath, cachepath string) (*LocalStore, error) {
6564
return nil, fmt.Errorf("storepath=%q cannot be the same as cachepath=%q", storepath, cachepath)
6665
}
6766

68-
pathCleaned := strings.TrimPrefix(storepath, "./")
67+
storepath = filepath.Join(storepath, bucket)
6968

7069
err := os.MkdirAll(storepath, 0775)
7170
if err != nil {
@@ -81,10 +80,9 @@ func NewLocalStore(storepath, cachepath string) (*LocalStore, error) {
8180
uid = strings.Replace(uid, "-", "", -1)
8281

8382
return &LocalStore{
84-
storepath: storepath,
85-
pathCleaned: pathCleaned,
86-
cachepath: cachepath,
87-
Id: uid,
83+
storepath: storepath,
84+
cachepath: cachepath,
85+
Id: uid,
8886
}, nil
8987
}
9088

@@ -137,7 +135,7 @@ func (l *LocalStore) List(ctx context.Context, query cloudstorage.Query) (*cloud
137135
return err
138136
}
139137

140-
obj := strings.Replace(fo, l.pathCleaned, "", 1)
138+
obj := strings.Replace(fo, l.storepath, "", 1)
141139

142140
if f.IsDir() {
143141
return nil

localfs/store_test.go

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@ package localfs_test
22

33
import (
44
"context"
5+
"io/ioutil"
56
"os"
7+
"path/filepath"
68
"testing"
79

8-
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
911

1012
"github.com/lytics/cloudstorage"
1113
"github.com/lytics/cloudstorage/localfs"
1214
"github.com/lytics/cloudstorage/testutils"
1315
)
1416

1517
func TestAll(t *testing.T) {
18+
t.Parallel()
1619

17-
os.RemoveAll("/tmp/mockcloud")
18-
os.RemoveAll("/tmp/localcache")
20+
tmpDir, err := ioutil.TempDir("/tmp", "all")
21+
require.NoError(t, err)
22+
defer os.RemoveAll(tmpDir)
1923

2024
localFsConf := &cloudstorage.Config{
2125
Type: localfs.StoreType,
2226
AuthMethod: localfs.AuthFileSystem,
23-
LocalFS: "/tmp/mockcloud",
24-
TmpDir: "/tmp/localcache",
27+
LocalFS: filepath.Join(tmpDir, "mockcloud"),
28+
TmpDir: filepath.Join(tmpDir, "localcache"),
29+
Bucket: "all",
2530
}
2631

2732
store, err := cloudstorage.NewStore(localFsConf)
@@ -30,16 +35,20 @@ func TestAll(t *testing.T) {
3035
return
3136
}
3237
testutils.RunTests(t, store, localFsConf)
38+
}
39+
40+
func TestBrusted(t *testing.T) {
41+
t.Parallel()
3342

3443
// invalid config: empty/missing LocalFS
35-
localFsConf = &cloudstorage.Config{
44+
localFsConf := &cloudstorage.Config{
3645
Type: localfs.StoreType,
3746
AuthMethod: localfs.AuthFileSystem,
3847
LocalFS: "",
3948
}
40-
store, err = cloudstorage.NewStore(localFsConf)
41-
assert.NotEqual(t, nil, err)
42-
assert.Equal(t, nil, store)
49+
store, err := cloudstorage.NewStore(localFsConf)
50+
require.Error(t, err)
51+
require.Equal(t, nil, store)
4352

4453
// invalid config: LocalFS = TempDir
4554
localFsConf = &cloudstorage.Config{
@@ -49,42 +58,56 @@ func TestAll(t *testing.T) {
4958
TmpDir: "/tmp/invalid",
5059
}
5160
store, err = cloudstorage.NewStore(localFsConf)
52-
assert.NotEqual(t, nil, err)
53-
assert.Equal(t, nil, store)
61+
require.Error(t, err)
62+
require.Equal(t, nil, store)
5463
}
5564

5665
func TestNewReaderDir(t *testing.T) {
66+
t.Parallel()
67+
68+
tmpDir, err := ioutil.TempDir("/tmp", "newreaderdir")
69+
require.NoError(t, err)
70+
defer os.RemoveAll(tmpDir)
71+
5772
// When a dir is requested, serve the index.html file instead
5873
localFsConf := &cloudstorage.Config{
5974
Type: localfs.StoreType,
6075
AuthMethod: localfs.AuthFileSystem,
61-
LocalFS: "/tmp/mockcloud",
62-
TmpDir: "/tmp/localcache",
76+
LocalFS: filepath.Join(tmpDir, "mockcloud"),
77+
TmpDir: filepath.Join(tmpDir, "localcache"),
78+
Bucket: "newreaderdir",
6379
}
6480
store, err := cloudstorage.NewStore(localFsConf)
6581
testutils.MockFile(store, "test/index.html", "test")
66-
assert.Equal(t, nil, err)
67-
assert.Equal(t, nil, err)
82+
require.NoError(t, err)
83+
require.Equal(t, nil, err)
6884
_, err = store.NewReader("test")
69-
assert.Equal(t, err, cloudstorage.ErrObjectNotFound)
85+
require.Equal(t, err, cloudstorage.ErrObjectNotFound)
7086
err = store.Delete(context.Background(), "test/index.html")
71-
assert.Equal(t, nil, err)
87+
require.NoError(t, err)
7288
}
7389

7490
func TestGetDir(t *testing.T) {
91+
t.Parallel()
92+
93+
tmpDir, err := ioutil.TempDir("/tmp", "getdir")
94+
require.NoError(t, err)
95+
defer os.RemoveAll(tmpDir)
96+
7597
// When a dir is requested, serve the index.html file instead
7698
localFsConf := &cloudstorage.Config{
7799
Type: localfs.StoreType,
78100
AuthMethod: localfs.AuthFileSystem,
79-
LocalFS: "/tmp/mockcloud",
80-
TmpDir: "/tmp/localcache",
101+
LocalFS: filepath.Join(tmpDir, "mockcloud"),
102+
TmpDir: filepath.Join(tmpDir, "localcache"),
103+
Bucket: "getdir",
81104
}
82105
store, err := cloudstorage.NewStore(localFsConf)
83-
testutils.MockFile(store, "test/index.html", "test")
84-
assert.Equal(t, nil, err)
85-
assert.Equal(t, nil, err)
106+
require.NoError(t, err)
107+
err = testutils.MockFile(store, "test/index.html", "test")
108+
require.NoError(t, err)
86109
_, err = store.Get(context.Background(), "test")
87-
assert.Equal(t, err, cloudstorage.ErrObjectNotFound)
110+
require.Equal(t, err, cloudstorage.ErrObjectNotFound)
88111
err = store.Delete(context.Background(), "test/index.html")
89-
assert.Equal(t, nil, err)
112+
require.NoError(t, err)
90113
}

0 commit comments

Comments
 (0)