diff --git a/modules/mariadb/mariadb.go b/modules/mariadb/mariadb.go index 4036cacc76..ab1437e94d 100644 --- a/modules/mariadb/mariadb.go +++ b/modules/mariadb/mariadb.go @@ -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 } @@ -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 } diff --git a/modules/meilisearch/meilisearch.go b/modules/meilisearch/meilisearch.go index 687a1d61ca..10c1632fed 100644 --- a/modules/meilisearch/meilisearch.go +++ b/modules/meilisearch/meilisearch.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "net" "net/http" "time" @@ -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") } diff --git a/modules/memcached/memcached.go b/modules/memcached/memcached.go index 384b18614a..acc3b480b0 100644 --- a/modules/memcached/memcached.go +++ b/modules/memcached/memcached.go @@ -3,7 +3,6 @@ package memcached import ( "context" "fmt" - "net" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" @@ -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, "") } diff --git a/modules/milvus/milvus.go b/modules/milvus/milvus.go index b35cc99335..79e886ee86 100644 --- a/modules/milvus/milvus.go +++ b/modules/milvus/milvus.go @@ -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 diff --git a/modules/minio/minio.go b/modules/minio/minio.go index 6547a9003b..4805716caf 100644 --- a/modules/minio/minio.go +++ b/modules/minio/minio.go @@ -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 diff --git a/modules/mockserver/mockserver.go b/modules/mockserver/mockserver.go index b53f164e86..022095540c 100644 --- a/modules/mockserver/mockserver.go +++ b/modules/mockserver/mockserver.go @@ -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") } diff --git a/modules/mongodb/mongodb.go b/modules/mongodb/mongodb.go index 83c7c6765a..76a3c57125 100644 --- a/modules/mongodb/mongodb.go +++ b/modules/mongodb/mongodb.go @@ -6,7 +6,6 @@ import ( _ "embed" "errors" "fmt" - "net" "net/url" "time" @@ -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: "/", } diff --git a/modules/mssql/mssql.go b/modules/mssql/mssql.go index 88b934cb13..d93de3feb0 100644 --- a/modules/mssql/mssql.go +++ b/modules/mssql/mssql.go @@ -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 }