From 65529fe6e9460748b60fdf0f4be601ae1e432adf Mon Sep 17 00:00:00 2001 From: Paco Xu Date: Mon, 13 Dec 2021 16:50:32 +0800 Subject: [PATCH 1/2] add probe warning event body like 'Probe terminated redirects' --- pkg/probe/http/http.go | 4 ++++ test/e2e/common/node/container_probe.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/probe/http/http.go b/pkg/probe/http/http.go index ad034ee3fa4..e1d97f1e2bc 100644 --- a/pkg/probe/http/http.go +++ b/pkg/probe/http/http.go @@ -129,6 +129,10 @@ func DoHTTPProbe(url *url.URL, headers http.Header, client GetHTTPInterface) (pr if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest { if res.StatusCode >= http.StatusMultipleChoices { // Redirect klog.V(4).Infof("Probe terminated redirects for %s, Response: %v", url.String(), *res) + // add some message in case body is empty when redirect is terminated + if len(body) == 0 { + return probe.Warning, "Probe terminated redirects", nil + } return probe.Warning, body, nil } klog.V(4).Infof("Probe succeeded for %s, Response: %v", url.String(), *res) diff --git a/test/e2e/common/node/container_probe.go b/test/e2e/common/node/container_probe.go index 14ec8956c6d..b9cbf3d66f2 100644 --- a/test/e2e/common/node/container_probe.go +++ b/test/e2e/common/node/container_probe.go @@ -310,7 +310,7 @@ var _ = SIGDescribe("Probing container", func() { "reason": events.ContainerProbeWarning, }.AsSelector().String() framework.ExpectNoError(e2eevents.WaitTimeoutForEvent( - f.ClientSet, f.Namespace.Name, expectedEvent, "0.0.0.0", framework.PodEventTimeout)) + f.ClientSet, f.Namespace.Name, expectedEvent, "Probe terminated redirects, Response body: Found.", framework.PodEventTimeout)) }) /* From 3c06ef9c0c425743913df9be5bddaa86becbf0ba Mon Sep 17 00:00:00 2001 From: Paco Xu Date: Wed, 15 Dec 2021 17:33:19 +0800 Subject: [PATCH 2/2] probe: add test case for redirect terminating cases --- pkg/probe/http/http.go | 6 +----- pkg/probe/http/http_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/probe/http/http.go b/pkg/probe/http/http.go index e1d97f1e2bc..b6cc57d512d 100644 --- a/pkg/probe/http/http.go +++ b/pkg/probe/http/http.go @@ -129,11 +129,7 @@ func DoHTTPProbe(url *url.URL, headers http.Header, client GetHTTPInterface) (pr if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest { if res.StatusCode >= http.StatusMultipleChoices { // Redirect klog.V(4).Infof("Probe terminated redirects for %s, Response: %v", url.String(), *res) - // add some message in case body is empty when redirect is terminated - if len(body) == 0 { - return probe.Warning, "Probe terminated redirects", nil - } - return probe.Warning, body, nil + return probe.Warning, fmt.Sprintf("Probe terminated redirects, Response body: %v", body), nil } klog.V(4).Infof("Probe succeeded for %s, Response: %v", url.String(), *res) return probe.Success, body, nil diff --git a/pkg/probe/http/http_test.go b/pkg/probe/http/http_test.go index fd2bab75878..8f30354da95 100644 --- a/pkg/probe/http/http_test.go +++ b/pkg/probe/http/http_test.go @@ -139,6 +139,17 @@ func TestHTTPProbeChecker(t *testing.T) { } } + redirectHandlerWithBody := func(s int, body string) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + http.Redirect(w, r, "/new", s) + } else if r.URL.Path == "/new" { + w.WriteHeader(s) + w.Write([]byte(body)) + } + } + } + followNonLocalRedirects := true prober := New(followNonLocalRedirects) testCases := []struct { @@ -336,6 +347,16 @@ func TestHTTPProbeChecker(t *testing.T) { handler: redirectHandler(http.StatusPermanentRedirect, true), // 308 health: probe.Failure, }, + { + handler: redirectHandlerWithBody(http.StatusPermanentRedirect, ""), // redirect with empty body + health: probe.Warning, + accBody: "Probe terminated redirects, Response body:", + }, + { + handler: redirectHandlerWithBody(http.StatusPermanentRedirect, "ok body"), // redirect with body + health: probe.Warning, + accBody: "Probe terminated redirects, Response body: ok body", + }, } for i, test := range testCases { t.Run(fmt.Sprintf("case-%2d", i), func(t *testing.T) {