Skip to content

Feat/conn-str #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2024
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
85 changes: 64 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!-- Code generated by gomarkdoc. DO NOT EDIT -->

[![tests](https://github.com/jfelipearaujo/testcontainers/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/jfelipearaujo/testcontainers/actions/workflows/tests.yml)
[![version](https://img.shields.io/github/v/release/jfelipearaujo/testcontainers.svg)](https://github.com/jfelipearaujo/testcontainers/releases/latest)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/jfelipearaujo/testcontainers#section-readme)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jfelipearaujo/testcontainers/blob/main/LICENSE)

# testcontainers
This is an addon to be used with Testcontainers package and with GoDog

Expand Down Expand Up @@ -749,10 +749,12 @@ import "github.com/jfelipearaujo/testcontainers/pkg/container/mongodb"
## Index

- [Constants](<#constants>)
- [func BuildConnectionString\(ctx context.Context, container testcontainers.Container, opts ...MongoOption\) \(string, error\)](<#BuildConnectionString>)
- [func BuildExternalConnectionString\(ctx context.Context, container testcontainers.Container, opts ...MongoOption\) \(string, error\)](<#BuildExternalConnectionString>)
- [func BuildInternalConnectionString\(ctx context.Context, container testcontainers.Container, opts ...MongoOption\) \(string, error\)](<#BuildInternalConnectionString>)
- [func WithMongoContainer\(\) container.ContainerOption](<#WithMongoContainer>)
- [type MongoOption](<#MongoOption>)
- [func WithExposedPort\(exposedPort string\) MongoOption](<#WithExposedPort>)
- [func WithNetwork\(network \*network.Network\) MongoOption](<#WithNetwork>)
- [func WithPass\(pass string\) MongoOption](<#WithPass>)
- [func WithUser\(user string\) MongoOption](<#WithUser>)
- [type Options](<#Options>)
Expand All @@ -770,21 +772,34 @@ const (
)
```

<a name="BuildConnectionString"></a>
## func [BuildConnectionString](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L65>)
<a name="BuildExternalConnectionString"></a>
## func [BuildExternalConnectionString](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L100>)

```go
func BuildConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error)
func BuildExternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error)
```

Return a MongoDB connection string for the given container with default options
Return a MongoDB connection string for the given container with default options when the container is NOT in a network

```
Example: "mongodb://mongo:mongo@localhost:27017/"
```

<a name="BuildInternalConnectionString"></a>
## func [BuildInternalConnectionString](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L76>)

```go
func BuildInternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error)
```

Return a MongoDB connection string for the given container with default options when the container is in a network

```
Example: "mongodb://mongo:mongo@network_alias:27017/"
```

<a name="WithMongoContainer"></a>
## func [WithMongoContainer](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L99>)
## func [WithMongoContainer](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L134>)

```go
func WithMongoContainer() container.ContainerOption
Expand All @@ -804,7 +819,7 @@ StartupTimeout: "30 seconds"
```

<a name="MongoOption"></a>
## type [MongoOption](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L33>)
## type [MongoOption](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L35>)

MongoOption is a type that represents a MongoDB option

Expand All @@ -813,7 +828,7 @@ type MongoOption func(*Options)
```

<a name="WithExposedPort"></a>
### func [WithExposedPort](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L38>)
### func [WithExposedPort](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L40>)

```go
func WithExposedPort(exposedPort string) MongoOption
Expand All @@ -825,8 +840,21 @@ WithExposedPort is a MongoOption that sets the exposed port of the MongoDB conta
Default: "27017"
```

<a name="WithNetwork"></a>
### func [WithNetwork](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L67>)

```go
func WithNetwork(network *network.Network) MongoOption
```

WithNetwork is a MongoOption that sets the network alias of the MongoDB container

```
Default: nil
```

<a name="WithPass"></a>
### func [WithPass](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L56>)
### func [WithPass](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L58>)

```go
func WithPass(pass string) MongoOption
Expand All @@ -839,7 +867,7 @@ Default: "test"
```

<a name="WithUser"></a>
### func [WithUser](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L47>)
### func [WithUser](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L49>)

```go
func WithUser(user string) MongoOption
Expand All @@ -852,7 +880,7 @@ Default: "test"
```

<a name="Options"></a>
## type [Options](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L26-L30>)
## type [Options](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/mongodb/mongodb.go#L27-L32>)

Options is a type that represents the options for a MongoDB container

Expand All @@ -865,9 +893,10 @@ Default options:

```go
type Options struct {
ExposedPort string
User string
Pass string
ExposedPort string
User string
Pass string
NetworkAlias *string
}
```

Expand All @@ -880,7 +909,8 @@ import "github.com/jfelipearaujo/testcontainers/pkg/container/postgres"
## Index

- [Constants](<#constants>)
- [func BuildConnectionString\(ctx context.Context, container testcontainers.Container, opts ...PostgresOption\) \(string, error\)](<#BuildConnectionString>)
- [func BuildExternalConnectionString\(ctx context.Context, container testcontainers.Container, opts ...PostgresOption\) \(string, error\)](<#BuildExternalConnectionString>)
- [func BuildInternalConnectionString\(ctx context.Context, container testcontainers.Container, opts ...PostgresOption\) \(string, error\)](<#BuildInternalConnectionString>)
- [func WithPostgresContainer\(\) container.ContainerOption](<#WithPostgresContainer>)
- [type Options](<#Options>)
- [type PostgresOption](<#PostgresOption>)
Expand All @@ -905,21 +935,34 @@ const (
)
```

<a name="BuildConnectionString"></a>
## func [BuildConnectionString](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/postgres/postgres.go#L91>)
<a name="BuildExternalConnectionString"></a>
## func [BuildExternalConnectionString](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/postgres/postgres.go#L116>)

```go
func BuildConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error)
func BuildExternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error)
```

Return a PostgreSQL connection string for the given container with default options
Return a PostgreSQL connection string for the given container with default options when the container is NOT in a network

```
Example: "postgres://postgres:postgres@localhost:5432/postgres_db?sslmode=disable"
```

<a name="BuildInternalConnectionString"></a>
## func [BuildInternalConnectionString](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/postgres/postgres.go#L91>)

```go
func BuildInternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error)
```

Return a PostgreSQL connection string for the given container with default options when the container is in a network

```
Example: "postgres://postgres:postgres@network_alias:5432/postgres_db?sslmode=disable"
```

<a name="WithPostgresContainer"></a>
## func [WithPostgresContainer](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/postgres/postgres.go#L140>)
## func [WithPostgresContainer](<https://github.com/jfelipearaujo/testcontainers/blob/main/pkg/container/postgres/postgres.go#L153>)

```go
func WithPostgresContainer() container.ContainerOption
Expand Down
2 changes: 1 addition & 1 deletion examples/example_02/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func initializeScenario(ctx *godog.ScenarioContext) {
return ctx, err
}

connectionString, err := postgres.BuildConnectionString(ctx, pgContainer)
connectionString, err := postgres.BuildExternalConnectionString(ctx, pgContainer)
if err != nil {
return ctx, err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/example_04/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func initializeScenario(ctx *godog.ScenarioContext) {
return ctx, err
}

connectionString, err := mongodb.BuildConnectionString(ctx, mongoContainer)
connectionString, err := mongodb.BuildExternalConnectionString(ctx, mongoContainer)
if err != nil {
return ctx, err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/example_06/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func initializeScenario(ctx *godog.ScenarioContext) {
return ctx, err
}

connString, err := postgres.BuildConnectionString(ctx, pgContainer, postgres.WithNetwork(ntwrkDefinition))
connString, err := postgres.BuildInternalConnectionString(ctx, pgContainer, postgres.WithNetwork(ntwrkDefinition))
if err != nil {
return ctx, err
}
Expand Down
45 changes: 40 additions & 5 deletions pkg/container/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/docker/go-connections/nat"
"github.com/jfelipearaujo/testcontainers/pkg/container"
"github.com/jfelipearaujo/testcontainers/pkg/network"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand All @@ -24,9 +25,10 @@ const (
// User: "mongo"
// Pass: "mongo"
type Options struct {
ExposedPort string
User string
Pass string
ExposedPort string
User string
Pass string
NetworkAlias *string
}

// MongoOption is a type that represents a MongoDB option
Expand Down Expand Up @@ -59,10 +61,43 @@ func WithPass(pass string) MongoOption {
}
}

// Return a MongoDB connection string for the given container with default options
// WithNetwork is a MongoOption that sets the network alias of the MongoDB container
//
// Default: nil
func WithNetwork(network *network.Network) MongoOption {
return func(options *Options) {
options.NetworkAlias = &network.Alias
}
}

// Return a MongoDB connection string for the given container with default options when the container is in a network
//
// Example: "mongodb://mongo:mongo@network_alias:27017/"
func BuildInternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error) {
options := &Options{
ExposedPort: ExposedPort,
User: User,
Pass: Pass,
}

for _, o := range opts {
o(options)
}

if options.NetworkAlias == nil {
return "", fmt.Errorf("the container is not in a network")
}

host := *options.NetworkAlias
mappedPort := nat.Port(options.ExposedPort)

return fmt.Sprintf("mongodb://%s:%s@%s:%s/", options.User, options.Pass, host, mappedPort.Port()), nil
}

// Return a MongoDB connection string for the given container with default options when the container is NOT in a network
//
// Example: "mongodb://mongo:mongo@localhost:27017/"
func BuildConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error) {
func BuildExternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...MongoOption) (string, error) {
options := &Options{
ExposedPort: ExposedPort,
User: User,
Expand Down
43 changes: 28 additions & 15 deletions pkg/container/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,35 @@ func WithNetwork(network *network.Network) PostgresOption {
}
}

// Return a PostgreSQL connection string for the given container with default options
// Return a PostgreSQL connection string for the given container with default options when the container is in a network
//
// Example: "postgres://postgres:postgres@localhost:5432/postgres_db?sslmode=disable"
func BuildConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error) {
var err error
// Example: "postgres://postgres:postgres@network_alias:5432/postgres_db?sslmode=disable"
func BuildInternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error) {
options := &Options{
ExposedPort: ExposedPort,
Database: Database,
User: User,
Pass: Pass,
}

for _, o := range opts {
o(options)
}

if options.NetworkAlias == nil {
return "", fmt.Errorf("the container is not in a network")
}

host := *options.NetworkAlias
mappedPort := nat.Port(options.ExposedPort)

return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", options.User, options.Pass, host, mappedPort.Port(), options.Database), nil
}

// Return a PostgreSQL connection string for the given container with default options when the container is NOT in a network
//
// Example: "postgres://postgres:postgres@localhost:5432/postgres_db?sslmode=disable"
func BuildExternalConnectionString(ctx context.Context, container testcontainers.Container, opts ...PostgresOption) (string, error) {
options := &Options{
ExposedPort: ExposedPort,
Database: Database,
Expand All @@ -103,9 +126,7 @@ func BuildConnectionString(ctx context.Context, container testcontainers.Contain
return "", fmt.Errorf("failed to get the host: %w", err)
}

var mappedPort nat.Port

mappedPort, err = container.MappedPort(ctx, nat.Port(options.ExposedPort))
mappedPort, err := container.MappedPort(ctx, nat.Port(options.ExposedPort))
if err != nil {
return "", fmt.Errorf("failed to get the mapped port: %w", err)
}
Expand All @@ -114,14 +135,6 @@ func BuildConnectionString(ctx context.Context, container testcontainers.Contain
o(options)
}

if options.NetworkAlias != nil {
// changed the host to be the network alias
host = *options.NetworkAlias

// changed the mapped port to be the exposed port, allowing the connection to be made between the containers
mappedPort = nat.Port(options.ExposedPort)
}

return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", options.User, options.Pass, host, mappedPort.Port(), options.Database), nil
}

Expand Down
Loading