Skip to content

Commit abada72

Browse files
committed
Added basic sample of rewriting CLI integration test in GO
1 parent 2ef3fe9 commit abada72

File tree

8 files changed

+302
-0
lines changed

8 files changed

+302
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2020 DSR Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dclauth_test_cli
16+
17+
import (
18+
"github.com/stretchr/testify/require"
19+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers"
20+
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
21+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
22+
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
23+
"testing"
24+
)
25+
26+
func TestAuthDemoCLI(t *testing.T) {
27+
suite := utils.SetupTest(t, testconstants.ChainID, false)
28+
29+
jack := testconstants.JackAccount
30+
alice := testconstants.AliceAccount
31+
32+
user1 := helpers.CreateAccountInfo(&suite)
33+
34+
// Propose user1 account by jack
35+
txResult, err := ProposeAccount(user1.Address, user1.Key, dclauthtypes.NodeAdmin, jack)
36+
require.NoError(suite.T, err)
37+
require.Equal(suite.T, txResult.Code, uint32(0))
38+
39+
// Approve user1 account by alice
40+
txResult, err = ApproveAccount(user1.Address, alice)
41+
require.NoError(suite.T, err)
42+
require.Equal(suite.T, txResult.Code, uint32(0))
43+
44+
// await transaction is written
45+
_, err = helpers.AwaitTxConfirmation(txResult.TxHash)
46+
require.NoError(suite.T, err)
47+
48+
// Query list of all active accounts
49+
accounts, err := QueryAccounts()
50+
require.NoError(suite.T, err)
51+
require.True(suite.T, AccountIsInList(user1.Address, accounts.Account))
52+
53+
// Query user1 account
54+
account, err := QueryAccount(user1.Address)
55+
require.NoError(suite.T, err)
56+
require.Equal(suite.T, account.Address, user1.Address)
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package dclauth_test_cli
2+
3+
import (
4+
"fmt"
5+
sdk "github.com/cosmos/cosmos-sdk/types"
6+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers"
7+
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
8+
)
9+
10+
func ProposeAccount(
11+
address string,
12+
key string,
13+
role dclauthtypes.AccountRole,
14+
from string) (sdk.TxResponse, error) {
15+
return helpers.Tx(
16+
"auth",
17+
"propose-add-account",
18+
from,
19+
fmt.Sprintf("--address=%s", address),
20+
fmt.Sprintf("--pubkey=%s", key),
21+
fmt.Sprintf("--roles=%s", string(role)))
22+
}
23+
24+
func ApproveAccount(
25+
address string,
26+
from string) (sdk.TxResponse, error) {
27+
return helpers.Tx(
28+
"auth",
29+
"approve-add-account",
30+
from,
31+
fmt.Sprintf("--address=%s", address))
32+
}
33+
34+
func QueryAccounts() (dclauthtypes.QueryAllAccountResponse, error) {
35+
res, err := helpers.Query("auth", "all-accounts")
36+
if err != nil {
37+
return dclauthtypes.QueryAllAccountResponse{}, err
38+
}
39+
40+
var resp dclauthtypes.QueryAllAccountResponse
41+
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
42+
if err != nil {
43+
return dclauthtypes.QueryAllAccountResponse{}, err
44+
}
45+
46+
return resp, nil
47+
}
48+
49+
func QueryAccount(address string) (dclauthtypes.Account, error) {
50+
res, err := helpers.Query(
51+
"auth",
52+
"account",
53+
fmt.Sprintf("--address=%s", address))
54+
if err != nil {
55+
return dclauthtypes.Account{}, err
56+
}
57+
58+
var resp dclauthtypes.Account
59+
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
60+
if err != nil {
61+
return dclauthtypes.Account{}, err
62+
}
63+
64+
return resp, nil
65+
}
66+
67+
func QueryPendingAccounts() (dclauthtypes.QueryAllPendingAccountResponse, error) {
68+
res, err := helpers.Query("auth", "all-proposed-accounts")
69+
if err != nil {
70+
return dclauthtypes.QueryAllPendingAccountResponse{}, err
71+
}
72+
73+
var resp dclauthtypes.QueryAllPendingAccountResponse
74+
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
75+
if err != nil {
76+
return dclauthtypes.QueryAllPendingAccountResponse{}, err
77+
}
78+
79+
return resp, nil
80+
}
81+
82+
func AccountIsInList(expected string, accounts []dclauthtypes.Account) bool {
83+
for _, account := range accounts {
84+
if expected == account.Address {
85+
return true
86+
}
87+
}
88+
return false
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package helpers
2+
3+
import (
4+
"github.com/cosmos/cosmos-sdk/codec"
5+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
6+
"github.com/cosmos/cosmos-sdk/crypto/hd"
7+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
"github.com/cosmos/go-bip39"
10+
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
11+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
12+
)
13+
14+
type AccountInfo struct {
15+
Name string
16+
Address string
17+
Key string
18+
}
19+
20+
func CreateAccountInfo(suite *utils.TestSuite) AccountInfo {
21+
name := RandomString()
22+
entropySeed, _ := bip39.NewEntropy(256)
23+
mnemonic, _ := bip39.NewMnemonic(entropySeed)
24+
account, _ := suite.Kr.NewAccount(name, mnemonic, testconstants.Passphrase, sdk.FullFundraiserPath, hd.Secp256k1)
25+
26+
address, _ := account.GetAddress()
27+
pubKey, _ := account.GetPubKey()
28+
29+
return AccountInfo{
30+
Name: name,
31+
Address: address.String(),
32+
Key: FormatKey(pubKey),
33+
}
34+
}
35+
36+
func FormatKey(pk cryptotypes.PubKey) string {
37+
apk, _ := codectypes.NewAnyWithValue(pk)
38+
bz, _ := codec.ProtoMarshalJSON(apk, nil)
39+
return string(bz)
40+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package helpers
2+
3+
import (
4+
"github.com/cosmos/cosmos-sdk/codec"
5+
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
6+
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
7+
"github.com/zigbee-alliance/distributed-compliance-ledger/app"
8+
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
9+
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
10+
)
11+
12+
var (
13+
Codec codec.Codec
14+
)
15+
16+
func init() {
17+
encodingConfig := app.MakeEncodingConfig()
18+
govtypesv1.RegisterInterfaces(encodingConfig.InterfaceRegistry)
19+
govtypesv1beta1.RegisterInterfaces(encodingConfig.InterfaceRegistry)
20+
dclauthtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
21+
pkitypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
22+
Codec = encodingConfig.Codec
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package helpers
2+
3+
import (
4+
"os/exec"
5+
)
6+
7+
const CliBinaryName = "dcld"
8+
9+
func Command(args ...string) ([]byte, error) {
10+
cmd := exec.Command(CliBinaryName, args...)
11+
12+
out, err := cmd.CombinedOutput()
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
return out, err
18+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package helpers
2+
3+
import (
4+
tmrand "github.com/cometbft/cometbft/libs/rand"
5+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
6+
)
7+
8+
func RandomString() string {
9+
return utils.RandString()
10+
}
11+
12+
func RandomVid() int32 {
13+
return int32(tmrand.Uint16())
14+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package helpers
2+
3+
func Query(module, command string, queryArgs ...string) (string, error) {
4+
args := []string{"query", module, command}
5+
args = append(args, queryArgs...)
6+
7+
output, err := Command(args...)
8+
if err != nil {
9+
return "", err
10+
}
11+
12+
return string(output), nil
13+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package helpers
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
"time"
6+
)
7+
8+
func Tx(module, command, from string, txArgs ...string) (sdk.TxResponse, error) {
9+
args := []string{"tx", module, command}
10+
11+
// TXN arguments
12+
args = append(args, txArgs...)
13+
14+
// Sender account
15+
args = append(args, "--from", from)
16+
17+
// Broadcast
18+
args = append(args, "--yes")
19+
20+
output, err := Command(args...)
21+
if err != nil {
22+
return sdk.TxResponse{}, err
23+
}
24+
25+
var resp sdk.TxResponse
26+
err = Codec.UnmarshalJSON(output, &resp)
27+
if err != nil {
28+
return sdk.TxResponse{}, err
29+
}
30+
31+
return resp, nil
32+
}
33+
34+
func AwaitTxConfirmation(hash string) (string, error) {
35+
var (
36+
result []byte
37+
err error
38+
)
39+
for i := 1; i <= 20; i++ {
40+
result, err = Command("query", "tx", hash)
41+
if err == nil {
42+
return string(result), nil
43+
} else {
44+
time.Sleep(2 * time.Second)
45+
}
46+
}
47+
return "", err
48+
}

0 commit comments

Comments
 (0)