Skip to content

Commit

Permalink
fix(tests): update port usage for testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Jul 11, 2024
1 parent 1b24a6e commit 6510ffe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require github.com/BurntSushi/toml v1.4.0
require (
github.com/Khan/genqlient v0.7.0
github.com/deepmap/oapi-codegen/v2 v2.1.0
github.com/docker/docker v27.0.3+incompatible
github.com/docker/go-connections v0.5.0
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/jackc/pgx/v5 v5.6.0
github.com/lmittmann/tint v1.0.4
Expand Down Expand Up @@ -52,8 +54,6 @@ require (
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.2 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
Expand Down
62 changes: 54 additions & 8 deletions internal/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync"
"time"

"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand All @@ -38,6 +40,11 @@ const (
devnetKey
)

const (
postgresContainerPort = "5432/tcp"
devnetContainerPort = "8545/tcp"
)

// Struct to hold Node dependencies containers configurations
type DepsConfig struct {
Postgres *PostgresConfig
Expand Down Expand Up @@ -159,6 +166,24 @@ func createHook(finishedWaitGroup *sync.WaitGroup) []testcontainers.ContainerLif
}
}

func buildPortMap(portSpec string) (nat.PortMap, error) {
portMappings, err := nat.ParsePortSpec(portSpec)
if err != nil {
return nil, err
}

portMap := nat.PortMap{}
for _, portMapping := range portMappings {
portMap[portMapping.Port] = append(
portMap[portMapping.Port],
nat.PortBinding{
HostIP: portMapping.Binding.HostIP,
HostPort: portMapping.Binding.HostPort,
})
}
return portMap, nil
}

// Run starts the Node dependencies containers.
// The returned DepContainers struct can be used to gracefully
// terminate the containers using the Terminate method
Expand All @@ -173,20 +198,30 @@ func Run(ctx context.Context, depsConfig DepsConfig) (*DepsContainers, error) {
WithOccurrence(numPostgresCheckReadyAttempts).
WithPollInterval(pollInterval)

postgresExposedPorts := "5432/tcp"
postgresPortSpec := postgresContainerPort
if depsConfig.Postgres.Port != "" {
postgresExposedPorts = strings.Join([]string{
depsConfig.Postgres.Port, ":", postgresExposedPorts}, "")
postgresPortSpec = strings.Join([]string{
depsConfig.Postgres.Port, ":", postgresPortSpec}, "")
}

portMap, err := buildPortMap(postgresPortSpec)
if err != nil {
return nil, err
}

postgresReq := testcontainers.ContainerRequest{
Image: depsConfig.Postgres.DockerImage,
ExposedPorts: []string{postgresExposedPorts},
ExposedPorts: []string{postgresContainerPort},
WaitingFor: postgresWaitStrategy,
Env: map[string]string{
"POSTGRES_PASSWORD": depsConfig.Postgres.Password,
},
LifecycleHooks: createHook(&finishedWaitGroup),
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.PortBindings = portMap
},
}

postgres, err := testcontainers.GenericContainer(
ctx,
testcontainers.GenericContainerRequest{
Expand All @@ -204,11 +239,18 @@ func Run(ctx context.Context, depsConfig DepsConfig) (*DepsContainers, error) {

if depsConfig.Devnet != nil {

devnetExposedPort := "8545/tcp"
devnetPortSpec := devnetContainerPort
if depsConfig.Devnet.Port != "" {
devnetExposedPort = strings.Join([]string{
depsConfig.Devnet.Port, ":", devnetExposedPort}, "")
devnetPortSpec = strings.Join([]string{
depsConfig.Devnet.Port, ":", devnetPortSpec}, "")
}

portMap, err := buildPortMap(devnetPortSpec)

if err != nil {
return nil, err
}

cmd := []string{
"anvil",
"--load-state",
Expand All @@ -225,11 +267,15 @@ func Run(ctx context.Context, depsConfig DepsConfig) (*DepsContainers, error) {
}
devNetReq := testcontainers.ContainerRequest{
Image: depsConfig.Devnet.DockerImage,
ExposedPorts: []string{devnetExposedPort},
ExposedPorts: []string{devnetContainerPort},
WaitingFor: waitStrategy,
Cmd: cmd,
LifecycleHooks: createHook(&finishedWaitGroup),
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.PortBindings = portMap
},
}

devnet, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: devNetReq,
Started: true,
Expand Down

0 comments on commit 6510ffe

Please sign in to comment.