2021-09-16 07:16:07 +00:00
|
|
|
//go:build !windows
|
2017-08-11 10:04:58 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-11-14 15:40:05 +00:00
|
|
|
"strings"
|
2017-08-11 10:04:58 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/integration/try"
|
2017-08-11 10:04:58 +00:00
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
const traefikTestAccessLogFileRotated = traefikTestAccessLogFile + ".rotated"
|
2021-11-25 10:10:06 +00:00
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Log rotation integration test suite.
|
2017-08-11 10:04:58 +00:00
|
|
|
type LogRotationSuite struct{ BaseSuite }
|
|
|
|
|
2018-01-11 09:04:03 +00:00
|
|
|
func (s *LogRotationSuite) SetUpSuite(c *check.C) {
|
|
|
|
s.createComposeProject(c, "access_log")
|
2021-11-25 10:10:06 +00:00
|
|
|
s.composeUp(c)
|
|
|
|
}
|
2018-01-11 09:04:03 +00:00
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
func (s *LogRotationSuite) TearDownSuite(c *check.C) {
|
|
|
|
s.composeDown(c)
|
|
|
|
|
|
|
|
generatedFiles := []string{
|
|
|
|
traefikTestLogFile,
|
|
|
|
traefikTestAccessLogFile,
|
|
|
|
traefikTestAccessLogFileRotated,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filename := range generatedFiles {
|
|
|
|
if err := os.Remove(filename); err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Warn().Err(err).Send()
|
2021-11-25 10:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-11 09:04:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 10:04:58 +00:00
|
|
|
func (s *LogRotationSuite) TestAccessLogRotation(c *check.C) {
|
|
|
|
// Start Traefik
|
2017-09-13 08:34:04 +00:00
|
|
|
cmd, display := s.traefikCmd(withConfigFile("fixtures/access_log_config.toml"))
|
|
|
|
defer display(c)
|
2018-02-05 08:48:03 +00:00
|
|
|
defer displayTraefikLogFile(c, traefikTestLogFile)
|
|
|
|
|
2017-08-11 10:04:58 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
2020-10-09 07:32:03 +00:00
|
|
|
defer s.killCmd(cmd)
|
2018-02-05 08:48:03 +00:00
|
|
|
|
2017-08-11 10:04:58 +00:00
|
|
|
// Verify Traefik started ok
|
|
|
|
verifyEmptyErrorLog(c, "traefik.log")
|
|
|
|
|
2018-01-11 09:04:03 +00:00
|
|
|
waitForTraefik(c, "server1")
|
2017-08-11 10:04:58 +00:00
|
|
|
|
|
|
|
// Make some requests
|
2018-01-11 09:04:03 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "frontend1.docker.local"
|
|
|
|
|
|
|
|
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK), try.HasBody())
|
2017-08-11 10:04:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Rename access log
|
2021-11-25 10:10:06 +00:00
|
|
|
err = os.Rename(traefikTestAccessLogFile, traefikTestAccessLogFileRotated)
|
2017-08-11 10:04:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// in the midst of the requests, issue SIGUSR1 signal to server process
|
|
|
|
err = cmd.Process.Signal(syscall.SIGUSR1)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// continue issuing requests
|
2018-01-11 09:04:03 +00:00
|
|
|
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK), try.HasBody())
|
2017-08-11 10:04:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2018-01-11 09:04:03 +00:00
|
|
|
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK), try.HasBody())
|
2017-08-11 10:04:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Verify access.log.rotated output as expected
|
2021-11-25 10:10:06 +00:00
|
|
|
logAccessLogFile(c, traefikTestAccessLogFileRotated)
|
|
|
|
lineCount := verifyLogLines(c, traefikTestAccessLogFileRotated, 0, true)
|
2017-08-11 10:04:58 +00:00
|
|
|
c.Assert(lineCount, checker.GreaterOrEqualThan, 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
|
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("access log file was not created in time"))
|
|
|
|
|
2017-08-11 10:04:58 +00:00
|
|
|
// Verify access.log output as expected
|
2017-10-06 07:20:13 +00:00
|
|
|
logAccessLogFile(c, traefikTestAccessLogFile)
|
2017-08-11 10:04:58 +00:00
|
|
|
lineCount = verifyLogLines(c, traefikTestAccessLogFile, lineCount, true)
|
|
|
|
c.Assert(lineCount, checker.Equals, 3)
|
|
|
|
|
|
|
|
verifyEmptyErrorLog(c, traefikTestLogFile)
|
|
|
|
}
|
|
|
|
|
2017-10-06 07:20:13 +00:00
|
|
|
func logAccessLogFile(c *check.C, fileName string) {
|
2021-03-04 19:08:03 +00:00
|
|
|
output, err := os.ReadFile(fileName)
|
2017-10-06 07:20:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Logf("Contents of file %s\n%s", fileName, string(output))
|
|
|
|
}
|
|
|
|
|
2017-08-11 10:04:58 +00:00
|
|
|
func verifyEmptyErrorLog(c *check.C, name string) {
|
|
|
|
err := try.Do(5*time.Second, func() error {
|
2021-03-04 19:08:03 +00:00
|
|
|
traefikLog, e2 := os.ReadFile(name)
|
2017-08-11 10:04:58 +00:00
|
|
|
if e2 != nil {
|
|
|
|
return e2
|
|
|
|
}
|
2019-01-18 14:18:04 +00:00
|
|
|
c.Assert(string(traefikLog), checker.HasLen, 0)
|
2017-08-11 10:04:58 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyLogLines(c *check.C, fileName string, countInit int, accessLog bool) int {
|
|
|
|
rotated, err := os.Open(fileName)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
rotatedLog := bufio.NewScanner(rotated)
|
|
|
|
count := countInit
|
|
|
|
for rotatedLog.Scan() {
|
|
|
|
line := rotatedLog.Text()
|
|
|
|
if accessLog {
|
|
|
|
if len(line) > 0 {
|
2019-11-14 15:40:05 +00:00
|
|
|
if !strings.Contains(line, "/api/rawdata") {
|
|
|
|
CheckAccessLogFormat(c, line, count)
|
|
|
|
count++
|
|
|
|
}
|
2017-08-11 10:04:58 +00:00
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
} else {
|
|
|
|
count++
|
2017-08-11 10:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|