From 759ae9c3ca4fed6bc1e097f1fe62dd726ba93341 Mon Sep 17 00:00:00 2001 From: Shilpa-Gokul Date: Tue, 27 Feb 2024 15:11:33 +0530 Subject: [PATCH 1/2] Add function back to execute commands via pod --- test/extended/prometheus/prometheus.go | 4 ++-- test/extended/util/prometheus/helpers.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/test/extended/prometheus/prometheus.go b/test/extended/prometheus/prometheus.go index 8f6d721b5545..f469962288f7 100644 --- a/test/extended/prometheus/prometheus.go +++ b/test/extended/prometheus/prometheus.go @@ -377,9 +377,9 @@ var _ = g.Describe("[sig-instrumentation] Prometheus [apigroup:image.openshift.i } return false, nil })).NotTo(o.HaveOccurred(), fmt.Sprintf("Did not find tsdb_samples_appended_total, tsdb_head_samples_appended_total, or prometheus_tsdb_head_samples_appended_total")) - + g.By("verifying the Thanos querier service requires authentication") - err := helper.ExpectHTTPStatusCode(querySvcURL, "", 401, 403) + err := helper.ExpectURLStatusCodeExecViaPod(ns, execPod.Name, querySvcURL, 401, 403) o.Expect(err).NotTo(o.HaveOccurred()) g.By("verifying a service account token is able to authenticate") diff --git a/test/extended/util/prometheus/helpers.go b/test/extended/util/prometheus/helpers.go index 3a0ea837626a..4fbbeb57f216 100644 --- a/test/extended/util/prometheus/helpers.go +++ b/test/extended/util/prometheus/helpers.go @@ -6,8 +6,10 @@ import ( "encoding/json" "fmt" "io" + e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output" "net/http" "net/url" + "strconv" "strings" "time" @@ -345,6 +347,23 @@ func ExpectHTTPStatusCode(url, bearerToken string, statusCodes ...int) error { return fmt.Errorf("%s: last response from server was not in %v: %d", url, statusCodes, resp.StatusCode) } +// ExpectURLStatusCodeExecViaPod attempts connection to url via exec pod and returns an error +// upon failure or if status return code is not equal to any of the statusCodes. +func ExpectURLStatusCodeExecViaPod(ns, execPodName, url string, statusCodes ...int) error { + cmd := fmt.Sprintf("curl -k -s -o /dev/null -w '%%{http_code}' %q", url) + output, err := e2eoutput.RunHostCmd(ns, execPodName, cmd) + if err != nil { + return fmt.Errorf("host command failed: %v\n%s", err, output) + } + for _, statusCode := range statusCodes { + if output == strconv.Itoa(statusCode) { + return nil + } + } + + return fmt.Errorf("last response from server was not in %v: %s", statusCodes, output) +} + // ExpectPrometheusEndpoint attempts to connect to the metrics endpoint with // delayed retries upon failure. func ExpectPrometheusEndpoint(url string) { From 6e6699af85d62ff24001f9f5b4bec0719770b96e Mon Sep 17 00:00:00 2001 From: Shilpa-Gokul Date: Wed, 28 Feb 2024 11:08:33 +0530 Subject: [PATCH 2/2] Apply go fmt --- test/extended/prometheus/prometheus.go | 2 +- test/extended/util/prometheus/helpers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extended/prometheus/prometheus.go b/test/extended/prometheus/prometheus.go index f469962288f7..0f13f193fde2 100644 --- a/test/extended/prometheus/prometheus.go +++ b/test/extended/prometheus/prometheus.go @@ -377,7 +377,7 @@ var _ = g.Describe("[sig-instrumentation] Prometheus [apigroup:image.openshift.i } return false, nil })).NotTo(o.HaveOccurred(), fmt.Sprintf("Did not find tsdb_samples_appended_total, tsdb_head_samples_appended_total, or prometheus_tsdb_head_samples_appended_total")) - + g.By("verifying the Thanos querier service requires authentication") err := helper.ExpectURLStatusCodeExecViaPod(ns, execPod.Name, querySvcURL, 401, 403) o.Expect(err).NotTo(o.HaveOccurred()) diff --git a/test/extended/util/prometheus/helpers.go b/test/extended/util/prometheus/helpers.go index 4fbbeb57f216..f96153bbd7f6 100644 --- a/test/extended/util/prometheus/helpers.go +++ b/test/extended/util/prometheus/helpers.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output" "net/http" "net/url" "strconv" @@ -29,6 +28,7 @@ import ( clientset "k8s.io/client-go/kubernetes" watchtools "k8s.io/client-go/tools/watch" "k8s.io/kubernetes/test/e2e/framework" + e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output" ) const (