2022-01-14 13:11:04 +00:00
|
|
|
package kit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-15 14:09:13 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2022-01-14 13:11:04 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-07-15 14:09:13 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-01-14 13:11:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-07-15 14:09:13 +00:00
|
|
|
"github.com/google/uuid"
|
2022-01-14 13:11:04 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2022-07-15 14:09:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-07-15 14:09:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2022-01-14 13:11:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestWorker represents a worker enrolled in an Ensemble.
|
|
|
|
type TestWorker struct {
|
|
|
|
api.Worker
|
|
|
|
|
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
// ListenAddr is the address on which an API server is listening, if an
|
|
|
|
// API server is created for this Node
|
|
|
|
ListenAddr multiaddr.Multiaddr
|
|
|
|
|
|
|
|
Stop func(context.Context) error
|
|
|
|
|
|
|
|
FetchHandler http.HandlerFunc
|
|
|
|
MinerNode *TestMiner
|
|
|
|
RemoteListener net.Listener
|
|
|
|
|
|
|
|
options nodeOpts
|
|
|
|
}
|
2022-07-15 14:09:13 +00:00
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
func (tm *TestWorker) AddStorage(ctx context.Context, t *testing.T, conf func(*storiface.LocalStorageMeta)) storiface.ID {
|
2022-07-15 14:09:13 +00:00
|
|
|
p := t.TempDir()
|
|
|
|
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := os.Stat(filepath.Join(p, metaFile))
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
cfg := &storiface.LocalStorageMeta{
|
2022-07-15 14:09:13 +00:00
|
|
|
ID: storiface.ID(uuid.New().String()),
|
|
|
|
Weight: 10,
|
|
|
|
CanSeal: false,
|
|
|
|
CanStore: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
conf(cfg)
|
|
|
|
|
|
|
|
if !(cfg.CanStore || cfg.CanSeal) {
|
|
|
|
t.Fatal("must specify at least one of CanStore or cfg.CanSeal")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(cfg, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(p, metaFile), b, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = tm.StorageAddLocal(ctx, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return cfg.ID
|
|
|
|
}
|