mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-14 21:53:52 +00:00
Added tests for log file modes and compressing unknown log error conditions.
This commit is contained in:
@@ -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 {
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user