Skip to content

Commit

Permalink
use random ports
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Sep 16, 2024
1 parent afbff88 commit 01b8768
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test:

test-e2e:
cd $(TOOLS_DIR); go install -trimpath $(BABYLON_PKG);
go test -race -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e
go test -race -mod=readonly --failfast -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e

build-docker:
$(DOCKER) build --tag babylonlabs-io/vigilante -f Dockerfile \
Expand Down
11 changes: 9 additions & 2 deletions e2etest/bitcoind_node_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/babylonlabs-io/vigilante/e2etest/container"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
"os"
"strconv"
Expand Down Expand Up @@ -40,15 +41,15 @@ func NewBitcoindHandler(t *testing.T, manager *container.Manager) *BitcoindTestH
}
}

func (h *BitcoindTestHandler) Start() {
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)
})

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

h.t.Cleanup(func() {
Expand All @@ -62,6 +63,8 @@ func (h *BitcoindTestHandler) Start() {
}
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.
Expand Down Expand Up @@ -112,3 +115,7 @@ 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()
}
4 changes: 2 additions & 2 deletions e2etest/container/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type ImageConfig struct {
const (
dockerBitcoindRepository = "lncm/bitcoind"
dockerBitcoindVersionTag = "v27.0"
dockerBabylondRepository = "babylonlabs/babylond"
dockerBabylondVersionTag = "b834df27bec4331b4962cdec85f71a5cff51bfa8"
dockerBabylondRepository = "babylonlabs-io/babylond"
dockerBabylondVersionTag = "latest" // todo(Lazar): we need version b1e255a
)

// NewImageConfig returns ImageConfig needed for running e2e test.
Expand Down
88 changes: 62 additions & 26 deletions e2etest/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"fmt"
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/cometbft/cometbft/libs/rand"
"net"
"regexp"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -48,6 +51,7 @@ func NewManager() (docker *Manager, err error) {
if err != nil {
return nil, err
}

return docker, nil
}

Expand Down Expand Up @@ -131,7 +135,9 @@ func (m *Manager) ExecCmd(t *testing.T, containerName string, command []string)
return outBuf, errBuf, nil
}

// RunBitcoindResource starts a bitcoind docker container
func (m *Manager) RunBitcoindResource(
t *testing.T,
bitcoindCfgPath string,
) (*dockertest.Resource, error) {
bitcoindResource, err := m.pool.RunWithOptions(
Expand All @@ -143,14 +149,6 @@ func (m *Manager) RunBitcoindResource(
Mounts: []string{
fmt.Sprintf("%s/:/data/.bitcoin", bitcoindCfgPath),
},
ExposedPorts: []string{
"8332",
"8333",
"28332",
"28333",
"18443",
"18444",
},
Cmd: []string{
"-regtest",
"-txindex",
Expand All @@ -164,12 +162,12 @@ func (m *Manager) RunBitcoindResource(
},
func(config *docker.HostConfig) {
config.PortBindings = map[docker.Port][]docker.PortBinding{
"8332/tcp": {{HostIP: "", HostPort: "8332"}},
"8333/tcp": {{HostIP: "", HostPort: "8333"}},
"28332/tcp": {{HostIP: "", HostPort: "28332"}},
"28333/tcp": {{HostIP: "", HostPort: "28333"}},
"18443/tcp": {{HostIP: "", HostPort: "18443"}},
"18444/tcp": {{HostIP: "", HostPort: "18444"}},
"8332/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"8333/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"28332/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"28333/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"18443/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"18444/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
}
},
noRestart,
Expand All @@ -181,7 +179,9 @@ func (m *Manager) RunBitcoindResource(
return bitcoindResource, nil
}

// RunBabylondResource starts a babylond container
func (m *Manager) RunBabylondResource(
t *testing.T,
mounthPath string,
baseHeaderHex string,
slashingPkScript string,
Expand Down Expand Up @@ -210,21 +210,23 @@ func (m *Manager) RunBabylondResource(
Mounts: []string{
fmt.Sprintf("%s/:/home/", mounthPath),
},
Cmd: cmd,
ExposedPorts: []string{
"26656",
"26657",
"1317",
"9090",
"2345",
"9090",
"26656",
"26657",
},
PortBindings: map[docker.Port][]docker.PortBinding{
"1317/tcp": {{HostIP: "", HostPort: "1317"}},
"2345/tcp": {{HostIP: "", HostPort: "2345"}},
"9090/tcp": {{HostIP: "", HostPort: "9090"}},
"26656/tcp": {{HostIP: "", HostPort: "26656"}},
"26657/tcp": {{HostIP: "", HostPort: "26657"}},
},
Cmd: cmd,
},
func(config *docker.HostConfig) {
config.PortBindings = map[docker.Port][]docker.PortBinding{
"1317/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"2345/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"9090/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"26656/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"26657/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
}
},
noRestart,
)
Expand All @@ -240,8 +242,9 @@ func (m *Manager) RunBabylondResource(
// ClearResources removes all outstanding Docker resources created by the Manager.
func (m *Manager) ClearResources() error {
for _, resource := range m.resources {
fmt.Printf("cleaning %s\n", resource.Container.Name)
if err := m.pool.Purge(resource); err != nil {
return err
continue
}
}

Expand All @@ -254,3 +257,36 @@ func noRestart(config *docker.HostConfig) {
Name: "no",
}
}

// randomAvailablePort tries to find an available TCP port on the localhost
// by testing multiple random ports within a specified range.
func randomAvailablePort(t *testing.T) int {
randPort := func(base, spread int) int {
return base + rand.Intn(spread)
}

// Base port and spread range for port selection
const (
basePort = 20000
portRange = 10000
)

// Seed the random number generator to ensure randomness
rand.Seed(time.Now().UnixNano())

// Try up to 5 times to find an available port
for i := 0; i < 5; i++ {
port := randPort(basePort, portRange)
address := fmt.Sprintf("127.0.0.1:%d", port)

listener, err := net.Listen("tcp", address)
if err == nil {
_ = listener.Close()
return port
}
}

// If no available port was found, fail the test
t.Fatalf("failed to find an available port in range %d-%d", basePort, basePort+portRange)
return 0
}
4 changes: 3 additions & 1 deletion e2etest/submitter_e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2etest

import (
"fmt"
"github.com/babylonlabs-io/vigilante/testutil"
"math/rand"
"testing"
Expand Down Expand Up @@ -187,9 +188,10 @@ func TestSubmitterSubmissionReplace(t *testing.T) {

func TestSubmitterDocker(t *testing.T) {

numMatureOutputs := uint32(300)
numMatureOutputs := uint32(10)

tm := StartManager(t, numMatureOutputs, defaultEpochInterval)
defer tm.Stop(t)

fmt.Printf("asdf")
}
8 changes: 6 additions & 2 deletions e2etest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ func StartManager(t *testing.T, numMatureOutputsInWallet uint32, epochInterval u
require.NoError(t, err)

btcHandler := NewBitcoindHandler(t, manager)
btcHandler.Start()
bitcoind := btcHandler.Start(t)
passphrase := "pass"
_ = btcHandler.CreateWallet("default", passphrase)

cfg := defaultVigilanteConfig()

cfg.BTC.Endpoint = fmt.Sprintf("127.0.0.1:%s", bitcoind.GetPort("18443/tcp"))
cfg.BTC.ZmqSeqEndpoint = fmt.Sprintf("tcp:// 127.0.0.1:%s", bitcoind.GetPort("28333/tcp"))

testRpcClient, err := rpcclient.New(&rpcclient.ConnConfig{
Host: cfg.BTC.Endpoint,
User: cfg.BTC.Username,
Expand Down Expand Up @@ -127,7 +130,7 @@ func StartManager(t *testing.T, numMatureOutputsInWallet uint32, epochInterval u
// start Babylon node

tmpDir := t.TempDir()
babylond, err := manager.RunBabylondResource(tmpDir, baseHeaderHex, hex.EncodeToString(pkScript), epochInterval)
babylond, err := manager.RunBabylondResource(t, tmpDir, baseHeaderHex, hex.EncodeToString(pkScript), epochInterval)
require.NoError(t, err)

// create Babylon client
Expand Down Expand Up @@ -163,6 +166,7 @@ func StartManager(t *testing.T, numMatureOutputsInWallet uint32, epochInterval u
}

func (tm *TestManager) Stop(t *testing.T) {
tm.BitcoindHandler.Stop()
if tm.BabylonClient.IsRunning() {
err := tm.BabylonClient.Stop()
require.NoError(t, err)
Expand Down

0 comments on commit 01b8768

Please sign in to comment.