2019-11-28 20:56:04 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-08-11 13:42:07 +00:00
|
|
|
"context"
|
2019-11-28 20:56:04 +00:00
|
|
|
"encoding/json"
|
2021-11-25 10:10:06 +00:00
|
|
|
"net"
|
2019-11-28 20:56:04 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-01-09 16:00:07 +00:00
|
|
|
"testing"
|
2019-11-28 20:56:04 +00:00
|
|
|
"time"
|
|
|
|
|
2022-09-12 15:40:09 +00:00
|
|
|
"github.com/kvtools/etcdv3"
|
2022-01-12 13:42:21 +00:00
|
|
|
"github.com/kvtools/valkeyrie"
|
|
|
|
"github.com/kvtools/valkeyrie/store"
|
2019-11-28 20:56:04 +00:00
|
|
|
"github.com/pmezard/go-difflib/difflib"
|
2024-01-09 16:00:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/integration/try"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/api"
|
2019-11-28 20:56:04 +00:00
|
|
|
)
|
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
// etcd test suites.
|
2019-11-28 20:56:04 +00:00
|
|
|
type EtcdSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
kvClient store.Store
|
2021-11-25 10:10:06 +00:00
|
|
|
etcdAddr string
|
2019-11-28 20:56:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func TestEtcdSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(EtcdSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdSuite) SetupSuite() {
|
|
|
|
s.BaseSuite.SetupSuite()
|
|
|
|
|
|
|
|
s.createComposeProject("etcd")
|
|
|
|
s.composeUp()
|
2019-11-28 20:56:04 +00:00
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
var err error
|
2024-01-09 16:00:07 +00:00
|
|
|
s.etcdAddr = net.JoinHostPort(s.getComposeServiceIP("etcd"), "2379")
|
2021-11-25 10:10:06 +00:00
|
|
|
s.kvClient, err = valkeyrie.NewStore(
|
2022-08-11 13:42:07 +00:00
|
|
|
context.Background(),
|
2022-09-12 15:40:09 +00:00
|
|
|
etcdv3.StoreName,
|
2021-11-25 10:10:06 +00:00
|
|
|
[]string{s.etcdAddr},
|
2022-09-12 15:40:09 +00:00
|
|
|
&etcdv3.Config{
|
2019-11-28 20:56:04 +00:00
|
|
|
ConnectionTimeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
|
|
|
|
// wait for etcd
|
2021-11-25 10:10:06 +00:00
|
|
|
err = try.Do(60*time.Second, try.KVExists(s.kvClient, "test"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdSuite) TearDownSuite() {
|
|
|
|
s.BaseSuite.TearDownSuite()
|
2019-11-28 20:56:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *EtcdSuite) TestSimpleConfiguration() {
|
|
|
|
file := s.adaptFile("fixtures/etcd/simple.toml", struct{ EtcdAddress string }{s.etcdAddr})
|
2019-11-28 20:56:04 +00:00
|
|
|
|
|
|
|
data := map[string]string{
|
|
|
|
"traefik/http/routers/Router0/entryPoints/0": "web",
|
|
|
|
"traefik/http/routers/Router0/middlewares/0": "compressor",
|
|
|
|
"traefik/http/routers/Router0/middlewares/1": "striper",
|
|
|
|
"traefik/http/routers/Router0/service": "simplesvc",
|
|
|
|
"traefik/http/routers/Router0/rule": "Host(`kv1.localhost`)",
|
|
|
|
"traefik/http/routers/Router0/priority": "42",
|
|
|
|
"traefik/http/routers/Router0/tls": "",
|
|
|
|
|
|
|
|
"traefik/http/routers/Router1/rule": "Host(`kv2.localhost`)",
|
|
|
|
"traefik/http/routers/Router1/priority": "42",
|
|
|
|
"traefik/http/routers/Router1/tls/domains/0/main": "aaa.localhost",
|
|
|
|
"traefik/http/routers/Router1/tls/domains/0/sans/0": "aaa.aaa.localhost",
|
|
|
|
"traefik/http/routers/Router1/tls/domains/0/sans/1": "bbb.aaa.localhost",
|
|
|
|
"traefik/http/routers/Router1/tls/domains/1/main": "bbb.localhost",
|
|
|
|
"traefik/http/routers/Router1/tls/domains/1/sans/0": "aaa.bbb.localhost",
|
|
|
|
"traefik/http/routers/Router1/tls/domains/1/sans/1": "bbb.bbb.localhost",
|
|
|
|
"traefik/http/routers/Router1/entryPoints/0": "web",
|
|
|
|
"traefik/http/routers/Router1/service": "simplesvc",
|
|
|
|
|
|
|
|
"traefik/http/services/simplesvc/loadBalancer/servers/0/url": "http://10.0.1.1:8888",
|
|
|
|
"traefik/http/services/simplesvc/loadBalancer/servers/1/url": "http://10.0.1.1:8889",
|
|
|
|
|
|
|
|
"traefik/http/services/srvcA/loadBalancer/servers/0/url": "http://10.0.1.2:8888",
|
|
|
|
"traefik/http/services/srvcA/loadBalancer/servers/1/url": "http://10.0.1.2:8889",
|
|
|
|
|
|
|
|
"traefik/http/services/srvcB/loadBalancer/servers/0/url": "http://10.0.1.3:8888",
|
|
|
|
"traefik/http/services/srvcB/loadBalancer/servers/1/url": "http://10.0.1.3:8889",
|
|
|
|
|
|
|
|
"traefik/http/services/mirror/mirroring/service": "simplesvc",
|
|
|
|
"traefik/http/services/mirror/mirroring/mirrors/0/name": "srvcA",
|
|
|
|
"traefik/http/services/mirror/mirroring/mirrors/0/percent": "42",
|
|
|
|
"traefik/http/services/mirror/mirroring/mirrors/1/name": "srvcB",
|
|
|
|
"traefik/http/services/mirror/mirroring/mirrors/1/percent": "42",
|
|
|
|
|
|
|
|
"traefik/http/services/Service03/weighted/services/0/name": "srvcA",
|
|
|
|
"traefik/http/services/Service03/weighted/services/0/weight": "42",
|
|
|
|
"traefik/http/services/Service03/weighted/services/1/name": "srvcB",
|
|
|
|
"traefik/http/services/Service03/weighted/services/1/weight": "42",
|
|
|
|
|
|
|
|
"traefik/http/middlewares/compressor/compress": "",
|
|
|
|
"traefik/http/middlewares/striper/stripPrefix/prefixes/0": "foo",
|
|
|
|
"traefik/http/middlewares/striper/stripPrefix/prefixes/1": "bar",
|
|
|
|
"traefik/http/middlewares/striper/stripPrefix/forceSlash": "true",
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range data {
|
2022-08-11 13:42:07 +00:00
|
|
|
err := s.kvClient.Put(context.Background(), k, []byte(v), nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file))
|
2020-02-10 14:48:06 +00:00
|
|
|
|
2019-11-28 20:56:04 +00:00
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 2*time.Second,
|
2020-02-10 14:48:06 +00:00
|
|
|
try.BodyContains(`"striper@etcd":`, `"compressor@etcd":`, `"srvcA@etcd":`, `"srvcB@etcd":`),
|
|
|
|
)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
|
|
|
|
resp, err := http.Get("http://127.0.0.1:8080/api/rawdata")
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
|
|
|
|
var obtained api.RunTimeRepresentation
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&obtained)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
got, err := json.MarshalIndent(obtained, "", " ")
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
|
|
|
|
expectedJSON := filepath.FromSlash("testdata/rawdata-etcd.json")
|
|
|
|
|
|
|
|
if *updateExpected {
|
2021-03-04 19:08:03 +00:00
|
|
|
err = os.WriteFile(expectedJSON, got, 0o666)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
expected, err := os.ReadFile(expectedJSON)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-11-28 20:56:04 +00:00
|
|
|
|
|
|
|
if !bytes.Equal(expected, got) {
|
|
|
|
diff := difflib.UnifiedDiff{
|
|
|
|
FromFile: "Expected",
|
|
|
|
A: difflib.SplitLines(string(expected)),
|
|
|
|
ToFile: "Got",
|
|
|
|
B: difflib.SplitLines(string(got)),
|
|
|
|
Context: 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
text, err := difflib.GetUnifiedDiffString(diff)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err, text)
|
2019-11-28 20:56:04 +00:00
|
|
|
}
|
|
|
|
}
|