-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup unit test for vote extension testing
- Loading branch information
1 parent
291bf66
commit 50afbc3
Showing
10 changed files
with
216 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
|
||
mockgen_cmd="mockgen" | ||
$mockgen_cmd -source=voteextension/expected_keepers.go -package testutil -destination voteextension/testutil/expected_keepers_mocks.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package voteextension | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/cosmos/ibc-go/v9/modules/core/exported" | ||
) | ||
|
||
type ClientKeeper interface { | ||
UpdateClient(ctx sdk.Context, clientID string, clientMsg exported.ClientMessage) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"google.golang.org/grpc" | ||
|
||
"github.com/cosmos/interchain-attestation/core/types" | ||
) | ||
|
||
type Server struct { | ||
types.UnimplementedSidecarServer | ||
grpcServer *grpc.Server | ||
|
||
Response *types.GetAttestationsResponse | ||
} | ||
|
||
var _ types.SidecarServer = &Server{} | ||
|
||
func NewServer() *Server { | ||
return &Server{} | ||
} | ||
|
||
func (s *Server) Serve(listenAddr string) error { | ||
lis, err := net.Listen("tcp", listenAddr) | ||
if err != nil { | ||
return fmt.Errorf("failed to start server: %w", err) | ||
} | ||
|
||
s.grpcServer = grpc.NewServer() | ||
types.RegisterSidecarServer(s.grpcServer, s) | ||
if err := s.grpcServer.Serve(lis); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Server) Stop() { | ||
s.grpcServer.GracefulStop() | ||
} | ||
|
||
func (s *Server) GetAttestations(_ context.Context, _ *types.GetAttestationsRequest) (*types.GetAttestationsResponse, error) { | ||
return s.Response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,92 @@ | ||
package voteextension_test | ||
|
||
import ( | ||
fmt "fmt" | ||
"os" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/exp/rand" | ||
|
||
storetypes "cosmossdk.io/store/types" | ||
|
||
sdktestutil "github.com/cosmos/cosmos-sdk/testutil" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
|
||
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" | ||
|
||
"github.com/cosmos/interchain-attestation/core/types" | ||
"github.com/cosmos/interchain-attestation/core/voteextension" | ||
"github.com/cosmos/interchain-attestation/core/voteextension/testutil" | ||
) | ||
|
||
func TestExtendVote(t *testing.T) { | ||
/*testKey := storetypes.NewKVStoreKey("upgrade") | ||
ctx := testutil.DefaultContext(testKey, storetypes.NewTransientStoreKey("transient_test")) | ||
testKey := storetypes.NewKVStoreKey("upgrade") | ||
ctx := sdktestutil.DefaultContext(testKey, storetypes.NewTransientStoreKey("transient_test")) | ||
|
||
randomPort := rand.Intn(65535-49152) + 49152 | ||
addr := fmt.Sprintf("localhost:%d", randomPort) | ||
|
||
err := os.Setenv(attestationabci.SidecarAddressEnv, "") | ||
mockServer := testutil.NewServer() | ||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
err := mockServer.Serve(addr) | ||
require.NoError(t, err) | ||
wg.Done() | ||
}() | ||
time.Sleep(1 * time.Second) | ||
|
||
err := os.Setenv(voteextension.SidecarAddressEnv, addr) | ||
require.NoError(t, err) | ||
|
||
appModule := attestationabci.NewAppModule() | ||
ctrl := gomock.NewController(t) | ||
clientKeeper := testutil.NewMockClientKeeper(ctrl) | ||
|
||
encodingCfg := moduletestutil.MakeTestEncodingConfig() | ||
appModule := voteextension.NewAppModule(clientKeeper, encodingCfg.Codec) | ||
|
||
mockServer.Response = &types.GetAttestationsResponse{ | ||
Attestations: []types.Attestation{ | ||
{ | ||
AttestatorId: []byte("mock-attestor-id"), | ||
AttestedData: types.IBCData{ | ||
ChainId: "mock-chain-id", | ||
ClientId: "mock-client-id", | ||
ClientToUpdate: "mock-client-to-update", | ||
Height: clienttypes.NewHeight(1, 1), | ||
Timestamp: time.Now(), | ||
PacketCommitments: [][]byte{ | ||
[]byte("pckt1"), | ||
[]byte("pckt2"), | ||
}, | ||
}, | ||
Signature: []byte("sig"), | ||
}, | ||
}, | ||
} | ||
responseExtendVote, err := appModule.ExtendVote(ctx, &abci.RequestExtendVote{}) | ||
require.NoError(t, err) | ||
_ = responseExtendVote*/ | ||
// TODO: Create a mock sidecar server and set the address to the environment variable and test with that | ||
require.NotEmpty(t, responseExtendVote.VoteExtension) | ||
|
||
var voteExt voteextension.VoteExtension | ||
err = encodingCfg.Codec.Unmarshal(responseExtendVote.VoteExtension, &voteExt) | ||
require.NoError(t, err) | ||
require.Len(t, voteExt.Attestations, 1) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestatorId, voteExt.Attestations[0].AttestatorId) | ||
require.Equal(t, mockServer.Response.Attestations[0].Signature, voteExt.Attestations[0].Signature) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestedData.ChainId, voteExt.Attestations[0].AttestedData.ChainId) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestedData.ClientId, voteExt.Attestations[0].AttestedData.ClientId) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestedData.ClientToUpdate, voteExt.Attestations[0].AttestedData.ClientToUpdate) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestedData.Height, voteExt.Attestations[0].AttestedData.Height) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestedData.Timestamp.UnixNano(), voteExt.Attestations[0].AttestedData.Timestamp.UnixNano()) | ||
|
||
require.Len(t, voteExt.Attestations[0].AttestedData.PacketCommitments, 2) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestedData.PacketCommitments[0], voteExt.Attestations[0].AttestedData.PacketCommitments[0]) | ||
require.Equal(t, mockServer.Response.Attestations[0].AttestedData.PacketCommitments[1], voteExt.Attestations[0].AttestedData.PacketCommitments[1]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters