Skip to content

Commit

Permalink
explicitly cleanup files to avoid windows issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri870 committed Feb 7, 2025
1 parent 09016dd commit 61807da
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 64 deletions.
62 changes: 62 additions & 0 deletions logp/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"github.com/elastic/elastic-agent-libs/file"
"github.com/elastic/elastic-agent-libs/logp"
)

Expand Down Expand Up @@ -437,3 +439,63 @@ func takeAllLogsFromPath(t *testing.T, path string) []map[string]any {

return entries
}

func TestLoggerRotateSymlink(t *testing.T) {
dir := t.TempDir()

cfg := logp.DefaultConfig(logp.DefaultEnvironment)
cfg.Beat = "logger"
cfg.ToFiles = true
cfg.Files.Path = dir
cfg.Files.RotateOnStartup = false

logname := cfg.Beat

privateFileContents := []byte("original contents")
privateFile := filepath.Join(dir, "private")
err := os.WriteFile(privateFile, privateFileContents, 0777)
require.NoError(t, err)

// Plant a symlink to the private file by guessing the log filename.
guessedFilename := filepath.Join(dir, fmt.Sprintf("%s-%s.ndjson", logname, time.Now().Format(file.DateFormat)))
err = os.Symlink(privateFile, guessedFilename)
require.NoError(t, err)

err = logp.Configure(cfg)
require.NoError(t, err)

logLine := "a info message"
logp.L().Info(logLine)

// The file rotation should have detected the destination is a symlink and rotated before writing.
rotatedFilename := filepath.Join(dir, fmt.Sprintf("%s-%s-1.ndjson", logname, time.Now().Format(file.DateFormat)))
assertDirContents(t, dir, filepath.Base(privateFile), filepath.Base(guessedFilename), filepath.Base(rotatedFilename))

got, err := os.ReadFile(privateFile)
require.NoError(t, err)
assert.Equal(t, privateFileContents, got, "The symlink target should not have been modified")

got, err = os.ReadFile(rotatedFilename)
require.NoError(t, err)
assert.Contains(t, string(got), logLine, "The rotated file should contain the log message")

// Prevents a windows issue with cleaning up files still in use:
// Error: TempDir RemoveAll cleanup: remove t.TempDir() The process cannot access the file because it is being used by another process.
assert.NoError(t, logp.L().Close())
}

func assertDirContents(t *testing.T, dir string, files ...string) {
t.Helper()

f, err := os.Open(dir)
if err != nil {
t.Fatal(err)
}

names, err := f.Readdirnames(-1)
if err != nil {
t.Fatal(err)
}

assert.ElementsMatch(t, files, names)
}
64 changes: 0 additions & 64 deletions logp/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@
package logp

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/elastic/elastic-agent-libs/file"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
Expand Down Expand Up @@ -84,62 +79,3 @@ func TestNewInMemory(t *testing.T) {
assert.Contains(t, logs[3], "error_key")
assert.Contains(t, logs[3], "error_val")
}

func TestLoggerRotateSymlink(t *testing.T) {
dir := t.TempDir()

cfg := DefaultConfig(DefaultEnvironment)
cfg.Beat = "logger"
cfg.ToFiles = true
cfg.Files.Path = dir
cfg.Files.RotateOnStartup = false

logname := cfg.Beat

privateFileContents := []byte("original contents")
privateFile := filepath.Join(dir, "private")
err := os.WriteFile(privateFile, privateFileContents, 0644)
require.NoError(t, err)

// Plant a symlink to the private file by guessing the log filename.
guessedFilename := filepath.Join(dir, fmt.Sprintf("%s-%s.ndjson", logname, time.Now().Format(file.DateFormat)))
err = os.Symlink(privateFile, guessedFilename)
require.NoError(t, err)

err = Configure(cfg)
require.NoError(t, err)

logLine := "a info message"
L().Info(logLine)

// The file rotation should have detected the destination is a symlink and rotated before writing.
rotatedFilename := filepath.Join(dir, fmt.Sprintf("%s-%s-1.ndjson", logname, time.Now().Format(file.DateFormat)))
assertDirContents(t, dir, filepath.Base(privateFile), filepath.Base(guessedFilename), filepath.Base(rotatedFilename))

got, err := os.ReadFile(privateFile)
require.NoError(t, err)
assert.Equal(t, privateFileContents, got, "The symlink target should not have been modified")

got, err = os.ReadFile(rotatedFilename)
require.NoError(t, err)
assert.Contains(t, string(got), logLine, "The rotated file should contain the log message")

// Prevent windows issues with cleaning up files still in use.
assert.NoError(t, L().Close())
}

func assertDirContents(t *testing.T, dir string, files ...string) {
t.Helper()

f, err := os.Open(dir)
if err != nil {
t.Fatal(err)
}

names, err := f.Readdirnames(-1)
if err != nil {
t.Fatal(err)
}

assert.ElementsMatch(t, files, names)
}

0 comments on commit 61807da

Please sign in to comment.