Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: unittest for stakingmiddleware module #426

Open
wants to merge 3 commits into
base: release/v6.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
---
on:
push:
paths:
- '**.go'
- 'go.sum'
pull_request:
paths:
- '**.go'
- 'go.sum'
name: build
on: [push, pull_request]
name: Build

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand All @@ -19,27 +11,27 @@ jobs:
runs-on: ubuntu-latest
name: build
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: '1.20'
- run: go build ./...

tidy:
runs-on: ubuntu-latest
name: tidy
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: '1.20'
- run: |
go mod tidy
CHANGES_IN_REPO=$(git status --porcelain)
if [[ -n "$CHANGES_IN_REPO" ]]; then
echo "Repository is dirty. Showing 'git status' and 'git --no-pager diff' for debugging now:"
git status && git --no-pager diff
exit 1
fi
fi
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.20'
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
26 changes: 13 additions & 13 deletions .github/workflows/interchaintest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ jobs:
push: true
platforms: linux/amd64
tags: |
ghcr.io/notional-labs/centauri-ictest:latest
ghcr.io/composablefi/centauri-ictest:latest
test-start-cosmos-chain:
runs-on: ubuntu-latest
needs: build-and-push-image
steps:
- name: Set up Go 1.19
- name: Set up Go 1.20
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.20'

- name: checkout code
uses: actions/checkout@v3
Expand All @@ -54,10 +54,10 @@ jobs:
runs-on: ubuntu-latest
needs: build-and-push-image
steps:
- name: Set up Go 1.19
- name: Set up Go 1.20
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.20'

- name: checkout code
uses: actions/checkout@v3
Expand All @@ -70,10 +70,10 @@ jobs:
runs-on: ubuntu-latest
needs: build-and-push-image
steps:
- name: Set up Go 1.19
- name: Set up Go 1.20
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.20'

- name: checkout code
uses: actions/checkout@v3
Expand All @@ -86,10 +86,10 @@ jobs:
runs-on: ubuntu-latest
needs: build-and-push-image
steps:
- name: Set up Go 1.19
- name: Set up Go 1.20
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.20'

- name: checkout code
uses: actions/checkout@v3
Expand All @@ -102,10 +102,10 @@ jobs:
# runs-on: ubuntu-latest
# needs: build-and-push-image
# steps:
# - name: Set up Go 1.19
# - name: Set up Go 1.20
# uses: actions/setup-go@v3
# with:
# go-version: 1.19
# go-version: 1.20

# - name: checkout code
# uses: actions/checkout@v3
Expand All @@ -118,10 +118,10 @@ jobs:
# runs-on: ubuntu-latest
# needs: build-and-push-image
# steps:
# - name: Set up Go 1.19
# - name: Set up Go 1.20
# uses: actions/setup-go@v3
# with:
# go-version: 1.19
# go-version: 1.20

# - name: checkout code
# uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: "1.20"
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
name: test
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: '1.20'
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Test
run: go test ./...
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ ARG RUNNER_IMAGE="gcr.io/distroless/static-debian11"
# Builder
# --------------------------------------------------------

FROM golang:${GO_VERSION}-alpine as builder
FROM golang:${GO_VERSION}-alpine3.18 as builder

ARG GIT_VERSION
ARG GIT_COMMIT

RUN apk add --no-cache \
ca-certificates \
musl-dev \
openssl-dev \
build-base \
linux-headers

Expand Down
110 changes: 110 additions & 0 deletions x/stakingmiddleware/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package keeper_test

import (
"testing"
"time"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/notional-labs/composable/v6/app"
"github.com/notional-labs/composable/v6/app/helpers"
stakingmiddlewarekeeper "github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper"
stakingmiddlewaretypes "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
"github.com/stretchr/testify/suite"
)

type KeeperTestSuite struct {
suite.Suite

ctx sdk.Context
// querier sdk.Querier
app *app.ComposableApp
msgServer stakingmiddlewaretypes.MsgServer
}

func (suite *KeeperTestSuite) SetupTest() {
suite.app = helpers.SetupComposableAppWithValSet(suite.T())
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "centauri-1", Time: time.Now().UTC()})
encCfg := moduletestutil.MakeTestEncodingConfig()
key := suite.app.GetKey(stakingmiddlewaretypes.StoreKey)
keeper := stakingmiddlewarekeeper.NewKeeper(
encCfg.Codec,
key,
suite.app.AccountKeeper,
suite.app.BankKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
keeper.RegisterKeepers(suite.app.StakingKeeper)
suite.msgServer = stakingmiddlewarekeeper.NewMsgServerImpl(keeper)
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

var (
newParams = stakingmiddlewaretypes.Params{
BlocksPerEpoch: 10,
AllowUnbondAfterEpochProgressBlockNumber: 7,
}
failParams = stakingmiddlewaretypes.Params{
BlocksPerEpoch: 3,
AllowUnbondAfterEpochProgressBlockNumber: 3,
}
failAllowParams = stakingmiddlewaretypes.Params{
BlocksPerEpoch: 6,
AllowUnbondAfterEpochProgressBlockNumber: 10,
}
)

func (suite *KeeperTestSuite) TestSetParams() {
for _, tc := range []struct {
desc string
expectedParams stakingmiddlewaretypes.Params
malleate func() error
shouldErr bool
expectedErr string
}{
{
desc: "Case success",
expectedParams: newParams,
malleate: func() error {
return suite.app.StakingMiddlewareKeeper.SetParams(suite.ctx, newParams)
},
shouldErr: false,
},
{
desc: "Case fail: BlocksPerEpoch < 5",
expectedParams: failParams,
malleate: func() error {
return suite.app.StakingMiddlewareKeeper.SetParams(suite.ctx, failParams)
},
shouldErr: true,
expectedErr: "BlocksPerEpoch must be greater than or equal to 5",
},
{
desc: "Case fail: BlocksPerEpoch < AllowUnbondAfterEpochProgressBlockNumber",
expectedParams: failAllowParams,
malleate: func() error {
return suite.app.StakingMiddlewareKeeper.SetParams(suite.ctx, failAllowParams)
},
shouldErr: true,
expectedErr: "AllowUnbondAfterEpochProgressBlockNumber must be less than or equal to BlocksPerEpoch",
},
} {
tc := tc
suite.Run(tc.desc, func() {
suite.SetupTest()
err := tc.malleate()
if !tc.shouldErr {
res := suite.app.StakingMiddlewareKeeper.GetParams(suite.ctx)
suite.Equal(res, tc.expectedParams)
} else {
suite.Equal(err.Error(), tc.expectedErr)
}
})
}
}
Loading
Loading