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

feat: fail-fast of wait.LogStrategy #1304

Merged

Conversation

frozenbonito
Copy link
Contributor

What does this PR do?

This PR fixes the check that the container is not emitting new logs (please refer this comment).

logs variable contains all logs after container start, so it can never be an empty string. Therefore, comparisons with empty strings are meaningless.
Instead, it is verified that the length of the logs has not changed since the last check in this PR.

This fix broke Test_GetLogsFromFailedContainer, so I fixed it too.

Why is it important?

When the container stop, LogStrategy should fail-fast, but it doesn't.
This PR will fix it.

Related issues

@frozenbonito frozenbonito requested a review from a team as a code owner June 23, 2023 13:36
@netlify
Copy link

netlify bot commented Jun 23, 2023

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 9102ab6
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/64ca1101c534970008c4f6f7
😎 Deploy Preview https://deploy-preview-1304--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.

@sonarcloud
Copy link

sonarcloud bot commented Jun 23, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@frozenbonito frozenbonito changed the title fix: check that no new logs have been output Fix Fail-fast of wait.LogStrategy Jun 23, 2023
@frozenbonito frozenbonito changed the title Fix Fail-fast of wait.LogStrategy Fix fail-fast of wait.LogStrategy Jun 23, 2023
@mdelapenya mdelapenya self-assigned this Aug 1, 2023
@mdelapenya mdelapenya added bug An issue with the library enhancement New feature or request and removed bug An issue with the library labels Aug 1, 2023
@mdelapenya
Copy link
Collaborator

Hi @frozenbonito thanks for opening the issue and taking the time to elaborate a PR 💪

I think it makes sense to check for logs length to verify if the log changed after each poll of the logs.

I'm labelling the PR as enhancement instead of bug, as the wait strategy indeed works and here we are just improving the fail-fast mechanism. wdyt?

@mdelapenya
Copy link
Collaborator

Taking a deeper look at the current code, I see that we are extracting the state of the container, but we are evaluating if there is an error too late 🤔. I've double checked all the usages of the checkTarget function and we are always evaluating the error it returns immediately.

We could probably fix that logic, keeping the bug nature, like this:

			checkErr := checkTarget(ctx, target)
+			if checkErr != nil {
+				return checkErr
+			}

			reader, err := target.Logs(ctx)
			if err != nil {
				time.Sleep(ws.PollInterval)
				continue
			}

			b, err := io.ReadAll(reader)
			if err != nil {
				time.Sleep(ws.PollInterval)
				continue
			}

			logs := string(b)
-			if logs == "" && checkErr != nil {
-				return checkErr
+ 			if logs == "" {
+				return fmt.Errorf("no logs found")
			} else if strings.Count(logs, ws.Log) >= ws.Occurrence {
				break LOOP
			} else {
				time.Sleep(ws.PollInterval)
				continue
			}

Thoughts?

@frozenbonito
Copy link
Contributor Author

@mdelapenya Thank you for comments.

I think LogStrategy is special case.
This is because sometimes we want to wait for specific logs whether the container has exited or not.
Please see this my comment.

If we evaluate and return the error of checkTarget immediately, some uses shown by tests are broken.
The most obvious example is TestCMD.

func TestCMD(t *testing.T) {
/*
echo a unique statement to ensure that we
can pass in a command to the ContainerRequest
and it will be run when we run the container
*/
ctx := context.Background()
req := ContainerRequest{
Image: "docker.io/alpine",
WaitingFor: wait.ForAll(
wait.ForLog("command override!"),
),
Cmd: []string{"echo", "command override!"},
}
c, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, c)
}

@mdelapenya
Copy link
Collaborator

mdelapenya commented Aug 1, 2023

Ah this makes even more sense now, sorry for not recalling that very same conversation 🤦

Please see this #944 (comment).

Then this changes makes sense. Let me fix the issue with the current state of the CI, caused by the recent changes in Go + Moby (see #1394), as any CI job will fail because of that (unless we pin the Go versions)

Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Will merge this one right after fixing the errors with Go+Moby+Host header validation

@sonarcloud
Copy link

sonarcloud bot commented Aug 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@mdelapenya mdelapenya changed the title Fix fail-fast of wait.LogStrategy feat: fail-fast of wait.LogStrategy Aug 2, 2023
@mdelapenya mdelapenya merged commit fd95a8f into testcontainers:main Aug 2, 2023
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Fail-fast of wait.LogStrategy doesn't work properly
2 participants