-
Notifications
You must be signed in to change notification settings - Fork 366
chore: replace ory/dockertest with testcontainers #2782
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
base: main
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2782 +/- ##
==========================================
- Coverage 77.72% 75.25% -2.47%
==========================================
Files 472 472
Lines 49707 49727 +20
==========================================
- Hits 38631 37416 -1215
- Misses 8228 9494 +1266
+ Partials 2848 2817 -31 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
mdelapenya
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really promising! I added some comments regarding best practices when using the library. Feel free to ping me for anything you need, I'm more than happy to help you with this 😊
| require.NoError(t, err) | ||
| t.Cleanup(func() { | ||
| _ = pool.Client.RemoveNetwork(network.ID) | ||
| _ = net.Remove(ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: use CleanupNetwork, and call it before require, as it handle nils
nw, err := network.New(ctx, network.WithDriver("bridge"), network.WithLabels(map[string]string{"name": bridgeNetworkName}))
testcontainers.CleanupNetwork(t, nw)
require.NoError(t, err)
| config.RestartPolicy = docker.RestartPolicy{ | ||
| Name: "no", | ||
| } | ||
| container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: why not using the module? https://github.com/Mariscal6/testcontainers-spicedb-go
If it does not provide the APIs need by these tests, we can contribute them to the upstream. I'm pretty sure @Mariscal6 will be happy to review that
| require.NoError(t, err) | ||
| t.Cleanup(func() { | ||
| _ = pool.Purge(resource) | ||
| _ = container.Terminate(ctx) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: use CleanupContainer, as for networks.
ctr, err := ... // module's Run function OR the GenericContainer() one.
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)| require.NoError(t, err) | ||
|
|
||
| if status != 0 { | ||
| if state.ExitCode != 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: you could define a log consumer at container creation, and print it here. Please see https://golang.testcontainers.org/features/follow_logs/#following-container-logs
| config.RestartPolicy = docker.RestartPolicy{ | ||
| Name: "no", | ||
| } | ||
| migrateContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: given there are a few of this, you could abstract the container creation building your own test "framework", something like this:
// pseudocode
func NewTestSpiceDB(ctx Context, opts ...ContainerOpts) {...}| } | ||
|
|
||
| func createNetworkBridge(t testing.TB, pool *dockertest.Pool) string { | ||
| func createNetworkBridge(t testing.TB) (string, *testcontainers.DockerNetwork) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: you could expose this in the "framework" and use it everywhere else that you create a network. Adding the CleanupNetwork here would automatically clean up resources with every call.
| "MAX_CLIENT_CONN=" + PostgresTestMaxConnections, | ||
| }, | ||
| req := testcontainers.ContainerRequest{ | ||
| Name: pgbouncerContainerHostname, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: remember the advice against names :)
| WithStartupTimeout(dockerBootTimeout), | ||
| } | ||
|
|
||
| if bridgeNetworkName != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: if moving to the Run function with container customisers as functional options, then you could be passing any of the WithNetworkXXX options. See https://golang.testcontainers.org/features/creating_container/#networking-options
| }, | ||
| req := testcontainers.ContainerRequest{ | ||
| Name: pgbouncerContainerHostname, | ||
| Image: "mirror.gcr.io/edoburu/pgbouncer:latest", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: is this a postgres db instance? Try using the Postgres module with your pgBouncer image, so you get the Postgres module's APIs. Then you could remove all the wait code below
| name := "spanner-" + uuid.New().String() | ||
| resource, err := pool.RunWithOptions(&dockertest.RunOptions{ | ||
|
|
||
| req := testcontainers.ContainerRequest{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: we have a module for Spanner :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the issue we've found is that the spanner emulator doesn't replicate the behaviors that we need faithfully enough to be of much use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that's good to know! Is this something we can add to the upstream module?
No description provided.