traefik/integration/log_rotation_test.go

190 lines
5 KiB
Go
Raw Normal View History

2021-09-16 07:16:07 +00:00
//go:build !windows
// +build !windows
package integration
import (
"bufio"
"net/http"
"os"
"strings"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/traefik/traefik/v2/integration/try"
"github.com/traefik/traefik/v2/pkg/log"
)
const (
traefikTestLogFileRotated = traefikTestLogFile + ".rotated"
traefikTestAccessLogFileRotated = traefikTestAccessLogFile + ".rotated"
)
2020-05-11 10:06:07 +00:00
// Log rotation integration test suite.
type LogRotationSuite struct{ BaseSuite }
2024-09-13 09:40:04 +00:00
func TestLogRotationSuite(t *testing.T) {
suite.Run(t, new(LogRotationSuite))
}
2018-01-11 09:04:03 +00:00
func (s *LogRotationSuite) SetupSuite() {
s.BaseSuite.SetupSuite()
os.Remove(traefikTestAccessLogFile)
os.Remove(traefikTestLogFile)
os.Remove(traefikTestAccessLogFileRotated)
s.createComposeProject("access_log")
s.composeUp()
}
func (s *LogRotationSuite) TearDownSuite() {
s.BaseSuite.TearDownSuite()
generatedFiles := []string{
traefikTestLogFile,
traefikTestLogFileRotated,
traefikTestAccessLogFile,
traefikTestAccessLogFileRotated,
}
for _, filename := range generatedFiles {
if err := os.Remove(filename); err != nil {
log.WithoutContext().Warning(err)
}
}
2018-01-11 09:04:03 +00:00
}
func (s *LogRotationSuite) TestAccessLogRotation() {
// Start Traefik
cmd, _ := s.cmdTraefik(withConfigFile("fixtures/access_log_config.toml"))
defer s.displayTraefikLogFile(traefikTestLogFile)
2018-02-05 08:48:03 +00:00
// Verify Traefik started ok
s.verifyEmptyErrorLog("traefik.log")
s.waitForTraefik("server1")
// Make some requests
2018-01-11 09:04:03 +00:00
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
require.NoError(s.T(), err)
2018-01-11 09:04:03 +00:00
req.Host = "frontend1.docker.local"
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK), try.HasBody())
require.NoError(s.T(), err)
// Rename access log
err = os.Rename(traefikTestAccessLogFile, traefikTestAccessLogFileRotated)
require.NoError(s.T(), err)
// in the midst of the requests, issue SIGUSR1 signal to server process
err = cmd.Process.Signal(syscall.SIGUSR1)
require.NoError(s.T(), err)
// continue issuing requests
2018-01-11 09:04:03 +00:00
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK), try.HasBody())
require.NoError(s.T(), err)
2018-01-11 09:04:03 +00:00
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK), try.HasBody())
require.NoError(s.T(), err)
// Verify access.log.rotated output as expected
s.logAccessLogFile(traefikTestAccessLogFileRotated)
lineCount := s.verifyLogLines(traefikTestAccessLogFileRotated, 0, true)
assert.GreaterOrEqual(s.T(), lineCount, 1)
2017-10-06 07:20:13 +00:00
// make sure that the access log file is at least created before we do assertions on it
err = try.Do(1*time.Second, func() error {
_, err := os.Stat(traefikTestAccessLogFile)
return err
})
assert.NoError(s.T(), err, "access log file was not created in time")
2017-10-06 07:20:13 +00:00
// Verify access.log output as expected
s.logAccessLogFile(traefikTestAccessLogFile)
lineCount = s.verifyLogLines(traefikTestAccessLogFile, lineCount, true)
assert.Equal(s.T(), 3, lineCount)
s.verifyEmptyErrorLog(traefikTestLogFile)
}
func (s *LogRotationSuite) TestTraefikLogRotation() {
// Start Traefik
cmd := s.traefikCmd(withConfigFile("fixtures/traefik_log_config.toml"))
2018-02-05 08:48:03 +00:00
s.waitForTraefik("server1")
// Rename traefik log
err := os.Rename(traefikTestLogFile, traefikTestLogFileRotated)
require.NoError(s.T(), err)
// issue SIGUSR1 signal to server process
err = cmd.Process.Signal(syscall.SIGUSR1)
require.NoError(s.T(), err)
err = cmd.Process.Signal(syscall.SIGTERM)
require.NoError(s.T(), err)
// Allow time for switch to be processed
err = try.Do(3*time.Second, func() error {
_, err = os.Stat(traefikTestLogFile)
return err
})
require.NoError(s.T(), err)
// we have at least 6 lines in traefik.log.rotated
lineCount := s.verifyLogLines(traefikTestLogFileRotated, 0, false)
// GreaterOrEqualThan used to ensure test doesn't break
// If more log entries are output on startup
assert.GreaterOrEqual(s.T(), lineCount, 5)
2018-07-03 08:02:03 +00:00
// Verify traefik.log output as expected
lineCount = s.verifyLogLines(traefikTestLogFile, lineCount, false)
assert.GreaterOrEqual(s.T(), lineCount, 7)
}
func (s *LogRotationSuite) logAccessLogFile(fileName string) {
2021-03-04 19:08:03 +00:00
output, err := os.ReadFile(fileName)
require.NoError(s.T(), err)
log.WithoutContext().Infof("Contents of file %s\n%s", fileName, string(output))
2017-10-06 07:20:13 +00:00
}
func (s *LogRotationSuite) verifyEmptyErrorLog(name string) {
err := try.Do(5*time.Second, func() error {
2021-03-04 19:08:03 +00:00
traefikLog, e2 := os.ReadFile(name)
if e2 != nil {
return e2
}
assert.Empty(s.T(), string(traefikLog))
return nil
})
require.NoError(s.T(), err)
}
func (s *LogRotationSuite) verifyLogLines(fileName string, countInit int, accessLog bool) int {
rotated, err := os.Open(fileName)
require.NoError(s.T(), err)
rotatedLog := bufio.NewScanner(rotated)
count := countInit
for rotatedLog.Scan() {
line := rotatedLog.Text()
if accessLog {
if len(line) > 0 {
if !strings.Contains(line, "/api/rawdata") {
s.CheckAccessLogFormat(line, count)
count++
}
}
} else {
count++
}
}
return count
}