From a35cbb3ff3cc444e7c16803c26025b02093229e9 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 22 Jan 2021 11:04:52 -0800 Subject: [PATCH] update healthcheck tests for new prometheus endpoint (#11333) --- mint/run/core/healthcheck/main.go | 61 ++++++++++++++++++++++++++++--- mint/run/core/minio-go/go.mod | 2 +- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/mint/run/core/healthcheck/main.go b/mint/run/core/healthcheck/main.go index b69751d7b..917b6ebbf 100644 --- a/mint/run/core/healthcheck/main.go +++ b/mint/run/core/healthcheck/main.go @@ -33,12 +33,13 @@ import ( ) const ( - pass = "PASS" // Indicate that a test passed - fail = "FAIL" // Indicate that a test failed - livenessPath = "/minio/health/live" - readinessPath = "/minio/health/ready" - prometheusPath = "/minio/prometheus/metrics" - timeout = time.Duration(30 * time.Second) + pass = "PASS" // Indicate that a test passed + fail = "FAIL" // Indicate that a test failed + livenessPath = "/minio/health/live" + readinessPath = "/minio/health/ready" + prometheusPath = "/minio/prometheus/metrics" + prometheusPathV2 = "/minio/v2/metrics/cluster" + timeout = time.Duration(30 * time.Second) ) type mintJSONFormatter struct { @@ -196,6 +197,53 @@ func testPrometheusEndpoint(endpoint string) { defer successLogger(function, nil, startTime).Info() } +func testPrometheusEndpointV2(endpoint string) { + startTime := time.Now() + function := "testPrometheusEndpoint" + + u, err := url.Parse(fmt.Sprintf("%s%s", endpoint, prometheusPathV2)) + if err != nil { + // Could not parse URL successfully + failureLog(function, nil, startTime, "", "URL Parsing for Healthcheck Prometheus handler failed", err).Fatal() + } + + jwt := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, jwtgo.StandardClaims{ + ExpiresAt: time.Now().UTC().Add(defaultPrometheusJWTExpiry).Unix(), + Subject: os.Getenv("ACCESS_KEY"), + Issuer: "prometheus", + }) + + token, err := jwt.SignedString([]byte(os.Getenv("SECRET_KEY"))) + if err != nil { + failureLog(function, nil, startTime, "", "jwt generation failed", err).Fatal() + } + + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: u.Scheme == "https"}, + } + client := &http.Client{Transport: tr, Timeout: timeout} + + req, err := http.NewRequest(http.MethodGet, u.String(), nil) + if err != nil { + failureLog(function, nil, startTime, "", "Initializing GET request to Prometheus endpoint failed", err).Fatal() + } + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) + + resp, err := client.Do(req) + if err != nil { + // GET request errored + failureLog(function, nil, startTime, "", "GET request to Prometheus endpoint failed", err).Fatal() + } + + if resp.StatusCode != http.StatusOK { + // Status not 200 OK + failureLog(function, nil, startTime, "", "GET /minio/prometheus/metrics returned non OK status", err).Fatal() + } + + defer resp.Body.Close() + defer successLogger(function, nil, startTime).Info() +} + func main() { endpoint := os.Getenv("SERVER_ENDPOINT") secure := os.Getenv("ENABLE_HTTPS") @@ -217,4 +265,5 @@ func main() { testLivenessEndpoint(endpoint) testReadinessEndpoint(endpoint) testPrometheusEndpoint(endpoint) + testPrometheusEndpointV2(endpoint) } diff --git a/mint/run/core/minio-go/go.mod b/mint/run/core/minio-go/go.mod index 0b54bef88..728764959 100644 --- a/mint/run/core/minio-go/go.mod +++ b/mint/run/core/minio-go/go.mod @@ -2,4 +2,4 @@ module mint.minio.io/minio-go go 1.14 -require github.com/minio/minio-go/v7 v7.0.6 // indirect +require github.com/minio/minio-go/v7 v7.0.7 // indirect