Skip to content

Commit f860057

Browse files
authored
Merge pull request #4226 from fahedouch/fix-logs-follow-newline
fix: remove trailing newline from log entries on last decode iteration
2 parents 575f9e6 + a5d1701 commit f860057

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

cmd/nerdctl/container/container_logs_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,34 @@ func TestLogsWithDetails(t *testing.T) {
394394
testCase.Run(t)
395395
}
396396

397+
func TestLogsFollowNoExtraneousLineFeed(t *testing.T) {
398+
testCase := nerdtest.Setup()
399+
// This test verifies that `nerdctl logs -f` does not add extraneous line feeds
400+
testCase.Require = require.Not(require.Windows)
401+
402+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
403+
// Create a container that outputs a message without a trailing newline
404+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage,
405+
"sh", "-c", "printf 'Hello without newline'")
406+
}
407+
408+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
409+
helpers.Anyhow("rm", "-f", data.Identifier())
410+
}
411+
412+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
413+
// Use logs -f to follow the logs
414+
// Arbitrary, but we need to wait until the logs show up
415+
time.Sleep(3 * time.Second)
416+
return helpers.Command("logs", "-f", data.Identifier())
417+
}
418+
419+
// Verify that the output is exactly "Hello without newline" without any additional line feeds
420+
testCase.Expected = test.Expects(0, nil, expect.Equals("Hello without newline"))
421+
422+
testCase.Run(t)
423+
}
424+
397425
func TestLogsWithStartContainer(t *testing.T) {
398426
testCase := nerdtest.Setup()
399427

pkg/logging/json_logger_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestReadRotatedJSONLog(t *testing.T) {
7373
time.Sleep(1 * time.Millisecond)
7474
logData, _ := json.Marshal(log)
7575
file.Write(logData)
76+
file.Write([]byte("\n"))
7677

7778
if line == 5 {
7879
file.Close()
@@ -104,7 +105,7 @@ func TestReadRotatedJSONLog(t *testing.T) {
104105
close(containerStopped)
105106

106107
if expectedStdout != stdoutBuf.String() {
107-
t.Errorf("expected: %s, acoutal: %s", expectedStdout, stdoutBuf.String())
108+
t.Errorf("expected: %s, actual: %s", expectedStdout, stdoutBuf.String())
108109
}
109110
}
110111

pkg/logging/jsonfile/jsonfile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Encode(stdout <-chan string, stderr <-chan string, writer io.Writer) error
5454
Stream: name,
5555
}
5656
for logEntry := range dataChan {
57-
e.Log = logEntry + "\n"
57+
e.Log = logEntry
5858
e.Time = time.Now().UTC()
5959
encMu.Lock()
6060
encErr := enc.Encode(e)

pkg/logging/logging.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func loggingProcessAdapter(ctx context.Context, driver Driver, dataStore, addres
255255
var s string
256256
s, err = r.ReadString('\n')
257257
if len(s) > 0 {
258-
dataChan <- strings.TrimSuffix(s, "\n")
258+
dataChan <- s
259259
}
260260

261261
if err != nil && err != io.EOF {

0 commit comments

Comments
 (0)