Skip to content

Commit

Permalink
fix: use code coverage feature from interchaintest 8.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency committed Jul 5, 2024
1 parent f1260c0 commit c1025dc
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 171 deletions.
9 changes: 6 additions & 3 deletions interchaintest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ replace (
github.com/ChainSafe/go-schnorrkel/1 => github.com/ChainSafe/go-schnorrkel v1.0.0
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/liftedinit/manifest-ledger => ../

// TODO: Remove when https://github.com/strangelove-ventures/interchaintest/pull/1173 is merged
github.com/strangelove-ventures/interchaintest/v8 => github.com/fmorency/interchaintest/v8 v8.0.0-20240705153035-7061d824d5a8
)

require (
Expand All @@ -18,9 +21,8 @@ require (
github.com/cosmos/cosmos-sdk v0.50.6
github.com/cosmos/gogoproto v1.4.12
github.com/cosmos/ibc-go/v8 v8.2.1
github.com/docker/docker v24.0.9+incompatible
github.com/liftedinit/manifest-ledger v0.0.0-00000000000000-000000000000
github.com/strangelove-ventures/interchaintest/v8 v8.4.0
github.com/strangelove-ventures/interchaintest/v8 v8.5.0
github.com/strangelove-ventures/poa v0.50.1
github.com/strangelove-ventures/tokenfactory v0.50.1
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -95,6 +97,7 @@ require (
github.com/dgraph-io/badger/v4 v4.2.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.9+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand Down Expand Up @@ -171,7 +174,7 @@ require (
github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/misko9/go-substrate-rpc-client/v4 v4.0.0-20230913220906-b988ea7da0c2 // indirect
github.com/misko9/go-substrate-rpc-client/v4 v4.0.0-20240603204351-26b456ae3afe // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions interchaintest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fmorency/interchaintest/v8 v8.0.0-20240705153035-7061d824d5a8 h1:FSAbAZdICdBaYFvCWTQcOaxcP6WWkIPCnWjslNA0t3g=
github.com/fmorency/interchaintest/v8 v8.0.0-20240705153035-7061d824d5a8/go.mod h1:UPMvR1joIXO8RMxDozoHIInLZWmd2345PSGLJYWyrwY=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
Expand Down Expand Up @@ -824,8 +826,8 @@ github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/misko9/go-substrate-rpc-client/v4 v4.0.0-20230913220906-b988ea7da0c2 h1:G/cVeTAbB9S/6FSWWqpFV0v49hiuHLbJPu9hTZ0UR2A=
github.com/misko9/go-substrate-rpc-client/v4 v4.0.0-20230913220906-b988ea7da0c2/go.mod h1:Q5BxOd9FxJqYp4vCiLGVdetecPcWTmUQIu0bRigYosU=
github.com/misko9/go-substrate-rpc-client/v4 v4.0.0-20240603204351-26b456ae3afe h1:0fcCSfvBgbagEsEMkZuxgA3Ex7IN9i1Hon0fwgMLpQw=
github.com/misko9/go-substrate-rpc-client/v4 v4.0.0-20240603204351-26b456ae3afe/go.mod h1:Q5BxOd9FxJqYp4vCiLGVdetecPcWTmUQIu0bRigYosU=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
Expand Down Expand Up @@ -1043,8 +1045,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
github.com/strangelove-ventures/interchaintest/v8 v8.4.0 h1:UHLmJfmkFXuJHfSE8qmOuEy4FWZWuRw4G6XZHm9hC6w=
github.com/strangelove-ventures/interchaintest/v8 v8.4.0/go.mod h1:nfPgRi1yjnzi+qF+0Fs9qN9kkS1Fk0oqgpKvqg5a200=
github.com/strangelove-ventures/poa v0.50.1 h1:84OjmMrJEvQxfNKsgUW4aCeeqTqA0dswCAF7J+017Dg=
github.com/strangelove-ventures/poa v0.50.1/go.mod h1:Jmdk38G8DN3rrvPfQwVF60uUzVmn4Q7k2N8Q5eSRb6Y=
github.com/strangelove-ventures/tokenfactory v0.50.1 h1:4P+wux9HEk+apmT3nrKsuCvPbWFNOHApaYYsqX5/zY8=
Expand Down
20 changes: 5 additions & 15 deletions interchaintest/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package interchaintest

import (
"context"
"fmt"
"path"
"testing"

"cosmossdk.io/math"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
interchaintestrelayer "github.com/strangelove-ventures/interchaintest/v8/relayer"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
Expand All @@ -21,22 +20,13 @@ import (
func TestIBC(t *testing.T) {
ctx := context.Background()

// Same as ChainNode.HomeDir() but we need it before the chain is created
// The node volume is always mounted at /var/cosmos-chain/[chain-name]
// This is a hackish way to get the coverage files from the ephemeral containers
cfgA := LocalChainConfig
cfgA.ChainID = "manifest-9"
internalGoCoverDirA := path.Join("/var/cosmos-chain", cfgA.ChainID)
cfgA.Env = []string{
fmt.Sprintf("GOCOVERDIR=%s", internalGoCoverDirA),
}
cfgA.WithCodeCoverage()

cfgB := LocalChainConfig
cfgB.ChainID = "manifest-10"
internalGoCoverDirB := path.Join("/var/cosmos-chain", cfgB.ChainID)
cfgB.Env = []string{
fmt.Sprintf("GOCOVERDIR=%s", internalGoCoverDirB),
}
cfgB.WithCodeCoverage()

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t, zaptest.Level(zapcore.DebugLevel)), []*interchaintest.ChainSpec{
{
Expand Down Expand Up @@ -147,7 +137,7 @@ func TestIBC(t *testing.T) {
require.True(t, osmosUserBalNew.Equal(amountToSend))

t.Cleanup(func() {
CopyCoverageFromContainer(ctx, t, client, manifestA.GetNode().ContainerID(), manifestA.HomeDir())
CopyCoverageFromContainer(ctx, t, client, manifestB.GetNode().ContainerID(), manifestB.HomeDir())
dockerutil.CopyCoverageFromContainer(ctx, t, client, manifestA.GetNode().ContainerID(), manifestA.HomeDir(), ExternalGoCoverDir)
_ = ic.Close()
})
}
10 changes: 3 additions & 7 deletions interchaintest/mainfest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package interchaintest
import (
"context"
"fmt"
"path"
"testing"

sdkmath "cosmossdk.io/math"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
Expand All @@ -23,15 +23,11 @@ import (
func TestManifestModule(t *testing.T) {
ctx := context.Background()

// Same as ChainNode.HomeDir() but we need it before the chain is created
// The node volume is always mounted at /var/cosmos-chain/[chain-name]
// This is a hackish way to get the coverage files from the ephemeral containers
cfgA := LocalChainConfig
internalGoCoverDir := path.Join("/var/cosmos-chain", cfgA.ChainID)
cfgA.Env = []string{
fmt.Sprintf("POA_ADMIN_ADDRESS=%s", accAddr),
fmt.Sprintf("GOCOVERDIR=%s", internalGoCoverDir),
}
cfgA.WithCodeCoverage()

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t, zaptest.Level(zapcore.DebugLevel)), []*interchaintest.ChainSpec{
{
Expand Down Expand Up @@ -204,7 +200,7 @@ func TestManifestModule(t *testing.T) {
})

t.Cleanup(func() {
CopyCoverageFromContainer(ctx, t, client, appChain.GetNode().ContainerID(), appChain.HomeDir())
dockerutil.CopyCoverageFromContainer(ctx, t, client, manifestA.GetNode().ContainerID(), manifestA.HomeDir(), ExternalGoCoverDir)
_ = ic.Close()
})
}
18 changes: 6 additions & 12 deletions interchaintest/poa_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package interchaintest
import (
"context"
"fmt"
"path"
"strconv"
"testing"
"time"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/cosmos/gogoproto/proto"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
poatypes "github.com/strangelove-ventures/poa"
tokenfactorytypes "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/types"
Expand Down Expand Up @@ -86,11 +86,7 @@ func TestGroupPOA(t *testing.T) {
t.Skip("skipping in short mode")
}

// Same as ChainNode.HomeDir() but we need it before the chain is created
// The node volume is always mounted at /var/cosmos-chain/[chain-name]
// This is a hackish way to get the coverage files from the ephemeral containers
name := "group-poa"
internalGoCoverDir := path.Join("/var/cosmos-chain", name)

err := groupPolicy.SetDecisionPolicy(createThresholdDecisionPolicy("1", 10*time.Second, 0*time.Second))
require.NoError(t, err)
Expand All @@ -106,26 +102,24 @@ func TestGroupPOA(t *testing.T) {
groupGenesis := createGroupGenesis()

cfgA := LocalChainConfig
cfgA.Name = name
cfgA.ModifyGenesis = cosmos.ModifyGenesis(groupGenesis)
cfgA.Env = []string{
fmt.Sprintf("GOCOVERDIR=%s", internalGoCoverDir),
fmt.Sprintf("POA_ADMIN_ADDRESS=%s", groupAddr), // This is required in order for GetPoAAdmin to return the Group address
}
cfgA.WithCodeCoverage()

// setup base chain
chains := interchaintest.CreateChainWithConfig(t, numVals, numNodes, name, "", cfgA)
chain := chains[0].(*cosmos.CosmosChain)

ctx, _, client, _ := interchaintest.BuildInitialChain(t, chains, false)
ctx, ic, client, _ := interchaintest.BuildInitialChain(t, chains, false)

user1Wallet, err := interchaintest.GetAndFundTestUserWithMnemonic(ctx, user1, accMnemonic, DefaultGenesisAmt, chain)
require.NoError(t, err)
_, err = interchaintest.GetAndFundTestUserWithMnemonic(ctx, user2, acc1Mnemonic, DefaultGenesisAmt, chain)
require.NoError(t, err)

// Make sure the chain's HomeDir and the GOCOVERDIR are the same
require.Equal(t, internalGoCoverDir, chain.GetNode().HomeDir())

// Software Upgrade
testSoftwareUpgrade(t, ctx, chain, &cfgA, accAddr)
// Manifest module
Expand All @@ -139,8 +133,8 @@ func TestGroupPOA(t *testing.T) {
testBankSend(t, ctx, chain, &cfgA, accAddr)
testBankSendIllegal(t, ctx, chain, &cfgA, accAddr)
t.Cleanup(func() {
// Copy coverage files from the container
CopyCoverageFromContainer(ctx, t, client, chain.GetNode().ContainerID(), chain.HomeDir())
dockerutil.CopyCoverageFromContainer(ctx, t, client, chain.GetNode().ContainerID(), chain.HomeDir(), ExternalGoCoverDir)
_ = ic.Close()
})
}

Expand Down
72 changes: 7 additions & 65 deletions interchaintest/poa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ package interchaintest

import (
"context"
"encoding/json"
"fmt"
"path"
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
"github.com/strangelove-ventures/poa"
"github.com/stretchr/testify/require"

Expand All @@ -23,7 +19,6 @@ import (
const (
// cosmos1hj5fveer5cjtn4wd6wstzugjfdxzl0xpxvjjvr (test_node.sh) accMnemonic
acc1Mnemonic = "wealth flavor believe regret funny network recall kiss grape useless pepper cram hint member few certain unveil rather brick bargain curious require crowd raise"
userFunds = 10_000_000_000
numVals = 2
numNodes = 0
)
Expand All @@ -33,26 +28,17 @@ func TestPOA(t *testing.T) {
t.Skip("skipping in short mode")
}

// Same as ChainNode.HomeDir() but we need it before the chain is created
// The node volume is always mounted at /var/cosmos-chain/[chain-name]
// This is a hackish way to get the coverage files from the ephemeral containers
name := "poa"
internalGoCoverDir := path.Join("/var/cosmos-chain", name)

cfgA := LocalChainConfig
cfgA.Env = []string{
fmt.Sprintf("GOCOVERDIR=%s", internalGoCoverDir),
}
cfgA.Name = name
cfgA.WithCodeCoverage()

// setup base chain
chains := interchaintest.CreateChainWithConfig(t, numVals, numNodes, name, "", cfgA)
chain := chains[0].(*cosmos.CosmosChain)

enableBlockDB := false
ctx, _, client, _ := interchaintest.BuildInitialChain(t, chains, enableBlockDB)

// Make sure the chain's HomeDir and the GOCOVERDIR are the same
require.Equal(t, internalGoCoverDir, chain.GetNode().HomeDir())
ctx, ic, client, _ := interchaintest.BuildInitialChain(t, chains, enableBlockDB)

// setup accounts
acc0, err := interchaintest.GetAndFundTestUserWithMnemonic(ctx, "acc0", accMnemonic, DefaultGenesisAmt, chain)
Expand Down Expand Up @@ -85,49 +71,11 @@ func TestPOA(t *testing.T) {

t.Cleanup(func() {
// Copy coverage files from the container
CopyCoverageFromContainer(ctx, t, client, chain.GetNode().ContainerID(), chain.HomeDir())
dockerutil.CopyCoverageFromContainer(ctx, t, client, chain.GetNode().ContainerID(), chain.HomeDir(), ExternalGoCoverDir)
_ = ic.Close()
})
}

// createAuthzJSON generates a JSON file for an authorization message.
// This is a copy of interchaintest/chain/cosmos/module_authz.go#createAuthzJSON with the addition of the chain environment variables to the Exec call
func createAuthzJSON(ctx context.Context, chain *cosmos.CosmosChain, filePath string, genMsgCmd []string) error {
if !strings.Contains(strings.Join(genMsgCmd, " "), "--generate-only") {
genMsgCmd = append(genMsgCmd, "--generate-only")
}

res, resErr, err := chain.GetNode().Exec(ctx, genMsgCmd, chain.Config().Env)
if resErr != nil {
return fmt.Errorf("failed to generate msg: %s", resErr)
}
if err != nil {
return err
}

return chain.GetNode().WriteFile(ctx, res, filePath)
}

// ExecTx executes a transaction, waits for 2 blocks if successful, then returns the tx hash.
// This is a copy of interchaintest/chain/cosmos/chain_node.go#ExecTx with the addition of the chain environment variables to the Exec call
func ExecTx(ctx context.Context, chain *cosmos.CosmosChain, keyName string, command ...string) (string, error) {
stdout, _, err := chain.GetNode().Exec(ctx, chain.GetNode().TxCommand(keyName, command...), chain.Config().Env)
if err != nil {
return "", err
}
output := cosmos.CosmosTx{}
err = json.Unmarshal([]byte(stdout), &output)
if err != nil {
return "", err
}
if output.Code != 0 {
return output.TxHash, fmt.Errorf("transaction failed with code %d: %s", output.Code, output.RawLog)
}
if err := testutil.WaitForBlocks(ctx, 2, chain.GetNode()); err != nil {
return "", err
}
return output.TxHash, nil
}

func testStakingDisabled(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, validators []string, acc0, acc1 ibc.Wallet) {
t.Log("\n===== TEST STAKING DISABLED =====")

Expand All @@ -147,13 +95,7 @@ func testStakingDisabled(t *testing.T, ctx context.Context, chain *cosmos.Cosmos
nested := []string{"tx", "staking", "delegate", validators[0], "1stake"}
nestedCmd := helpers.TxCommandBuilder(ctx, chain, nested, granter.FormattedAddress())

// Execute nested message via a wrapped Exec
// Workaround AuthzExec which doesn't propagate the environment variables to the Exec call
fileName := "authz.json"
err = createAuthzJSON(ctx, chain, fileName, nestedCmd)
require.NoError(t, err)

_, err = ExecTx(ctx, chain, grantee.KeyName(), []string{"authz", "exec", path.Join(chain.GetNode().HomeDir(), fileName)}...)
_, err = chain.GetNode().AuthzExec(ctx, grantee, nestedCmd)
require.Error(t, err)
require.ErrorContains(t, err, poa.ErrStakingActionNotAllowed.Error())
}
Expand Down
Loading

0 comments on commit c1025dc

Please sign in to comment.