2016-11-16 22:21:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-20 17:25:13 +00:00
|
|
|
"bytes"
|
2016-11-16 22:21:47 +00:00
|
|
|
"net/http"
|
2016-11-20 17:25:13 +00:00
|
|
|
"os"
|
2016-11-16 22:21:47 +00:00
|
|
|
"os/exec"
|
2016-11-20 17:25:13 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
2016-11-16 22:21:47 +00:00
|
|
|
"time"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
"github.com/containous/traefik/integration/try"
|
2016-11-16 22:21:47 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
|
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Eureka test suites (using libcompose)
|
2017-05-17 13:22:44 +00:00
|
|
|
type EurekaSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
eurekaIP string
|
|
|
|
eurekaURL string
|
|
|
|
}
|
2016-11-16 22:21:47 +00:00
|
|
|
|
|
|
|
func (s *EurekaSuite) SetUpSuite(c *check.C) {
|
|
|
|
s.createComposeProject(c, "eureka")
|
|
|
|
s.composeProject.Start(c)
|
2016-11-20 17:25:13 +00:00
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
eureka := s.composeProject.Container(c, "eureka")
|
|
|
|
s.eurekaIP = eureka.NetworkSettings.IPAddress
|
|
|
|
s.eurekaURL = "http://" + s.eurekaIP + ":8761/eureka/apps"
|
|
|
|
|
|
|
|
// wait for eureka
|
|
|
|
err := try.GetRequest(s.eurekaURL, 60*time.Second)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-11-16 22:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EurekaSuite) TestSimpleConfiguration(c *check.C) {
|
2016-11-20 17:25:13 +00:00
|
|
|
|
|
|
|
whoami1Host := s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
file := s.adaptFile(c, "fixtures/eureka/simple.toml", struct{ EurekaHost string }{s.eurekaIP})
|
2016-11-20 17:25:13 +00:00
|
|
|
defer os.Remove(file)
|
|
|
|
cmd := exec.Command(traefikBinary, "--configFile="+file)
|
2016-11-16 22:21:47 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
2016-11-20 17:25:13 +00:00
|
|
|
eurekaTemplate := `
|
|
|
|
{
|
|
|
|
"instance": {
|
|
|
|
"hostName": "{{ .IP }}",
|
|
|
|
"app": "{{ .ID }}",
|
|
|
|
"ipAddr": "{{ .IP }}",
|
|
|
|
"status": "UP",
|
|
|
|
"port": {
|
|
|
|
"$": {{ .Port }},
|
|
|
|
"@enabled": "true"
|
|
|
|
},
|
|
|
|
"dataCenterInfo": {
|
|
|
|
"name": "MyOwn"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
tmpl, err := template.New("eurekaTemplate").Parse(eurekaTemplate)
|
2016-11-20 17:25:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
templateVars := map[string]string{
|
|
|
|
"ID": "tests-integration-traefik",
|
|
|
|
"IP": whoami1Host,
|
|
|
|
"Port": "80",
|
|
|
|
}
|
|
|
|
// add in eureka
|
|
|
|
err = tmpl.Execute(buf, templateVars)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, s.eurekaURL+"/tests-integration-traefik", strings.NewReader(buf.String()))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusNoContent))
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-11-20 17:25:13 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/providers", 60*time.Second, try.BodyContains("Host:tests-integration-traefik"))
|
2016-11-20 17:25:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2016-11-20 17:25:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
req.Host = "tests-integration-traefik"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
2016-11-20 17:25:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2016-11-16 22:21:47 +00:00
|
|
|
// TODO validate : run on 80
|
|
|
|
// Expected a 404 as we did not configure anything
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/", 500*time.Millisecond, try.StatusCodeIs(http.StatusNotFound))
|
2016-11-16 22:21:47 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|