2017-07-06 14:28:13 +00:00
|
|
|
package integration
|
2017-02-06 22:00:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
"github.com/containous/traefik/integration/try"
|
2017-02-06 22:00:54 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
2017-04-25 23:08:15 +00:00
|
|
|
// HealthCheck test suites (using libcompose)
|
2017-08-10 16:00:31 +00:00
|
|
|
type HealthCheckSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
whoami1IP string
|
|
|
|
whoami2IP string
|
|
|
|
}
|
2017-02-06 22:00:54 +00:00
|
|
|
|
2017-04-25 23:08:15 +00:00
|
|
|
func (s *HealthCheckSuite) SetUpSuite(c *check.C) {
|
2017-02-06 22:00:54 +00:00
|
|
|
s.createComposeProject(c, "healthcheck")
|
|
|
|
s.composeProject.Start(c)
|
2017-08-10 16:00:31 +00:00
|
|
|
|
|
|
|
s.whoami1IP = s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
|
|
|
|
s.whoami2IP = s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress
|
2017-02-06 22:00:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 23:08:15 +00:00
|
|
|
func (s *HealthCheckSuite) TestSimpleConfiguration(c *check.C) {
|
2017-02-06 22:00:54 +00:00
|
|
|
|
|
|
|
file := s.adaptFile(c, "fixtures/healthcheck/simple.toml", struct {
|
|
|
|
Server1 string
|
|
|
|
Server2 string
|
2017-08-10 16:00:31 +00:00
|
|
|
}{s.whoami1IP, s.whoami2IP})
|
2017-02-06 22:00:54 +00:00
|
|
|
defer os.Remove(file)
|
|
|
|
|
2017-07-10 12:58:31 +00:00
|
|
|
cmd, _ := s.cmdTraefik(withConfigFile(file))
|
2017-02-06 22:00:54 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// 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:test.localhost"))
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
frontendHealthReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/health", nil)
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
frontendHealthReq.Host = "test.localhost"
|
2017-02-06 22:00:54 +00:00
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
err = try.Request(frontendHealthReq, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
// Fix all whoami health to 500
|
|
|
|
client := &http.Client{}
|
2017-08-10 16:00:31 +00:00
|
|
|
whoamiHosts := []string{s.whoami1IP, s.whoami2IP}
|
2017-05-17 13:22:44 +00:00
|
|
|
for _, whoami := range whoamiHosts {
|
|
|
|
statusInternalServerErrorReq, err := http.NewRequest(http.MethodPost, "http://"+whoami+"/health", bytes.NewBuffer([]byte("500")))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
_, err = client.Do(statusInternalServerErrorReq)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Waiting for Traefik healthcheck
|
|
|
|
try.Sleep(2 * time.Second)
|
|
|
|
|
2017-07-10 10:11:44 +00:00
|
|
|
// Verify no backend service is available due to failing health checks
|
|
|
|
err = try.Request(frontendHealthReq, 3*time.Second, try.StatusCodeIs(http.StatusServiceUnavailable))
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
// Change one whoami health to 200
|
2017-08-10 16:00:31 +00:00
|
|
|
statusOKReq1, err := http.NewRequest(http.MethodPost, "http://"+s.whoami1IP+"/health", bytes.NewBuffer([]byte("200")))
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
_, err = client.Do(statusOKReq1)
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
// Verify frontend health : after
|
|
|
|
err = try.Request(frontendHealthReq, 3*time.Second, try.StatusCodeIs(http.StatusOK))
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-02-06 22:00:54 +00:00
|
|
|
|
2017-05-17 13:22:44 +00:00
|
|
|
frontendReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
frontendReq.Host = "test.localhost"
|
2017-02-06 22:00:54 +00:00
|
|
|
|
2017-07-10 10:11:44 +00:00
|
|
|
// Check if whoami1 responds
|
2017-08-10 16:00:31 +00:00
|
|
|
err = try.Request(frontendReq, 500*time.Millisecond, try.BodyContains(s.whoami1IP))
|
2017-02-06 22:00:54 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
|
|
|
|
// Check if the service with bad health check (whoami2) never respond.
|
2017-08-10 16:00:31 +00:00
|
|
|
err = try.Request(frontendReq, 2*time.Second, try.BodyContains(s.whoami2IP))
|
2017-05-17 13:22:44 +00:00
|
|
|
c.Assert(err, checker.Not(checker.IsNil))
|
2017-02-06 22:00:54 +00:00
|
|
|
|
|
|
|
// TODO validate : run on 80
|
2017-05-17 13:22:44 +00:00
|
|
|
resp, err := http.Get("http://127.0.0.1:8000/")
|
2017-02-06 22:00:54 +00:00
|
|
|
|
|
|
|
// Expected a 404 as we did not configure anything
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-05-17 13:22:44 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
2017-02-06 22:00:54 +00:00
|
|
|
}
|
2017-08-10 16:00:31 +00:00
|
|
|
|
|
|
|
func (s *HealthCheckSuite) TestMultipleEntrypointsWrr(c *check.C) {
|
|
|
|
s.doTestMultipleEntrypoints(c, "fixtures/healthcheck/multiple-entrypoints-wrr.toml")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HealthCheckSuite) TestMultipleEntrypointsDrr(c *check.C) {
|
|
|
|
s.doTestMultipleEntrypoints(c, "fixtures/healthcheck/multiple-entrypoints-drr.toml")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HealthCheckSuite) doTestMultipleEntrypoints(c *check.C, fixture string) {
|
|
|
|
file := s.adaptFile(c, fixture, struct {
|
|
|
|
Server1 string
|
|
|
|
Server2 string
|
|
|
|
}{s.whoami1IP, s.whoami2IP})
|
|
|
|
defer os.Remove(file)
|
|
|
|
|
|
|
|
cmd, _ := s.cmdTraefik(withConfigFile(file))
|
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
// Wait for traefik
|
|
|
|
err = try.GetRequest("http://localhost:8080/api/providers", 60*time.Second, try.BodyContains("Host:test.localhost"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Check entrypoint http1
|
|
|
|
frontendHealthReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/health", nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
frontendHealthReq.Host = "test.localhost"
|
|
|
|
|
|
|
|
err = try.Request(frontendHealthReq, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Check entrypoint http2
|
|
|
|
frontendHealthReq, err = http.NewRequest(http.MethodGet, "http://127.0.0.1:9000/health", nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
frontendHealthReq.Host = "test.localhost"
|
|
|
|
|
|
|
|
err = try.Request(frontendHealthReq, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Set one whoami health to 500
|
|
|
|
client := &http.Client{}
|
|
|
|
statusInternalServerErrorReq, err := http.NewRequest(http.MethodPost, "http://"+s.whoami1IP+"/health", bytes.NewBuffer([]byte("500")))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
_, err = client.Do(statusInternalServerErrorReq)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// Waiting for Traefik healthcheck
|
|
|
|
try.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
frontend1Req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/", nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
frontend1Req.Host = "test.localhost"
|
|
|
|
|
|
|
|
frontend2Req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:9000/", nil)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
frontend2Req.Host = "test.localhost"
|
|
|
|
|
|
|
|
// Check if whoami1 never responds
|
|
|
|
err = try.Request(frontend2Req, 2*time.Second, try.BodyContains(s.whoami1IP))
|
|
|
|
c.Assert(err, checker.Not(checker.IsNil))
|
|
|
|
|
|
|
|
// Check if whoami1 never responds
|
|
|
|
err = try.Request(frontend1Req, 2*time.Second, try.BodyContains(s.whoami1IP))
|
|
|
|
c.Assert(err, checker.Not(checker.IsNil))
|
|
|
|
}
|