Skip to content

Commit 4a7770b

Browse files
authored
fix: use PortEndpoint() in a few more modules (#3209)
1 parent 42824ba commit 4a7770b

File tree

4 files changed

+9
-45
lines changed

4 files changed

+9
-45
lines changed

modules/vearch/vearch.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package vearch
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"time"
87

@@ -71,15 +70,5 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
7170

7271
// RESTEndpoint returns the REST endpoint of the Vearch container
7372
func (c *VearchContainer) RESTEndpoint(ctx context.Context) (string, error) {
74-
containerPort, err := c.MappedPort(ctx, "8817/tcp")
75-
if err != nil {
76-
return "", fmt.Errorf("failed to get container port: %w", err)
77-
}
78-
79-
host, err := c.Host(ctx)
80-
if err != nil {
81-
return "", errors.New("failed to get container host")
82-
}
83-
84-
return fmt.Sprintf("http://%s:%s", host, containerPort.Port()), nil
73+
return c.PortEndpoint(ctx, "8817/tcp", "http")
8574
}

modules/weaviate/weaviate.go

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

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"time"
87

@@ -72,31 +71,16 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
7271
//
7372
//nolint:revive,staticcheck //FIXME
7473
func (c *WeaviateContainer) HttpHostAddress(ctx context.Context) (string, string, error) {
75-
port, err := c.MappedPort(ctx, httpPort)
74+
endpoint, err := c.PortEndpoint(ctx, httpPort, "")
7675
if err != nil {
77-
return "", "", fmt.Errorf("failed to get container port: %w", err)
76+
return "", "", fmt.Errorf("port endpoint: %w", err)
7877
}
7978

80-
host, err := c.Host(ctx)
81-
if err != nil {
82-
return "", "", errors.New("failed to get container host")
83-
}
84-
85-
return "http", fmt.Sprintf("%s:%s", host, port.Port()), nil
79+
return "http", endpoint, nil
8680
}
8781

8882
// GrpcHostAddress returns the gRPC host of the Weaviate container.
8983
// At the moment, it only supports unsecured gRPC connection.
9084
func (c *WeaviateContainer) GrpcHostAddress(ctx context.Context) (string, error) {
91-
port, err := c.MappedPort(ctx, grpcPort)
92-
if err != nil {
93-
return "", fmt.Errorf("failed to get container port: %w", err)
94-
}
95-
96-
host, err := c.Host(ctx)
97-
if err != nil {
98-
return "", errors.New("failed to get container host")
99-
}
100-
101-
return fmt.Sprintf("%s:%s", host, port.Port()), nil
85+
return c.PortEndpoint(ctx, grpcPort, "")
10286
}

modules/yugabytedb/yugabytedb.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package yugabytedb
33
import (
44
"context"
55
"fmt"
6-
"net"
76
"strings"
87

98
"github.com/testcontainers/testcontainers-go"
@@ -105,21 +104,16 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
105104
// Additional arguments are appended to the connection string as query parameters
106105
// in the form of key=value pairs separated by "&".
107106
func (y *Container) YSQLConnectionString(ctx context.Context, args ...string) (string, error) {
108-
host, err := y.Host(ctx)
107+
endpoint, err := y.PortEndpoint(ctx, ysqlPort, "")
109108
if err != nil {
110-
return "", fmt.Errorf("host: %w", err)
111-
}
112-
113-
mappedPort, err := y.MappedPort(ctx, ysqlPort)
114-
if err != nil {
115-
return "", fmt.Errorf("mapped port: %w", err)
109+
return "", fmt.Errorf("port endpoint: %w", err)
116110
}
117111

118112
return fmt.Sprintf(
119113
"postgres://%s:%s@%s/%s?%s",
120114
y.ysqlDatabaseUser,
121115
y.ysqlDatabasePassword,
122-
net.JoinHostPort(host, mappedPort.Port()),
116+
endpoint,
123117
y.ysqlDatabaseName,
124118
strings.Join(args, "&"),
125119
), nil

wait/host_port.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net"
88
"os"
9-
"strconv"
109
"time"
1110

1211
"github.com/docker/go-connections/nat"
@@ -226,11 +225,9 @@ func (hp *HostPortStrategy) WaitUntilReady(ctx context.Context, target StrategyT
226225

227226
func externalCheck(ctx context.Context, ipAddress string, port nat.Port, target StrategyTarget, waitInterval time.Duration) error {
228227
proto := port.Proto()
229-
portNumber := port.Int()
230-
portString := strconv.Itoa(portNumber)
231228

232229
dialer := net.Dialer{}
233-
address := net.JoinHostPort(ipAddress, portString)
230+
address := net.JoinHostPort(ipAddress, port.Port())
234231
for i := 0; ; i++ {
235232
if err := checkTarget(ctx, target); err != nil {
236233
return fmt.Errorf("check target: retries: %d address: %s: %w", i, address, err)

0 commit comments

Comments
 (0)