2022-06-14 18:25:52 +00:00
|
|
|
package paths
|
2020-05-20 16:36:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2022-06-14 18:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/fsutil"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2020-05-20 16:36:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const pathSize = 16 << 20
|
|
|
|
|
|
|
|
type TestingLocalStorage struct {
|
|
|
|
root string
|
2022-11-01 11:01:31 +00:00
|
|
|
c storiface.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
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
func (t *TestingLocalStorage) GetStorage() (storiface.StorageConfig, error) {
|
2020-05-20 16:36:46 +00:00
|
|
|
return t.c, nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
func (t *TestingLocalStorage) SetStorage(f func(*storiface.StorageConfig)) error {
|
2020-05-20 16:36:46 +00:00
|
|
|
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{
|
2021-02-18 15:44:34 +00:00
|
|
|
Capacity: pathSize,
|
|
|
|
Available: pathSize,
|
|
|
|
FSAvailable: pathSize,
|
2020-05-20 16:36:46 +00:00
|
|
|
}, 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)
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
meta := &storiface.LocalStorageMeta{
|
2022-01-18 10:57:04 +00:00
|
|
|
ID: storiface.ID(uuid.New().String()),
|
2020-05-20 16:36:46 +00:00
|
|
|
Weight: 1,
|
|
|
|
CanSeal: true,
|
|
|
|
CanStore: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
mb, err := json.MarshalIndent(meta, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
if err := os.WriteFile(metaFile, mb, 0644); err != nil {
|
2020-05-20 16:36:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ LocalStorage = &TestingLocalStorage{}
|
|
|
|
|
|
|
|
func TestLocalStorage(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
2022-03-11 06:27:36 +00:00
|
|
|
root := t.TempDir()
|
2020-05-20 16:36:46 +00:00
|
|
|
|
|
|
|
tstor := &TestingLocalStorage{
|
|
|
|
root: root,
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:59:35 +00:00
|
|
|
index := NewMemIndex(nil)
|
2020-05-20 16:36:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|