From 59e06bd2200cbde8af3915d3cfed764003925863 Mon Sep 17 00:00:00 2001 From: Madhur Shrimal Date: Tue, 6 Aug 2024 12:36:12 -0800 Subject: [PATCH] testutils --- Makefile | 2 +- testutils/anvil.go | 24 ++++++++++++++++++------ testutils/localstack.go | 15 +++++++++++---- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 24416d0a..0073e5e6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ GO_LINES_IGNORED_DIRS=contracts GO_PACKAGES=./chainio/... ./crypto/... ./logging/... \ ./types/... ./utils/... ./signer/... ./cmd/... \ ./signerv2/... ./aws/... ./internal/... ./metrics/... \ - ./nodeapi/... ./cmd/... ./services/... + ./nodeapi/... ./cmd/... ./services/... ./testutils/... GO_FOLDERS=$(shell echo ${GO_PACKAGES} | sed -e "s/\.\///g" | sed -e "s/\/\.\.\.//g") help: @grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/testutils/anvil.go b/testutils/anvil.go index c4da31ca..ebaf0a9c 100644 --- a/testutils/anvil.go +++ b/testutils/anvil.go @@ -3,12 +3,13 @@ package testutils import ( "context" "fmt" - "github.com/ethereum/go-ethereum/ethclient" "log" "os/exec" "path/filepath" "runtime" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/testcontainers/testcontainers-go" @@ -69,8 +70,12 @@ func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContrac panic(err) } // The ContractsRegistry contract should always be deployed at this address on anvil - // it's the address of the contract created at nonce 0 by the first anvil account (0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) - contractsRegistry, err := contractreg.NewContractContractsRegistry(common.HexToAddress("0x5FbDB2315678afecb367f032d93F642f64180aa3"), ethHttpClient) + // it's the address of the contract created at nonce 0 by the first anvil account + // (0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) + contractsRegistry, err := contractreg.NewContractContractsRegistry( + common.HexToAddress("0x5FbDB2315678afecb367f032d93F642f64180aa3"), + ethHttpClient, + ) if err != nil { panic(err) } @@ -89,7 +94,10 @@ func GetContractAddressesFromContractRegistry(ethHttpUrl string) (mockAvsContrac if mockAvsRegistryCoordinatorAddr == (common.Address{}) { panic("mockAvsRegistryCoordinatorAddr is empty") } - mockAvsOperatorStateRetrieverAddr, err := contractsRegistry.Contracts(&bind.CallOpts{}, "mockAvsOperatorStateRetriever") + mockAvsOperatorStateRetrieverAddr, err := contractsRegistry.Contracts( + &bind.CallOpts{}, + "mockAvsOperatorStateRetriever", + ) if err != nil { panic(err) } @@ -133,9 +141,13 @@ func AdvanceChainByNBlocks(n int, anvilEndpoint string) { } } -// Prefer this function over AdvanceChainByNBlocks b/c it doesn't require cast to be installed on the host machine, whereas this one doesn't. +// Prefer this function over AdvanceChainByNBlocks b/c it doesn't require cast to be installed on the host machine, +// whereas this one doesn't. func AdvanceChainByNBlocksExecInContainer(ctx context.Context, n int, anvilC testcontainers.Container) { - c, _, err := anvilC.Exec(ctx, []string{"cast", "rpc", "anvil_mine", fmt.Sprintf("%d", n), "--rpc-url", "http://localhost:8545"}) + c, _, err := anvilC.Exec( + ctx, + []string{"cast", "rpc", "anvil_mine", fmt.Sprintf("%d", n), "--rpc-url", "http://localhost:8545"}, + ) if err != nil { panic(err) } diff --git a/testutils/localstack.go b/testutils/localstack.go index 41e31049..bf3722df 100644 --- a/testutils/localstack.go +++ b/testutils/localstack.go @@ -16,9 +16,11 @@ const LocalStackPort = "4566" func StartLocalstackContainer(name string) (testcontainers.Container, error) { fmt.Println("Starting Localstack container") req := testcontainers.ContainerRequest{ - Image: "localstack/localstack:latest", - Name: fmt.Sprintf("localstack-test-%s", name), - Env: map[string]string{"LOCALSTACK_HOST": fmt.Sprintf("localhost.localstack.cloud:%s", LocalStackPort)}, + Image: "localstack/localstack:latest", + Name: fmt.Sprintf("localstack-test-%s", name), + Env: map[string]string{ + "LOCALSTACK_HOST": fmt.Sprintf("localhost.localstack.cloud:%s", LocalStackPort), + }, ExposedPorts: []string{LocalStackPort}, WaitingFor: wait.ForLog("Ready."), AutoRemove: true, @@ -30,7 +32,12 @@ func StartLocalstackContainer(name string) (testcontainers.Container, error) { } func NewKMSClient(localStackPort string) (*kms.Client, error) { - cfg, err := aws.GetAWSConfig("localstack", "localstack", "us-east-1", fmt.Sprintf("http://127.0.0.1:%s", localStackPort)) + cfg, err := aws.GetAWSConfig( + "localstack", + "localstack", + "us-east-1", + fmt.Sprintf("http://127.0.0.1:%s", localStackPort), + ) if err != nil { return nil, fmt.Errorf("failed to load AWS config: %w", err) }