2015-09-27 13:59:51 +00:00
|
|
|
// This is the main file that sets up integration tests using go-check.
|
2017-07-06 14:28:13 +00:00
|
|
|
package integration
|
2015-09-27 13:59:51 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-17 13:22:44 +00:00
|
|
|
"bytes"
|
2017-10-13 09:08:03 +00:00
|
|
|
"flag"
|
2015-09-27 13:59:51 +00:00
|
|
|
"fmt"
|
2015-09-28 20:37:19 +00:00
|
|
|
"io/ioutil"
|
2016-12-12 17:30:31 +00:00
|
|
|
"net"
|
2015-09-28 20:37:19 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2019-06-17 09:48:05 +00:00
|
|
|
"strings"
|
2015-09-27 13:59:51 +00:00
|
|
|
"testing"
|
2015-09-28 20:37:19 +00:00
|
|
|
"text/template"
|
2015-09-27 13:59:51 +00:00
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/log"
|
2019-07-15 08:22:03 +00:00
|
|
|
"github.com/fatih/structs"
|
2016-04-02 10:40:21 +00:00
|
|
|
"github.com/go-check/check"
|
2016-07-21 14:19:51 +00:00
|
|
|
compose "github.com/libkermit/compose/check"
|
2015-09-27 13:59:51 +00:00
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
2017-10-13 09:08:03 +00:00
|
|
|
var integration = flag.Bool("integration", false, "run integration tests")
|
2017-10-30 09:02:03 +00:00
|
|
|
var container = flag.Bool("container", false, "run container integration tests")
|
|
|
|
var host = flag.Bool("host", false, "run host integration tests")
|
2018-06-27 13:08:05 +00:00
|
|
|
var showLog = flag.Bool("tlog", false, "always show Traefik logs")
|
2017-10-13 09:08:03 +00:00
|
|
|
|
2015-09-27 13:59:51 +00:00
|
|
|
func Test(t *testing.T) {
|
2017-10-13 09:08:03 +00:00
|
|
|
if !*integration {
|
2019-09-13 17:28:04 +00:00
|
|
|
log.WithoutContext().Info("Integration tests disabled.")
|
2017-10-13 09:08:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-30 09:02:03 +00:00
|
|
|
if *container {
|
|
|
|
// tests launched from a container
|
2019-01-18 14:18:04 +00:00
|
|
|
check.Suite(&AccessLogSuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
check.Suite(&AcmeSuite{})
|
2019-11-28 20:56:04 +00:00
|
|
|
check.Suite(&EtcdSuite{})
|
|
|
|
check.Suite(&ConsulSuite{})
|
2019-10-15 15:34:08 +00:00
|
|
|
check.Suite(&ConsulCatalogSuite{})
|
2019-01-18 14:18:04 +00:00
|
|
|
check.Suite(&DockerComposeSuite{})
|
|
|
|
check.Suite(&DockerSuite{})
|
2017-11-17 16:22:03 +00:00
|
|
|
check.Suite(&ErrorPagesSuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
check.Suite(&FileSuite{})
|
|
|
|
check.Suite(&GRPCSuite{})
|
|
|
|
check.Suite(&HealthCheckSuite{})
|
2019-04-02 08:40:04 +00:00
|
|
|
check.Suite(&HeadersSuite{})
|
2019-01-18 14:18:04 +00:00
|
|
|
check.Suite(&HostResolverSuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
check.Suite(&HTTPSSuite{})
|
2019-06-27 22:36:04 +00:00
|
|
|
check.Suite(&KeepAliveSuite{})
|
2019-01-18 14:18:04 +00:00
|
|
|
check.Suite(&LogRotationSuite{})
|
2019-03-18 10:30:07 +00:00
|
|
|
check.Suite(&MarathonSuite{})
|
|
|
|
check.Suite(&MarathonSuite15{})
|
2019-08-26 10:20:06 +00:00
|
|
|
check.Suite(&RateLimitSuite{})
|
2019-11-28 20:56:04 +00:00
|
|
|
check.Suite(&RedisSuite{})
|
2019-01-18 14:18:04 +00:00
|
|
|
check.Suite(&RestSuite{})
|
2018-01-26 17:22:03 +00:00
|
|
|
check.Suite(&RetrySuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
check.Suite(&SimpleSuite{})
|
|
|
|
check.Suite(&TimeoutSuite{})
|
2019-01-18 14:18:04 +00:00
|
|
|
check.Suite(&TLSClientHeadersSuite{})
|
2018-01-25 11:00:05 +00:00
|
|
|
check.Suite(&TracingSuite{})
|
2020-02-11 00:26:04 +00:00
|
|
|
check.Suite(&UDPSuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
check.Suite(&WebsocketSuite{})
|
2019-11-28 20:56:04 +00:00
|
|
|
check.Suite(&ZookeeperSuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
}
|
|
|
|
if *host {
|
|
|
|
// tests launched from the host
|
2019-03-14 14:56:06 +00:00
|
|
|
check.Suite(&K8sSuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
check.Suite(&ProxyProtocolSuite{})
|
2019-03-14 08:30:04 +00:00
|
|
|
check.Suite(&TCPSuite{})
|
2017-10-30 09:02:03 +00:00
|
|
|
}
|
2019-08-26 13:06:05 +00:00
|
|
|
|
|
|
|
check.TestingT(t)
|
2015-09-27 13:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var traefikBinary = "../dist/traefik"
|
|
|
|
|
|
|
|
type BaseSuite struct {
|
2016-03-27 17:58:08 +00:00
|
|
|
composeProject *compose.Project
|
2015-09-27 13:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseSuite) TearDownSuite(c *check.C) {
|
|
|
|
// shutdown and delete compose project
|
|
|
|
if s.composeProject != nil {
|
2016-04-02 10:40:21 +00:00
|
|
|
s.composeProject.Stop(c)
|
2015-09-27 13:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseSuite) createComposeProject(c *check.C, name string) {
|
2016-03-27 17:58:08 +00:00
|
|
|
projectName := fmt.Sprintf("integration-test-%s", name)
|
|
|
|
composeFile := fmt.Sprintf("resources/compose/%s.yml", name)
|
2016-12-12 17:30:31 +00:00
|
|
|
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ip, _, err := net.ParseCIDR(addr.String())
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
if !ip.IsLoopback() && ip.To4() != nil {
|
2019-09-13 17:28:04 +00:00
|
|
|
_ = os.Setenv("DOCKER_HOST_IP", ip.String())
|
2016-12-12 17:30:31 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 10:40:21 +00:00
|
|
|
s.composeProject = compose.CreateProject(c, projectName, composeFile)
|
2015-09-27 13:59:51 +00:00
|
|
|
}
|
2015-09-28 20:37:19 +00:00
|
|
|
|
2017-07-10 12:58:31 +00:00
|
|
|
func withConfigFile(file string) string {
|
|
|
|
return "--configFile=" + file
|
2017-05-17 13:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseSuite) cmdTraefik(args ...string) (*exec.Cmd, *bytes.Buffer) {
|
|
|
|
cmd := exec.Command(traefikBinary, args...)
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stderr = &out
|
|
|
|
return cmd, &out
|
|
|
|
}
|
|
|
|
|
2017-09-13 08:34:04 +00:00
|
|
|
func (s *BaseSuite) traefikCmd(args ...string) (*exec.Cmd, func(*check.C)) {
|
|
|
|
cmd, out := s.cmdTraefik(args...)
|
|
|
|
return cmd, func(c *check.C) {
|
2018-06-27 13:08:05 +00:00
|
|
|
if c.Failed() || *showLog {
|
2019-08-11 10:22:14 +00:00
|
|
|
s.displayLogK3S(c)
|
2017-09-13 08:34:04 +00:00
|
|
|
s.displayTraefikLog(c, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 10:22:14 +00:00
|
|
|
func (s *BaseSuite) displayLogK3S(c *check.C) {
|
|
|
|
filePath := "./fixtures/k8s/config.skip/k3s.log"
|
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
|
|
content, errR := ioutil.ReadFile(filePath)
|
|
|
|
if errR != nil {
|
|
|
|
log.WithoutContext().Error(errR)
|
|
|
|
}
|
|
|
|
log.WithoutContext().Println(string(content))
|
|
|
|
}
|
|
|
|
log.WithoutContext().Println()
|
|
|
|
log.WithoutContext().Println("################################")
|
|
|
|
log.WithoutContext().Println()
|
|
|
|
}
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
func (s *BaseSuite) displayTraefikLog(c *check.C, output *bytes.Buffer) {
|
|
|
|
if output == nil || output.Len() == 0 {
|
2019-08-11 10:22:14 +00:00
|
|
|
log.WithoutContext().Infof("%s: No Traefik logs.", c.TestName())
|
2017-05-17 13:22:44 +00:00
|
|
|
} else {
|
2019-08-11 10:22:14 +00:00
|
|
|
log.WithoutContext().Infof("%s: Traefik logs: ", c.TestName())
|
|
|
|
log.WithoutContext().Infof(output.String())
|
2017-05-17 13:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 18:06:02 +00:00
|
|
|
func (s *BaseSuite) getDockerHost() string {
|
2015-09-28 20:37:19 +00:00
|
|
|
dockerHost := os.Getenv("DOCKER_HOST")
|
|
|
|
if dockerHost == "" {
|
|
|
|
// Default docker socket
|
|
|
|
dockerHost = "unix:///var/run/docker.sock"
|
|
|
|
}
|
2019-01-21 18:06:02 +00:00
|
|
|
return dockerHost
|
2016-04-27 23:43:43 +00:00
|
|
|
}
|
2015-09-28 20:37:19 +00:00
|
|
|
|
2016-04-27 23:43:43 +00:00
|
|
|
func (s *BaseSuite) adaptFile(c *check.C, path string, tempObjects interface{}) string {
|
2015-09-28 20:37:19 +00:00
|
|
|
// Load file
|
|
|
|
tmpl, err := template.ParseFiles(path)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
folder, prefix := filepath.Split(path)
|
2019-06-17 09:48:05 +00:00
|
|
|
tmpFile, err := ioutil.TempFile(folder, strings.TrimSuffix(prefix, filepath.Ext(prefix))+"_*"+filepath.Ext(prefix))
|
2015-09-28 20:37:19 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer tmpFile.Close()
|
|
|
|
|
2019-07-15 08:22:03 +00:00
|
|
|
model := structs.Map(tempObjects)
|
|
|
|
model["SelfFilename"] = tmpFile.Name()
|
|
|
|
|
|
|
|
err = tmpl.ExecuteTemplate(tmpFile, prefix, model)
|
2015-09-28 20:37:19 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
err = tmpFile.Sync()
|
|
|
|
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
return tmpFile.Name()
|
|
|
|
}
|