Skip to content

Commit 37d18dc

Browse files
authored
fix: use PortEndpoint() in a few more modules (#3206)
1 parent 27b2436 commit 37d18dc

File tree

8 files changed

+13
-74
lines changed

8 files changed

+13
-74
lines changed

modules/mariadb/mariadb.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,7 @@ func (c *MariaDBContainer) MustConnectionString(ctx context.Context, args ...str
197197
}
198198

199199
func (c *MariaDBContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
200-
containerPort, err := c.MappedPort(ctx, "3306/tcp")
201-
if err != nil {
202-
return "", err
203-
}
204-
205-
host, err := c.Host(ctx)
200+
endpoint, err := c.PortEndpoint(ctx, "3306/tcp", "")
206201
if err != nil {
207202
return "", err
208203
}
@@ -215,6 +210,6 @@ func (c *MariaDBContainer) ConnectionString(ctx context.Context, args ...string)
215210
extraArgs = "?" + extraArgs
216211
}
217212

218-
connectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s%s", c.username, c.password, host, containerPort.Port(), c.database, extraArgs)
213+
connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s%s", c.username, c.password, endpoint, c.database, extraArgs)
219214
return connectionString, nil
220215
}

modules/meilisearch/meilisearch.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"net"
98
"net/http"
109
"time"
1110

@@ -104,15 +103,5 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
104103
// Address retrieves the address of the Meilisearch container.
105104
// It will use http as protocol, as TLS is not supported at the moment.
106105
func (c *MeilisearchContainer) Address(ctx context.Context) (string, error) {
107-
containerPort, err := c.MappedPort(ctx, defaultHTTPPort)
108-
if err != nil {
109-
return "", fmt.Errorf("mapped port: %w", err)
110-
}
111-
112-
host, err := c.Host(ctx)
113-
if err != nil {
114-
return "", fmt.Errorf("host: %w", err)
115-
}
116-
117-
return "http://" + net.JoinHostPort(host, containerPort.Port()), nil
106+
return c.PortEndpoint(ctx, defaultHTTPPort, "http")
118107
}

modules/memcached/memcached.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package memcached
33
import (
44
"context"
55
"fmt"
6-
"net"
76

87
"github.com/testcontainers/testcontainers-go"
98
"github.com/testcontainers/testcontainers-go/wait"
@@ -52,15 +51,5 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5251

5352
// HostPort returns the host and port of the Memcached container
5453
func (c *Container) HostPort(ctx context.Context) (string, error) {
55-
host, err := c.Host(ctx)
56-
if err != nil {
57-
return "", fmt.Errorf("host: %w", err)
58-
}
59-
60-
port, err := c.MappedPort(ctx, defaultPort)
61-
if err != nil {
62-
return "", fmt.Errorf("port: %w", err)
63-
}
64-
65-
return net.JoinHostPort(host, port.Port()), nil
54+
return c.PortEndpoint(ctx, defaultPort, "")
6655
}

modules/milvus/milvus.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ type MilvusContainer struct {
3030
// ConnectionString returns the connection string for the milvus container, using the default 19530 port, and
3131
// obtaining the host and exposed port from the container.
3232
func (c *MilvusContainer) ConnectionString(ctx context.Context) (string, error) {
33-
host, err := c.Host(ctx)
34-
if err != nil {
35-
return "", err
36-
}
37-
port, err := c.MappedPort(ctx, grpcPort)
38-
if err != nil {
39-
return "", err
40-
}
41-
return fmt.Sprintf("%s:%s", host, port.Port()), nil
33+
return c.PortEndpoint(ctx, grpcPort, "")
4234
}
4335

4436
// Deprecated: use Run instead

modules/minio/minio.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,7 @@ func WithPassword(password string) testcontainers.CustomizeRequestOption {
4646
// ConnectionString returns the connection string for the minio container, using the default 9000 port, and
4747
// obtaining the host and exposed port from the container.
4848
func (c *MinioContainer) ConnectionString(ctx context.Context) (string, error) {
49-
host, err := c.Host(ctx)
50-
if err != nil {
51-
return "", err
52-
}
53-
port, err := c.MappedPort(ctx, "9000/tcp")
54-
if err != nil {
55-
return "", err
56-
}
57-
return fmt.Sprintf("%s:%s", host, port.Port()), nil
49+
return c.PortEndpoint(ctx, "9000/tcp", "")
5850
}
5951

6052
// Deprecated: use Run instead

modules/mockserver/mockserver.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5454
return c, nil
5555
}
5656

57-
// GetURL returns the URL of the MockServer container
57+
// URL returns the URL of the MockServer container
5858
func (c *MockServerContainer) URL(ctx context.Context) (string, error) {
59-
host, err := c.Host(ctx)
60-
if err != nil {
61-
return "", err
62-
}
63-
port, err := c.MappedPort(ctx, "1080/tcp")
64-
if err != nil {
65-
return "", err
66-
}
67-
return fmt.Sprintf("http://%s:%d", host, port.Int()), nil
59+
return c.PortEndpoint(ctx, "1080/tcp", "http")
6860
}

modules/mongodb/mongodb.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
_ "embed"
77
"errors"
88
"fmt"
9-
"net"
109
"net/url"
1110
"time"
1211

@@ -119,17 +118,13 @@ func WithReplicaSet(replSetName string) testcontainers.CustomizeRequestOption {
119118
// ConnectionString returns the connection string for the MongoDB container.
120119
// If you provide a username and a password, the connection string will also include them.
121120
func (c *MongoDBContainer) ConnectionString(ctx context.Context) (string, error) {
122-
host, err := c.Host(ctx)
123-
if err != nil {
124-
return "", err
125-
}
126-
port, err := c.MappedPort(ctx, "27017/tcp")
121+
endpoint, err := c.PortEndpoint(ctx, "27017/tcp", "")
127122
if err != nil {
128123
return "", err
129124
}
130125
u := url.URL{
131126
Scheme: "mongodb",
132-
Host: net.JoinHostPort(host, port.Port()),
127+
Host: endpoint,
133128
Path: "/",
134129
}
135130

modules/mssql/mssql.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,14 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
154154

155155
// ConnectionString returns the connection string for the MSSQLServer container
156156
func (c *MSSQLServerContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
157-
host, err := c.Host(ctx)
157+
endpoint, err := c.PortEndpoint(ctx, defaultPort, "")
158158
if err != nil {
159-
return "", fmt.Errorf("host: %w", err)
160-
}
161-
162-
containerPort, err := c.MappedPort(ctx, defaultPort)
163-
if err != nil {
164-
return "", fmt.Errorf("mapped port: %w", err)
159+
return "", fmt.Errorf("port endpoint: %w", err)
165160
}
166161

167162
extraArgs := strings.Join(args, "&")
168163

169-
connStr := fmt.Sprintf("sqlserver://%s:%s@%s:%s?%s", c.username, c.password, host, containerPort.Port(), extraArgs)
164+
connStr := fmt.Sprintf("sqlserver://%s:%s@%s?%s", c.username, c.password, endpoint, extraArgs)
170165

171166
return connStr, nil
172167
}

0 commit comments

Comments
 (0)