Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
asafadar committed Oct 26, 2023
1 parent 4a9f392 commit 880f991
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package history
import (
"context"
"fmt"
"net/http"
"os"
"testing"
"time"

Expand Down Expand Up @@ -51,6 +53,9 @@ func getDefaultPrometheusHistoryProviderConfigForTest() PrometheusHistoryProvide
CtrPodNameLabel: "pod_name",
CtrNameLabel: "name",
CadvisorMetricsJobName: "kubernetes-cadvisor",
Headers: map[string]string{
"Token": "TEST_TOKEN",
},
}
}

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions vertical-pod-autoscaler/pkg/recommender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down

0 comments on commit 880f991

Please sign in to comment.