github.com/minio/madmin-go/v3@v3.0.51/metrics_client_test.go (about) 1 // 2 // Copyright (c) 2015-2023 MinIO, Inc. 3 // 4 // This file is part of MinIO Object Storage stack 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU Affero General Public License as 8 // published by the Free Software Foundation, either version 3 of the 9 // License, or (at your option) any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU Affero General Public License for more details. 15 // 16 // You should have received a copy of the GNU Affero General Public License 17 // along with this program. If not, see <http://www.gnu.org/licenses/>. 18 // 19 20 package madmin 21 22 import ( 23 "fmt" 24 "net/url" 25 "testing" 26 27 jwtgo "github.com/golang-jwt/jwt/v4" 28 ) 29 30 func TestMakeTargetUrlBuildsURLWithClientAndRelativePath(t *testing.T) { 31 clnt := MetricsClient{ 32 endpointURL: &url.URL{ 33 Host: "localhost:9000", 34 Scheme: "http", 35 }, 36 } 37 requestData := metricsRequestData{ 38 relativePath: "/some/path", 39 } 40 41 targetURL, err := clnt.makeTargetURL(requestData) 42 if err != nil { 43 t.Errorf("error not expected, got: %v", err) 44 } 45 46 expectedURL := "http://localhost:9000/minio/some/path" 47 if expectedURL != targetURL.String() { 48 t.Errorf("target url: %s not equal to expected url: %s", targetURL, expectedURL) 49 } 50 } 51 52 func TestMakeTargetUrlReturnsErrorIfEndpointURLNotSet(t *testing.T) { 53 clnt := MetricsClient{} 54 requestData := metricsRequestData{ 55 relativePath: "/some/path", 56 } 57 58 _, err := clnt.makeTargetURL(requestData) 59 if err == nil { 60 t.Errorf("error expected got nil") 61 } 62 } 63 64 func TestMakeTargetUrlReturnsErrorOnURLParse(t *testing.T) { 65 clnt := MetricsClient{ 66 endpointURL: &url.URL{}, 67 } 68 requestData := metricsRequestData{ 69 relativePath: "/some/path", 70 } 71 72 _, err := clnt.makeTargetURL(requestData) 73 if err == nil { 74 t.Errorf("error expected got nil") 75 } 76 } 77 78 func TestGetPrometheusTokenReturnsValidJwtTokenFromAccessAndSecretKey(t *testing.T) { 79 accessKey := "myaccessKey" 80 secretKey := "mysecretKey" 81 82 jwtToken, err := getPrometheusToken(accessKey, secretKey) 83 if err != nil { 84 t.Errorf("error not expected, got: %v", err) 85 } 86 87 token, err := jwtgo.Parse(jwtToken, func(token *jwtgo.Token) (interface{}, error) { 88 // Set same signing method used in our function 89 if _, ok := token.Method.(*jwtgo.SigningMethodHMAC); !ok { 90 return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) 91 } 92 return []byte(secretKey), nil 93 }) 94 if err != nil { 95 t.Errorf("error not expected, got: %v", err) 96 } 97 if !token.Valid { 98 t.Errorf("invalid token: %s", jwtToken) 99 } 100 }