2018-12-03 10:32:05 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-11-25 10:10:06 +00:00
|
|
|
"net"
|
2018-12-03 10:32:05 +00:00
|
|
|
"net/http"
|
2019-09-06 13:08:04 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2018-12-03 10:32:05 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/integration/try"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
2018-12-03 10:32:05 +00:00
|
|
|
checker "github.com/vdemeester/shakers"
|
|
|
|
)
|
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
type RestSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
whoamiAddr string
|
|
|
|
}
|
2018-12-03 10:32:05 +00:00
|
|
|
|
|
|
|
func (s *RestSuite) SetUpSuite(c *check.C) {
|
|
|
|
s.createComposeProject(c, "rest")
|
2021-11-25 10:10:06 +00:00
|
|
|
s.composeUp(c)
|
2018-12-03 10:32:05 +00:00
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
s.whoamiAddr = net.JoinHostPort(s.getComposeServiceIP(c, "whoami1"), "80")
|
2018-12-03 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 13:08:04 +00:00
|
|
|
func (s *RestSuite) TestSimpleConfigurationInsecure(c *check.C) {
|
2018-12-03 10:32:05 +00:00
|
|
|
cmd, display := s.traefikCmd(withConfigFile("fixtures/rest/simple.toml"))
|
|
|
|
|
|
|
|
defer display(c)
|
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
2020-10-09 07:32:03 +00:00
|
|
|
defer s.killCmd(cmd)
|
2018-12-03 10:32:05 +00:00
|
|
|
|
2019-11-14 15:40:05 +00:00
|
|
|
// wait for Traefik
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1000*time.Millisecond, try.BodyContains("rest@internal"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2018-12-03 10:32:05 +00:00
|
|
|
// Expected a 404 as we did not configure anything.
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2019-07-12 23:24:03 +00:00
|
|
|
testCase := []struct {
|
|
|
|
desc string
|
|
|
|
config *dynamic.Configuration
|
|
|
|
ruleMatch string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "deploy http configuration",
|
|
|
|
config: &dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-11-14 15:40:05 +00:00
|
|
|
"routerHTTP": {
|
2019-07-12 23:24:03 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Middlewares: []string{},
|
2019-11-14 15:40:05 +00:00
|
|
|
Service: "serviceHTTP",
|
2019-07-12 23:24:03 +00:00
|
|
|
Rule: "PathPrefix(`/`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-11-14 15:40:05 +00:00
|
|
|
"serviceHTTP": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-12 23:24:03 +00:00
|
|
|
Servers: []dynamic.Server{
|
|
|
|
{
|
2021-11-25 10:10:06 +00:00
|
|
|
URL: "http://" + s.whoamiAddr,
|
2019-07-12 23:24:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-03 10:32:05 +00:00
|
|
|
},
|
2019-07-12 23:24:03 +00:00
|
|
|
ruleMatch: "PathPrefix(`/`)",
|
2018-12-03 10:32:05 +00:00
|
|
|
},
|
2019-07-12 23:24:03 +00:00
|
|
|
{
|
|
|
|
desc: "deploy tcp configuration",
|
|
|
|
config: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{
|
2019-11-14 15:40:05 +00:00
|
|
|
"routerTCP": {
|
2019-07-12 23:24:03 +00:00
|
|
|
EntryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
Service: "serviceTCP",
|
2019-07-12 23:24:03 +00:00
|
|
|
Rule: "HostSNI(`*`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Services: map[string]*dynamic.TCPService{
|
2019-11-14 15:40:05 +00:00
|
|
|
"serviceTCP": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-12 23:24:03 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
|
|
|
{
|
2021-11-25 10:10:06 +00:00
|
|
|
Address: s.whoamiAddr,
|
2019-07-12 23:24:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-03 10:32:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-12 23:24:03 +00:00
|
|
|
ruleMatch: "HostSNI(`*`)",
|
2018-12-03 10:32:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-12 23:24:03 +00:00
|
|
|
for _, test := range testCase {
|
2019-11-14 15:40:05 +00:00
|
|
|
data, err := json.Marshal(test.config)
|
2019-07-12 23:24:03 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2018-12-03 10:32:05 +00:00
|
|
|
|
2019-11-14 15:40:05 +00:00
|
|
|
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/api/providers/rest", bytes.NewReader(data))
|
2019-07-12 23:24:03 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2018-12-03 10:32:05 +00:00
|
|
|
|
2019-07-12 23:24:03 +00:00
|
|
|
response, err := http.DefaultClient.Do(request)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(response.StatusCode, checker.Equals, http.StatusOK)
|
2018-12-03 10:32:05 +00:00
|
|
|
|
2019-11-14 15:40:05 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 3*time.Second, try.BodyContains(test.ruleMatch))
|
2019-07-12 23:24:03 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2018-12-03 10:32:05 +00:00
|
|
|
|
2019-07-12 23:24:03 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
2018-12-03 10:32:05 +00:00
|
|
|
}
|
2019-09-06 13:08:04 +00:00
|
|
|
|
|
|
|
func (s *RestSuite) TestSimpleConfiguration(c *check.C) {
|
|
|
|
file := s.adaptFile(c, "fixtures/rest/simple_secure.toml", struct{}{})
|
|
|
|
defer os.Remove(file)
|
|
|
|
|
|
|
|
cmd, display := s.traefikCmd(withConfigFile(file))
|
|
|
|
|
|
|
|
defer display(c)
|
|
|
|
err := cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
2020-10-09 07:32:03 +00:00
|
|
|
defer s.killCmd(cmd)
|
2019-09-06 13:08:04 +00:00
|
|
|
|
|
|
|
// Expected a 404 as we did not configure anything.
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 2000*time.Millisecond, try.BodyContains("PathPrefix(`/secure`)"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/api/providers/rest", strings.NewReader("{}"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(request)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(response.StatusCode, checker.Equals, http.StatusNotFound)
|
|
|
|
|
|
|
|
testCase := []struct {
|
|
|
|
desc string
|
|
|
|
config *dynamic.Configuration
|
|
|
|
ruleMatch string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "deploy http configuration",
|
|
|
|
config: &dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
|
|
|
"router1": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Middlewares: []string{},
|
|
|
|
Service: "service1",
|
|
|
|
Rule: "PathPrefix(`/`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Services: map[string]*dynamic.Service{
|
|
|
|
"service1": {
|
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
|
|
|
Servers: []dynamic.Server{
|
|
|
|
{
|
2021-11-25 10:10:06 +00:00
|
|
|
URL: "http://" + s.whoamiAddr,
|
2019-09-06 13:08:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ruleMatch: "PathPrefix(`/`)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "deploy tcp configuration",
|
|
|
|
config: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{
|
|
|
|
"router1": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "service1",
|
|
|
|
Rule: "HostSNI(`*`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Services: map[string]*dynamic.TCPService{
|
|
|
|
"service1": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-09-06 13:08:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
|
|
|
{
|
2021-11-25 10:10:06 +00:00
|
|
|
Address: s.whoamiAddr,
|
2019-09-06 13:08:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ruleMatch: "HostSNI(`*`)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCase {
|
2021-03-04 08:02:03 +00:00
|
|
|
data, err := json.Marshal(test.config)
|
2019-09-06 13:08:04 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2021-03-04 08:02:03 +00:00
|
|
|
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8000/secure/api/providers/rest", bytes.NewReader(data))
|
2019-09-06 13:08:04 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(request)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(response.StatusCode, checker.Equals, http.StatusOK)
|
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", time.Second, try.BodyContains(test.ruleMatch))
|
2019-09-06 13:08:04 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
err = try.GetRequest("http://127.0.0.1:8000/", time.Second, try.StatusCodeIs(http.StatusOK))
|
2019-09-06 13:08:04 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
}
|