2017-09-09 11:36:03 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/integration/try"
|
2017-09-09 11:36:03 +00:00
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RateLimitSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
ServerIP string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RateLimitSuite) SetUpSuite(c *check.C) {
|
|
|
|
s.createComposeProject(c, "ratelimit")
|
|
|
|
s.composeProject.Start(c)
|
|
|
|
|
2018-02-21 15:28:03 +00:00
|
|
|
s.ServerIP = s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress
|
2017-09-09 11:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RateLimitSuite) TestSimpleConfiguration(c *check.C) {
|
|
|
|
file := s.adaptFile(c, "fixtures/ratelimit/simple.toml", struct {
|
|
|
|
Server1 string
|
|
|
|
}{s.ServerIP})
|
|
|
|
defer os.Remove(file)
|
|
|
|
|
2019-06-17 16:14:08 +00:00
|
|
|
cmd, display := s.traefikCmd(withConfigFile(file))
|
|
|
|
defer display(c)
|
2017-09-09 11:36:03 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
2019-06-17 16:14:08 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1*time.Second, try.BodyContains("ratelimit"))
|
2017-09-09 11:36:03 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2019-06-17 16:14:08 +00:00
|
|
|
|
2019-08-26 10:20:06 +00:00
|
|
|
start := time.Now()
|
|
|
|
count := 0
|
|
|
|
for {
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8081/", 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
count++
|
|
|
|
if count > 100 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stop := time.Now()
|
|
|
|
elapsed := stop.Sub(start)
|
|
|
|
if elapsed < time.Second*99/100 {
|
|
|
|
c.Fatalf("requests throughput was too fast wrt to rate limiting: 100 requests in %v", elapsed)
|
|
|
|
}
|
2017-09-09 11:36:03 +00:00
|
|
|
}
|