traefik/integration/healthcheck_test.go

96 lines
3.1 KiB
Go
Raw Normal View History

package integration
2017-02-06 22:00:54 +00:00
import (
"bytes"
"net/http"
"os"
"os/exec"
"time"
"github.com/containous/traefik/integration/try"
2017-02-06 22:00:54 +00:00
"github.com/go-check/check"
checker "github.com/vdemeester/shakers"
)
// HealthCheck test suites (using libcompose)
type HealthCheckSuite struct{ BaseSuite }
2017-02-06 22:00:54 +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)
}
func (s *HealthCheckSuite) TestSimpleConfiguration(c *check.C) {
2017-02-06 22:00:54 +00:00
whoami1Host := s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
whoami2Host := s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress
file := s.adaptFile(c, "fixtures/healthcheck/simple.toml", struct {
Server1 string
Server2 string
}{whoami1Host, whoami2Host})
defer os.Remove(file)
cmd := exec.Command(traefikBinary, "--configFile="+file)
err := cmd.Start()
c.Assert(err, checker.IsNil)
defer cmd.Process.Kill()
// wait for traefik
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)
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)
frontendHealthReq.Host = "test.localhost"
2017-02-06 22:00:54 +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)
// Fix all whoami health to 500
client := &http.Client{}
whoamiHosts := []string{whoami1Host, whoami2Host}
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)
// Verify frontend health : 500
err = try.Request(frontendHealthReq, 3*time.Second, try.StatusCodeIs(http.StatusInternalServerError))
2017-02-06 22:00:54 +00:00
c.Assert(err, checker.IsNil)
// Change one whoami health to 200
statusOKReq1, err := http.NewRequest(http.MethodPost, "http://"+whoami1Host+"/health", bytes.NewBuffer([]byte("200")))
2017-02-06 22:00:54 +00:00
c.Assert(err, checker.IsNil)
_, err = client.Do(statusOKReq1)
2017-02-06 22:00:54 +00:00
c.Assert(err, checker.IsNil)
// 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
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)
frontendReq.Host = "test.localhost"
2017-02-06 22:00:54 +00:00
// Check if whoami1 respond
err = try.Request(frontendReq, 500*time.Millisecond, try.BodyContains(whoami1Host))
2017-02-06 22:00:54 +00:00
c.Assert(err, checker.IsNil)
// Check if the service with bad health check (whoami2) never respond.
err = try.Request(frontendReq, 2*time.Second, try.BodyContains(whoami2Host))
c.Assert(err, checker.Not(checker.IsNil))
2017-02-06 22:00:54 +00:00
// TODO validate : run on 80
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)
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
2017-02-06 22:00:54 +00:00
}