Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

kezhenxu94
Copy link

@kezhenxu94 kezhenxu94 commented Aug 4, 2024

What does this PR do?

The NewDockerCompose.Up sends termSignal as soon as the function finishes, this causes the compose project to be destroyed and future usages of it is impossible, this PR only sends termSignal when there is any error when invoking Up 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 the compose_api_test.go, an error will be thrown:

 Container 415c1584-bbc5-4984-95ee-51aac9e7e8a4-api-nginx-1  Healthy
    compose_api_test.go:724: 
                Error Trace:    /Users/kezhenxu94/workspace/testcontainers-go/modules/compose/compose_api_test.go:724
                Error:          Received unexpected error:
                                Error response from daemon: No such container: 03b490452d68941c348d83cb4a8f2f26ad3068c3f45c46f77436cf2430006bd1
                Test:           TestDockerComposeUp
                Messages:       Ports

while the container must exist as it is what we aim to start up

@kezhenxu94 kezhenxu94 requested a review from a team as a code owner August 4, 2024 13:46
Copy link

netlify bot commented Aug 4, 2024

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 290dcba
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/66b0e95aef7fa10008a2fa03
😎 Deploy Preview https://deploy-preview-2679--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@kezhenxu94 kezhenxu94 force-pushed the bugfixcompose branch 7 times, most recently from 7b7f1fb to bf21276 Compare August 4, 2024 14:08
@@ -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) {
Copy link
Member

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?

Copy link
Author

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?

Copy link
Member

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

Copy link
Author

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 😄

@mdelapenya
Copy link
Member

mdelapenya commented Aug 5, 2024

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? 🙏

@kezhenxu94
Copy link
Author

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 main_test.go with the following:

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 go test main_test.go, it prints something like:

❯ go test main_test.go
time="2024-08-05T22:37:54+08:00" level=warning msg="/private/tmp/testcontainers-go-compose/testdata/docker-compose.yml: `version` is obsolete"
 Network 6a8a23a0-541c-4182-bb0e-56e95c57a62a_default  Creating
 Network 6a8a23a0-541c-4182-bb0e-56e95c57a62a_default  Created
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-nginx-1  Creating
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-mysql-1  Creating
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-nginx-1  Created
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-mysql-1  Created
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-nginx-1  Starting
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-mysql-1  Starting
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-mysql-1  Started
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-nginx-1  Started
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-mysql-1  Waiting
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-nginx-1  Waiting
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-nginx-1  Healthy
 Container 6a8a23a0-541c-4182-bb0e-56e95c57a62a-mysql-1  Healthy
--- FAIL: ExampleCompose (21.97s)
got:
[mysql nginx]
<nil>
Error response from daemon: No such container: e3a60e7c7a174de3d1dfbc947c91d677236647b1deed2ac81607e2861272eaba
want:
[mysql nginx]
<nil>
<nil>
FAIL
FAIL    command-line-arguments  22.675s
FAIL

@kezhenxu94 kezhenxu94 changed the title fix(compose): fix compose destroies right after Up fix(compose): fix compose destroys right after Up Aug 5, 2024
@mdelapenya mdelapenya self-assigned this Aug 5, 2024
@mdelapenya mdelapenya added the bug An issue with the library label Aug 5, 2024
@stevenh
Copy link
Collaborator

stevenh commented Aug 5, 2024

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.

@stevenh
Copy link
Collaborator

stevenh commented Aug 8, 2024

I've extracted the compose fix from the bug bash and its here #2722 let me know if that fixes the issues.

@kezhenxu94
Copy link
Author

kezhenxu94 commented Aug 9, 2024

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?

@stevenh
Copy link
Collaborator

stevenh commented Aug 9, 2024

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.

@mdelapenya
Copy link
Member

@kezhenxu94 could you please verify if the main branch, which contains #2722, works for you? 🙏

If so, let's close this one, thanks!

@kezhenxu94 kezhenxu94 closed this Aug 14, 2024
@kezhenxu94 kezhenxu94 deleted the bugfixcompose branch August 14, 2024 12:12
@mdelapenya
Copy link
Member

Thanks @kezhenxu94 for taking the time to contribute to the project, and for verifying the fix. Very much appreciated 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue with the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants