Skip to content

Commit

Permalink
Move shared testing setup to FAST
Browse files Browse the repository at this point in the history
  • Loading branch information
travisperson committed Mar 29, 2019
1 parent 6779f08 commit 900ae8d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 77 deletions.
88 changes: 11 additions & 77 deletions commands/payment_channel_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ package commands_test

import (
"context"
"io/ioutil"
"math/big"
"strings"
"testing"
"time"

"gx/ipfs/QmPVkJMTeRC6iBByPWdrRkD3BE5UXsj5HPzb4kPqL186mS/testify/assert"
"gx/ipfs/QmPVkJMTeRC6iBByPWdrRkD3BE5UXsj5HPzb4kPqL186mS/testify/require"
"gx/ipfs/QmQmhotPUzVrMEWNK3x1R5jQ5ZHWyL7tVUrmRPjrBrvyCb/go-ipfs-files"
cbor "gx/ipfs/QmcZLyosDwMKdB6NLRsiss9HXzDPhVhhRtPy67JFKTDQDX/go-ipld-cbor"
"gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase"

"github.com/filecoin-project/go-filecoin/actor/builtin/paymentbroker"
"github.com/filecoin-project/go-filecoin/address"
th "github.com/filecoin-project/go-filecoin/testhelpers"
"github.com/filecoin-project/go-filecoin/tools/fast"
"github.com/filecoin-project/go-filecoin/tools/fast/fasting"
"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"
)

Expand All @@ -37,68 +33,6 @@ func VoucherFromString(data string) (paymentbroker.PaymentVoucher, error) {
return voucher, nil
}

// BasiFastSetup creates a environment and a single node, and environment options
func BasicFastSetup(t *testing.T, ctx context.Context, fastenvOpts fast.EnvironmentOpts) (fast.Environment, *fast.Filecoin, func() *fast.Filecoin, context.Context) {
require := require.New(t)

// Create a directory for the test using the test name (mostly for FAST)
// TODO(tperson) use a different TempDir that uses MkdirAll
dir, err := ioutil.TempDir("", strings.Replace(t.Name(), "/", ".", -1))
require.NoError(err)

// Create an environment that includes a genesis block with 1MM FIL
env, err := fast.NewEnvironmentMemoryGenesis(big.NewInt(1000000), dir)
require.NoError(err)

// Setup options for nodes.
options := make(map[string]string)
options[localplugin.AttrLogJSON] = "1" // Enable JSON logs
options[localplugin.AttrLogLevel] = "5" // Set log level to Debug
options[localplugin.AttrUseSmallSectors] = "true" // Enable small sectors
options[localplugin.AttrFilecoinBinary] = th.MustGetFilecoinBinary() // Get the filecoin binary

genesisURI := env.GenesisCar()
genesisMiner, err := env.GenesisMiner()
require.NoError(err)

fastenvOpts = fast.EnvironmentOpts{
InitOpts: append([]fast.ProcessInitOption{fast.POGenesisFile(genesisURI)}, fastenvOpts.InitOpts...),
DaemonOpts: append([]fast.ProcessDaemonOption{fast.POBlockTime(time.Millisecond)}, fastenvOpts.DaemonOpts...),
}

// Create a node for the test
genesis, err := env.NewProcess(ctx, localplugin.PluginName, options, fastenvOpts)
require.NoError(err)

err = series.SetupGenesisNode(ctx, genesis, genesisMiner.Address, files.NewReaderFile(genesisMiner.Owner))
require.NoError(err)

var MiningOnce series.MiningOnceFunc = func() {
_, err := genesis.MiningOnce(ctx)
require.NoError(err)
}

ctx = context.WithValue(ctx, series.CKMiningOnce, MiningOnce)

NewNode := func() *fast.Filecoin {
p, err := env.NewProcess(ctx, localplugin.PluginName, options, fastenvOpts)
require.NoError(err)

err = series.InitAndStart(ctx, p)
require.NoError(err)

err = series.Connect(ctx, genesis, p)
require.NoError(err)

err = series.SendFilecoinDefaults(ctx, genesis, p, 10000)
require.NoError(err)

return p
}

return env, genesis, NewNode, ctx
}

func TestPaymentChannelCreateSuccess(t *testing.T) {
t.Parallel()
require := require.New(t)
Expand All @@ -108,7 +42,7 @@ func TestPaymentChannelCreateSuccess(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -162,7 +96,7 @@ func TestPaymentChannelLs(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -206,7 +140,7 @@ func TestPaymentChannelLs(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -250,7 +184,7 @@ func TestPaymentChannelLs(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -289,7 +223,7 @@ func TestPaymentChannelVoucherSuccess(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -332,7 +266,7 @@ func TestPaymentChannelRedeemSuccess(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -386,7 +320,7 @@ func TestPaymentChannelRedeemTooEarlyFails(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -440,7 +374,7 @@ func TestPaymentChannelReclaimSuccess(t *testing.T) {
defer cancel()

// Get basic testing environment
env, p, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, p, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -523,7 +457,7 @@ func TestPaymentChannelCloseSuccess(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down Expand Up @@ -588,7 +522,7 @@ func TestPaymentChannelExtendSuccess(t *testing.T) {
defer cancel()

// Get basic testing environment
env, _, NewNode, ctx := BasicFastSetup(t, ctx, fast.EnvironmentOpts{})
env, _, NewNode, ctx := fasting.BasicFastSetup(ctx, t, fast.EnvironmentOpts{})

// Teardown after test ends
defer env.Teardown(ctx)
Expand Down
80 changes: 80 additions & 0 deletions tools/fast/fasting/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package fasting

import (
"context"
"io/ioutil"
"math/big"
"strings"
"testing"
"time"

"gx/ipfs/QmPVkJMTeRC6iBByPWdrRkD3BE5UXsj5HPzb4kPqL186mS/testify/require"
"gx/ipfs/QmQmhotPUzVrMEWNK3x1R5jQ5ZHWyL7tVUrmRPjrBrvyCb/go-ipfs-files"

"github.com/filecoin-project/go-filecoin/testhelpers"
"github.com/filecoin-project/go-filecoin/tools/fast"
"github.com/filecoin-project/go-filecoin/tools/fast/series"
localplugin "github.com/filecoin-project/go-filecoin/tools/iptb-plugins/filecoin/local"
)

// BasicFastSetup creates a environment and a single node, and environment options
func BasicFastSetup(ctx context.Context, t *testing.T, fastenvOpts fast.EnvironmentOpts) (fast.Environment, *fast.Filecoin, func() *fast.Filecoin, context.Context) {
require := require.New(t)

// Create a directory for the test using the test name (mostly for FAST)
// TODO(tperson) use a different TempDir that uses MkdirAll
dir, err := ioutil.TempDir("", strings.Replace(t.Name(), "/", ".", -1))
require.NoError(err)

// Create an environment that includes a genesis block with 1MM FIL
env, err := fast.NewEnvironmentMemoryGenesis(big.NewInt(1000000), dir)
require.NoError(err)

// Setup options for nodes.
options := make(map[string]string)
options[localplugin.AttrLogJSON] = "1" // Enable JSON logs
options[localplugin.AttrLogLevel] = "5" // Set log level to Debug
options[localplugin.AttrUseSmallSectors] = "true" // Enable small sectors
options[localplugin.AttrFilecoinBinary] = testhelpers.MustGetFilecoinBinary() // Get the filecoin binary

genesisURI := env.GenesisCar()
genesisMiner, err := env.GenesisMiner()
require.NoError(err)

fastenvOpts = fast.EnvironmentOpts{
InitOpts: append([]fast.ProcessInitOption{fast.POGenesisFile(genesisURI)}, fastenvOpts.InitOpts...),
DaemonOpts: append([]fast.ProcessDaemonOption{fast.POBlockTime(time.Millisecond)}, fastenvOpts.DaemonOpts...),
}

// Create a node for the test
genesis, err := env.NewProcess(ctx, localplugin.PluginName, options, fastenvOpts)
require.NoError(err)

err = series.SetupGenesisNode(ctx, genesis, genesisMiner.Address, files.NewReaderFile(genesisMiner.Owner))
require.NoError(err)

var MiningOnce series.MiningOnceFunc = func() {
_, err := genesis.MiningOnce(ctx)
require.NoError(err)
}

ctx = context.WithValue(ctx, series.CKMiningOnce, MiningOnce)

NewNode := func() *fast.Filecoin {
p, err := env.NewProcess(ctx, localplugin.PluginName, options, fastenvOpts)
require.NoError(err)

err = series.InitAndStart(ctx, p)
require.NoError(err)

err = series.Connect(ctx, genesis, p)
require.NoError(err)

err = series.SendFilecoinDefaults(ctx, genesis, p, 10000)
require.NoError(err)

return p
}

return env, genesis, NewNode, ctx
}

0 comments on commit 900ae8d

Please sign in to comment.