Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions modules/mariadb/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,7 @@ func (c *MariaDBContainer) MustConnectionString(ctx context.Context, args ...str
}

func (c *MariaDBContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
containerPort, err := c.MappedPort(ctx, "3306/tcp")
if err != nil {
return "", err
}

host, err := c.Host(ctx)
endpoint, err := c.PortEndpoint(ctx, "3306/tcp", "")
if err != nil {
return "", err
}
Expand All @@ -215,6 +210,6 @@ func (c *MariaDBContainer) ConnectionString(ctx context.Context, args ...string)
extraArgs = "?" + extraArgs
}

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s%s", c.username, c.password, host, containerPort.Port(), c.database, extraArgs)
connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s%s", c.username, c.password, endpoint, c.database, extraArgs)
return connectionString, nil
}
13 changes: 1 addition & 12 deletions modules/meilisearch/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -104,15 +103,5 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
// Address retrieves the address of the Meilisearch container.
// It will use http as protocol, as TLS is not supported at the moment.
func (c *MeilisearchContainer) Address(ctx context.Context) (string, error) {
containerPort, err := c.MappedPort(ctx, defaultHTTPPort)
if err != nil {
return "", fmt.Errorf("mapped port: %w", err)
}

host, err := c.Host(ctx)
if err != nil {
return "", fmt.Errorf("host: %w", err)
}

return "http://" + net.JoinHostPort(host, containerPort.Port()), nil
return c.PortEndpoint(ctx, defaultHTTPPort, "http")
}
13 changes: 1 addition & 12 deletions modules/memcached/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package memcached
import (
"context"
"fmt"
"net"

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

// HostPort returns the host and port of the Memcached container
func (c *Container) HostPort(ctx context.Context) (string, error) {
host, err := c.Host(ctx)
if err != nil {
return "", fmt.Errorf("host: %w", err)
}

port, err := c.MappedPort(ctx, defaultPort)
if err != nil {
return "", fmt.Errorf("port: %w", err)
}

return net.JoinHostPort(host, port.Port()), nil
return c.PortEndpoint(ctx, defaultPort, "")
}
10 changes: 1 addition & 9 deletions modules/milvus/milvus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ type MilvusContainer struct {
// ConnectionString returns the connection string for the milvus container, using the default 19530 port, and
// obtaining the host and exposed port from the container.
func (c *MilvusContainer) ConnectionString(ctx context.Context) (string, error) {
host, err := c.Host(ctx)
if err != nil {
return "", err
}
port, err := c.MappedPort(ctx, grpcPort)
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%s", host, port.Port()), nil
return c.PortEndpoint(ctx, grpcPort, "")
}

// Deprecated: use Run instead
Expand Down
10 changes: 1 addition & 9 deletions modules/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ func WithPassword(password string) testcontainers.CustomizeRequestOption {
// ConnectionString returns the connection string for the minio container, using the default 9000 port, and
// obtaining the host and exposed port from the container.
func (c *MinioContainer) ConnectionString(ctx context.Context) (string, error) {
host, err := c.Host(ctx)
if err != nil {
return "", err
}
port, err := c.MappedPort(ctx, "9000/tcp")
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%s", host, port.Port()), nil
return c.PortEndpoint(ctx, "9000/tcp", "")
}

// Deprecated: use Run instead
Expand Down
12 changes: 2 additions & 10 deletions modules/mockserver/mockserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
return c, nil
}

// GetURL returns the URL of the MockServer container
// URL returns the URL of the MockServer container
func (c *MockServerContainer) URL(ctx context.Context) (string, error) {
host, err := c.Host(ctx)
if err != nil {
return "", err
}
port, err := c.MappedPort(ctx, "1080/tcp")
if err != nil {
return "", err
}
return fmt.Sprintf("http://%s:%d", host, port.Int()), nil
return c.PortEndpoint(ctx, "1080/tcp", "http")
}
9 changes: 2 additions & 7 deletions modules/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
_ "embed"
"errors"
"fmt"
"net"
"net/url"
"time"

Expand Down Expand Up @@ -119,17 +118,13 @@ func WithReplicaSet(replSetName string) testcontainers.CustomizeRequestOption {
// ConnectionString returns the connection string for the MongoDB container.
// If you provide a username and a password, the connection string will also include them.
func (c *MongoDBContainer) ConnectionString(ctx context.Context) (string, error) {
host, err := c.Host(ctx)
if err != nil {
return "", err
}
port, err := c.MappedPort(ctx, "27017/tcp")
endpoint, err := c.PortEndpoint(ctx, "27017/tcp", "")
if err != nil {
return "", err
}
u := url.URL{
Scheme: "mongodb",
Host: net.JoinHostPort(host, port.Port()),
Host: endpoint,
Path: "/",
}

Expand Down
11 changes: 3 additions & 8 deletions modules/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,14 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom

// ConnectionString returns the connection string for the MSSQLServer container
func (c *MSSQLServerContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
host, err := c.Host(ctx)
endpoint, err := c.PortEndpoint(ctx, defaultPort, "")
if err != nil {
return "", fmt.Errorf("host: %w", err)
}

containerPort, err := c.MappedPort(ctx, defaultPort)
if err != nil {
return "", fmt.Errorf("mapped port: %w", err)
return "", fmt.Errorf("port endpoint: %w", err)
}

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

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

return connStr, nil
}