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: logs updated on starting of a stopped container #3896

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Shubhranshu153
Copy link
Contributor

@Shubhranshu153 Shubhranshu153 commented Feb 18, 2025

Issue:

When we start a container that was running and stopped or exited, logs are not updated.

Steps to Reproduce:

step 1: Run and exit container

sudo nerdctl run -it --name test-1 alpine:latest
/ # echo hello
hello
/ # exit
  1. start the same container
 sudo nerdctl start -a test-1
/ # echo bar
bar
/ # exit
  1. Check logs. Bar is missing.
 sudo nerdctl logs test-1
/ # echo hello
hello
/ # exit

fix:

The pr fixes it by adding multiwriters to start of containers.

@Shubhranshu153 Shubhranshu153 force-pushed the fix-logs-on-attach branch 6 times, most recently from 49019a2 to ccb179d Compare February 18, 2025 09:03
@Shubhranshu153
Copy link
Contributor Author

@AkihiroSuda
PTAL when you get a chance.
Thank You.

@apostasie
Copy link
Contributor

Docker failure is #3908

Rootful failure is #3513

),
Setup: func(data test.Data, helpers test.Helpers) {
cmd := helpers.Command("run", "-it", "--name", data.Identifier(), testutil.CommonImage)
cmd.WithWrapper("unbuffer", "-p")
Copy link
Contributor

Choose a reason for hiding this comment

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

Hopefully, #3591 should be merged soon and remove the need to use unbuffer and DelayOnceReader.

return &test.Expected{
ExitCode: 0,
Errors: []error{},
Output: test.All(
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess test.All( is unnecessary here as you only have one condition to test.

Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Errors: []error{},
Copy link
Contributor

Choose a reason for hiding this comment

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

You can remove this line entirely.

),
Setup: func(data test.Data, helpers test.Helpers) {
cmd := helpers.Command("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sh", "-c", "while true; do echo foo; sleep 1; done")
cmd.Run(&test.Expected{
Copy link
Contributor

@apostasie apostasie Feb 22, 2025

Choose a reason for hiding this comment

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

You can simplify to just: helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sh", "-c", "while true; do echo foo; sleep 1; done")
(that does the command and checks it is succesful)

initialFooCount := strings.Count(initialLogs, "foo")
data.Set("initialFooCount", strconv.Itoa(initialFooCount))
helpers.Anyhow("start", data.Identifier())
time.Sleep(5 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

You possibly want to replace helpers.Anyhow("start", data.Identifier()) with helpers.Ensure("start", data.Identifier()) to make sure it is succesful.

Also nerdtest.EnsureContainerStarted should give you a more determistic way to achieve this without relying on an arbitrary wait.

data.Set("initialFooCount", strconv.Itoa(initialFooCount))
helpers.Anyhow("start", data.Identifier())
time.Sleep(5 * time.Second)
cmd := helpers.Command("logs", data.Identifier())
Copy link
Contributor

Choose a reason for hiding this comment

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

return helpers.Command("logs", data.Identifier())

Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Errors: []error{},
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary.

return &test.Expected{
ExitCode: 0,
Errors: []error{},
Output: test.All(
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above.

func(stdout string, info string, t *testing.T) {
finalLogsCount := strings.Count(stdout, "foo")
initialFooCount, _ := strconv.Atoi(data.Get("initialFooCount"))
assert.Assert(t, finalLogsCount > initialFooCount, "Expected 'foo' count to increase after restart")
Copy link
Contributor

Choose a reason for hiding this comment

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

assert.Assert(t, finalLogsCount > initialFooCount, "Expected 'foo' count to increase after restart", info)

That will display context if it fails.

@@ -255,3 +258,97 @@ func TestTailFollowRotateLogs(t *testing.T) {
}
assert.Equal(t, true, len(tailLogs) > linesPerFile, logRun.Stderr())
}

func TestLogsWithStartContainer(t *testing.T) {
if runtime.GOOS == "windows" {
Copy link
Contributor

Choose a reason for hiding this comment

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

testCase.Require = test.Not(test.Windows)

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if you do it at the test root, no need for it in the subtests.

@apostasie
Copy link
Contributor

@Shubhranshu153 I just have a batch of nits on the test.

Btw, feedback on the test framework is highly welcome.

Ideally, if we get #3591 in first, we can get rid of the added DelayOnceReaders here (and unbuffer).

But anyhow, don't sweat it - I can clean that up afterwards if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants