2018-11-14 09:18:03 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-05-16 08:58:06 +00:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
2019-06-19 16:34:04 +00:00
|
|
|
"fmt"
|
2018-11-14 09:18:03 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-06-19 16:34:04 +00:00
|
|
|
"strconv"
|
2018-11-14 09:18:03 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containous/mux"
|
2019-03-15 08:42:03 +00:00
|
|
|
"github.com/containous/traefik/pkg/config"
|
2019-05-16 08:58:06 +00:00
|
|
|
"github.com/containous/traefik/pkg/config/static"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
var updateExpected = flag.Bool("update_expected", false, "Update expected files in testdata")
|
|
|
|
|
2019-06-19 16:34:04 +00:00
|
|
|
func TestHandlerTCP_API(t *testing.T) {
|
|
|
|
type expected struct {
|
|
|
|
statusCode int
|
|
|
|
nextPage string
|
|
|
|
jsonFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
path string
|
|
|
|
conf config.RuntimeConfiguration
|
|
|
|
expected expected
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "all TCP routers, but no config",
|
|
|
|
path: "/api/tcp/routers",
|
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/tcprouters-empty.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all TCP routers",
|
|
|
|
path: "/api/tcp/routers",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPRouters: map[string]*config.TCPRouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"test@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar.other`)",
|
|
|
|
TLS: &config.RouterTCPTLSConfig{
|
|
|
|
Passthrough: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/tcprouters.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all TCP routers, pagination, 1 res per page, want page 2",
|
|
|
|
path: "/api/tcp/routers?page=2&per_page=1",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPRouters: map[string]*config.TCPRouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"baz@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`toto.bar`)",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"test@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar.other`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "3",
|
|
|
|
jsonFile: "testdata/tcprouters-page2.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one TCP router by id",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/tcp/routers/bar@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPRouters: map[string]*config.TCPRouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
jsonFile: "testdata/tcprouter-bar.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one TCP router by id, that does not exist",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/tcp/routers/foo@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPRouters: map[string]*config.TCPRouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one TCP router by id, but no config",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/tcp/routers/bar@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all tcp services, but no config",
|
|
|
|
path: "/api/tcp/services",
|
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/tcpservices-empty.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all tcp services",
|
|
|
|
path: "/api/tcp/services",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPServices: map[string]*config.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.1:2345",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"baz@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.2:2345",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/tcpservices.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all tcp services, 1 res per page, want page 2",
|
|
|
|
path: "/api/tcp/services?page=2&per_page=1",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPServices: map[string]*config.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.1:2345",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"baz@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.2:2345",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"test@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.3:2345",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "3",
|
|
|
|
jsonFile: "testdata/tcpservices-page2.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one tcp service by id",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/tcp/services/bar@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPServices: map[string]*config.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.1:2345",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
jsonFile: "testdata/tcpservice-bar.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one tcp service by id, that does not exist",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/tcp/services/nono@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
TCPServices: map[string]*config.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.1:2345",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one tcp service by id, but no config",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/tcp/services/foo@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
rtConf := &test.conf
|
|
|
|
handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, rtConf)
|
|
|
|
router := mux.NewRouter()
|
|
|
|
handler.Append(router)
|
|
|
|
|
|
|
|
server := httptest.NewServer(router)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Get(server.URL + test.path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected.nextPage, resp.Header.Get(nextPageHeader))
|
|
|
|
|
|
|
|
require.Equal(t, test.expected.statusCode, resp.StatusCode)
|
|
|
|
|
|
|
|
if test.expected.jsonFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-25 15:44:03 +00:00
|
|
|
assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
|
|
|
|
|
2019-06-19 16:34:04 +00:00
|
|
|
contents, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = resp.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if *updateExpected {
|
|
|
|
var results interface{}
|
|
|
|
err := json.Unmarshal(contents, &results)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newJSON, err := json.MarshalIndent(results, "", "\t")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(test.expected.jsonFile, newJSON, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(test.expected.jsonFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, string(data), string(contents))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandlerHTTP_API(t *testing.T) {
|
|
|
|
type expected struct {
|
|
|
|
statusCode int
|
|
|
|
nextPage string
|
|
|
|
jsonFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
path string
|
|
|
|
conf config.RuntimeConfiguration
|
|
|
|
expected expected
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "all routers, but no config",
|
|
|
|
path: "/api/http/routers",
|
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/routers-empty.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all routers",
|
|
|
|
path: "/api/http/routers",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Routers: map[string]*config.RouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"test@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar.other`)",
|
|
|
|
Middlewares: []string{"addPrefixTest", "auth"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-06-21 07:54:04 +00:00
|
|
|
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/routers.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all routers, pagination, 1 res per page, want page 2",
|
|
|
|
path: "/api/http/routers?page=2&per_page=1",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Routers: map[string]*config.RouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-06-21 07:54:04 +00:00
|
|
|
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"baz@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`toto.bar`)",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"test@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar.other`)",
|
|
|
|
Middlewares: []string{"addPrefixTest", "auth"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "3",
|
|
|
|
jsonFile: "testdata/routers-page2.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all routers, pagination, 19 results overall, 7 res per page, want page 3",
|
|
|
|
path: "/api/http/routers?page=3&per_page=7",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Routers: generateHTTPRouters(19),
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/routers-many-lastpage.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all routers, pagination, 5 results overall, 10 res per page, want page 2",
|
|
|
|
path: "/api/http/routers?page=2&per_page=10",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Routers: generateHTTPRouters(5),
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all routers, pagination, 10 results overall, 10 res per page, want page 2",
|
|
|
|
path: "/api/http/routers?page=2&per_page=10",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Routers: generateHTTPRouters(10),
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one router by id",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/routers/bar@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Routers: map[string]*config.RouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-06-21 07:54:04 +00:00
|
|
|
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
jsonFile: "testdata/router-bar.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one router by id, that does not exist",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/routers/foo@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Routers: map[string]*config.RouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-06-21 07:54:04 +00:00
|
|
|
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one router by id, but no config",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/routers/foo@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all services, but no config",
|
|
|
|
path: "/api/http/services",
|
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/services-empty.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all services",
|
|
|
|
path: "/api/http/services",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Services: map[string]*config.ServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": func() *config.ServiceInfo {
|
2019-06-19 16:34:04 +00:00
|
|
|
si := &config.ServiceInfo{
|
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
|
|
|
URL: "http://127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
}
|
|
|
|
si.UpdateStatus("http://127.0.0.1", "UP")
|
|
|
|
return si
|
|
|
|
}(),
|
2019-06-21 07:54:04 +00:00
|
|
|
"baz@myprovider": func() *config.ServiceInfo {
|
2019-06-19 16:34:04 +00:00
|
|
|
si := &config.ServiceInfo{
|
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
|
|
|
URL: "http://127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
}
|
|
|
|
si.UpdateStatus("http://127.0.0.2", "UP")
|
|
|
|
return si
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/services.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all services, 1 res per page, want page 2",
|
|
|
|
path: "/api/http/services?page=2&per_page=1",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Services: map[string]*config.ServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": func() *config.ServiceInfo {
|
2019-06-19 16:34:04 +00:00
|
|
|
si := &config.ServiceInfo{
|
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
|
|
|
URL: "http://127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
}
|
|
|
|
si.UpdateStatus("http://127.0.0.1", "UP")
|
|
|
|
return si
|
|
|
|
}(),
|
2019-06-21 07:54:04 +00:00
|
|
|
"baz@myprovider": func() *config.ServiceInfo {
|
2019-06-19 16:34:04 +00:00
|
|
|
si := &config.ServiceInfo{
|
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
|
|
|
URL: "http://127.0.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
}
|
|
|
|
si.UpdateStatus("http://127.0.0.2", "UP")
|
|
|
|
return si
|
|
|
|
}(),
|
2019-06-21 07:54:04 +00:00
|
|
|
"test@myprovider": func() *config.ServiceInfo {
|
2019-06-19 16:34:04 +00:00
|
|
|
si := &config.ServiceInfo{
|
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
|
|
|
URL: "http://127.0.0.3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
}
|
|
|
|
si.UpdateStatus("http://127.0.0.4", "UP")
|
|
|
|
return si
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "3",
|
|
|
|
jsonFile: "testdata/services-page2.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service by id",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/services/bar@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Services: map[string]*config.ServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": func() *config.ServiceInfo {
|
2019-06-19 16:34:04 +00:00
|
|
|
si := &config.ServiceInfo{
|
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
|
|
|
URL: "http://127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
}
|
|
|
|
si.UpdateStatus("http://127.0.0.1", "UP")
|
|
|
|
return si
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
jsonFile: "testdata/service-bar.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service by id, that does not exist",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/services/nono@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Services: map[string]*config.ServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": func() *config.ServiceInfo {
|
2019-06-19 16:34:04 +00:00
|
|
|
si := &config.ServiceInfo{
|
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
|
|
|
URL: "http://127.0.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"foo@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
}
|
|
|
|
si.UpdateStatus("http://127.0.0.1", "UP")
|
|
|
|
return si
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service by id, but no config",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/services/foo@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all middlewares, but no config",
|
|
|
|
path: "/api/http/middlewares",
|
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/middlewares-empty.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all middlewares",
|
|
|
|
path: "/api/http/middlewares",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Middlewares: map[string]*config.MiddlewareInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"auth@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
BasicAuth: &config.BasicAuth{
|
|
|
|
Users: []string{"admin:admin"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"bar@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/titi",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@anotherprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/toto",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"bar@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "1",
|
|
|
|
jsonFile: "testdata/middlewares.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all middlewares, 1 res per page, want page 2",
|
|
|
|
path: "/api/http/middlewares?page=2&per_page=1",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Middlewares: map[string]*config.MiddlewareInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"auth@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
BasicAuth: &config.BasicAuth{
|
|
|
|
Users: []string{"admin:admin"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"bar@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/titi",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@anotherprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/toto",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"bar@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
nextPage: "3",
|
|
|
|
jsonFile: "testdata/middlewares-page2.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one middleware by id",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/middlewares/auth@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Middlewares: map[string]*config.MiddlewareInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"auth@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
BasicAuth: &config.BasicAuth{
|
|
|
|
Users: []string{"admin:admin"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"bar@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/titi",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@anotherprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/toto",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"bar@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
jsonFile: "testdata/middleware-auth.json",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one middleware by id, that does not exist",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/middlewares/foo@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Middlewares: map[string]*config.MiddlewareInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"auth@myprovider": {
|
2019-06-19 16:34:04 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
BasicAuth: &config.BasicAuth{
|
|
|
|
Users: []string{"admin:admin"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
UsedBy: []string{"bar@myprovider", "test@myprovider"},
|
2019-06-19 16:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one middleware by id, but no config",
|
2019-06-21 07:54:04 +00:00
|
|
|
path: "/api/http/middlewares/foo@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
conf: config.RuntimeConfiguration{},
|
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
rtConf := &test.conf
|
|
|
|
handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, rtConf)
|
|
|
|
router := mux.NewRouter()
|
|
|
|
handler.Append(router)
|
|
|
|
|
|
|
|
server := httptest.NewServer(router)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Get(server.URL + test.path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, test.expected.statusCode, resp.StatusCode)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected.nextPage, resp.Header.Get(nextPageHeader))
|
|
|
|
|
|
|
|
if test.expected.jsonFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-25 15:44:03 +00:00
|
|
|
assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
|
2019-06-19 16:34:04 +00:00
|
|
|
contents, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = resp.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if *updateExpected {
|
|
|
|
var results interface{}
|
|
|
|
err := json.Unmarshal(contents, &results)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newJSON, err := json.MarshalIndent(results, "", "\t")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(test.expected.jsonFile, newJSON, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(test.expected.jsonFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, string(data), string(contents))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func TestHandler_Configuration(t *testing.T) {
|
|
|
|
type expected struct {
|
|
|
|
statusCode int
|
2019-05-16 08:58:06 +00:00
|
|
|
json string
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2019-05-16 08:58:06 +00:00
|
|
|
desc string
|
|
|
|
path string
|
|
|
|
conf config.RuntimeConfiguration
|
|
|
|
expected expected
|
2018-11-14 09:18:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-05-16 08:58:06 +00:00
|
|
|
desc: "Get rawdata",
|
|
|
|
path: "/api/rawdata",
|
|
|
|
conf: config.RuntimeConfiguration{
|
|
|
|
Services: map[string]*config.ServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"foo-service@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
Service: &config.Service{
|
|
|
|
LoadBalancer: &config.LoadBalancerService{
|
|
|
|
Servers: []config.Server{
|
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
Middlewares: map[string]*config.MiddlewareInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"auth@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
BasicAuth: &config.BasicAuth{
|
|
|
|
Users: []string{"admin:admin"},
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/titi",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"addPrefixTest@anotherprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
Middleware: &config.Middleware{
|
|
|
|
AddPrefix: &config.AddPrefix{
|
|
|
|
Prefix: "/toto",
|
|
|
|
},
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
Routers: map[string]*config.RouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"bar@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-05-16 08:58:06 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-06-21 07:54:04 +00:00
|
|
|
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"test@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-05-16 08:58:06 +00:00
|
|
|
Rule: "Host(`foo.bar.other`)",
|
|
|
|
Middlewares: []string{"addPrefixTest", "auth"},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPServices: map[string]*config.TCPServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"tcpfoo-service@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPService: &config.TCPService{
|
|
|
|
LoadBalancer: &config.TCPLoadBalancerService{
|
|
|
|
Servers: []config.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPRouters: map[string]*config.TCPRouterInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"tcpbar@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "tcpfoo-service@myprovider",
|
2019-05-16 08:58:06 +00:00
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"tcptest@myprovider": {
|
2019-05-16 08:58:06 +00:00
|
|
|
TCPRouter: &config.TCPRouter{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "tcpfoo-service@myprovider",
|
2019-05-16 08:58:06 +00:00
|
|
|
Rule: "HostSNI(`foo.bar.other`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-16 08:58:06 +00:00
|
|
|
expected: expected{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
json: "testdata/getrawdata.json",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-06-19 16:34:04 +00:00
|
|
|
// TODO: server status
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
rtConf := &test.conf
|
2019-06-21 07:54:04 +00:00
|
|
|
|
2019-06-19 16:34:04 +00:00
|
|
|
rtConf.PopulateUsedBy()
|
2019-05-16 08:58:06 +00:00
|
|
|
handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, rtConf)
|
2018-11-14 09:18:03 +00:00
|
|
|
router := mux.NewRouter()
|
|
|
|
handler.Append(router)
|
|
|
|
|
|
|
|
server := httptest.NewServer(router)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Get(server.URL + test.path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected.statusCode, resp.StatusCode)
|
2019-06-25 15:44:03 +00:00
|
|
|
assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
contents, err := ioutil.ReadAll(resp.Body)
|
2018-11-14 09:18:03 +00:00
|
|
|
require.NoError(t, err)
|
2019-05-16 08:58:06 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
err = resp.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
if test.expected.json == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if *updateExpected {
|
|
|
|
var rtRepr RunTimeRepresentation
|
|
|
|
err := json.Unmarshal(contents, &rtRepr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newJSON, err := json.MarshalIndent(rtRepr, "", "\t")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(test.expected.json, newJSON, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(test.expected.json)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, string(data), string(contents))
|
2018-11-14 09:18:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 16:34:04 +00:00
|
|
|
|
|
|
|
func generateHTTPRouters(nbRouters int) map[string]*config.RouterInfo {
|
|
|
|
routers := make(map[string]*config.RouterInfo, nbRouters)
|
|
|
|
for i := 0; i < nbRouters; i++ {
|
2019-06-21 07:54:04 +00:00
|
|
|
routers[fmt.Sprintf("bar%2d@myprovider", i)] = &config.RouterInfo{
|
2019-06-19 16:34:04 +00:00
|
|
|
Router: &config.Router{
|
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@myprovider",
|
2019-06-19 16:34:04 +00:00
|
|
|
Rule: "Host(`foo.bar" + strconv.Itoa(i) + "`)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return routers
|
|
|
|
}
|