From aa2edcc6e529929a70edbd03f480e41025d291fb Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Mon, 6 Feb 2017 23:00:54 +0100 Subject: [PATCH] Add some integration test --- integration/fixtures/healthcheck/simple.toml | 27 ++++++ integration/healthcheck_test.go | 91 +++++++++++++++++++ integration/resources/compose/healthcheck.yml | 5 + 3 files changed, 123 insertions(+) create mode 100644 integration/fixtures/healthcheck/simple.toml create mode 100644 integration/healthcheck_test.go create mode 100644 integration/resources/compose/healthcheck.yml diff --git a/integration/fixtures/healthcheck/simple.toml b/integration/fixtures/healthcheck/simple.toml new file mode 100644 index 000000000..c3ebb33e3 --- /dev/null +++ b/integration/fixtures/healthcheck/simple.toml @@ -0,0 +1,27 @@ +defaultEntryPoints = ["http"] + +logLevel = "DEBUG" + +[entryPoints] + [entryPoints.http] + address = ":8000" + +[web] + address = ":8080" + +[file] +[backends] + [backends.backend1] + [backends.backend1.healthcheck] + url = "/health" + interval = "1s" + [backends.backend1.servers.server1] + url = "http://{{.Server1}}:80" + [backends.backend1.servers.server2] + url = "http://{{.Server2}}:80" + +[frontends] + [frontends.frontend1] + backend = "backend1" + [frontends.frontend1.routes.test_1] + rule = "Host:test.localhost" diff --git a/integration/healthcheck_test.go b/integration/healthcheck_test.go new file mode 100644 index 000000000..532a431bd --- /dev/null +++ b/integration/healthcheck_test.go @@ -0,0 +1,91 @@ +package main + +import ( + "bytes" + "errors" + "io/ioutil" + "net/http" + "os" + "os/exec" + "strings" + "time" + + "github.com/containous/traefik/integration/utils" + "github.com/go-check/check" + + checker "github.com/vdemeester/shakers" +) + +// HealchCheck test suites (using libcompose) +type HealchCheckSuite struct{ BaseSuite } + +func (s *HealchCheckSuite) SetUpSuite(c *check.C) { + s.createComposeProject(c, "healthcheck") + s.composeProject.Start(c) + +} + +func (s *HealchCheckSuite) TestSimpleConfiguration(c *check.C) { + + 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 = utils.TryRequest("http://127.0.0.1:8080/api/providers", 60*time.Second, func(res *http.Response) error { + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return err + } + if !strings.Contains(string(body), "Host:test.localhost") { + return errors.New("Incorrect traefik config: " + string(body)) + } + return nil + }) + c.Assert(err, checker.IsNil) + + client := &http.Client{} + req, err := http.NewRequest("GET", "http://127.0.0.1:8000/health", nil) + c.Assert(err, checker.IsNil) + req.Host = "test.localhost" + + resp, err := client.Do(req) + c.Assert(err, checker.IsNil) + c.Assert(resp.StatusCode, checker.Equals, 200) + + resp, err = client.Do(req) + c.Assert(err, checker.IsNil) + c.Assert(resp.StatusCode, checker.Equals, 200) + + healthReq, err := http.NewRequest("POST", "http://"+whoami1Host+"/health", bytes.NewBuffer([]byte("500"))) + c.Assert(err, checker.IsNil) + _, err = client.Do(healthReq) + c.Assert(err, checker.IsNil) + + time.Sleep(time.Second * 3) + + resp, err = client.Do(req) + c.Assert(err, checker.IsNil) + c.Assert(resp.StatusCode, checker.Equals, 200) + + resp, err = client.Do(req) + c.Assert(err, checker.IsNil) + c.Assert(resp.StatusCode, checker.Equals, 200) + + // TODO validate : run on 80 + resp, err = http.Get("http://127.0.0.1:8000/") + + // Expected a 404 as we did not configure anything + c.Assert(err, checker.IsNil) + c.Assert(resp.StatusCode, checker.Equals, 404) +} diff --git a/integration/resources/compose/healthcheck.yml b/integration/resources/compose/healthcheck.yml new file mode 100644 index 000000000..e2b6f7083 --- /dev/null +++ b/integration/resources/compose/healthcheck.yml @@ -0,0 +1,5 @@ +whoami1: + image: juliens/whoami + +whoami2: + image: juliens/whoami