-
-
Notifications
You must be signed in to change notification settings - Fork 512
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
fix(compose): fix compose destroys right after Up #2679
Conversation
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
7b7f1fb
to
bf21276
Compare
modules/compose/compose_api.go
Outdated
@@ -267,12 +267,10 @@ func (d *dockerCompose) Down(ctx context.Context, opts ...StackDownOption) error | |||
return d.composeService.Down(ctx, d.name, options.DownOptions) | |||
} | |||
|
|||
func (d *dockerCompose) Up(ctx context.Context, opts ...StackUpOption) error { | |||
func (d *dockerCompose) Up(ctx context.Context, opts ...StackUpOption) (err error) { |
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.
Is there a way to avoid naked returns?
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.
Is there a way to avoid naked returns?
If we really want to avoid that, we have to check err != nil
or not in every single return
statement, and if err != nil
cleanup by sending a termSignal
, this is more error prune in my opinion, what do you think?
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.
I try to avoid naked returns 😅 The verbosity of Go comes with readability, which comes with maintainability
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.
I refactor the code to avoid naked return, let me know how you like it 😄
I have one question regarding my example project for compose: https://github.com/mdelapenya/testcontainers-go-compose Can you elaborate a repro snippet based on that project? 🙏 |
You can replace the entire file package main
import (
"context"
"fmt"
"log"
"path/filepath"
"time"
tccompose "github.com/testcontainers/testcontainers-go/modules/compose"
)
func ExampleCompose() {
compose, err := tccompose.NewDockerCompose(filepath.Join("testdata", "docker-compose.yml"))
if err != nil {
log.Fatal(err)
}
defer func() {
if err := compose.Down(context.Background(),
tccompose.RemoveOrphans(true), tccompose.RemoveImagesLocal); err != nil {
log.Fatal(err)
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = compose.Up(ctx, tccompose.Wait(true))
if err != nil {
log.Fatal(err)
}
serviceNames := compose.Services()
fmt.Println(serviceNames)
time.Sleep(20 * time.Second)
mysqlContainer, err := compose.ServiceContainer(context.Background(), "mysql")
fmt.Println(err)
_, err = mysqlContainer.Inspect(ctx)
fmt.Println(err) // I expect there is a container for mysql service, which is not the case
// Output:
// [mysql nginx]
// <nil>
// <nil>
} and run
|
f4af01f
to
197da11
Compare
There's actually a more in depth fix for this in #2664 @kezhenxu94 would you mind testing that branch to make sure if fixes your issue please. |
I've extracted the compose fix from the bug bash and its here #2722 let me know if that fixes the issues. |
Are you sure a chore issue "Remove unused parameters from private functions." also fix a bug? |
Sorry @kezhenxu94 right link but wrong cherry-pick commit id included, have fixed that now. |
@kezhenxu94 could you please verify if the main branch, which contains #2722, works for you? 🙏 If so, let's close this one, thanks! |
Thanks @kezhenxu94 for taking the time to contribute to the project, and for verifying the fix. Very much appreciated 🙇 |
What does this PR do?
The
NewDockerCompose.Up
sendstermSignal
as soon as the function finishes, this causes the compose project to be destroyed and future usages of it is impossible, this PR only sendstermSignal
when there is any error when invokingUp
method, otherwise do not send terminal signal.Why is it important?
Without this patch, the docker compose API is useless, without this patch it just starts the docker compose project and then immediately destroys it, no container can be used after calling
Up
function.Related issues
How to test this PR
It can be easily reproduced by reverting the fix in the
compose_api.go
and run thecompose_api_test.go
, an error will be thrown:while the container must exist as it is what we aim to start up