diff --git a/tests/e2e/asset/cli_test.go b/tests/e2e/asset/cli_test.go index 3621897..158193c 100644 --- a/tests/e2e/asset/cli_test.go +++ b/tests/e2e/asset/cli_test.go @@ -14,3 +14,8 @@ func TestE2ETestSuite(t *testing.T) { cfg.NumValidators = 1 suite.Run(t, NewE2ETestSuite(cfg)) } + +func TestRESTMachineAttestationSuite(t *testing.T) { + cfg := network.DefaultConfig() + suite.Run(t, NewRestE2ETestSuite(cfg)) +} diff --git a/tests/e2e/asset/rest.go b/tests/e2e/asset/rest.go index b4f50f9..70f08f1 100644 --- a/tests/e2e/asset/rest.go +++ b/tests/e2e/asset/rest.go @@ -3,16 +3,61 @@ package asset import ( "fmt" + "github.com/planetmint/planetmint-go/config" + "github.com/planetmint/planetmint-go/lib" "github.com/planetmint/planetmint-go/testutil" + e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e" + "github.com/planetmint/planetmint-go/testutil/network" "github.com/planetmint/planetmint-go/testutil/sample" + "github.com/stretchr/testify/suite" assettypes "github.com/planetmint/planetmint-go/x/asset/types" txtypes "github.com/cosmos/cosmos-sdk/types/tx" ) +type RestE2ETestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network +} + +func NewRestE2ETestSuite(cfg network.Config) *RestE2ETestSuite { + return &RestE2ETestSuite{cfg: cfg} +} + +// SetupSuite initializes asset E2ETestSuite +func (s *RestE2ETestSuite) SetupSuite() { + conf := config.GetConfig() + conf.FeeDenom = "stake" + + s.T().Log("setting up e2e test suite") + + s.network = network.New(s.T()) + err := e2etestutil.AttestMachine(s.network, sample.Name, sample.Mnemonic, 0) + s.Require().NoError(err) + + val := s.network.Validators[0] + k, err := val.ClientCtx.Keyring.Key(sample.Name) + s.Require().NoError(err) + + addr, _ := k.GetAddress() + + clientCtx := val.ClientCtx. + WithFromAddress(addr). + WithFromName(sample.Name) + libConfig := lib.GetConfig() + libConfig.SetClientCtx(clientCtx) +} + +// TearDownSuite clean up after testing +func (s *RestE2ETestSuite) TearDownSuite() { + s.T().Log("tearing down e2e test suite") +} + // TestNotarizeAssetREST notarizes asset over REST endpoint -func (s *E2ETestSuite) TestNotarizeAssetREST() { +func (s *RestE2ETestSuite) TestNotarizeAssetREST() { val := s.network.Validators[0] // Create Msg diff --git a/tests/e2e/asset/suite.go b/tests/e2e/asset/suite.go index d6d1a6a..0252d52 100644 --- a/tests/e2e/asset/suite.go +++ b/tests/e2e/asset/suite.go @@ -7,22 +7,13 @@ import ( "github.com/planetmint/planetmint-go/testutil/sample" clitestutil "github.com/planetmint/planetmint-go/testutil/cli" + e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e" assettypes "github.com/planetmint/planetmint-go/x/asset/types" - machinetypes "github.com/planetmint/planetmint-go/x/machine/types" - "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) -var ( - pubKey string - prvKey string -) - // E2ETestSuite struct definition of asset suite type E2ETestSuite struct { suite.Suite @@ -44,58 +35,20 @@ func (s *E2ETestSuite) SetupSuite() { s.T().Log("setting up e2e test suite") s.network = network.New(s.T()) + err := e2etestutil.AttestMachine(s.network, sample.Name, sample.Mnemonic, 0) + s.Require().NoError(err) + val := s.network.Validators[0] - - kb := val.ClientCtx.Keyring - account, err := kb.NewAccount(sample.Name, sample.Mnemonic, keyring.DefaultBIP39Passphrase, sample.DefaultDerivationPath, hd.Secp256k1) + k, err := val.ClientCtx.Keyring.Key(sample.Name) s.Require().NoError(err) - addr, _ := account.GetAddress() + addr, _ := k.GetAddress() - // sending funds to machine to initialize account on chain - coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000)) - msg1 := banktypes.NewMsgSend(val.Address, addr, coin) - out, err := lib.BroadcastTxWithFileLock(val.Address, msg1) - s.Require().NoError(err) - - s.Require().NoError(s.network.WaitForNextBlock()) - rawLog, err := clitestutil.GetRawLogFromTxOut(val, out) - s.Require().NoError(err) - - assert.Contains(s.T(), rawLog, "cosmos.bank.v1beta1.MsgSend") - - s.Require().NoError(s.network.WaitForNextBlock()) - - prvKey, pubKey = sample.KeyPair() - - ta := sample.TrustAnchor(pubKey) - msg2 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta) - out, err = lib.BroadcastTxWithFileLock(val.Address, msg2) - s.Require().NoError(err) - - s.Require().NoError(s.network.WaitForNextBlock()) - _, err = clitestutil.GetRawLogFromTxOut(val, out) - s.Require().NoError(err) - - s.Require().NoError(s.network.WaitForNextBlock()) - - // name and address of private key with which to sign clientCtx := val.ClientCtx. WithFromAddress(addr). WithFromName(sample.Name) libConfig := lib.GetConfig() libConfig.SetClientCtx(clientCtx) - - machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String()) - msg3 := machinetypes.NewMsgAttestMachine(addr.String(), &machine) - out, err = lib.BroadcastTxWithFileLock(addr, msg3) - s.Require().NoError(err) - - s.Require().NoError(s.network.WaitForNextBlock()) - rawLog, err = clitestutil.GetRawLogFromTxOut(val, out) - s.Require().NoError(err) - - assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgAttestMachine") } // TearDownSuite clean up after testing diff --git a/tests/e2e/dao/pop_participant_selection_suite.go b/tests/e2e/dao/pop_participant_selection_suite.go index cd75529..c88d918 100644 --- a/tests/e2e/dao/pop_participant_selection_suite.go +++ b/tests/e2e/dao/pop_participant_selection_suite.go @@ -3,17 +3,11 @@ package dao import ( "strconv" - "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/planetmint/planetmint-go/config" - "github.com/planetmint/planetmint-go/lib" clitestutil "github.com/planetmint/planetmint-go/testutil/cli" + e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e" "github.com/planetmint/planetmint-go/testutil/network" - "github.com/planetmint/planetmint-go/testutil/sample" daocli "github.com/planetmint/planetmint-go/x/dao/client/cli" - machinetypes "github.com/planetmint/planetmint-go/x/machine/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -55,7 +49,8 @@ func (s *PopSelectionE2ETestSuite) SetupSuite() { // create 2 machines accounts for i, machine := range machines { - s.attestMachine(machine.name, machine.mnemonic, i) + err := e2etestutil.AttestMachine(s.network, machine.name, machine.mnemonic, i) + s.Require().NoError(err) } } @@ -86,45 +81,3 @@ func (s *PopSelectionE2ETestSuite) TestPopSelection() { assert.Contains(s.T(), out.String(), machines[0].address) assert.Contains(s.T(), out.String(), machines[1].address) } - -func (s *PopSelectionE2ETestSuite) attestMachine(name string, mnemonic string, num int) { - val := s.network.Validators[0] - - kb := val.ClientCtx.Keyring - account, err := kb.NewAccount(name, mnemonic, keyring.DefaultBIP39Passphrase, sample.DefaultDerivationPath, hd.Secp256k1) - s.Require().NoError(err) - - addr, _ := account.GetAddress() - - // sending funds to machine to initialize account on chain - coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000)) - sendMsg := banktypes.NewMsgSend(val.Address, addr, coin) - _, err = lib.BroadcastTxWithFileLock(val.Address, sendMsg) - s.Require().NoError(err) - s.Require().NoError(s.network.WaitForNextBlock()) - - // register Ta - prvKey, pubKey := sample.KeyPair(num) - - ta := sample.TrustAnchor(pubKey) - registerMsg := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta) - _, err = lib.BroadcastTxWithFileLock(val.Address, registerMsg) - s.Require().NoError(err) - s.Require().NoError(s.network.WaitForNextBlock()) - - // name and address of private key with which to sign - clientCtx := val.ClientCtx. - WithFromAddress(addr). - WithFromName(name) - libConfig := lib.GetConfig() - libConfig.SetClientCtx(clientCtx) - - machine := sample.Machine(name, pubKey, prvKey, addr.String()) - attestMsg := machinetypes.NewMsgAttestMachine(addr.String(), &machine) - _, err = lib.BroadcastTxWithFileLock(addr, attestMsg) - s.Require().NoError(err) - s.Require().NoError(s.network.WaitForNextBlock()) - - // reset clientCtx to validator ctx - libConfig.SetClientCtx(val.ClientCtx) -} diff --git a/tests/e2e/dao/suite.go b/tests/e2e/dao/suite.go index 5d99215..95f0553 100644 --- a/tests/e2e/dao/suite.go +++ b/tests/e2e/dao/suite.go @@ -9,12 +9,12 @@ import ( "strings" "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" bank "github.com/cosmos/cosmos-sdk/x/bank/client/cli" "github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/lib" clitestutil "github.com/planetmint/planetmint-go/testutil/cli" + e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e" "github.com/planetmint/planetmint-go/testutil/network" "github.com/planetmint/planetmint-go/testutil/sample" daocli "github.com/planetmint/planetmint-go/x/dao/client/cli" @@ -184,20 +184,12 @@ func (s *E2ETestSuite) TestMintToken() { s.Require().NoError(err) // send mint token request from non mint address - kb := val.ClientCtx.Keyring - account, err := kb.NewAccount(sample.Name, sample.Mnemonic, keyring.DefaultBIP39Passphrase, sample.DefaultDerivationPath, hd.Secp256k1) + account, err := e2etestutil.CreateAccount(s.network, sample.Name, sample.Mnemonic) + s.Require().NoError(err) + err = e2etestutil.FundAccount(s.network, account) s.Require().NoError(err) - addr, _ := account.GetAddress() - // sending funds to account to initialize on chain - coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000)) - msg2 := banktypes.NewMsgSend(val.Address, addr, coin) - _, err = lib.BroadcastTxWithFileLock(val.Address, msg2) - s.Require().NoError(err) - - s.Require().NoError(s.network.WaitForNextBlock()) - msg1 = daotypes.NewMsgMintToken(addr.String(), &mintRequest) out, err = lib.BroadcastTxWithFileLock(addr, msg1) s.Require().NoError(err) diff --git a/tests/e2e/machine/cli_test.go b/tests/e2e/machine/cli_test.go index 130ce81..41312c4 100644 --- a/tests/e2e/machine/cli_test.go +++ b/tests/e2e/machine/cli_test.go @@ -14,3 +14,8 @@ func TestE2ETestSuite(t *testing.T) { cfg.NumValidators = 1 suite.Run(t, NewE2ETestSuite(cfg)) } + +func TestRESTMachineAttestationSuite(t *testing.T) { + cfg := network.DefaultConfig() + suite.Run(t, NewRestE2ETestSuite(cfg)) +} diff --git a/tests/e2e/machine/rest.go b/tests/e2e/machine/rest.go index cbe28f3..d5649a7 100644 --- a/tests/e2e/machine/rest.go +++ b/tests/e2e/machine/rest.go @@ -3,14 +3,49 @@ package machine import ( "fmt" + "github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/testutil" + "github.com/planetmint/planetmint-go/testutil/network" "github.com/planetmint/planetmint-go/testutil/sample" machinetypes "github.com/planetmint/planetmint-go/x/machine/types" + "github.com/stretchr/testify/suite" + + e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e" txtypes "github.com/cosmos/cosmos-sdk/types/tx" ) -func (s *E2ETestSuite) TestAttestMachineREST() { +type RestE2ETestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network +} + +func NewRestE2ETestSuite(cfg network.Config) *RestE2ETestSuite { + return &RestE2ETestSuite{cfg: cfg} +} + +func (s *RestE2ETestSuite) SetupSuite() { + cfg := config.GetConfig() + cfg.FeeDenom = "stake" + + s.T().Log("setting up e2e test suite") + + s.network = network.New(s.T()) + // create machine account for attestation + account, err := e2etestutil.CreateAccount(s.network, sample.Name, sample.Mnemonic) + s.Require().NoError(err) + err = e2etestutil.FundAccount(s.network, account) + s.Require().NoError(err) +} + +// TearDownSuite clean up after testing +func (s *RestE2ETestSuite) TearDownSuite() { + s.T().Log("tearing down e2e test suite") +} + +func (s *RestE2ETestSuite) TestAttestMachineREST() { val := s.network.Validators[0] baseURL := val.APIAddress @@ -21,7 +56,7 @@ func (s *E2ETestSuite) TestAttestMachineREST() { addr, err := k.GetAddress() s.Require().NoError(err) - prvKey, pubKey := sample.KeyPair(1) + prvKey, pubKey := sample.KeyPair() // Register TA ta := sample.TrustAnchor(pubKey) diff --git a/tests/e2e/machine/suite.go b/tests/e2e/machine/suite.go index f23cd7a..491e3b3 100644 --- a/tests/e2e/machine/suite.go +++ b/tests/e2e/machine/suite.go @@ -12,8 +12,8 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/feegrant" + e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -39,25 +39,11 @@ func (s *E2ETestSuite) SetupSuite() { s.T().Log("setting up e2e test suite") s.network = network.New(s.T()) - val := s.network.Validators[0] - - kb := val.ClientCtx.Keyring - account, err := kb.NewAccount(sample.Name, sample.Mnemonic, keyring.DefaultBIP39Passphrase, sample.DefaultDerivationPath, hd.Secp256k1) + // create machine account for attestation + account, err := e2etestutil.CreateAccount(s.network, sample.Name, sample.Mnemonic) s.Require().NoError(err) - - addr, _ := account.GetAddress() - - // sending funds to machine to initialize account on chain - coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000)) - msg := banktypes.NewMsgSend(val.Address, addr, coin) - out, err := lib.BroadcastTxWithFileLock(val.Address, msg) + err = e2etestutil.FundAccount(s.network, account) s.Require().NoError(err) - - s.Require().NoError(s.network.WaitForNextBlock()) - rawLog, err := clitestutil.GetRawLogFromTxOut(val, out) - s.Require().NoError(err) - - assert.Contains(s.T(), rawLog, "cosmos.bank.v1beta1.MsgSend") } // TearDownSuite clean up after testing @@ -116,8 +102,8 @@ func (s *E2ETestSuite) TestAttestMachine() { func (s *E2ETestSuite) TestInvalidAttestMachine() { val := s.network.Validators[0] - // already used in REST test case - prvKey, pubKey := sample.KeyPair(1) + // already used in previous test case + prvKey, pubKey := sample.KeyPair() k, err := val.ClientCtx.Keyring.Key(sample.Name) s.Require().NoError(err) diff --git a/testutil/e2e/e2e.go b/testutil/e2e/e2e.go new file mode 100644 index 0000000..59bcbcd --- /dev/null +++ b/testutil/e2e/e2e.go @@ -0,0 +1,118 @@ +package e2e + +import ( + "errors" + "strings" + + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/planetmint/planetmint-go/lib" + clitestutil "github.com/planetmint/planetmint-go/testutil/cli" + "github.com/planetmint/planetmint-go/testutil/network" + "github.com/planetmint/planetmint-go/testutil/sample" + machinetypes "github.com/planetmint/planetmint-go/x/machine/types" +) + +func CreateAccount(network *network.Network, name string, mnemonic string) (account *keyring.Record, err error) { + val := network.Validators[0] + + kb := val.ClientCtx.Keyring + account, err = kb.NewAccount(name, mnemonic, keyring.DefaultBIP39Passphrase, sample.DefaultDerivationPath, hd.Secp256k1) + if err != nil { + return nil, err + } + + return account, nil +} + +func FundAccount(network *network.Network, account *keyring.Record) (err error) { + val := network.Validators[0] + + addr, err := account.GetAddress() + if err != nil { + return err + } + + // sending funds to account to initialize account on chain + coin := sdk.NewCoins(sdk.NewInt64Coin("stake", 1000)) // TODO: make denom dependent on cfg + msg := banktypes.NewMsgSend(val.Address, addr, coin) + out, err := lib.BroadcastTxWithFileLock(val.Address, msg) + if err != nil { + return err + } + + err = network.WaitForNextBlock() + if err != nil { + return err + } + + rawLog, err := clitestutil.GetRawLogFromTxOut(val, out) + if err != nil { + return err + } + + if !strings.Contains(rawLog, "cosmos.bank.v1beta1.MsgSend") { + err = errors.New("failed to fund account") + } + + return +} + +func AttestMachine(network *network.Network, name string, mnemonic string, num int) (err error) { + val := network.Validators[0] + + account, err := CreateAccount(network, name, mnemonic) + if err != nil { + return err + } + + err = FundAccount(network, account) + if err != nil { + return err + } + + // register Ta + prvKey, pubKey := sample.KeyPair(num) + + ta := sample.TrustAnchor(pubKey) + registerMsg := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta) + _, err = lib.BroadcastTxWithFileLock(val.Address, registerMsg) + if err != nil { + return err + } + err = network.WaitForNextBlock() + if err != nil { + return err + } + + addr, err := account.GetAddress() + if err != nil { + return err + } + + // name and address of private key with which to sign + clientCtx := val.ClientCtx. + WithFromAddress(addr). + WithFromName(name) + libConfig := lib.GetConfig() + libConfig.SetClientCtx(clientCtx) + + machine := sample.Machine(name, pubKey, prvKey, addr.String()) + attestMsg := machinetypes.NewMsgAttestMachine(addr.String(), &machine) + _, err = lib.BroadcastTxWithFileLock(addr, attestMsg) + if err != nil { + return err + } + + err = network.WaitForNextBlock() + if err != nil { + return err + } + + // reset clientCtx to validator ctx + libConfig.SetClientCtx(val.ClientCtx) + + return +}