Skip to content

Commit

Permalink
Merge branch 'main' into v1
Browse files Browse the repository at this point in the history
* main:
  chore(deps): bump mkdocs-include-markdown-plugin from 6.0.4 to 6.2.1 (#2617)
  chore(deps): bump peter-evans/slash-command-dispatch from 3.0.2 to 4.0.0 (#2561)
  docs: document ryuk timeouts for compose (#2620)
  chore: use self-hosted worker for Windows tests (#2619)
  feat(postgres): use faster sql.DB instead of docker exec psql for snapshot/restore [rebased for main] (#2600)
  • Loading branch information
mdelapenya committed Jul 4, 2024
2 parents 3a6fa88 + a32fe92 commit 37d839d
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 768 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-windows-trigger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
windows-test-command-trigger:
permissions:
pull-requests: write # for peter-evans/slash-command-dispatch to create PR reaction
runs-on: ubuntu-22.04
runs-on: ubuntu-latest

steps:
- name: Trigger windows-test command
uses: peter-evans/slash-command-dispatch@f996d7b7aae9059759ac55e978cff76d91853301 # v3.0.2
uses: peter-evans/slash-command-dispatch@13bc09769d122a64f75aa5037256f6f2d78be8c4 # v4.0.0
with:
token: ${{ secrets.WINDOWS_WORKERS_TOKEN }}
# The command to trigger the pipeline: e.g. /windows-test
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ on:

jobs:
test-windows:
# At the moment, we are running a self-hosted runner on Windows 2022.
runs-on: [self-hosted, Windows, X64]
strategy:
fail-fast: false
runs-on: [self-hosted, Windows, X64, desktop-windows-intel]
timeout-minutes: 30
steps:
- name: Create pending status
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ verify_ssl = true
[packages]
mkdocs = "==1.5.3"
mkdocs-codeinclude-plugin = "==0.2.1"
mkdocs-include-markdown-plugin = "==6.0.7"
mkdocs-include-markdown-plugin = "==6.2.1"
mkdocs-material = "==9.5.18"
mkdocs-markdownextradata-plugin = "==0.2.5"

Expand Down
28 changes: 14 additions & 14 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions docs/features/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ but does not allow starting privileged containers, you can turn off the Ryuk con
!!!info
For more information about Ryuk, see [Garbage Collector](garbage_collector.md).

!!!warn
If using Ryuk and the Compose module, please increase the `ryuk.connection.timeout` to at least 5 minutes.
This is because the Compose module may take longer to start all the services. Besides, the `ryuk.reconnection.timeout`
should be increased to at least 30 seconds. For further information, please check [https://github.com/testcontainers/testcontainers-go/pull/2485](https://github.com/testcontainers/testcontainers-go/pull/2485).

## Docker host detection

_Testcontainers for Go_ will attempt to detect the Docker environment and configure everything to work automatically.
Expand Down
4 changes: 4 additions & 0 deletions docs/features/docker_compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Because `compose` v2 is implemented in Go it's possible for _Testcontainers for
use [`github.com/docker/compose`](https://github.com/docker/compose) directly and skip any process execution/_docker-compose-in-a-container_ scenario.
The `ComposeStack` API exposes this variant of using `docker compose` in an easy way.

Before using the Compose module, there is some configuration that needs to be applied first.
It customizes the behaviour of the `Ryuk` container, which is used to clean up the resources created by the `docker compose` stack.
Please refer to [the Ryuk configuration](../configuration/#customizing-ryuk-the-resource-reaper) for more information.

### Usage

Use the convenience `NewDockerCompose(...)` constructor which creates a random identifier and takes a variable number
Expand Down
39 changes: 38 additions & 1 deletion docs/modules/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ It's possible to use the Postgres container with PGVector, Timescale or Postgis,
## Examples
### Wait Strategies
The postgres module works best with these wait strategies.
No default is supplied, so you need to set it explicitly.
<!--codeinclude-->
[Example Wait Strategies](../../modules/postgres/wait_strategies.go) inside_block:waitStrategy
<!--/codeinclude-->
### Using Snapshots
This example shows the usage of the postgres module's Snapshot feature to give each test a clean database without having
to recreate the database container on every test or run heavy scripts to clean your database. This makes the individual
Expand All @@ -102,7 +111,35 @@ tests very modular, since they always run on a brand-new database.
The Snapshot logic requires dropping the connected database and using the system database to run commands, which will
not work if the database for the container is set to `"postgres"`.


<!--codeinclude-->
[Test with a reusable Postgres container](../../modules/postgres/postgres_test.go) inside_block:snapshotAndReset
<!--/codeinclude-->

### Snapshot/Restore with custom driver

- Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

The snapshot/restore feature tries to use the `postgres` driver with go's included `sql.DB` package to perform database operations.
If the `postgres` driver is not installed, it will fall back to using `docker exec`, which works, but is slower.
You can tell the module to use the database driver you have imported in your test package by setting `postgres.WithSQLDriver("name")` to your driver name.
For example, if you use pgx, see the example below.
```go
package my_test
import (
"testing"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/testcontainers/testcontainers-go/modules/postgres"
)
```
The above code snippet is importing the `pgx` driver and the _Testcontainers for Go_ Postgres module.
<!--codeinclude-->
[Snapshot/Restore with custom driver](../../modules/postgres/postgres_test.go) inside_block:snapshotAndReset
<!--/codeinclude-->
2 changes: 1 addition & 1 deletion modulegen/internal/mkdocs/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func overrideData(data []byte) []byte {

func setEmoji(content string, key string, value string) string {
old := "emoji_" + key + `: ""`
new := "emoji_" + key + ": !!python/name:materialx.emoji." + value
new := "emoji_" + key + ": !!python/name:material.extensions.emoji." + value
return strings.ReplaceAll(content, old, new)
}
2 changes: 2 additions & 0 deletions modules/postgres/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand All @@ -60,6 +61,7 @@ require (
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
Expand Down
37 changes: 37 additions & 0 deletions modules/postgres/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package postgres

import (
"github.com/testcontainers/testcontainers-go"
)

type options struct {
// SQLDriverName is the name of the SQL driver to use.
SQLDriverName string
}

func defaultOptions() options {
return options{
SQLDriverName: "postgres",
}
}

// Compiler check to ensure that Option implements the testcontainers.RequestCustomizer interface.
var _ testcontainers.RequestCustomizer = (Option)(nil)

// Option is an option for the Redpanda container.
type Option func(*options)

// Customize is a NOOP. It's defined to satisfy the testcontainers.RequestCustomizer interface.
func (o Option) Customize(*testcontainers.Request) error {
// NOOP to satisfy interface.
return nil
}

// WithSQLDriver sets the SQL driver to use for the container.
// It is passed to sql.Open() to connect to the database when making or restoring snapshots.
// This can be set if your app imports a different postgres driver, f.ex. "pgx"
func WithSQLDriver(driver string) Option {
return func(o *options) {
o.SQLDriverName = driver
}
}
Loading

0 comments on commit 37d839d

Please sign in to comment.