traefik/pkg/api/handler_tcp_test.go

862 lines
23 KiB
Go
Raw Normal View History

2019-07-12 09:10:03 +00:00
package api
import (
2019-09-02 09:38:04 +00:00
"context"
2019-07-12 09:10:03 +00:00
"encoding/json"
2021-03-04 19:08:03 +00:00
"io"
2019-07-12 09:10:03 +00:00
"net/http"
"net/http/httptest"
2021-03-04 19:08:03 +00:00
"os"
2019-07-12 09:10:03 +00:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/dynamic"
"github.com/traefik/traefik/v3/pkg/config/runtime"
"github.com/traefik/traefik/v3/pkg/config/static"
2019-07-12 09:10:03 +00:00
)
func TestHandler_TCP(t *testing.T) {
type expected struct {
statusCode int
nextPage string
jsonFile string
}
testCases := []struct {
desc string
path string
conf runtime.Configuration
2019-07-12 09:10:03 +00:00
expected expected
}{
{
desc: "all TCP routers, but no config",
path: "/api/tcp/routers",
conf: runtime.Configuration{},
2019-07-12 09:10:03 +00:00
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcprouters-empty.json",
},
},
{
desc: "all TCP routers",
path: "/api/tcp/routers",
conf: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
2019-07-12 09:10:03 +00:00
"test@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
TLS: &dynamic.RouterTCPTLSConfig{
Passthrough: false,
},
},
Status: runtime.StatusEnabled,
2019-07-12 09:10:03 +00:00
},
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusWarning,
},
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusDisabled,
2019-07-12 09:10:03 +00:00
},
},
},
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: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
2019-07-12 09:10:03 +00:00
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
},
"baz@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`toto.bar`)",
},
},
"test@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
},
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "3",
jsonFile: "testdata/tcprouters-page2.json",
},
},
2019-09-02 09:38:04 +00:00
{
desc: "TCP routers filtered by status",
path: "/api/tcp/routers?status=enabled",
conf: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"test@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
TLS: &dynamic.RouterTCPTLSConfig{
Passthrough: false,
},
},
Status: runtime.StatusEnabled,
},
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusWarning,
},
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcprouters-filtered-status.json",
},
},
{
desc: "TCP routers filtered by search",
path: "/api/tcp/routers?search=bar@my",
conf: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"test@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
TLS: &dynamic.RouterTCPTLSConfig{
Passthrough: false,
},
},
Status: runtime.StatusEnabled,
},
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusWarning,
},
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcprouters-filtered-search.json",
},
},
{
desc: "TCP routers filtered by service",
path: "/api/tcp/routers?serviceName=foo-service@myprovider",
conf: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"test@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
TLS: &dynamic.RouterTCPTLSConfig{
Passthrough: false,
},
},
Status: runtime.StatusEnabled,
},
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusWarning,
},
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "bar-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcprouters-filtered-serviceName.json",
},
},
{
desc: "TCP routers filtered by middleware",
path: "/api/tcp/routers?middlewareName=auth",
conf: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"test@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
Middlewares: []string{"inflightconn@myprovider"},
TLS: &dynamic.RouterTCPTLSConfig{
Passthrough: false,
},
},
Status: runtime.StatusEnabled,
},
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service",
Rule: "Host(`foo.bar`)",
Middlewares: []string{"auth", "inflightconn@myprovider"},
},
Status: runtime.StatusWarning,
},
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "bar-service@myprovider",
Rule: "Host(`foo.bar`)",
Middlewares: []string{"inflightconn@myprovider", "auth"},
},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcprouters-filtered-middlewareName.json",
},
},
2019-07-12 09:10:03 +00:00
{
desc: "one TCP router by id",
path: "/api/tcp/routers/bar@myprovider",
conf: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
2019-07-12 09:10:03 +00:00
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
},
},
},
expected: expected{
statusCode: http.StatusOK,
jsonFile: "testdata/tcprouter-bar.json",
},
},
{
desc: "one TCP router by id, that does not exist",
path: "/api/tcp/routers/foo@myprovider",
conf: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
2019-07-12 09:10:03 +00:00
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
},
},
},
},
expected: expected{
statusCode: http.StatusNotFound,
},
},
{
desc: "one TCP router by id, but no config",
path: "/api/tcp/routers/bar@myprovider",
conf: runtime.Configuration{},
2019-07-12 09:10:03 +00:00
expected: expected{
statusCode: http.StatusNotFound,
},
},
{
desc: "all tcp services, but no config",
path: "/api/tcp/services",
conf: runtime.Configuration{},
2019-07-12 09:10:03 +00:00
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcpservices-empty.json",
},
},
{
desc: "all tcp services",
path: "/api/tcp/services",
conf: runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
2019-07-12 09:10:03 +00:00
"bar@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-12 09:10:03 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.1:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
2019-07-12 09:10:03 +00:00
},
"baz@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-12 09:10:03 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.2:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusWarning,
},
"foz@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.2:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusDisabled,
2019-07-12 09:10:03 +00:00
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcpservices.json",
},
},
2019-09-02 09:38:04 +00:00
{
desc: "tcp services filtered by status",
path: "/api/tcp/services?status=enabled",
conf: runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
"bar@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-09-02 09:38:04 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.1:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
},
"baz@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-09-02 09:38:04 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.2:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusWarning,
},
"foz@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-09-02 09:38:04 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.2:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcpservices-filtered-status.json",
},
},
{
desc: "tcp services filtered by search",
path: "/api/tcp/services?search=baz@my",
conf: runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
"bar@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-09-02 09:38:04 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.1:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
},
"baz@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-09-02 09:38:04 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.2:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusWarning,
},
"foz@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-09-02 09:38:04 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.2:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
Status: runtime.StatusDisabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcpservices-filtered-search.json",
},
},
2019-07-12 09:10:03 +00:00
{
desc: "all tcp services, 1 res per page, want page 2",
path: "/api/tcp/services?page=2&per_page=1",
conf: runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
2019-07-12 09:10:03 +00:00
"bar@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-12 09:10:03 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.1:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
},
"baz@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-12 09:10:03 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.2:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider"},
},
"test@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-12 09:10:03 +00:00
Servers: []dynamic.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",
path: "/api/tcp/services/bar@myprovider",
conf: runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
2019-07-12 09:10:03 +00:00
"bar@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-12 09:10:03 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.1:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
},
},
},
expected: expected{
statusCode: http.StatusOK,
jsonFile: "testdata/tcpservice-bar.json",
},
},
{
desc: "one tcp service by id, that does not exist",
path: "/api/tcp/services/nono@myprovider",
conf: runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
2019-07-12 09:10:03 +00:00
"bar@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-12 09:10:03 +00:00
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.1:2345",
},
},
},
},
UsedBy: []string{"foo@myprovider", "test@myprovider"},
},
},
},
expected: expected{
statusCode: http.StatusNotFound,
},
},
{
desc: "one tcp service by id, but no config",
path: "/api/tcp/services/foo@myprovider",
conf: runtime.Configuration{},
2019-07-12 09:10:03 +00:00
expected: expected{
statusCode: http.StatusNotFound,
},
},
2021-06-11 13:30:05 +00:00
{
desc: "all middlewares",
path: "/api/tcp/middlewares",
conf: runtime.Configuration{
TCPMiddlewares: map[string]*runtime.TCPMiddlewareInfo{
2022-10-26 15:16:05 +00:00
"ipallowlist1@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
2022-10-26 15:16:05 +00:00
"ipallowlist2@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.2/32"},
},
},
UsedBy: []string{"test@myprovider"},
},
2022-10-26 15:16:05 +00:00
"ipallowlist1@anotherprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider"},
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcpmiddlewares.json",
},
},
{
desc: "middlewares filtered by status",
path: "/api/tcp/middlewares?status=enabled",
conf: runtime.Configuration{
TCPMiddlewares: map[string]*runtime.TCPMiddlewareInfo{
2022-10-26 15:16:05 +00:00
"ipallowlist@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
},
2022-10-26 15:16:05 +00:00
"ipallowlist2@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.2/32"},
},
},
UsedBy: []string{"test@myprovider"},
Status: runtime.StatusDisabled,
},
2022-10-26 15:16:05 +00:00
"ipallowlist@anotherprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider"},
Status: runtime.StatusEnabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcpmiddlewares-filtered-status.json",
},
},
{
desc: "middlewares filtered by search",
2022-10-26 15:16:05 +00:00
path: "/api/tcp/middlewares?search=ipallowlist",
2021-06-11 13:30:05 +00:00
conf: runtime.Configuration{
TCPMiddlewares: map[string]*runtime.TCPMiddlewareInfo{
"bad@myprovider": {
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
Status: runtime.StatusEnabled,
},
2022-10-26 15:16:05 +00:00
"ipallowlist@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"test@myprovider"},
Status: runtime.StatusDisabled,
},
2022-10-26 15:16:05 +00:00
"ipallowlist@anotherprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider"},
Status: runtime.StatusEnabled,
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "1",
jsonFile: "testdata/tcpmiddlewares-filtered-search.json",
},
},
{
desc: "all middlewares, 1 res per page, want page 2",
path: "/api/tcp/middlewares?page=2&per_page=1",
conf: runtime.Configuration{
TCPMiddlewares: map[string]*runtime.TCPMiddlewareInfo{
2022-10-26 15:16:05 +00:00
"ipallowlist@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
2022-10-26 15:16:05 +00:00
"ipallowlist2@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.2/32"},
},
},
UsedBy: []string{"test@myprovider"},
},
2022-10-26 15:16:05 +00:00
"ipallowlist@anotherprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider"},
},
},
},
expected: expected{
statusCode: http.StatusOK,
nextPage: "3",
jsonFile: "testdata/tcpmiddlewares-page2.json",
},
},
{
desc: "one middleware by id",
2022-10-26 15:16:05 +00:00
path: "/api/tcp/middlewares/ipallowlist@myprovider",
2021-06-11 13:30:05 +00:00
conf: runtime.Configuration{
TCPMiddlewares: map[string]*runtime.TCPMiddlewareInfo{
2022-10-26 15:16:05 +00:00
"ipallowlist@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
2022-10-26 15:16:05 +00:00
"ipallowlist2@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.2/32"},
},
},
UsedBy: []string{"test@myprovider"},
},
2022-10-26 15:16:05 +00:00
"ipallowlist@anotherprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider"},
},
},
},
expected: expected{
statusCode: http.StatusOK,
2022-10-26 15:16:05 +00:00
jsonFile: "testdata/tcpmiddleware-ipallowlist.json",
2021-06-11 13:30:05 +00:00
},
},
{
desc: "one middleware by id, that does not exist",
path: "/api/tcp/middlewares/foo@myprovider",
conf: runtime.Configuration{
TCPMiddlewares: map[string]*runtime.TCPMiddlewareInfo{
2022-10-26 15:16:05 +00:00
"ipallowlist@myprovider": {
2021-06-11 13:30:05 +00:00
TCPMiddleware: &dynamic.TCPMiddleware{
2022-10-26 15:16:05 +00:00
IPAllowList: &dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"127.0.0.1/32"},
},
},
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
},
},
expected: expected{
statusCode: http.StatusNotFound,
},
},
{
desc: "one middleware by id, but no config",
path: "/api/tcp/middlewares/foo@myprovider",
conf: runtime.Configuration{},
expected: expected{
statusCode: http.StatusNotFound,
},
},
2019-07-12 09:10:03 +00:00
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
rtConf := &test.conf
2019-09-02 09:38:04 +00:00
// To lazily initialize the Statuses.
rtConf.PopulateUsedBy()
rtConf.GetTCPRoutersByEntryPoints(context.Background(), []string{"web"})
2019-07-12 09:10:03 +00:00
handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, rtConf)
server := httptest.NewServer(handler.createRouter())
2019-07-12 09:10:03 +00:00
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
}
assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
2021-03-04 19:08:03 +00:00
contents, err := io.ReadAll(resp.Body)
2019-07-12 09:10:03 +00:00
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)
2021-03-04 19:08:03 +00:00
err = os.WriteFile(test.expected.jsonFile, newJSON, 0o644)
2019-07-12 09:10:03 +00:00
require.NoError(t, err)
}
2021-03-04 19:08:03 +00:00
data, err := os.ReadFile(test.expected.jsonFile)
2019-07-12 09:10:03 +00:00
require.NoError(t, err)
assert.JSONEq(t, string(data), string(contents))
})
}
}