Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(customheaders): add test for annotation added #12479

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/ingress/annotations/customheaders/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package customheaders

import (
"fmt"
"reflect"
"regexp"

"k8s.io/klog/v2"
Expand All @@ -35,6 +36,18 @@ type Config struct {
Headers map[string]string `json:"headers,omitempty"`
}

// Equal tests for equality between two Config types
func (c1 *Config) Equal(c2 *Config) bool {
if c1 == c2 {
return true
}
if c1 == nil || c2 == nil {
return false
}

return reflect.DeepEqual(c1.Headers, c2.Headers)
}

var (
headerRegexp = regexp.MustCompile(`^[a-zA-Z\d\-_]+$`)
valueRegexp = regexp.MustCompile(`^[a-zA-Z\d_ :;.,\\/"'?!(){}\[\]@<>=\-+*#$&\x60|~^%]+$`)
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/ingress/types_equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ func (l1 *Location) Equal(l2 *Location) bool {
return false
}

if !l1.CustomHeaders.Equal(&l2.CustomHeaders) {
return false
}

return true
}

Expand Down
49 changes: 49 additions & 0 deletions test/e2e/annotations/customheaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,53 @@ var _ = framework.DescribeAnnotation("custom-headers-*", func() {
Status(http.StatusOK).
Header("My-Custom-Header-Dollar").Contains("$remote_addr")
})

ginkgo.It(`should set "more_set_headers 'My-Custom-Header' '42';" when custom-headers annotation is added`, func() {
f.CreateConfigMap("custom-headers", map[string]string{
"My-Custom-Header": "42",
"My-Custom-Header-Dollar": "$remote_addr",
})
f.UpdateNginxConfigMapData("global-allowed-response-headers", "My-Custom-Header,My-Custom-Header-Dollar")

annotations := map[string]string{}
ing := framework.NewSingleIngress(customHeaderHost, "/", customHeaderHost, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(customHeaderHost,
func(server string) bool {
return strings.Contains(server, "server_name custom-headers")
})

f.HTTPTestClient().
GET("/").
WithHeader("Host", customHeaderHost).
Expect().
Status(http.StatusOK).
Header("My-Custom-Header").Equal("")

annotations = map[string]string{
"nginx.ingress.kubernetes.io/custom-headers": f.Namespace + "/custom-headers",
}

ing.Annotations = annotations

f.UpdateIngress(ing)
f.WaitForNginxServer(customHeaderHost,
func(server string) bool {
return strings.Contains(server, `more_set_headers "My-Custom-Header: 42";`)
})

f.HTTPTestClient().
GET("/").
WithHeader("Host", customHeaderHost).
Expect().
Status(http.StatusOK).
Header("My-Custom-Header").Contains("42")
f.HTTPTestClient().
GET("/").
WithHeader("Host", customHeaderHost).
Expect().
Status(http.StatusOK).
Header("My-Custom-Header-Dollar").Contains("$remote_addr")
})
})
7 changes: 6 additions & 1 deletion test/manifests/configuration-a.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,12 @@
"validationDepth": 0
},
"use-port-in-redirects": false,
"configuration-snippet": ""
"configuration-snippet": "",
"customHeaders": {
"headers": {
"Server": "HAL9000"
}
}
}]
}, {
"hostname": "dev.mycompany.com",
Expand Down
7 changes: 6 additions & 1 deletion test/manifests/configuration-b.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,12 @@
"validationDepth": 0
},
"use-port-in-redirects": false,
"configuration-snippet": ""
"configuration-snippet": "",
"customHeaders": {
"headers": {
"Server": "HAL9000"
}
}
}]
}, {
"hostname": "dev.mycompany.com",
Expand Down
Loading