diff --git a/tests/e2e/machine/cli_test.go b/tests/e2e/machine/cli_test.go new file mode 100644 index 0000000..4f289e6 --- /dev/null +++ b/tests/e2e/machine/cli_test.go @@ -0,0 +1,14 @@ +package machine + +import ( + "planetmint-go/testutil/network" + "testing" + + "github.com/stretchr/testify/suite" +) + +func TestE2ETestSuite(t *testing.T) { + cfg := network.DefaultConfig() + cfg.NumValidators = 1 + suite.Run(t, NewE2ETestSuite(cfg)) +} diff --git a/tests/e2e/machine/suite.go b/tests/e2e/machine/suite.go new file mode 100644 index 0000000..2f48049 --- /dev/null +++ b/tests/e2e/machine/suite.go @@ -0,0 +1,79 @@ +package machine + +import ( + "fmt" + clitestutil "planetmint-go/testutil/cli" + "planetmint-go/testutil/network" + machine "planetmint-go/x/machine/client/cli" + + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type E2ETestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network +} + +func NewE2ETestSuite(cfg network.Config) *E2ETestSuite { + return &E2ETestSuite{cfg: cfg} +} + +func (s *E2ETestSuite) SetupSuite() { + s.T().Log("setting up e2e test suite") + + s.network = network.New(s.T()) + + kb := s.network.Validators[0].ClientCtx.Keyring + _, _, err := kb.NewMnemonic("alice", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1) + s.Require().NoError(err) + + _, _, err = kb.NewMnemonic("machine", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1) + s.Require().NoError(err) + + s.Require().NoError(s.network.WaitForNextBlock()) +} + +func (s *E2ETestSuite) TearDownSuite() { + s.T().Log("tearing down e2e test suite") +} + +func (s *E2ETestSuite) TestAttestMachine() { + val := s.network.Validators[0] + + account, err := val.ClientCtx.Keyring.Key("alice") + s.Require().NoError(err) + + addr, _ := account.GetAddress() + s.T().Log(fmt.Sprintf("address: %s", addr)) + + account, err = val.ClientCtx.Keyring.Key("machine") + s.Require().NoError(err) + + addr, _ = account.GetAddress() + s.T().Log(fmt.Sprintf("address: %s", addr)) + + // cmd := machine.CmdAttestMachine() + // args := []string{"{\"name\": \"machine\", \"ticker\": \"machine_ticker\", \"issued\": 1, \"amount\": 1000, \"precision\": 8, \"issuerPlanetmint\": \"A/ZrbETECRq5DNGJZ0aH0DjlV4Y1opMlRfGoEJH454eB\", \"issuerLiquid\": \"A/ZrbETECRq5DNGJZ0aH0DjlV4Y1opMlRfGoEJH454eB\", \"machineId\": \"A/ZrbETECRq5DNGJZ0aH0DjlV4Y1opMlRfGoEJH454eB\", \"metadata\": {\"additionalDataCID\": \"CID\", \"gps\": \"{'Latitude':'-48.876667','Longitude':'-123.393333'}\"}}"} + + // testCmd := &cobra.Command{} + // testCmd.SetArgs(args) + + // err = cmd.RunE(testCmd, args) + // s.Require().NoError(err) + + args := []string{ + "{\"name\": \"machine\", \"ticker\": \"machine_ticker\", \"issued\": 1, \"amount\": 1000, \"precision\": 8, \"issuerPlanetmint\": \"A/ZrbETECRq5DNGJZ0aH0DjlV4Y1opMlRfGoEJH454eB\", \"issuerLiquid\": \"A/ZrbETECRq5DNGJZ0aH0DjlV4Y1opMlRfGoEJH454eB\", \"machineId\": \"A/ZrbETECRq5DNGJZ0aH0DjlV4Y1opMlRfGoEJH454eB\", \"metadata\": {\"additionalDataCID\": \"CID\", \"gps\": \"{'Latitude':'-48.876667','Longitude':'-123.393333'}\"}}", + // addr.String(), + fmt.Sprintf("--%s=%s", flags.FlagFrom, addr.String()), + } + + _, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machine.CmdAttestMachine(), args) + s.Require().NoError(err) +} diff --git a/testutil/cli/cmd.go b/testutil/cli/cmd.go new file mode 100644 index 0000000..6662ddd --- /dev/null +++ b/testutil/cli/cmd.go @@ -0,0 +1,26 @@ +package cli + +import ( + "context" + "planetmint-go/testutil" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/spf13/cobra" +) + +// ExecTestCLICmd builds the client context, mocks the output and executes the command. +func ExecTestCLICmd(clientCtx client.Context, cmd *cobra.Command, extraArgs []string) (testutil.BufferWriter, error) { + cmd.SetArgs(extraArgs) + + _, out := testutil.ApplyMockIO(cmd) + clientCtx = clientCtx.WithOutput(out) + + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + + if err := cmd.ExecuteContext(ctx); err != nil { + return out, err + } + + return out, nil +} diff --git a/testutil/ioutil.go b/testutil/ioutil.go new file mode 100644 index 0000000..8bb8e30 --- /dev/null +++ b/testutil/ioutil.go @@ -0,0 +1,32 @@ +package testutil + +import ( + "bytes" + "io" + "strings" + + "github.com/spf13/cobra" +) + +type BufferReader interface { + io.Reader + Reset(string) +} + +type BufferWriter interface { + io.Writer + Reset() + Bytes() []byte + String() string +} + +func ApplyMockIO(c *cobra.Command) (BufferReader, BufferWriter) { + mockIn := strings.NewReader("") + mockOut := bytes.NewBufferString("") + + c.SetIn(mockIn) + c.SetOut(mockOut) + c.SetErr(mockOut) + + return mockIn, mockOut +}