Skip to content

Commit 3c376a8

Browse files
committed
fix: avoid adding extraneous line feed when tail logs
Signed-off-by: fahed dorgaa <[email protected]>
1 parent e556a92 commit 3c376a8

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

cmd/nerdctl/container/container_logs_test.go

Lines changed: 28 additions & 0 deletions
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+
// and then sleeps to keep the container running for the logs -f command
405+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage,
406+
"sh", "-c", "printf 'Hello without newline'; sleep 5")
407+
}
408+
409+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
410+
helpers.Anyhow("rm", "-f", data.Identifier())
411+
}
412+
413+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
414+
// Use logs -f to follow the logs
415+
// The container will exit after 5 seconds, so we don't need an explicit timeout
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)