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

feat: setup test suite #90

Merged
merged 32 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 28 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
18 changes: 10 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ on:

jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/reusable_go_lint_test.yml@v0.6.0
uses: babylonlabs-io/.github/.github/workflows/reusable_go_lint_test.yml@v0.7.0
with:
run-unit-tests: true
run-integration-tests: false
run-lint: false
run-build: true
run-gosec: true
gosec-args: "-no-fail ./..."
go-version: '1.23'
go-lint-version: 'v1.60.2'
run-unit-tests: true
run-integration-tests: true
run-lint: true
run-build: true
run-gosec: true
gosec-args: "-no-fail ./..."

docker_pipeline:
uses: babylonlabs-io/.github/.github/workflows/reusable_docker_pipeline.yml@v0.6.0
uses: babylonlabs-io/.github/.github/workflows/reusable_docker_pipeline.yml@v0.7.0
secrets: inherit
with:
publish: false
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ on:

jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/reusable_go_lint_test.yml@v0.6.0
uses: babylonlabs-io/.github/.github/workflows/reusable_go_lint_test.yml@v0.7.0
with:
go-version: '1.23'
go-lint-version: 'v1.60.2'
run-unit-tests: true
run-integration-tests: false
run-lint: false
run-integration-tests: true

docker_pipeline:
needs: ["lint_test"]
uses: babylonlabs-io/.github/.github/workflows/reusable_docker_pipeline.yml@v0.6.0
uses: babylonlabs-io/.github/.github/workflows/reusable_docker_pipeline.yml@v0.7.0
secrets: inherit
with:
publish: true
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TOOLS_DIR := tools

PACKAGES_E2E=$(shell go list ./... | grep '/e2etest')
BUILDDIR ?= $(CURDIR)/build

ldflags := $(LDFLAGS)
Expand Down Expand Up @@ -50,3 +50,7 @@ generate-mock-interface:
test:
./bin/local-startup.sh;
go test -v -cover ./...

test-e2e:
./bin/local-startup.sh;
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e
11 changes: 9 additions & 2 deletions cmd/babylon-staking-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ func main() {
}

// Create a basic zap logger
zapLogger, _ := zap.NewProduction()
defer zapLogger.Sync()
zapLogger, err := zap.NewProduction()
if err != nil {
log.Fatal().Err(err).Msg("error while creating zap logger")
}
defer func() {
if err := zapLogger.Sync(); err != nil {
log.Fatal().Err(err).Msg("error while syncing zap logger")
}
}()

queueConsumer, err := queuemngr.NewQueueManager(&cfg.Queue, zapLogger)
if err != nil {
Expand Down
122 changes: 122 additions & 0 deletions e2etest/bitcoind_node_setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package e2etest

import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/babylonlabs-io/babylon-staking-indexer/e2etest/container"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
)

var (
startTimeout = 30 * time.Second
)

type CreateWalletResponse struct {
Name string `json:"name"`
Warning string `json:"warning"`
}

type GenerateBlockResponse struct {
// address of the recipient of rewards
Address string `json:"address"`
// blocks generated
Blocks []string `json:"blocks"`
}

type BitcoindTestHandler struct {
t *testing.T
m *container.Manager
}

func NewBitcoindHandler(t *testing.T, manager *container.Manager) *BitcoindTestHandler {
return &BitcoindTestHandler{
t: t,
m: manager,
}
}

func (h *BitcoindTestHandler) Start(t *testing.T) *dockertest.Resource {
tempPath, err := os.MkdirTemp("", "vigilante-test-*")
require.NoError(h.t, err)

h.t.Cleanup(func() {
_ = os.RemoveAll(tempPath)
})

bitcoinResource, err := h.m.RunBitcoindResource(t, tempPath)
require.NoError(h.t, err)

h.t.Cleanup(func() {
_ = h.m.ClearResources()
})

require.Eventually(h.t, func() bool {
_, err := h.GetBlockCount()
if err != nil {
h.t.Logf("failed to get block count: %v", err)
}
return err == nil
}, startTimeout, 500*time.Millisecond, "bitcoind did not start")

return bitcoinResource
}

// GetBlockCount retrieves the current number of blocks in the blockchain from the Bitcoind.
func (h *BitcoindTestHandler) GetBlockCount() (int, error) {
buff, _, err := h.m.ExecBitcoindCliCmd(h.t, []string{"getblockcount"})
if err != nil {
return 0, err
}

parsedBuffStr := strings.TrimSuffix(buff.String(), "\n")

return strconv.Atoi(parsedBuffStr)
}

// GenerateBlocks mines a specified number of blocks in the Bitcoind.
func (h *BitcoindTestHandler) GenerateBlocks(count int) *GenerateBlockResponse {
buff, _, err := h.m.ExecBitcoindCliCmd(h.t, []string{"-generate", fmt.Sprintf("%d", count)})
require.NoError(h.t, err)

var response GenerateBlockResponse
err = json.Unmarshal(buff.Bytes(), &response)
require.NoError(h.t, err)

return &response
}

// CreateWallet creates a new wallet with the specified name and passphrase in the Bitcoind
func (h *BitcoindTestHandler) CreateWallet(walletName string, passphrase string) *CreateWalletResponse {
// last arg is true which indicates we are using descriptor wallets they do not allow dumping private keys.
buff, _, err := h.m.ExecBitcoindCliCmd(h.t, []string{"createwallet", walletName, "false", "false", passphrase, "false", "true"})
require.NoError(h.t, err)

var response CreateWalletResponse
err = json.Unmarshal(buff.Bytes(), &response)
require.NoError(h.t, err)

return &response
}

// InvalidateBlock invalidates blocks starting from specified block hash
func (h *BitcoindTestHandler) InvalidateBlock(blockHash string) {
_, _, err := h.m.ExecBitcoindCliCmd(h.t, []string{"invalidateblock", blockHash})
require.NoError(h.t, err)
}

// ImportDescriptors imports a given Bitcoin address descriptor into the Bitcoind
func (h *BitcoindTestHandler) ImportDescriptors(descriptor string) {
_, _, err := h.m.ExecBitcoindCliCmd(h.t, []string{"importdescriptors", descriptor})
require.NoError(h.t, err)
}

func (h *BitcoindTestHandler) Stop() {
_ = h.m.ClearResources()
}
37 changes: 37 additions & 0 deletions e2etest/container/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package container

import (
"testing"

"github.com/babylonlabs-io/babylon-staking-indexer/testutil"
"github.com/stretchr/testify/require"
)

// ImageConfig contains all images and their respective tags
// needed for running e2e tests.
type ImageConfig struct {
BitcoindRepository string
BitcoindVersion string
BabylonRepository string
BabylonVersion string
}

//nolint:deadcode
const (
dockerBitcoindRepository = "lncm/bitcoind"
dockerBitcoindVersionTag = "v27.0"
dockerBabylondRepository = "babylonlabs/babylond"
)

// NewImageConfig returns ImageConfig needed for running e2e test.
func NewImageConfig(t *testing.T) ImageConfig {
babylonVersion, err := testutil.GetBabylonVersion()
require.NoError(t, err)

return ImageConfig{
BitcoindRepository: dockerBitcoindRepository,
BitcoindVersion: dockerBitcoindVersionTag,
BabylonRepository: dockerBabylondRepository,
BabylonVersion: babylonVersion,
}
}
Loading
Loading