Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SantoDE committed Apr 11, 2024
1 parent 1de5f6f commit 95fd2e0
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
kind: GatewayClass
apiVersion: gateway.networking.k8s.io/v1
metadata:
name: my-gateway-class
spec:
controllerName: traefik.io/gateway-controller

---
kind: Gateway
apiVersion: gateway.networking.k8s.io/v1
metadata:
name: my-gateway
namespace: default
spec:
gatewayClassName: my-gateway-class
listeners: # Use GatewayClass defaults for listener definition.
- name: http
protocol: HTTP
port: 80
allowedRoutes:
kinds:
- kind: HTTPRoute
group: gateway.networking.k8s.io
namespaces:
from: Same

---
kind: HTTPRoute
apiVersion: gateway.networking.k8s.io/v1
metadata:
name: http-app-1
namespace: default
spec:
parentRefs:
- name: my-gateway
kind: Gateway
group: gateway.networking.k8s.io
hostnames:
- "example.com"
rules:
- matches:
- path:
type: PathPrefix
value: /foo
backendRefs:
- name: whoami
port: 80
weight: 1
kind: Service
group: ""
filters:
- type: URLRewrite
urlRewrite:
hostname: www.foo.bar
path:
type: ReplacePrefixMatch
replacePrefixMatch: /xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
kind: GatewayClass
apiVersion: gateway.networking.k8s.io/v1
metadata:
name: my-gateway-class
spec:
controllerName: traefik.io/gateway-controller

---
kind: Gateway
apiVersion: gateway.networking.k8s.io/v1
metadata:
name: my-gateway
namespace: default
spec:
gatewayClassName: my-gateway-class
listeners: # Use GatewayClass defaults for listener definition.
- name: http
protocol: HTTP
port: 80
allowedRoutes:
kinds:
- kind: HTTPRoute
group: gateway.networking.k8s.io
namespaces:
from: Same

---
kind: HTTPRoute
apiVersion: gateway.networking.k8s.io/v1
metadata:
name: http-app-1
namespace: default
spec:
parentRefs:
- name: my-gateway
kind: Gateway
group: gateway.networking.k8s.io
hostnames:
- "example.com"
rules:
- matches:
- path:
type: PathPrefix
value: /foo
backendRefs:
- name: whoami
port: 80
weight: 1
kind: Service
group: ""
filters:
- type: URLRewrite
urlRewrite:
hostname: www.foo.bar
214 changes: 210 additions & 4 deletions pkg/provider/kubernetes/gateway/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1747,11 +1747,11 @@ func TestLoadHTTPRoutes(t *testing.T) {
Service: "default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-wrr",
Rule: "Host(`example.com`) && PathPrefix(`/foo`)",
RuleSyntax: "v3",
Middlewares: []string{"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-0"},
Middlewares: []string{"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-path-0"},
},
},
Middlewares: map[string]*dynamic.Middleware{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-0": {
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-path-0": {
ReplacePath: &dynamic.ReplacePath{
Path: "/bar",
},
Expand Down Expand Up @@ -1814,11 +1814,11 @@ func TestLoadHTTPRoutes(t *testing.T) {
Service: "default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-wrr",
Rule: "Host(`example.com`) && PathPrefix(`/foo`)",
RuleSyntax: "v3",
Middlewares: []string{"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-0"},
Middlewares: []string{"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-path-0"},
},
},
Middlewares: map[string]*dynamic.Middleware{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-0": {
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-path-0": {
ReplacePathRegex: &dynamic.ReplacePathRegex{
Regex: "^/foo",
Replacement: "/xyz",
Expand Down Expand Up @@ -1858,6 +1858,212 @@ func TestLoadHTTPRoutes(t *testing.T) {
TLS: &dynamic.TLSConfiguration{},
},
},
{
desc: "Simple HTTPRoute URL rewrite Hostname",
paths: []string{"services.yml", "httproute/filter_url_rewrite_hostname.yml"},
entryPoints: map[string]Entrypoint{"web": {
Address: ":80",
}},
expected: &dynamic.Configuration{
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7": {
EntryPoints: []string{"web"},
Service: "default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-wrr",
Rule: "Host(`example.com`) && PathPrefix(`/foo`)",
RuleSyntax: "v3",
Middlewares: []string{"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-host-0"},
},
},
Middlewares: map[string]*dynamic.Middleware{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-host-0": {
Headers: &dynamic.Headers{
CustomRequestHeaders: map[string]string{
"Host": "www.foo.bar",
},
CustomResponseHeaders: nil,
AccessControlAllowCredentials: false,
AccessControlAllowHeaders: nil,
AccessControlAllowMethods: nil,
AccessControlAllowOriginList: nil,
AccessControlAllowOriginListRegex: nil,
AccessControlExposeHeaders: nil,
AccessControlMaxAge: 0,
AddVaryHeader: false,
AllowedHosts: nil,
HostsProxyHeaders: nil,
SSLProxyHeaders: nil,
STSSeconds: 0,
STSIncludeSubdomains: false,
STSPreload: false,
ForceSTSHeader: false,
FrameDeny: false,
CustomFrameOptionsValue: "",
ContentTypeNosniff: false,
BrowserXSSFilter: false,
CustomBrowserXSSValue: "",
ContentSecurityPolicy: "",
PublicKey: "",
ReferrerPolicy: "",
PermissionsPolicy: "",
IsDevelopment: false,
FeaturePolicy: nil,
SSLRedirect: nil,
SSLTemporaryRedirect: nil,
SSLHost: nil,
SSLForceHost: nil,
},
},
},
Services: map[string]*dynamic.Service{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-wrr": {
Weighted: &dynamic.WeightedRoundRobin{
Services: []dynamic.WRRService{
{
Name: "default-whoami-80",
Weight: func(i int) *int { return &i }(1),
},
},
},
},
"default-whoami-80": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://10.10.0.1:80",
},
{
URL: "http://10.10.0.2:80",
},
},
PassHostHeader: ptr.To(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{},
},
},
{
desc: "Simple HTTPRoute URL rewrite Combined",
paths: []string{"services.yml", "httproute/filter_url_rewrite_combined.yml"},
entryPoints: map[string]Entrypoint{"web": {
Address: ":80",
}},
expected: &dynamic.Configuration{
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7": {
EntryPoints: []string{"web"},
Service: "default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-wrr",
Rule: "Host(`example.com`) && PathPrefix(`/foo`)",
RuleSyntax: "v3",
Middlewares: []string{"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-host-0", "default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-path-0"},
},
},
Middlewares: map[string]*dynamic.Middleware{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-path-0": {
ReplacePathRegex: &dynamic.ReplacePathRegex{
Regex: "^/foo",
Replacement: "/xyz",
},
},
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-urlrewrite-host-0": {
Headers: &dynamic.Headers{
CustomRequestHeaders: map[string]string{
"Host": "www.foo.bar",
},
CustomResponseHeaders: nil,
AccessControlAllowCredentials: false,
AccessControlAllowHeaders: nil,
AccessControlAllowMethods: nil,
AccessControlAllowOriginList: nil,
AccessControlAllowOriginListRegex: nil,
AccessControlExposeHeaders: nil,
AccessControlMaxAge: 0,
AddVaryHeader: false,
AllowedHosts: nil,
HostsProxyHeaders: nil,
SSLProxyHeaders: nil,
STSSeconds: 0,
STSIncludeSubdomains: false,
STSPreload: false,
ForceSTSHeader: false,
FrameDeny: false,
CustomFrameOptionsValue: "",
ContentTypeNosniff: false,
BrowserXSSFilter: false,
CustomBrowserXSSValue: "",
ContentSecurityPolicy: "",
PublicKey: "",
ReferrerPolicy: "",
PermissionsPolicy: "",
IsDevelopment: false,
FeaturePolicy: nil,
SSLRedirect: nil,
SSLTemporaryRedirect: nil,
SSLHost: nil,
SSLForceHost: nil,
},
},
},
Services: map[string]*dynamic.Service{
"default-http-app-1-my-gateway-web-76f78beeda6e3d06c2b7-wrr": {
Weighted: &dynamic.WeightedRoundRobin{
Services: []dynamic.WRRService{
{
Name: "default-whoami-80",
Weight: func(i int) *int { return &i }(1),
},
},
},
},
"default-whoami-80": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://10.10.0.1:80",
},
{
URL: "http://10.10.0.2:80",
},
},
PassHostHeader: ptr.To(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{},
},
},
}

for _, test := range testCases {
Expand Down

0 comments on commit 95fd2e0

Please sign in to comment.