diff --git a/commands/client_daemon_test.go b/commands/client_daemon_test.go index 4f7ff2ae79..c9060ab2f0 100644 --- a/commands/client_daemon_test.go +++ b/commands/client_daemon_test.go @@ -279,7 +279,7 @@ func TestPieceRejectionInProposeStorageDeal(t *testing.T) { func TestSelfDialStorageGoodError(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends. defer func() { err := env.Teardown(ctx) diff --git a/commands/inspector_daemon_test.go b/commands/inspector_daemon_test.go index cee9ba35a6..0116bc2e01 100644 --- a/commands/inspector_daemon_test.go +++ b/commands/inspector_daemon_test.go @@ -15,7 +15,7 @@ import ( func TestInspectConfig(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { diff --git a/commands/payment_channel_daemon_test.go b/commands/payment_channel_daemon_test.go index 8c925acc15..5dc595e21e 100644 --- a/commands/payment_channel_daemon_test.go +++ b/commands/payment_channel_daemon_test.go @@ -21,7 +21,7 @@ import ( func TestPaymentChannelCreateSuccess(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -42,7 +42,7 @@ func TestPaymentChannelLs(t *testing.T) { tf.IntegrationTest(t) t.Run("Works with default payer", func(t *testing.T) { - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -72,7 +72,7 @@ func TestPaymentChannelLs(t *testing.T) { }) t.Run("Works with specified payer", func(t *testing.T) { - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -106,7 +106,7 @@ func TestPaymentChannelLs(t *testing.T) { defer cancel() // Get basic testing environment - ctx, env := fastesting.NewTestEnvironment(ctx, t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(ctx, t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -133,7 +133,7 @@ func TestPaymentChannelLs(t *testing.T) { func TestPaymentChannelVoucherSuccess(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -163,7 +163,7 @@ func TestPaymentChannelVoucherSuccess(t *testing.T) { func TestPaymentChannelRedeemSuccess(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -204,7 +204,7 @@ func TestPaymentChannelRedeemSuccess(t *testing.T) { func TestPaymentChannelRedeemTooEarlyFails(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -245,7 +245,7 @@ func TestPaymentChannelRedeemTooEarlyFails(t *testing.T) { func TestPaymentChannelReclaimSuccess(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -317,7 +317,7 @@ func TestPaymentChannelReclaimSuccess(t *testing.T) { func TestPaymentChannelCloseSuccess(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -369,7 +369,7 @@ func TestPaymentChannelCloseSuccess(t *testing.T) { func TestPaymentChannelExtendSuccess(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { @@ -425,7 +425,7 @@ func TestPaymentChannelExtendSuccess(t *testing.T) { func TestPaymentChannelCancelSuccess(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends defer func() { diff --git a/commands/retrieval_client_daemon_test.go b/commands/retrieval_client_daemon_test.go index 6243e627d0..7f2b920fac 100644 --- a/commands/retrieval_client_daemon_test.go +++ b/commands/retrieval_client_daemon_test.go @@ -19,7 +19,7 @@ import ( func TestSelfDialRetrievalGoodError(t *testing.T) { tf.IntegrationTest(t) - ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.EnvironmentOpts{}) + ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fast.FilecoinOpts{}) // Teardown after test ends. defer func() { err := env.Teardown(ctx) diff --git a/commands/show_test.go b/commands/show_test.go index 76dce13292..fa07a78743 100644 --- a/commands/show_test.go +++ b/commands/show_test.go @@ -71,7 +71,7 @@ func TestBlockDaemon(t *testing.T) { func TestShowDeal(t *testing.T) { tf.IntegrationTest(t) - fastenvOpts := fast.EnvironmentOpts{} + fastenvOpts := fast.FilecoinOpts{} ctx, env := fastesting.NewTestEnvironment(context.Background(), t, fastenvOpts) defer func() { diff --git a/tools/fast/bin/localnet/main.go b/tools/fast/bin/localnet/main.go index 99332fa511..39b6df01c9 100644 --- a/tools/fast/bin/localnet/main.go +++ b/tools/fast/bin/localnet/main.go @@ -30,6 +30,7 @@ import ( "github.com/filecoin-project/go-filecoin/proofs" "github.com/filecoin-project/go-filecoin/protocol/storage/storagedeal" "github.com/filecoin-project/go-filecoin/tools/fast" + "github.com/filecoin-project/go-filecoin/tools/fast/environment" "github.com/filecoin-project/go-filecoin/tools/fast/series" lpfc "github.com/filecoin-project/go-filecoin/tools/iptb-plugins/filecoin/local" ) @@ -169,7 +170,7 @@ func main() { return } - env, err := fast.NewEnvironmentMemoryGenesis(&balance, workdir, getProofsMode(smallSectors)) + env, err := environment.NewMemoryGenesis(&balance, workdir, getProofsMode(smallSectors)) if err != nil { exitcode = handleError(err) return @@ -191,7 +192,7 @@ func main() { return } - fastenvOpts := fast.EnvironmentOpts{ + fastenvOpts := fast.FilecoinOpts{ InitOpts: []fast.ProcessInitOption{fast.POGenesisFile(genesisURI)}, DaemonOpts: []fast.ProcessDaemonOption{fast.POBlockTime(blocktime)}, } diff --git a/tools/fast/environment.go b/tools/fast/environment/environment.go similarity index 86% rename from tools/fast/environment.go rename to tools/fast/environment/environment.go index fd5473c006..4f12fbf7a8 100644 --- a/tools/fast/environment.go +++ b/tools/fast/environment/environment.go @@ -1,4 +1,4 @@ -package fast +package environment import ( "context" @@ -8,6 +8,7 @@ import ( logging "github.com/ipfs/go-log" "github.com/filecoin-project/go-filecoin/address" + "github.com/filecoin-project/go-filecoin/tools/fast" ) // ErrNoGenesisMiner is returned by GenesisMiner if the environment does not @@ -24,12 +25,6 @@ type GenesisMiner struct { Owner io.Reader } -// EnvironmentOpts are used define process init and daemon options for the environment. -type EnvironmentOpts struct { - InitOpts []ProcessInitOption - DaemonOpts []ProcessDaemonOption -} - // Environment defines the interface common among all environments that the // FAST lib can work across. It helps smooth out the differences by providing // a common ground to work from @@ -53,11 +48,11 @@ type Environment interface { // environments may create a Filecoin process that interacts with // an already running filecoin node, and supplied the API multiaddr // as options. - NewProcess(ctx context.Context, processType string, options map[string]string, eo EnvironmentOpts) (*Filecoin, error) + NewProcess(ctx context.Context, processType string, options map[string]string, eo fast.FilecoinOpts) (*fast.Filecoin, error) // Processes returns a slice of all processes the environment knows // about. - Processes() []*Filecoin + Processes() []*fast.Filecoin // Teardown runs anything that the environment may need to do to // be nice to the the execution area of this code. @@ -65,5 +60,5 @@ type Environment interface { // TeardownProcess runs anything that the environment may need to do // to remove a process from the environment in a clean way. - TeardownProcess(context.Context, *Filecoin) error + TeardownProcess(context.Context, *fast.Filecoin) error } diff --git a/tools/fast/environment_devnet.go b/tools/fast/environment/environment_devnet.go similarity index 74% rename from tools/fast/environment_devnet.go rename to tools/fast/environment/environment_devnet.go index a76db66868..f53b26802f 100644 --- a/tools/fast/environment_devnet.go +++ b/tools/fast/environment/environment_devnet.go @@ -1,4 +1,4 @@ -package fast +package environment // The devnet FAST environment provides an environment for using FAST with the deployed kittyhawk // devnet infrasturture run by the Filecoin development team. It can be used to setup and manage nodes @@ -17,24 +17,25 @@ import ( iptb "github.com/ipfs/iptb/testbed" "github.com/filecoin-project/go-filecoin/address" + "github.com/filecoin-project/go-filecoin/tools/fast" ) -// EnvironmentDevnet is a FAST lib environment that is meant to be used +// Devnet is a FAST lib environment that is meant to be used // when working with kittyhawk devnets run by the Filecoin development team. -type EnvironmentDevnet struct { +type Devnet struct { network string location string log logging.EventLogger processesMu sync.Mutex - processes []*Filecoin + processes []*fast.Filecoin } -// NewEnvironmentDevnet builds an environment that uses deployed infrastructure to +// NewDevnet builds an environment that uses deployed infrastructure to // the kittyhawk devnets. -func NewEnvironmentDevnet(network, location string) (Environment, error) { - env := &EnvironmentDevnet{ +func NewDevnet(network, location string) (Environment, error) { + env := &Devnet{ network: network, location: location, log: logging.Logger("environment"), @@ -48,7 +49,7 @@ func NewEnvironmentDevnet(network, location string) (Environment, error) { } // GenesisCar provides a url where the genesis file can be fetched from -func (e *EnvironmentDevnet) GenesisCar() string { +func (e *Devnet) GenesisCar() string { uri := url.URL{ Host: fmt.Sprintf("genesis.%s.kittyhawk.wtf", e.network), Path: "genesis.car", @@ -59,18 +60,18 @@ func (e *EnvironmentDevnet) GenesisCar() string { } // GenesisMiner returns a ErrNoGenesisMiner for this environment -func (e *EnvironmentDevnet) GenesisMiner() (*GenesisMiner, error) { +func (e *Devnet) GenesisMiner() (*GenesisMiner, error) { return nil, ErrNoGenesisMiner } // Log returns the logger for the environment. -func (e *EnvironmentDevnet) Log() logging.EventLogger { +func (e *Devnet) Log() logging.EventLogger { return e.log } // NewProcess builds a iptb process of the given type and options passed. The // process is tracked by the environment and returned. -func (e *EnvironmentDevnet) NewProcess(ctx context.Context, processType string, options map[string]string, eo EnvironmentOpts) (*Filecoin, error) { +func (e *Devnet) NewProcess(ctx context.Context, processType string, options map[string]string, eo fast.FilecoinOpts) (*fast.Filecoin, error) { e.processesMu.Lock() defer e.processesMu.Unlock() @@ -92,31 +93,31 @@ func (e *EnvironmentDevnet) NewProcess(ctx context.Context, processType string, } // We require a slightly more extended core interface - fc, ok := c.(IPTBCoreExt) + fc, ok := c.(fast.IPTBCoreExt) if !ok { return nil, fmt.Errorf("%s does not implement the extended IPTB.Core interface IPTBCoreExt", processType) } - p := NewFilecoinProcess(ctx, fc, eo) + p := fast.NewFilecoinProcess(ctx, fc, eo) e.processes = append(e.processes, p) return p, nil } // Processes returns all processes the environment knows about. -func (e *EnvironmentDevnet) Processes() []*Filecoin { +func (e *Devnet) Processes() []*fast.Filecoin { e.processesMu.Lock() defer e.processesMu.Unlock() return e.processes[:] } // Teardown stops all of the nodes and cleans up the environment. -func (e *EnvironmentDevnet) Teardown(ctx context.Context) error { +func (e *Devnet) Teardown(ctx context.Context) error { e.processesMu.Lock() defer e.processesMu.Unlock() e.log.Info("Teardown environment") for _, p := range e.processes { - if err := p.core.Stop(ctx); err != nil { + if err := p.StopDaemon(ctx); err != nil { return err } } @@ -126,12 +127,12 @@ func (e *EnvironmentDevnet) Teardown(ctx context.Context) error { // TeardownProcess stops the running process and removes it from the // environment. -func (e *EnvironmentDevnet) TeardownProcess(ctx context.Context, p *Filecoin) error { +func (e *Devnet) TeardownProcess(ctx context.Context, p *fast.Filecoin) error { e.processesMu.Lock() defer e.processesMu.Unlock() - e.log.Infof("Teardown process: %s", p.core.String()) - if err := p.core.Stop(ctx); err != nil { + e.log.Infof("Teardown process: %s", p.String()) + if err := p.StopDaemon(ctx); err != nil { return err } @@ -143,13 +144,13 @@ func (e *EnvironmentDevnet) TeardownProcess(ctx context.Context, p *Filecoin) er } // remove the provess from the process list - return os.RemoveAll(p.core.Dir()) + return os.RemoveAll(p.Dir()) } // GetFunds retrieves a fixed amount of tokens from an environment -func GetFunds(ctx context.Context, env Environment, p *Filecoin) error { +func GetFunds(ctx context.Context, env Environment, p *fast.Filecoin) error { switch devenv := env.(type) { - case *EnvironmentDevnet: + case *Devnet: var toAddr address.Address if err := p.ConfigGet(ctx, "wallet.defaultAddress", &toAddr); err != nil { return err diff --git a/tools/fast/environment_memory_genesis.go b/tools/fast/environment/environment_memory_genesis.go similarity index 78% rename from tools/fast/environment_memory_genesis.go rename to tools/fast/environment/environment_memory_genesis.go index 6831ed731c..e4b38cd898 100644 --- a/tools/fast/environment_memory_genesis.go +++ b/tools/fast/environment/environment_memory_genesis.go @@ -1,4 +1,4 @@ -package fast +package environment import ( "bytes" @@ -18,15 +18,16 @@ import ( "github.com/filecoin-project/go-filecoin/address" "github.com/filecoin-project/go-filecoin/commands" "github.com/filecoin-project/go-filecoin/gengen/util" + "github.com/filecoin-project/go-filecoin/tools/fast" "github.com/filecoin-project/go-filecoin/types" iptb "github.com/ipfs/iptb/testbed" ) -// EnvironmentMemoryGenesis is a FAST lib environment that is meant to be used +// MemoryGenesis is a FAST lib environment that is meant to be used // when working locally, on the same network / machine. It's great for writing // functional tests! -type EnvironmentMemoryGenesis struct { +type MemoryGenesis struct { genesisCar []byte genesisMinerOwner commands.WalletSerializeResult genesisMinerAddr address.Address @@ -39,16 +40,16 @@ type EnvironmentMemoryGenesis struct { log logging.EventLogger processesMu sync.Mutex - processes []*Filecoin + processes []*fast.Filecoin proofsMode types.ProofsMode } -// NewEnvironmentMemoryGenesis builds an environment with a local genesis that can be used +// NewMemoryGenesis builds an environment with a local genesis that can be used // to initialize nodes and create a genesis node. The genesis file is provided by an http // server. -func NewEnvironmentMemoryGenesis(funds *big.Int, location string, proofsMode types.ProofsMode) (Environment, error) { - env := &EnvironmentMemoryGenesis{ +func NewMemoryGenesis(funds *big.Int, location string, proofsMode types.ProofsMode) (Environment, error) { + env := &MemoryGenesis{ location: location, log: logging.Logger("environment"), proofsMode: proofsMode, @@ -70,7 +71,7 @@ func NewEnvironmentMemoryGenesis(funds *big.Int, location string, proofsMode typ } // GenesisCar provides a url where the genesis file can be fetched from -func (e *EnvironmentMemoryGenesis) GenesisCar() string { +func (e *MemoryGenesis) GenesisCar() string { uri := url.URL{ Host: e.genesisServerAddr, Path: "genesis.car", @@ -82,7 +83,7 @@ func (e *EnvironmentMemoryGenesis) GenesisCar() string { // GenesisMiner provides required information to create a genesis node and // load the wallet. -func (e *EnvironmentMemoryGenesis) GenesisMiner() (*GenesisMiner, error) { +func (e *MemoryGenesis) GenesisMiner() (*GenesisMiner, error) { owner, err := json.Marshal(e.genesisMinerOwner) if err != nil { return nil, err @@ -95,13 +96,13 @@ func (e *EnvironmentMemoryGenesis) GenesisMiner() (*GenesisMiner, error) { } // Log returns the logger for the environment. -func (e *EnvironmentMemoryGenesis) Log() logging.EventLogger { +func (e *MemoryGenesis) Log() logging.EventLogger { return e.log } // NewProcess builds a iptb process of the given type and options passed. The // process is tracked by the environment and returned. -func (e *EnvironmentMemoryGenesis) NewProcess(ctx context.Context, processType string, options map[string]string, eo EnvironmentOpts) (*Filecoin, error) { +func (e *MemoryGenesis) NewProcess(ctx context.Context, processType string, options map[string]string, eo fast.FilecoinOpts) (*fast.Filecoin, error) { e.processesMu.Lock() defer e.processesMu.Unlock() @@ -123,31 +124,31 @@ func (e *EnvironmentMemoryGenesis) NewProcess(ctx context.Context, processType s } // We require a slightly more extended core interface - fc, ok := c.(IPTBCoreExt) + fc, ok := c.(fast.IPTBCoreExt) if !ok { return nil, fmt.Errorf("%s does not implement the extended IPTB.Core interface IPTBCoreExt", processType) } - p := NewFilecoinProcess(ctx, fc, eo) + p := fast.NewFilecoinProcess(ctx, fc, eo) e.processes = append(e.processes, p) return p, nil } // Processes returns all processes the environment knows about. -func (e *EnvironmentMemoryGenesis) Processes() []*Filecoin { +func (e *MemoryGenesis) Processes() []*fast.Filecoin { e.processesMu.Lock() defer e.processesMu.Unlock() return e.processes[:] } // Teardown stops all of the nodes and cleans up the environment. -func (e *EnvironmentMemoryGenesis) Teardown(ctx context.Context) error { +func (e *MemoryGenesis) Teardown(ctx context.Context) error { e.processesMu.Lock() defer e.processesMu.Unlock() e.log.Info("Teardown environment") for _, p := range e.processes { - if err := p.core.Stop(ctx); err != nil { + if err := p.StopDaemon(ctx); err != nil { return err } } @@ -161,12 +162,12 @@ func (e *EnvironmentMemoryGenesis) Teardown(ctx context.Context) error { // TeardownProcess stops the running process and removes it from the // environment. -func (e *EnvironmentMemoryGenesis) TeardownProcess(ctx context.Context, p *Filecoin) error { +func (e *MemoryGenesis) TeardownProcess(ctx context.Context, p *fast.Filecoin) error { e.processesMu.Lock() defer e.processesMu.Unlock() - e.log.Infof("Teardown process: %s", p.core.String()) - if err := p.core.Stop(ctx); err != nil { + e.log.Infof("Teardown process: %s", p.String()) + if err := p.StopDaemon(ctx); err != nil { return err } @@ -178,12 +179,12 @@ func (e *EnvironmentMemoryGenesis) TeardownProcess(ctx context.Context, p *Filec } // remove the provess from the process list - return os.RemoveAll(p.core.Dir()) + return os.RemoveAll(p.Dir()) } // startGenesisServer builds and starts a server which will serve the genesis // file, the url for the genesis.car is returned by GenesisCar() -func (e *EnvironmentMemoryGenesis) startGenesisServer() error { +func (e *MemoryGenesis) startGenesisServer() error { handler := http.NewServeMux() handler.HandleFunc("/genesis.car", func(w http.ResponseWriter, req *http.Request) { car := bytes.NewBuffer(e.genesisCar) @@ -211,7 +212,7 @@ func (e *EnvironmentMemoryGenesis) startGenesisServer() error { } // buildGenesis builds a genesis with the specified funds. -func (e *EnvironmentMemoryGenesis) buildGenesis(funds *big.Int) error { +func (e *MemoryGenesis) buildGenesis(funds *big.Int) error { cfg := &gengen.GenesisCfg{ Keys: 1, PreAlloc: []string{ diff --git a/tools/fast/environment_memory_genesis_test.go b/tools/fast/environment/environment_memory_genesis_test.go similarity index 80% rename from tools/fast/environment_memory_genesis_test.go rename to tools/fast/environment/environment_memory_genesis_test.go index 6ea7bab8e0..68b5174ec1 100644 --- a/tools/fast/environment_memory_genesis_test.go +++ b/tools/fast/environment/environment_memory_genesis_test.go @@ -1,4 +1,4 @@ -package fast +package environment import ( "context" @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/require" tf "github.com/filecoin-project/go-filecoin/testhelpers/testflags" + "github.com/filecoin-project/go-filecoin/tools/fast" mockplugin "github.com/filecoin-project/go-filecoin/tools/iptb-plugins/filecoin/mock" "github.com/filecoin-project/go-filecoin/types" ) @@ -30,7 +31,7 @@ func init() { } } -func TestEnvironmentMemoryGenesis(t *testing.T) { +func TestMemoryGenesis(t *testing.T) { tf.UnitTest(t) t.Run("SetupTeardown", func(t *testing.T) { @@ -42,8 +43,8 @@ func TestEnvironmentMemoryGenesis(t *testing.T) { require.NoError(t, os.RemoveAll(testDir)) }() - env, err := NewEnvironmentMemoryGenesis(big.NewInt(100000), testDir, types.TestProofsMode) - localenv := env.(*EnvironmentMemoryGenesis) + env, err := NewMemoryGenesis(big.NewInt(100000), testDir, types.TestProofsMode) + localenv := env.(*MemoryGenesis) assert.NoError(t, err) assert.NotNil(t, env) assert.Equal(t, testDir, localenv.location) @@ -68,23 +69,23 @@ func TestEnvironmentMemoryGenesis(t *testing.T) { require.NoError(t, os.RemoveAll(testDir)) }() - env, err := NewEnvironmentMemoryGenesis(big.NewInt(100000), testDir, types.TestProofsMode) + env, err := NewMemoryGenesis(big.NewInt(100000), testDir, types.TestProofsMode) require.NoError(t, err) - p, err := env.NewProcess(ctx, mockplugin.PluginName, nil, EnvironmentOpts{}) + p, err := env.NewProcess(ctx, mockplugin.PluginName, nil, fast.FilecoinOpts{}) assert.NoError(t, err) assert.NotNil(t, p) assert.Equal(t, 1, len(env.Processes())) // did we create the process dir correctly? - _, err = os.Stat(p.core.Dir()) + _, err = os.Stat(p.Dir()) assert.NoError(t, err) assert.NoError(t, env.TeardownProcess(ctx, p)) assert.Equal(t, 0, len(env.Processes())) // did we teardown the process correctly? - _, existsErr := os.Stat(p.core.Dir()) + _, existsErr := os.Stat(p.Dir()) assert.True(t, os.IsNotExist(existsErr)) }) } diff --git a/tools/fast/fastesting/basic.go b/tools/fast/fastesting/basic.go index a82df73c8b..4f3397a7c0 100644 --- a/tools/fast/fastesting/basic.go +++ b/tools/fast/fastesting/basic.go @@ -13,6 +13,7 @@ import ( "github.com/filecoin-project/go-filecoin/testhelpers" "github.com/filecoin-project/go-filecoin/tools/fast" + "github.com/filecoin-project/go-filecoin/tools/fast/environment" "github.com/filecoin-project/go-filecoin/tools/fast/series" localplugin "github.com/filecoin-project/go-filecoin/tools/iptb-plugins/filecoin/local" "github.com/filecoin-project/go-filecoin/types" @@ -20,7 +21,7 @@ import ( // TestEnvironment provides common setup for writing tests using FAST type TestEnvironment struct { - fast.Environment + environment.Environment t *testing.T ctx context.Context @@ -28,20 +29,20 @@ type TestEnvironment struct { pluginName string pluginOpts map[string]string - fastenvOpts fast.EnvironmentOpts + fastenvOpts fast.FilecoinOpts GenesisMiner *fast.Filecoin } // NewTestEnvironment creates a TestEnvironment with a basic setup for writing tests using the FAST library. -func NewTestEnvironment(ctx context.Context, t *testing.T, fastenvOpts fast.EnvironmentOpts) (context.Context, *TestEnvironment) { +func NewTestEnvironment(ctx context.Context, t *testing.T, fastenvOpts fast.FilecoinOpts) (context.Context, *TestEnvironment) { // Create a directory for the test using the test name (mostly for FAST) // Replace the forward slash as tempdir can't handle them dir, err := ioutil.TempDir("", strings.Replace(t.Name(), "/", ".", -1)) require.NoError(t, err) // Create an environment that includes a genesis block with 1MM FIL - env, err := fast.NewEnvironmentMemoryGenesis(big.NewInt(1000000), dir, types.TestProofsMode) + env, err := environment.NewMemoryGenesis(big.NewInt(1000000), dir, types.TestProofsMode) require.NoError(t, err) defer func() { @@ -58,7 +59,7 @@ func NewTestEnvironment(ctx context.Context, t *testing.T, fastenvOpts fast.Envi genesisMiner, err := env.GenesisMiner() require.NoError(t, err) - fastenvOpts = fast.EnvironmentOpts{ + fastenvOpts = fast.FilecoinOpts{ InitOpts: append([]fast.ProcessInitOption{fast.POGenesisFile(genesisURI)}, fastenvOpts.InitOpts...), DaemonOpts: append([]fast.ProcessDaemonOption{fast.POBlockTime(50 * time.Millisecond)}, fastenvOpts.DaemonOpts...), } diff --git a/tools/fast/process.go b/tools/fast/process.go index 79067da0ce..acfbd54d7e 100644 --- a/tools/fast/process.go +++ b/tools/fast/process.go @@ -19,15 +19,21 @@ import ( ) var ( - // ErrDoubleInitOpts is returned by InitDaemon when both init options are provided by EnvironmentOpts + // ErrDoubleInitOpts is returned by InitDaemon when both init options are provided by FilecoinOpts // in NewProcess as well as passed to InitDaemon directly. ErrDoubleInitOpts = errors.New("cannot provide both init options through environment and arguments") - // ErrDoubleDaemonOpts is returned by StartDaemon when both init options are provided by EnvironmentOpts + // ErrDoubleDaemonOpts is returned by StartDaemon when both init options are provided by FilecoinOpts // in NewProcess as well as passed to StartDaemon directly. ErrDoubleDaemonOpts = errors.New("cannot provide both daemon options through environment and arguments") ) +// FilecoinOpts are used define process init and daemon options for the environment. +type FilecoinOpts struct { + InitOpts []ProcessInitOption + DaemonOpts []ProcessDaemonOption +} + // must register all filecoin iptb plugins first. func init() { _, err := iptb.RegisterPlugin(iptb.IptbPlugin{ @@ -86,7 +92,7 @@ type Filecoin struct { } // NewFilecoinProcess returns a pointer to a Filecoin process that wraps the IPTB core interface `c`. -func NewFilecoinProcess(ctx context.Context, c IPTBCoreExt, eo EnvironmentOpts) *Filecoin { +func NewFilecoinProcess(ctx context.Context, c IPTBCoreExt, eo FilecoinOpts) *Filecoin { return &Filecoin{ core: c, Log: logging.Logger(c.String()), @@ -158,11 +164,16 @@ func (f *Filecoin) Shell() error { return f.core.Shell(f.ctx, []testbedi.Core{}) } -// Dir returns the dirtectory used by the filecoin process +// Dir returns the dirtectory used by the filecoin process. func (f *Filecoin) Dir() string { return f.core.Dir() } +// String returns the string representation of the filecoin process. +func (f *Filecoin) String() string { + return f.core.String() +} + // DumpLastOutput writes all the output (args, exit-code, error, stderr, stdout) of the last ran // command from RunCmdWithStdin, RunCmdJSONWithStdin, or RunCmdLDJSONWithStdin. func (f *Filecoin) DumpLastOutput(w io.Writer) { diff --git a/tools/fast/process_stderr_recorder.go b/tools/fast/process_stderr_recorder.go index b2bc08837a..9344c46e9c 100644 --- a/tools/fast/process_stderr_recorder.go +++ b/tools/fast/process_stderr_recorder.go @@ -36,8 +36,13 @@ func (f *Filecoin) setupStderrCapturing() error { } func (f *Filecoin) teardownStderrCapturing() error { - f.lpCancel() - return f.stderr.Close() + if f.lp != nil { + f.lpCancel() + } + if f.stderr != nil { + return f.stderr.Close() + } + return nil } // StartLogCapture returns a fastutil.Interval, after calling fastutil.Interval#Stop diff --git a/tools/fast/process_stderr_recorder_test.go b/tools/fast/process_stderr_recorder_test.go index f0a3e06f49..40d48e60a2 100644 --- a/tools/fast/process_stderr_recorder_test.go +++ b/tools/fast/process_stderr_recorder_test.go @@ -31,7 +31,7 @@ func TestStartLogCapture(t *testing.T) { fc, ok := c.(IPTBCoreExt) require.True(t, ok) - mfc := NewFilecoinProcess(ctx, fc, EnvironmentOpts{}) + mfc := NewFilecoinProcess(ctx, fc, FilecoinOpts{}) err = mfc.setupStderrCapturing() require.NoError(t, err) diff --git a/tools/fast/process_test.go b/tools/fast/process_test.go index b43eccc2bb..45b06a826c 100644 --- a/tools/fast/process_test.go +++ b/tools/fast/process_test.go @@ -58,7 +58,7 @@ func TestRunCmds(t *testing.T) { fc, ok := c.(IPTBCoreExt) require.True(t, ok) - mfc := NewFilecoinProcess(ctx, fc, EnvironmentOpts{}) + mfc := NewFilecoinProcess(ctx, fc, FilecoinOpts{}) t.Run("test RunCmdWithStdin", func(t *testing.T) { out, err := mfc.RunCmdWithStdin(ctx, nil, "") @@ -105,7 +105,7 @@ func TestInitDaemon(t *testing.T) { t.Run("providing both InitDaemon options and environment options", func(t *testing.T) { - fastenvOpts := EnvironmentOpts{ + fastenvOpts := FilecoinOpts{ InitOpts: []ProcessInitOption{POGenesisFile("http://example.com/genesis.car")}, } @@ -135,7 +135,7 @@ func TestStartDaemon(t *testing.T) { t.Run("providing both InitDaemon options and environment options", func(t *testing.T) { - fastenvOpts := EnvironmentOpts{ + fastenvOpts := FilecoinOpts{ DaemonOpts: []ProcessDaemonOption{POBlockTime(time.Second)}, } diff --git a/tools/fast/tests/retrieval_test.go b/tools/fast/tests/retrieval_test.go index 27577f83ad..f83c99998b 100644 --- a/tools/fast/tests/retrieval_test.go +++ b/tools/fast/tests/retrieval_test.go @@ -19,6 +19,7 @@ import ( th "github.com/filecoin-project/go-filecoin/testhelpers" tf "github.com/filecoin-project/go-filecoin/testhelpers/testflags" "github.com/filecoin-project/go-filecoin/tools/fast" + "github.com/filecoin-project/go-filecoin/tools/fast/environment" "github.com/filecoin-project/go-filecoin/tools/fast/series" localplugin "github.com/filecoin-project/go-filecoin/tools/iptb-plugins/filecoin/local" "github.com/filecoin-project/go-filecoin/types" @@ -47,7 +48,7 @@ func TestRetrievalLocalNetwork(t *testing.T) { require.NoError(t, err) // Create an environment that includes a genesis block with 1MM FIL - env, err := fast.NewEnvironmentMemoryGenesis(big.NewInt(1000000), dir, types.TestProofsMode) + env, err := environment.NewMemoryGenesis(big.NewInt(1000000), dir, types.TestProofsMode) require.NoError(t, err) // Teardown will shutdown all running processes the environment knows about @@ -69,7 +70,7 @@ func TestRetrievalLocalNetwork(t *testing.T) { genesisMiner, err := env.GenesisMiner() require.NoError(t, err) - fastenvOpts := fast.EnvironmentOpts{ + fastenvOpts := fast.FilecoinOpts{ InitOpts: []fast.ProcessInitOption{fast.POGenesisFile(genesisURI)}, DaemonOpts: []fast.ProcessDaemonOption{fast.POBlockTime(blocktime)}, } @@ -138,7 +139,7 @@ func TestRetrievalDevnet(t *testing.T) { require.NoError(t, err) // Create an environment that includes a genesis block with 1MM FIL - env, err := fast.NewEnvironmentDevnet("nightly", dir) + env, err := environment.NewDevnet("nightly", dir) require.NoError(t, err) // Teardown will shutdown all running processes the environment knows about @@ -158,7 +159,7 @@ func TestRetrievalDevnet(t *testing.T) { genesisURI := env.GenesisCar() - fastenvOpts := fast.EnvironmentOpts{ + fastenvOpts := fast.FilecoinOpts{ InitOpts: []fast.ProcessInitOption{fast.POGenesisFile(genesisURI), fast.PODevnetNightly()}, DaemonOpts: []fast.ProcessDaemonOption{}, } @@ -179,10 +180,10 @@ func TestRetrievalDevnet(t *testing.T) { // Everyone needs FIL to deal with gas costs and make sure their wallets // exists (sending FIL to a wallet addr creates it) - err = fast.GetFunds(ctx, env, miner) + err = environment.GetFunds(ctx, env, miner) require.NoError(t, err) - err = fast.GetFunds(ctx, env, client) + err = environment.GetFunds(ctx, env, client) require.NoError(t, err) RunRetrievalTest(ctx, t, miner, client, sectorSize)