Skip to content

Commit

Permalink
Added basic sample of rewriting CLI integration test in GO
Browse files Browse the repository at this point in the history
  • Loading branch information
Artemkaaas committed Nov 7, 2024
1 parent 2ef3fe9 commit abada72
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 0 deletions.
57 changes: 57 additions & 0 deletions integration_tests/cli_go/dclauth/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 DSR Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dclauth_test_cli

import (

Check failure on line 17 in integration_tests/cli_go/dclauth/cli_test.go

View workflow job for this annotation

GitHub Actions / Check linter issues with golangci-lint tool

File is not `goimports`-ed (goimports)
"github.com/stretchr/testify/require"
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers"
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
"testing"
)

func TestAuthDemoCLI(t *testing.T) {
suite := utils.SetupTest(t, testconstants.ChainID, false)

jack := testconstants.JackAccount
alice := testconstants.AliceAccount

user1 := helpers.CreateAccountInfo(&suite)

// Propose user1 account by jack
txResult, err := ProposeAccount(user1.Address, user1.Key, dclauthtypes.NodeAdmin, jack)
require.NoError(suite.T, err)
require.Equal(suite.T, txResult.Code, uint32(0))

// Approve user1 account by alice
txResult, err = ApproveAccount(user1.Address, alice)
require.NoError(suite.T, err)
require.Equal(suite.T, txResult.Code, uint32(0))

// await transaction is written
_, err = helpers.AwaitTxConfirmation(txResult.TxHash)
require.NoError(suite.T, err)

// Query list of all active accounts
accounts, err := QueryAccounts()
require.NoError(suite.T, err)
require.True(suite.T, AccountIsInList(user1.Address, accounts.Account))

// Query user1 account
account, err := QueryAccount(user1.Address)
require.NoError(suite.T, err)
require.Equal(suite.T, account.Address, user1.Address)
}
89 changes: 89 additions & 0 deletions integration_tests/cli_go/dclauth/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package dclauth_test_cli

import (
"fmt"

Check failure on line 4 in integration_tests/cli_go/dclauth/helpers.go

View workflow job for this annotation

GitHub Actions / Check linter issues with golangci-lint tool

File is not `goimports`-ed (goimports)
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers"
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
)

func ProposeAccount(
address string,
key string,
role dclauthtypes.AccountRole,
from string) (sdk.TxResponse, error) {
return helpers.Tx(
"auth",
"propose-add-account",
from,
fmt.Sprintf("--address=%s", address),
fmt.Sprintf("--pubkey=%s", key),
fmt.Sprintf("--roles=%s", string(role)))
}

func ApproveAccount(
address string,
from string) (sdk.TxResponse, error) {
return helpers.Tx(
"auth",
"approve-add-account",
from,
fmt.Sprintf("--address=%s", address))
}

func QueryAccounts() (dclauthtypes.QueryAllAccountResponse, error) {
res, err := helpers.Query("auth", "all-accounts")
if err != nil {
return dclauthtypes.QueryAllAccountResponse{}, err
}

var resp dclauthtypes.QueryAllAccountResponse
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
if err != nil {
return dclauthtypes.QueryAllAccountResponse{}, err
}

return resp, nil
}

func QueryAccount(address string) (dclauthtypes.Account, error) {
res, err := helpers.Query(
"auth",
"account",
fmt.Sprintf("--address=%s", address))
if err != nil {
return dclauthtypes.Account{}, err
}

var resp dclauthtypes.Account
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
if err != nil {
return dclauthtypes.Account{}, err
}

return resp, nil
}

func QueryPendingAccounts() (dclauthtypes.QueryAllPendingAccountResponse, error) {
res, err := helpers.Query("auth", "all-proposed-accounts")
if err != nil {
return dclauthtypes.QueryAllPendingAccountResponse{}, err
}

var resp dclauthtypes.QueryAllPendingAccountResponse
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
if err != nil {
return dclauthtypes.QueryAllPendingAccountResponse{}, err
}

return resp, nil
}

func AccountIsInList(expected string, accounts []dclauthtypes.Account) bool {
for _, account := range accounts {
if expected == account.Address {
return true
}
}
return false

Check failure on line 88 in integration_tests/cli_go/dclauth/helpers.go

View workflow job for this annotation

GitHub Actions / Check linter issues with golangci-lint tool

return with no blank line before (nlreturn)
}
40 changes: 40 additions & 0 deletions integration_tests/cli_go/helpers/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package helpers

import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
)

type AccountInfo struct {
Name string
Address string
Key string
}

func CreateAccountInfo(suite *utils.TestSuite) AccountInfo {
name := RandomString()
entropySeed, _ := bip39.NewEntropy(256)
mnemonic, _ := bip39.NewMnemonic(entropySeed)
account, _ := suite.Kr.NewAccount(name, mnemonic, testconstants.Passphrase, sdk.FullFundraiserPath, hd.Secp256k1)

address, _ := account.GetAddress()
pubKey, _ := account.GetPubKey()

return AccountInfo{
Name: name,
Address: address.String(),
Key: FormatKey(pubKey),
}
}

func FormatKey(pk cryptotypes.PubKey) string {
apk, _ := codectypes.NewAnyWithValue(pk)
bz, _ := codec.ProtoMarshalJSON(apk, nil)
return string(bz)

Check failure on line 39 in integration_tests/cli_go/helpers/account.go

View workflow job for this annotation

GitHub Actions / Check linter issues with golangci-lint tool

return with no blank line before (nlreturn)
}
23 changes: 23 additions & 0 deletions integration_tests/cli_go/helpers/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helpers

import (
"github.com/cosmos/cosmos-sdk/codec"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/zigbee-alliance/distributed-compliance-ledger/app"
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
)

var (
Codec codec.Codec
)

func init() {
encodingConfig := app.MakeEncodingConfig()
govtypesv1.RegisterInterfaces(encodingConfig.InterfaceRegistry)
govtypesv1beta1.RegisterInterfaces(encodingConfig.InterfaceRegistry)
dclauthtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
pkitypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
Codec = encodingConfig.Codec
}
18 changes: 18 additions & 0 deletions integration_tests/cli_go/helpers/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package helpers

import (
"os/exec"
)

const CliBinaryName = "dcld"

func Command(args ...string) ([]byte, error) {
cmd := exec.Command(CliBinaryName, args...)

out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}

return out, err
}
14 changes: 14 additions & 0 deletions integration_tests/cli_go/helpers/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package helpers

import (
tmrand "github.com/cometbft/cometbft/libs/rand"
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
)

func RandomString() string {
return utils.RandString()
}

func RandomVid() int32 {
return int32(tmrand.Uint16())
}
13 changes: 13 additions & 0 deletions integration_tests/cli_go/helpers/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package helpers

func Query(module, command string, queryArgs ...string) (string, error) {
args := []string{"query", module, command}
args = append(args, queryArgs...)

output, err := Command(args...)
if err != nil {
return "", err
}

return string(output), nil
}
48 changes: 48 additions & 0 deletions integration_tests/cli_go/helpers/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package helpers

import (
sdk "github.com/cosmos/cosmos-sdk/types"

Check failure on line 4 in integration_tests/cli_go/helpers/tx.go

View workflow job for this annotation

GitHub Actions / Check linter issues with golangci-lint tool

File is not `goimports`-ed (goimports)
"time"
)

func Tx(module, command, from string, txArgs ...string) (sdk.TxResponse, error) {
args := []string{"tx", module, command}

// TXN arguments
args = append(args, txArgs...)

// Sender account
args = append(args, "--from", from)

// Broadcast
args = append(args, "--yes")

output, err := Command(args...)
if err != nil {
return sdk.TxResponse{}, err
}

var resp sdk.TxResponse
err = Codec.UnmarshalJSON(output, &resp)
if err != nil {
return sdk.TxResponse{}, err
}

return resp, nil
}

func AwaitTxConfirmation(hash string) (string, error) {
var (
result []byte
err error
)
for i := 1; i <= 20; i++ {
result, err = Command("query", "tx", hash)
if err == nil {
return string(result), nil
} else {
time.Sleep(2 * time.Second)
}
}
return "", err

Check failure on line 47 in integration_tests/cli_go/helpers/tx.go

View workflow job for this annotation

GitHub Actions / Check linter issues with golangci-lint tool

return with no blank line before (nlreturn)
}

0 comments on commit abada72

Please sign in to comment.