Skip to content

Commit

Permalink
fix(windows-agent): Truncate log files (#672)
Browse files Browse the repository at this point in the history
This PR renames any existing log to `log.old` when the program starts,
overwriting the file if it already exists. It also tests that if a log
exists a log.old is successfully created.

---

UDENG-2355
  • Loading branch information
ashuntu authored Mar 7, 2024
2 parents fd319ec + 7811fb9 commit 3de7e71
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
8 changes: 8 additions & 0 deletions windows-agent/cmd/ubuntu-pro-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -66,6 +67,13 @@ func setLoggerOutput(a app) (func(), error) {

logFile := filepath.Join(publicDir, "log")

// Move old log file
oldLogFile := filepath.Join(publicDir, "log.old")
err = os.Rename(logFile, oldLogFile)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Warnf("Could not archive previous log file: %v", err)
}

f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("could not open log file: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion windows-agent/cmd/ubuntu-pro-agent/main_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestRunSignal(t *testing.T) {
// Signal handlers tests: can’t be parallel

a := myApp{
done: make(chan struct{}),
done: make(chan struct{}),
tmpDir: t.TempDir(),
}

var rc int
Expand Down
50 changes: 49 additions & 1 deletion windows-agent/cmd/ubuntu-pro-agent/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"errors"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -42,16 +44,28 @@ func (a *myApp) PublicDir() (string, error) {
func TestRun(t *testing.T) {
t.Parallel()

fooContent := "foo"
emptyContent := ""

tests := map[string]struct {
existingLogContent string

runError bool
usageErrorReturn bool
logDirError bool

wantReturnCode int
wantReturnCode int
wantOldLogFileContent *string
}{
"Run and exit successfully": {},
"Run and exit successfully despite logs not being written": {logDirError: true},

// Log file handling
"Existing log file has been renamed to old": {existingLogContent: "foo", wantOldLogFileContent: &fooContent},
"Existing empty log file has been renamed to old": {existingLogContent: "-", wantOldLogFileContent: &emptyContent},
"Ignore when failing to archive log file": {existingLogContent: "OLD_IS_DIRECTORY", wantReturnCode: 0},

// Error cases
"Run and return error": {runError: true, wantReturnCode: 1},
"Run and return usage error": {usageErrorReturn: true, runError: true, wantReturnCode: 2},
"Run and usage error only does not fail": {usageErrorReturn: true, runError: false, wantReturnCode: 0},
Expand All @@ -71,6 +85,27 @@ func TestRun(t *testing.T) {
a.tmpDir = "PUBLIC_DIR_ERROR"
}

var logFile, oldLogFile string
publicDir, err := a.PublicDir()
if err == nil {
logFile = filepath.Join(publicDir, "log")
oldLogFile = logFile + ".old"
switch tc.existingLogContent {
case "":
case "OLD_IS_DIRECTORY":
err := os.Mkdir(oldLogFile, 0700)
require.NoError(t, err, "Setup: create invalid log.old file")
err = os.WriteFile(logFile, []byte("Old log content"), 0600)
require.NoError(t, err, "Setup: creating pre-existing log file")
case "-":
tc.existingLogContent = ""
fallthrough
default:
err := os.WriteFile(logFile, []byte(tc.existingLogContent), 0600)
require.NoError(t, err, "Setup: creating pre-existing log file")
}
}

var rc int
wait := make(chan struct{})
go func() {
Expand All @@ -84,6 +119,19 @@ func TestRun(t *testing.T) {
<-wait

require.Equal(t, tc.wantReturnCode, rc, "Return expected code")

// Don't check for log files if the directory was not writable
if logFile == "" {
return
}
if tc.wantOldLogFileContent != nil {
require.FileExists(t, oldLogFile, "Old log file should exist")
content, err := os.ReadFile(oldLogFile)
require.NoError(t, err, "Should be able to read old log file")
require.Equal(t, tc.existingLogContent, string(content), "Old log file content should be log's content")
} else {
require.NoFileExists(t, oldLogFile, "Old log file should not exist")
}
})
}
}

0 comments on commit 3de7e71

Please sign in to comment.