2017-07-06 14:28:13 +00:00
|
|
|
package integration
|
2016-04-20 18:54:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
"github.com/containous/traefik/integration/try"
|
2016-04-20 18:54:57 +00:00
|
|
|
"github.com/go-check/check"
|
2017-07-10 12:58:31 +00:00
|
|
|
"github.com/mattn/go-shellwords"
|
2016-04-20 18:54:57 +00:00
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
2017-07-10 14:58:12 +00:00
|
|
|
const (
|
|
|
|
traefikTestLogFile = "traefik.log"
|
|
|
|
traefikTestAccessLogFile = "access.log"
|
|
|
|
)
|
|
|
|
|
2016-04-20 18:54:57 +00:00
|
|
|
// AccessLogSuite
|
|
|
|
type AccessLogSuite struct{ BaseSuite }
|
|
|
|
|
|
|
|
func (s *AccessLogSuite) TestAccessLog(c *check.C) {
|
|
|
|
// Ensure working directory is clean
|
2017-07-10 14:58:12 +00:00
|
|
|
os.Remove(traefikTestAccessLogFile)
|
|
|
|
os.Remove(traefikTestLogFile)
|
2016-04-20 18:54:57 +00:00
|
|
|
|
|
|
|
// Start Traefik
|
2017-07-10 12:58:31 +00:00
|
|
|
cmd, _ := s.cmdTraefik(withConfigFile("fixtures/access_log_config.toml"))
|
2016-04-20 18:54:57 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
2017-05-17 13:22:44 +00:00
|
|
|
|
2017-07-10 14:58:12 +00:00
|
|
|
defer os.Remove(traefikTestAccessLogFile)
|
|
|
|
defer os.Remove(traefikTestLogFile)
|
2016-04-20 18:54:57 +00:00
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Do(1*time.Second, func() error {
|
2017-07-10 14:58:12 +00:00
|
|
|
if _, err := os.Stat(traefikTestLogFile); err != nil {
|
2017-05-17 13:22:44 +00:00
|
|
|
return fmt.Errorf("could not get stats for log file: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-04-20 18:54:57 +00:00
|
|
|
|
|
|
|
// Verify Traefik started OK
|
2017-07-10 14:58:12 +00:00
|
|
|
traefikLog, err := ioutil.ReadFile(traefikTestLogFile)
|
2016-04-28 10:53:02 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
if len(traefikLog) > 0 {
|
2016-04-20 18:54:57 +00:00
|
|
|
fmt.Printf("%s\n", string(traefikLog))
|
2017-07-10 12:58:31 +00:00
|
|
|
c.Assert(traefikLog, checker.HasLen, 0)
|
2016-04-20 18:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start test servers
|
|
|
|
ts1 := startAccessLogServer(8081)
|
|
|
|
defer ts1.Close()
|
|
|
|
ts2 := startAccessLogServer(8082)
|
|
|
|
defer ts2.Close()
|
|
|
|
ts3 := startAccessLogServer(8083)
|
|
|
|
defer ts3.Close()
|
|
|
|
|
|
|
|
// Make some requests
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/test1", 500*time.Millisecond)
|
2016-04-20 18:54:57 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/test2", 500*time.Millisecond)
|
2016-04-20 18:54:57 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/test2", 500*time.Millisecond)
|
2016-04-20 18:54:57 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Verify access.log output as expected
|
2017-07-10 14:58:12 +00:00
|
|
|
accessLog, err := ioutil.ReadFile(traefikTestAccessLogFile)
|
2016-04-28 10:53:02 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
lines := strings.Split(string(accessLog), "\n")
|
|
|
|
count := 0
|
|
|
|
for i, line := range lines {
|
|
|
|
if len(line) > 0 {
|
|
|
|
count++
|
|
|
|
tokens, err := shellwords.Parse(line)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-07-10 12:58:31 +00:00
|
|
|
c.Assert(tokens, checker.HasLen, 14)
|
|
|
|
c.Assert(tokens[6], checker.Matches, `^\d{3}$`)
|
2017-05-22 19:39:29 +00:00
|
|
|
c.Assert(tokens[10], checker.Equals, fmt.Sprintf("%d", i+1))
|
2017-07-10 12:58:31 +00:00
|
|
|
c.Assert(tokens[11], checker.HasPrefix, "frontend")
|
|
|
|
c.Assert(tokens[12], checker.HasPrefix, "http://127.0.0.1:808")
|
|
|
|
c.Assert(tokens[13], checker.Matches, `^\d+ms$`)
|
2016-04-20 18:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 13:22:44 +00:00
|
|
|
c.Assert(count, checker.GreaterOrEqualThan, 3)
|
2016-04-20 18:54:57 +00:00
|
|
|
|
2016-04-27 16:25:13 +00:00
|
|
|
// Verify no other Traefik problems
|
2017-07-10 14:58:12 +00:00
|
|
|
traefikLog, err = ioutil.ReadFile(traefikTestLogFile)
|
2016-04-28 10:53:02 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
if len(traefikLog) > 0 {
|
2016-04-20 18:54:57 +00:00
|
|
|
fmt.Printf("%s\n", string(traefikLog))
|
2017-07-10 12:58:31 +00:00
|
|
|
c.Assert(traefikLog, checker.HasLen, 0)
|
2016-04-20 18:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startAccessLogServer(port int) (ts *httptest.Server) {
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "Received query %s!\n", r.URL.Path[1:])
|
|
|
|
})
|
|
|
|
if listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
ts = &httptest.Server{
|
|
|
|
Listener: listener,
|
|
|
|
Config: &http.Server{Handler: handler},
|
|
|
|
}
|
|
|
|
ts.Start()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|