Added tests for log file modes and compressing unknown log error conditions.

This commit is contained in:
Simon Fogliato
2025-02-05 13:00:29 -08:00
parent 8431aa7539
commit 5942cd8138
2 changed files with 13 additions and 5 deletions

View File

@@ -413,15 +413,15 @@ func (c *containerLogManager) removeExcessLogs(logs []string) ([]string, error)
// compressLog compresses a log to log.gz with gzip.
func (c *containerLogManager) compressLog(log string) error {
logInfo, err := os.Stat(log)
if err != nil {
return fmt.Errorf("failed to stat log file: %w", err)
}
r, err := c.osInterface.Open(log)
if err != nil {
return fmt.Errorf("failed to open log %q: %w", log, err)
}
defer r.Close()
logInfo, err := os.Stat(log)
if err != nil {
return fmt.Errorf("failed to get log info %q: %w", log, err)
}
tmpLog := log + tmpSuffix
f, err := c.osInterface.OpenFile(tmpLog, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, logInfo.Mode())
if err != nil {

View File

@@ -368,10 +368,18 @@ func TestCompressLog(t *testing.T) {
testFile.Close()
testLog := testFile.Name()
testLogInfo, err := os.Stat(testLog)
assert.NoError(t, err)
c := &containerLogManager{osInterface: container.RealOS{}}
require.NoError(t, c.compressLog(testLog))
_, err = os.Stat(testLog + compressSuffix)
testLogCompressInfo, err := os.Stat(testLog + compressSuffix)
assert.NoError(t, err, "log should be compressed")
if testLogInfo.Mode() != testLogCompressInfo.Mode() {
t.Errorf("compressed and uncompressed test log file modes do not match")
}
if err := c.compressLog("test-unknown-log"); err == nil {
t.Errorf("compressing unknown log should return error")
}
_, err = os.Stat(testLog + tmpSuffix)
assert.Error(t, err, "temporary log should be renamed")
_, err = os.Stat(testLog)