Adds mirroring service
This commit is contained in:
parent
fd24b1898e
commit
602a2ea541
10 changed files with 465 additions and 10 deletions
|
@ -2,6 +2,7 @@ package integration
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
@ -9,6 +10,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
|
@ -671,3 +673,111 @@ func (s *SimpleSuite) TestWRRSticky(c *check.C) {
|
|||
c.Assert(repartition[server1], checker.Equals, 4)
|
||||
c.Assert(repartition[server2], checker.Equals, 0)
|
||||
}
|
||||
|
||||
func (s *SimpleSuite) TestMirror(c *check.C) {
|
||||
var count, countMirror1, countMirror2 int32
|
||||
|
||||
main := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
atomic.AddInt32(&count, 1)
|
||||
}))
|
||||
|
||||
mirror1 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
atomic.AddInt32(&countMirror1, 1)
|
||||
}))
|
||||
|
||||
mirror2 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
atomic.AddInt32(&countMirror2, 1)
|
||||
}))
|
||||
|
||||
mainServer := main.URL
|
||||
mirror1Server := mirror1.URL
|
||||
mirror2Server := mirror2.URL
|
||||
|
||||
file := s.adaptFile(c, "fixtures/mirror.toml", struct {
|
||||
MainServer string
|
||||
Mirror1Server string
|
||||
Mirror2Server string
|
||||
}{MainServer: mainServer, Mirror1Server: mirror1Server, Mirror2Server: mirror2Server})
|
||||
defer os.Remove(file)
|
||||
|
||||
cmd, output := s.traefikCmd(withConfigFile(file))
|
||||
defer output(c)
|
||||
|
||||
err := cmd.Start()
|
||||
c.Assert(err, checker.IsNil)
|
||||
defer cmd.Process.Kill()
|
||||
|
||||
err = try.GetRequest("http://127.0.0.1:8080/api/http/services", 1000*time.Millisecond, try.BodyContains("mirror1", "mirror2", "service1"))
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/whoami", nil)
|
||||
c.Assert(err, checker.IsNil)
|
||||
for i := 0; i < 10; i++ {
|
||||
response, err := http.DefaultClient.Do(req)
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(response.StatusCode, checker.Equals, http.StatusOK)
|
||||
}
|
||||
|
||||
countTotal := atomic.LoadInt32(&count)
|
||||
val1 := atomic.LoadInt32(&countMirror1)
|
||||
val2 := atomic.LoadInt32(&countMirror2)
|
||||
|
||||
c.Assert(countTotal, checker.Equals, int32(10))
|
||||
c.Assert(val1, checker.Equals, int32(1))
|
||||
c.Assert(val2, checker.Equals, int32(5))
|
||||
}
|
||||
|
||||
func (s *SimpleSuite) TestMirrorCanceled(c *check.C) {
|
||||
var count, countMirror1, countMirror2 int32
|
||||
|
||||
main := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
atomic.AddInt32(&count, 1)
|
||||
time.Sleep(time.Second * 2)
|
||||
}))
|
||||
|
||||
mirror1 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
atomic.AddInt32(&countMirror1, 1)
|
||||
}))
|
||||
|
||||
mirror2 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
atomic.AddInt32(&countMirror2, 1)
|
||||
}))
|
||||
|
||||
mainServer := main.URL
|
||||
mirror1Server := mirror1.URL
|
||||
mirror2Server := mirror2.URL
|
||||
|
||||
file := s.adaptFile(c, "fixtures/mirror.toml", struct {
|
||||
MainServer string
|
||||
Mirror1Server string
|
||||
Mirror2Server string
|
||||
}{MainServer: mainServer, Mirror1Server: mirror1Server, Mirror2Server: mirror2Server})
|
||||
defer os.Remove(file)
|
||||
|
||||
cmd, output := s.traefikCmd(withConfigFile(file))
|
||||
defer output(c)
|
||||
|
||||
err := cmd.Start()
|
||||
c.Assert(err, checker.IsNil)
|
||||
defer cmd.Process.Kill()
|
||||
|
||||
err = try.GetRequest("http://127.0.0.1:8080/api/http/services", 1000*time.Millisecond, try.BodyContains("mirror1", "mirror2", "service1"))
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/whoami", nil)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
newCtx, _ := context.WithTimeout(req.Context(), time.Second)
|
||||
req = req.WithContext(newCtx)
|
||||
http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
countTotal := atomic.LoadInt32(&count)
|
||||
val1 := atomic.LoadInt32(&countMirror1)
|
||||
val2 := atomic.LoadInt32(&countMirror2)
|
||||
|
||||
c.Assert(countTotal, checker.Equals, int32(5))
|
||||
c.Assert(val1, checker.Equals, int32(0))
|
||||
c.Assert(val2, checker.Equals, int32(0))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue