Skip to content

Commit ec239c6

Browse files
committed
Add e2e tests for custom-error-pages
Signed-off-by: Ricardo Lopes <ricardoapl.dev@gmail.com>
1 parent 9e56860 commit ec239c6

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

docs/e2e-tests.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ Do not try to edit it manually.
283283
- [should return a self generated SSL certificate](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/defaultbackend/ssl.go#L29)
284284
### [[Default Backend] change default settings](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/defaultbackend/with_hosts.go#L30)
285285
- [should apply the annotation to the default backend](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/defaultbackend/with_hosts.go#L38)
286+
### [[Default Backend] custom-error-pages](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/defaultbackend/custom_error_pages.go#L33)
287+
- [should export /metrics and /debug/vars by default](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/defaultbackend/custom_error_pages.go#L36)
288+
- [shouldn't export /metrics and /debug/vars when IS_METRICS_EXPORT is set to false](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/defaultbackend/custom_error_pages.go#L57)
289+
- [shouldn't export /metrics and /debug/vars when METRICS_PORT is set to a different port](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/defaultbackend/custom_error_pages.go#L80)
286290
### [[Disable Leader] Routing works when leader election was disabled](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/disableleaderelection/disable_leader.go#L28)
287291
- [should create multiple ingress routings rules when leader election has disabled](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/disableleaderelection/disable_leader.go#L35)
288292
### [[Endpointslices] long service name](https://github.com/kubernetes/ingress-nginx/tree/main//test/e2e/endpointslices/longname.go#L29)

test/e2e/CUSTOMERRORPAGES_IMAGE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
localhost/custom-error-pages:e2e
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package defaultbackend
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"net/http"
23+
"strings"
24+
25+
"github.com/onsi/ginkgo/v2"
26+
"github.com/stretchr/testify/assert"
27+
appsv1 "k8s.io/api/apps/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
30+
"k8s.io/ingress-nginx/test/e2e/framework"
31+
)
32+
33+
var _ = framework.IngressNginxDescribe("[Default Backend] custom-error-pages", func() {
34+
f := framework.NewDefaultFramework("custom-error-pages")
35+
36+
ginkgo.It("should export /metrics and /debug/vars by default", func() {
37+
tt := []struct {
38+
Name string
39+
Path string
40+
Status int
41+
}{
42+
{"request to /metrics should return HTTP 200", "/metrics", http.StatusOK},
43+
{"request to /debug/vars should return HTTP 200", "/debug/vars", http.StatusOK},
44+
}
45+
46+
setupIngressControllerWithCustomErrorPages(f, nil)
47+
48+
for _, t := range tt {
49+
ginkgo.By(t.Name)
50+
f.HTTPTestClient().
51+
GET(t.Path).
52+
Expect().
53+
Status(t.Status)
54+
}
55+
})
56+
57+
ginkgo.It("shouldn't export /metrics and /debug/vars when IS_METRICS_EXPORT is set to false", func() {
58+
tt := []struct {
59+
Name string
60+
Path string
61+
Status int
62+
}{
63+
{"request to /metrics should return HTTP 404", "/metrics", http.StatusNotFound},
64+
{"request to /debug/vars should return HTTP 404", "/debug/vars", http.StatusNotFound},
65+
}
66+
67+
setupIngressControllerWithCustomErrorPages(f, map[string]string{
68+
"IS_METRICS_EXPORT": "false",
69+
})
70+
71+
for _, t := range tt {
72+
ginkgo.By(t.Name)
73+
f.HTTPTestClient().
74+
GET(t.Path).
75+
Expect().
76+
Status(t.Status)
77+
}
78+
})
79+
80+
ginkgo.It("shouldn't export /metrics and /debug/vars when METRICS_PORT is set to a different port", func() {
81+
tt := []struct {
82+
Name string
83+
Path string
84+
Status int
85+
}{
86+
{"request to /metrics should return HTTP 404", "/metrics", http.StatusNotFound},
87+
{"request to /debug/vars should return HTTP 404", "/debug/vars", http.StatusNotFound},
88+
}
89+
90+
setupIngressControllerWithCustomErrorPages(f, map[string]string{
91+
"IS_METRICS_EXPORT": "true",
92+
"METRICS_PORT": "8081",
93+
})
94+
95+
for _, t := range tt {
96+
ginkgo.By(t.Name)
97+
f.HTTPTestClient().
98+
GET(t.Path).
99+
Expect().
100+
Status(t.Status)
101+
}
102+
})
103+
})
104+
105+
func setupIngressControllerWithCustomErrorPages(f *framework.Framework, envVars map[string]string) {
106+
f.NewCustomErrorPagesDeployment(framework.WithEnvVars(envVars))
107+
108+
err := f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error {
109+
args := deployment.Spec.Template.Spec.Containers[0].Args
110+
args = append(args, fmt.Sprintf("--default-backend-service=%v/%v", f.Namespace, framework.CustomErrorPagesService))
111+
deployment.Spec.Template.Spec.Containers[0].Args = args
112+
_, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
113+
return err
114+
})
115+
assert.Nil(ginkgo.GinkgoT(), err, "updating deployment")
116+
117+
f.WaitForNginxServer("_",
118+
func(server string) bool {
119+
return strings.Contains(server, `set $proxy_upstream_name "upstream-default-backend"`)
120+
})
121+
}

test/e2e/framework/deployment.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ var HTTPBunImage = os.Getenv("HTTPBUN_IMAGE")
4949
// EchoImage is the default image to be used by the echo service
5050
const EchoImage = "registry.k8s.io/ingress-nginx/e2e-test-echo:v1.0.1@sha256:1cec65aa768720290d05d65ab1c297ca46b39930e56bc9488259f9114fcd30e2" //#nosec G101
5151

52+
// CustomErrorPagesService name of the deployment for the custom-error-pages app
53+
const CustomErrorPagesService = "custom-error-pages"
54+
55+
// CustomErrorPagesImage is the default image that is used to deploy custom-error-pages with the framework
56+
var CustomErrorPagesImage = os.Getenv("CUSTOMERRORPAGES_IMAGE")
57+
5258
// TODO: change all Deployment functions to use these options
5359
// in order to reduce complexity and have a unified API across the
5460
// framework
@@ -58,6 +64,7 @@ type deploymentOptions struct {
5864
image string
5965
replicas int
6066
svcAnnotations map[string]string
67+
envVars map[string]string
6168
}
6269

6370
// WithDeploymentNamespace allows configuring the deployment's namespace
@@ -103,6 +110,74 @@ func WithImage(i string) func(*deploymentOptions) {
103110
}
104111
}
105112

113+
// WithEnvVars allows configuring environment variables for the deployment
114+
func WithEnvVars(e map[string]string) func(*deploymentOptions) {
115+
return func(o *deploymentOptions) {
116+
o.envVars = e
117+
}
118+
}
119+
120+
// NewCustomErrorPagesDeployment creates a new single replica deployment of the custom-error-pages server image in a particular namespace
121+
func (f *Framework) NewCustomErrorPagesDeployment(opts ...func(*deploymentOptions)) {
122+
options := &deploymentOptions{
123+
namespace: f.Namespace,
124+
name: CustomErrorPagesService,
125+
replicas: 1,
126+
image: CustomErrorPagesImage,
127+
}
128+
for _, o := range opts {
129+
o(options)
130+
}
131+
132+
envVars := []corev1.EnvVar{}
133+
for k, v := range options.envVars {
134+
envVars = append(envVars, corev1.EnvVar{Name: k, Value: v})
135+
}
136+
137+
f.EnsureDeployment(newDeployment(
138+
options.name,
139+
options.namespace,
140+
options.image,
141+
8080,
142+
int32(options.replicas),
143+
nil, nil,
144+
envVars,
145+
[]corev1.VolumeMount{},
146+
[]corev1.Volume{},
147+
false,
148+
))
149+
150+
f.EnsureService(&corev1.Service{
151+
ObjectMeta: metav1.ObjectMeta{
152+
Name: options.name,
153+
Namespace: options.namespace,
154+
Annotations: options.svcAnnotations,
155+
},
156+
Spec: corev1.ServiceSpec{
157+
Ports: []corev1.ServicePort{
158+
{
159+
Name: "http",
160+
Port: 8080,
161+
TargetPort: intstr.FromInt(8080),
162+
Protocol: corev1.ProtocolTCP,
163+
},
164+
},
165+
Selector: map[string]string{
166+
"app": options.name,
167+
},
168+
},
169+
})
170+
171+
err := WaitForEndpoints(
172+
f.KubeClientSet,
173+
DefaultTimeout,
174+
options.name,
175+
options.namespace,
176+
options.replicas,
177+
)
178+
assert.Nil(ginkgo.GinkgoT(), err, "waiting for endpoints to become ready")
179+
}
180+
106181
// NewEchoDeployment creates a new single replica deployment of the echo server image in a particular namespace
107182
func (f *Framework) NewEchoDeployment(opts ...func(*deploymentOptions)) {
108183
options := &deploymentOptions{

test/e2e/run-e2e-suite.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fi
5252
BASEDIR=$(dirname "$0")
5353
NGINX_BASE_IMAGE=$(cat $BASEDIR/../../NGINX_BASE)
5454
HTTPBUN_IMAGE=$(cat $BASEDIR/HTTPBUN_IMAGE)
55+
CUSTOMERRORPAGES_IMAGE=$(cat $BASEDIR/CUSTOMERRORPAGES_IMAGE)
5556

5657
echo -e "${BGREEN}Granting permissions to ingress-nginx e2e service account...${NC}"
5758
kubectl create serviceaccount ingress-nginx-e2e || true
@@ -82,6 +83,7 @@ kubectl run --rm \
8283
--env="E2E_CHECK_LEAKS=${E2E_CHECK_LEAKS}" \
8384
--env="NGINX_BASE_IMAGE=${NGINX_BASE_IMAGE}" \
8485
--env="HTTPBUN_IMAGE=${HTTPBUN_IMAGE}" \
86+
--env="CUSTOMERRORPAGES_IMAGE=${CUSTOMERRORPAGES_IMAGE}" \
8587
--overrides='{ "apiVersion": "v1", "spec":{"serviceAccountName": "ingress-nginx-e2e"}}' \
8688
e2e --image=nginx-ingress-controller:e2e
8789

test/e2e/run-kind-e2e.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export KUBECONFIG="${KUBECONFIG:-$HOME/.kube/kind-config-$KIND_CLUSTER_NAME}"
5252
SKIP_INGRESS_IMAGE_CREATION="${SKIP_INGRESS_IMAGE_CREATION:-false}"
5353
SKIP_E2E_IMAGE_CREATION="${SKIP_E2E_IMAGE_CREATION:=false}"
5454
SKIP_CLUSTER_CREATION="${SKIP_CLUSTER_CREATION:-false}"
55+
SKIP_CUSTOMERRORPAGES_IMAGE_CREATION="${SKIP_CUSTOMERRORPAGES_IMAGE_CREATION:-false}"
5556

5657
if ! command -v kind --version &> /dev/null; then
5758
echo "kind is not installed. Use the package manager or visit the official site https://kind.sigs.k8s.io/"
@@ -104,12 +105,19 @@ if [ "${SKIP_E2E_IMAGE_CREATION}" = "false" ]; then
104105
echo "[dev-env] ..done building e2e-image"
105106
fi
106107

108+
if [ "${SKIP_CUSTOMERRORPAGES_IMAGE_CREATION}" = "false" ]; then
109+
echo "[dev-env] building custom-error-pages image"
110+
REGISTRY=localhost NAME=custom-error-pages TAG=e2e make -C "${DIR}"/../../images build
111+
echo "[dev-env] .. done building custom-error-pages image"
112+
fi
113+
107114
# Preload images used in e2e tests
108115
KIND_WORKERS=$(kind get nodes --name="${KIND_CLUSTER_NAME}" | grep worker | awk '{printf (NR>1?",":"") $1}')
109116

110117
echo "[dev-env] copying docker images to cluster..."
111118

112119
kind load docker-image --name="${KIND_CLUSTER_NAME}" --nodes="${KIND_WORKERS}" nginx-ingress-controller:e2e
113120
kind load docker-image --name="${KIND_CLUSTER_NAME}" --nodes="${KIND_WORKERS}" "${REGISTRY}"/controller:"${TAG}"
121+
kind load docker-image --name="${KIND_CLUSTER_NAME}" --nodes="${KIND_WORKERS}" "localhost/custom-error-pages:e2e"
114122
echo "[dev-env] running e2e tests..."
115123
make -C "${DIR}"/../../ e2e-test

0 commit comments

Comments
 (0)