From 880f9919804a30416ad0487a67032d1e0e727e5c Mon Sep 17 00:00:00 2001 From: asaf Date: Fri, 27 Oct 2023 01:31:39 +0300 Subject: [PATCH] Add unit test --- .../input/history/history_provider_test.go | 24 +++++++++++++++++++ .../pkg/recommender/main.go | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/vertical-pod-autoscaler/pkg/recommender/input/history/history_provider_test.go b/vertical-pod-autoscaler/pkg/recommender/input/history/history_provider_test.go index b66552faee1b..44e5f5498617 100644 --- a/vertical-pod-autoscaler/pkg/recommender/input/history/history_provider_test.go +++ b/vertical-pod-autoscaler/pkg/recommender/input/history/history_provider_test.go @@ -19,6 +19,8 @@ package history import ( "context" "fmt" + "net/http" + "os" "testing" "time" @@ -51,6 +53,9 @@ func getDefaultPrometheusHistoryProviderConfigForTest() PrometheusHistoryProvide CtrPodNameLabel: "pod_name", CtrNameLabel: "name", CadvisorMetricsJobName: "kubernetes-cadvisor", + Headers: map[string]string{ + "Token": "TEST_TOKEN", + }, } } @@ -257,6 +262,25 @@ func TestGetMemorySamples(t *testing.T) { assert.Equal(t, histories, map[model.PodID]*PodHistory{podID: podHistory}) } +func TestAuthHeader(t *testing.T) { + historyProvider, err := NewPrometheusHistoryProvider(getDefaultPrometheusHistoryProviderConfigForTest()) + if err != nil { + t.Fatal(err) + } + authHeaderName := "Token" + testToken := "aaaaaaaaaaaa" + tokenEnvVariableName := "TEST_TOKEN" + os.Setenv(tokenEnvVariableName, testToken) + + req, _ := http.NewRequest("GET", "http://example.com", nil) + + _, err = historyProvider.GetClusterHistory() + + assert.Nil(t, err) + assert.Equal(t, testToken, req.Header.Get(authHeaderName)) + defer os.Unsetenv(tokenEnvVariableName) +} + func TestGetNamespacedMemorySamples(t *testing.T) { mockClient := mockPrometheusAPI{} promConfig := getDefaultPrometheusHistoryProviderConfigForTest() diff --git a/vertical-pod-autoscaler/pkg/recommender/main.go b/vertical-pod-autoscaler/pkg/recommender/main.go index edf1e14a31ae..7ca20accf331 100644 --- a/vertical-pod-autoscaler/pkg/recommender/main.go +++ b/vertical-pod-autoscaler/pkg/recommender/main.go @@ -75,8 +75,8 @@ var ( password = flag.String("password", "", "The password used in the prometheus server basic auth") memorySaver = flag.Bool("memory-saver", false, `If true, only track pods which have an associated VPA`) authHeader = flag.Bool("auth-header", false, `If True will use header authentication, If true aslo authHeaderName and authHeaderValueName must be supplied`) - authHeaderName = flag.String("auth-header-name", "", `What is the header name to use for header based Auth`) - authHeaderValueName = flag.String("auth-header-value-name", "", `The env variable name holding the auth header value e.g the value of the bearer token etc..`) + authHeaderName = flag.String("auth-header-name", "", `Header name to use for header based Auth`) + authHeaderValueName = flag.String("auth-header-value-name", "", `Env variable name holding the auth header value e.g the value of the bearer token etc..`) // external metrics provider config useExternalMetrics = flag.Bool("use-external-metrics", false, "ALPHA. Use an external metrics provider instead of metrics_server.")