Skip to content

Commit 456673e

Browse files
Fix PortMappings (#1536)
* Fix host port binding syntax * Expose testcontainer.Container so code can clean up it's containers --------- Co-authored-by: smickovskid <[email protected]>
1 parent d70db91 commit 456673e

File tree

8 files changed

+28
-45
lines changed

8 files changed

+28
-45
lines changed

framework/components/blockchain/anvil.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func newAnvil(in *Input) (*Output, error) {
7676
Family: "evm",
7777
ChainID: in.ChainID,
7878
ContainerName: containerName,
79+
Container: c,
7980
Nodes: []*Node{
8081
{
8182
HostWSUrl: fmt.Sprintf("ws://%s:%s", host, mp.Port()),

framework/components/blockchain/besu.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,7 @@ func newBesu(in *Input) (*Output, error) {
6767
},
6868
Labels: framework.DefaultTCLabels(),
6969
HostConfigModifier: func(h *container.HostConfig) {
70-
h.PortBindings = nat.PortMap{
71-
nat.Port(bindPortWs): []nat.PortBinding{
72-
{
73-
HostIP: "0.0.0.0",
74-
HostPort: bindPortWs,
75-
},
76-
},
77-
nat.Port(bindPort): []nat.PortBinding{
78-
{
79-
HostIP: "0.0.0.0",
80-
HostPort: bindPort,
81-
},
82-
},
83-
}
70+
h.PortBindings = framework.MapTheSamePort(bindPortWs, bindPort)
8471
},
8572
WaitingFor: wait.ForListeningPort(nat.Port(in.Port)).WithStartupTimeout(15 * time.Second).WithPollInterval(200 * time.Millisecond),
8673
Cmd: entryPoint,
@@ -113,6 +100,7 @@ func newBesu(in *Input) (*Output, error) {
113100
ChainID: in.ChainID,
114101
Family: "evm",
115102
ContainerName: containerName,
103+
Container: c,
116104
Nodes: []*Node{
117105
{
118106
HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, mp.Port()),

framework/components/blockchain/blockchain.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package blockchain
22

33
import (
44
"fmt"
5+
"github.com/testcontainers/testcontainers-go"
56
)
67

78
// Input is a blockchain network configuration params
@@ -29,11 +30,12 @@ type Input struct {
2930

3031
// Output is a blockchain network output, ChainID and one or more nodes that forms the network
3132
type Output struct {
32-
UseCache bool `toml:"use_cache"`
33-
Family string `toml:"family"`
34-
ContainerName string `toml:"container_name"`
35-
ChainID string `toml:"chain_id"`
36-
Nodes []*Node `toml:"nodes"`
33+
UseCache bool `toml:"use_cache"`
34+
Family string `toml:"family"`
35+
ContainerName string `toml:"container_name"`
36+
Container testcontainers.Container `toml:"-"`
37+
ChainID string `toml:"chain_id"`
38+
Nodes []*Node `toml:"nodes"`
3739
}
3840

3941
// Node represents blockchain node output, URLs required for connection locally and inside docker network

framework/components/blockchain/geth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func newGeth(in *Input) (*Output, error) {
214214
Family: "evm",
215215
ChainID: in.ChainID,
216216
ContainerName: containerName,
217+
Container: c,
217218
Nodes: []*Node{
218219
{
219220
HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, mp.Port()),

framework/components/blockchain/solana.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/docker/docker/api/types/container"
1313
"github.com/docker/docker/api/types/mount"
14-
"github.com/docker/go-connections/nat"
1514
"github.com/testcontainers/testcontainers-go"
1615
"github.com/testcontainers/testcontainers-go/wait"
1716

@@ -105,20 +104,7 @@ func newSolana(in *Input) (*Output, error) {
105104
WithStartupTimeout(30 * time.Second).
106105
WithPollInterval(100 * time.Millisecond),
107106
HostConfigModifier: func(h *container.HostConfig) {
108-
h.PortBindings = nat.PortMap{
109-
nat.Port(bindPort): []nat.PortBinding{
110-
{
111-
HostIP: "0.0.0.0",
112-
HostPort: bindPort,
113-
},
114-
},
115-
nat.Port(wsBindPort): []nat.PortBinding{
116-
{
117-
HostIP: "0.0.0.0",
118-
HostPort: wsBindPort,
119-
},
120-
},
121-
}
107+
h.PortBindings = framework.MapTheSamePort(bindPort, wsBindPort)
122108
h.Mounts = append(h.Mounts, mount.Mount{
123109
Type: mount.TypeBind,
124110
Source: contractsDir,
@@ -157,6 +143,7 @@ func newSolana(in *Input) (*Output, error) {
157143
UseCache: true,
158144
Family: "solana",
159145
ContainerName: containerName,
146+
Container: c,
160147
Nodes: []*Node{
161148
{
162149
HostWSUrl: fmt.Sprintf("ws://%s:%s", host, in.WSPort),

framework/components/clnode/clnode.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,17 @@ func generateEntryPoint() []string {
135135
func generatePortBindings(in *Input) ([]string, nat.PortMap, error) {
136136
httpPort := fmt.Sprintf("%s/tcp", DefaultHTTPPort)
137137
innerDebuggerPort := fmt.Sprintf("%d/tcp", DefaultDebuggerPort)
138-
debuggerPort := fmt.Sprintf("%d/tcp", in.Node.DebuggerPort)
139138
portBindings := nat.PortMap{
140139
nat.Port(httpPort): []nat.PortBinding{
141140
{
142141
HostIP: "0.0.0.0",
143-
HostPort: fmt.Sprintf("%d/tcp", in.Node.HTTPPort),
142+
HostPort: strconv.Itoa(in.Node.HTTPPort),
144143
},
145144
},
146145
nat.Port(innerDebuggerPort): []nat.PortBinding{
147146
{
148147
HostIP: "0.0.0.0",
149-
HostPort: debuggerPort,
148+
HostPort: strconv.Itoa(in.Node.DebuggerPort),
150149
},
151150
},
152151
}
@@ -160,7 +159,7 @@ func generatePortBindings(in *Input) ([]string, nat.PortMap, error) {
160159
customPorts = append(customPorts, fmt.Sprintf("%s/tcp", pp[1]))
161160

162161
dockerPort := nat.Port(fmt.Sprintf("%s/tcp", pp[1]))
163-
hostPort := fmt.Sprintf("%s/tcp", pp[0])
162+
hostPort := pp[0]
164163
portBindings[dockerPort] = []nat.PortBinding{
165164
{
166165
HostIP: "0.0.0.0",
@@ -171,7 +170,7 @@ func generatePortBindings(in *Input) ([]string, nat.PortMap, error) {
171170
customPorts = append(customPorts, fmt.Sprintf("%s/tcp", p))
172171

173172
dockerPort := nat.Port(fmt.Sprintf("%s/tcp", p))
174-
hostPort := fmt.Sprintf("%s/tcp", p)
173+
hostPort := p
175174
portBindings[dockerPort] = []nat.PortBinding{
176175
{
177176
HostIP: "0.0.0.0",

framework/components/postgres/postgres.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/testcontainers/testcontainers-go"
1010
tcwait "github.com/testcontainers/testcontainers-go/wait"
1111
"os"
12+
"strconv"
1213
"strings"
1314
"time"
1415
)
@@ -121,7 +122,7 @@ func NewPostgreSQL(in *Input) (*Output, error) {
121122
nat.Port(bindPort): []nat.PortBinding{
122123
{
123124
HostIP: "0.0.0.0",
124-
HostPort: fmt.Sprintf("%d/tcp", portToExpose),
125+
HostPort: strconv.Itoa(portToExpose),
125126
},
126127
},
127128
}

framework/docker.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ func GetHost(container tc.Container) (string, error) {
4747
return host, nil
4848
}
4949

50-
func MapTheSamePort(port string) nat.PortMap {
51-
return nat.PortMap{
52-
nat.Port(port): []nat.PortBinding{
50+
func MapTheSamePort(ports ...string) nat.PortMap {
51+
portMap := nat.PortMap{}
52+
for _, port := range ports {
53+
// need to split off /tcp or /udp
54+
onlyPort := strings.SplitN(port, "/", 2)
55+
portMap[nat.Port(port)] = []nat.PortBinding{
5356
{
5457
HostIP: "0.0.0.0",
55-
HostPort: port,
58+
HostPort: onlyPort[0],
5659
},
57-
},
60+
}
5861
}
62+
return portMap
5963
}
6064

6165
func DefaultTCLabels() map[string]string {

0 commit comments

Comments
 (0)