2020-05-20 16:36:46 +00:00
|
|
|
package stores
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/fsutil"
|
2020-08-16 10:40:35 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-05-20 16:36:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const pathSize = 16 << 20
|
|
|
|
|
|
|
|
type TestingLocalStorage struct {
|
|
|
|
root string
|
2020-05-26 08:25:29 +00:00
|
|
|
c StorageConfig
|
2020-05-20 16:36:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:19:13 +00:00
|
|
|
func (t *TestingLocalStorage) DiskUsage(path string) (int64, error) {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:36:46 +00:00
|
|
|
func (t *TestingLocalStorage) GetStorage() (StorageConfig, error) {
|
|
|
|
return t.c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestingLocalStorage) SetStorage(f func(*StorageConfig)) error {
|
|
|
|
f(&t.c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:58:09 +00:00
|
|
|
func (t *TestingLocalStorage) Stat(path string) (fsutil.FsStat, error) {
|
|
|
|
return fsutil.FsStat{
|
2020-05-20 16:36:46 +00:00
|
|
|
Capacity: pathSize,
|
|
|
|
Available: pathSize,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestingLocalStorage) init(subpath string) error {
|
|
|
|
path := filepath.Join(t.root, subpath)
|
|
|
|
if err := os.Mkdir(path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
metaFile := filepath.Join(path, MetaFile)
|
|
|
|
|
|
|
|
meta := &LocalStorageMeta{
|
|
|
|
ID: ID(uuid.New().String()),
|
|
|
|
Weight: 1,
|
|
|
|
CanSeal: true,
|
|
|
|
CanStore: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
mb, err := json.MarshalIndent(meta, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(metaFile, mb, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ LocalStorage = &TestingLocalStorage{}
|
|
|
|
|
|
|
|
func TestLocalStorage(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
root, err := ioutil.TempDir("", "sector-storage-teststorage-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tstor := &TestingLocalStorage{
|
|
|
|
root: root,
|
|
|
|
}
|
|
|
|
|
|
|
|
index := NewIndex()
|
|
|
|
|
|
|
|
st, err := NewLocal(ctx, tstor, index, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
p1 := "1"
|
|
|
|
require.NoError(t, tstor.init("1"))
|
|
|
|
|
|
|
|
err = st.OpenPath(ctx, filepath.Join(tstor.root, p1))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// TODO: put more things here
|
|
|
|
}
|