Skip to content

Commit

Permalink
chore: support for copying the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Mar 23, 2023
1 parent e1989d5 commit 6705bde
Show file tree
Hide file tree
Showing 4 changed files with 758 additions and 8 deletions.
27 changes: 19 additions & 8 deletions docs/modules/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ func StartContainer(ctx context.Context, opts ...PostgresContainerOption) (*Post
- `context.Context`, the Go context.
- `PostgresContainerOption`, a variad argument for passing options.

## Container Options
### Container Options

When starting the Postgres container, you can pass options in a variadic way to configure it.

!!!tip
You can find all the available configuration and environment variables for the Postgres Docker image on [Docker Hub](https://hub.docker.com/_/postgres).

### Image
#### Image

If you need to set a different Postgres Docker image, you can use `WithImage` with a valid Docker image
for Postgres. E.g. `WithImage("docker.io/postgres:9.6")`.

### Initial Database
#### Initial Database

If you need to set a different database, and its credentials, you can use the `WithInitialDatabase`.

<!--codeinclude-->
[Set Initial database](../../modules/postgres/postgres_test.go) inside_block:withInitialDatabase
<!--/codeinclude-->

### Init Scripts
#### Init Scripts

If you would like to do additional initialization in the Postgres container, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts to the container request.
Those files will be copied after the container is created but before it's started under `/docker-entrypoint-initdb.d`. According to Postgres Docker image,
Expand All @@ -60,7 +60,18 @@ initialization before starting the service.
[Init script content](../../modules/postgres/testresources/init-user-db.sh)
<!--/codeinclude-->

### Wait Strategies
#### Database configuration

In the case you have a custom config file for Postgres, it's possible to copy that file into the container before it's started.

!!!tip
For information on what is available to configure, see the [PostgreSQL docs](https://www.postgresql.org/docs/14/runtime-config.html) for the specific version of PostgreSQL that you are running.

<!--codeinclude-->
[Include custom configuration file](../../modules/postgres/postgres_test.go) inside_block:withConfigFile
<!--/codeinclude-->

#### Wait Strategies

Given you could need to wait for different conditions, in particular using a wait.ForSQL strategy,
the Postgres container exposes a `WithWaitStrategy` option to set a custom wait strategy.
Expand All @@ -69,9 +80,9 @@ the Postgres container exposes a `WithWaitStrategy` option to set a custom wait
[Set Wait Strategy](../../modules/postgres/postgres_test.go) inside_block:withWaitStrategy
<!--/codeinclude-->

## Container Methods
### Container Methods

### ConnectionString
#### ConnectionString

This method returns the connection string to connect to the Postgres container, using the default `5432` port and SSL disabled.
It's possible to pass extra parameters to the connection string, e.g. `application_name=myapp`, in a variadic way.
Expand All @@ -80,7 +91,7 @@ It's possible to pass extra parameters to the connection string, e.g. `applicati
[Get connection string](../../modules/postgres/postgres_test.go) inside_block:connectionString
<!--/codeinclude-->

## Postgres variants
### Postgres variants

It's possible to use the Postgres container with Timescale or Postgis, to name a few. You simply need to update the image name and the wait strategy.

Expand Down
17 changes: 17 additions & 0 deletions modules/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ func WithImage(image string) func(req *testcontainers.ContainerRequest) {
}
}

// WithConfigFile sets the config file to be used for the postgres container
// It will also set the "config_file" parameter to the path of the config file
// as a command line argument to the container
func WithConfigFile(cfg string) func(req *testcontainers.ContainerRequest) {
return func(req *testcontainers.ContainerRequest) {
cfgFile := testcontainers.ContainerFile{
HostFilePath: cfg,
ContainerFilePath: "/etc/postgresql.conf",
FileMode: 0755,
}

req.Files = append(req.Files, cfgFile)
req.Cmd = append(req.Cmd, "-c", "config_file=/etc/postgresql.conf")
}

}

// WithDatabase sets the initial database to be created when the container starts
// It can be used to define a different name for the default database that is created when the image is first started.
// If it is not specified, then the value of WithUser will be used.
Expand Down
31 changes: 31 additions & 0 deletions modules/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ func TestContainerWithWaitForSQL(t *testing.T) {
})
}

func TestWithConfigFile(t *testing.T) {
ctx := context.Background()

// withConfigFile {
container, err := StartContainer(ctx,
WithConfigFile(filepath.Join("testresources", "my-postgres.conf")),
WithDatabase(dbname),
WithUsername(user),
WithPassword(password),
WithWaitStrategy(wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(5*time.Second)),
)
if err != nil {
t.Fatal(err)
}
// }

t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

connStr, err := container.ConnectionString(ctx)
assert.NoError(t, err)

db, err := sql.Open("postgres", connStr)
assert.NoError(t, err)
assert.NotNil(t, db)
defer db.Close()
}

func TestWithInitScript(t *testing.T) {
ctx := context.Background()

Expand Down
Loading

0 comments on commit 6705bde

Please sign in to comment.