Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions pkg/controller/gather_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -554,3 +555,66 @@ func TestGetCustomStoragePath(t *testing.T) {
})
}
}

// Test_storagePathExists tests the storagePathExists method
func Test_storagePathExists(t *testing.T) {
tests := []struct {
name string
setupPath func() string
cleanupPath func(string)
expectError bool
errorContains string
}{
{
name: "storage path already exists",
setupPath: func() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to refactor the inline anonymous functions setupPath and cleanupPath into a helper function and pass arguments to it from the test.

// Create a temp directory that exists
tmpDir := t.TempDir()
return tmpDir
},
cleanupPath: func(string) {
// TempDir cleans up automatically
},
expectError: false,
},
{
name: "storage path does not exist and can be created",
setupPath: func() string {
tmpDir := t.TempDir()
return tmpDir + "/new-storage-path"
},
cleanupPath: func(string) {
// TempDir cleans up automatically
},
expectError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.setupPath()
defer tt.cleanupPath(path)

gatherJob := &GatherJob{
Controller: config.Controller{
StoragePath: path,
},
}

err := gatherJob.storagePathExists()

if tt.expectError {
assert.Error(t, err)
if tt.errorContains != "" {
assert.Contains(t, err.Error(), tt.errorContains)
}
} else {
assert.NoError(t, err)
// Verify the directory was created
info, statErr := os.Stat(path)
assert.NoError(t, statErr, "Directory should exist after storagePathExists")
assert.True(t, info.IsDir(), "Path should be a directory")
}
})
}
}
200 changes: 200 additions & 0 deletions pkg/controller/kubeconfigs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package controller

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

func Test_prepareGatherConfigs(t *testing.T) {
tests := []struct {
name string
protoKubeConfig *rest.Config
kubeConfig *rest.Config
impersonate string
prometheusTokenEnvValue string
expectImpersonation bool
expectInsecureMetrics bool
expectInsecureAlerts bool
}{
{
name: "with impersonation",
protoKubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
},
kubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
},
impersonate: "system:serviceaccount:openshift-insights:gather",
prometheusTokenEnvValue: "",
expectImpersonation: true,
expectInsecureMetrics: false,
expectInsecureAlerts: false,
},
{
name: "with prometheus token",
protoKubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
},
kubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
},
impersonate: "",
prometheusTokenEnvValue: "test-token-67890",
expectImpersonation: false,
expectInsecureMetrics: true,
expectInsecureAlerts: true,
},
{
name: "prometheus token with whitespace is trimmed",
protoKubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
},
kubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
},
impersonate: "",
prometheusTokenEnvValue: " test-token-with-spaces \n",
expectImpersonation: false,
expectInsecureMetrics: true,
expectInsecureAlerts: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup environment
if tt.prometheusTokenEnvValue != "" {
os.Setenv(insecurePrometheusTokenEnvVariable, tt.prometheusTokenEnvValue)
defer os.Unsetenv(insecurePrometheusTokenEnvVariable)
} else {
os.Unsetenv(insecurePrometheusTokenEnvVariable)
}

// Call the function
gatherProto, gatherKube, metricsGather, alertsGather := prepareGatherConfigs(
tt.protoKubeConfig,
tt.kubeConfig,
tt.impersonate,
)

assert.NotNil(t, gatherProto)
assert.NotNil(t, gatherKube)
assert.NotNil(t, metricsGather)
assert.NotNil(t, alertsGather)

assert.Empty(t, tt.protoKubeConfig.Impersonate.UserName)
assert.Empty(t, tt.kubeConfig.Impersonate.UserName)

if tt.expectImpersonation {
assert.Equal(t, tt.impersonate, gatherProto.Impersonate.UserName)
assert.Equal(t, tt.impersonate, gatherKube.Impersonate.UserName)
} else {
assert.Empty(t, gatherProto.Impersonate.UserName)
assert.Empty(t, gatherKube.Impersonate.UserName)
}

assert.Equal(t, metricHost, metricsGather.Host)
assert.Equal(t, "/", metricsGather.APIPath)
assert.NotNil(t, metricsGather.GroupVersion)
assert.NotNil(t, metricsGather.NegotiatedSerializer)

if tt.expectInsecureMetrics {
assert.True(t, metricsGather.Insecure)
assert.NotEmpty(t, metricsGather.BearerToken)
assert.Empty(t, metricsGather.CAFile)
assert.Empty(t, metricsGather.CAData)
} else {
assert.False(t, metricsGather.Insecure)
assert.Empty(t, metricsGather.BearerToken)
assert.Equal(t, metricCAFile, metricsGather.CAFile)
}

assert.Equal(t, alertManagerHost, alertsGather.Host)
assert.Equal(t, "/", alertsGather.APIPath)
assert.NotNil(t, alertsGather.GroupVersion)
assert.NotNil(t, alertsGather.NegotiatedSerializer)

if tt.expectInsecureAlerts {
assert.True(t, alertsGather.Insecure)
assert.NotEmpty(t, alertsGather.BearerToken)
assert.Empty(t, alertsGather.CAFile)
assert.Empty(t, alertsGather.CAData)
} else {
assert.False(t, alertsGather.Insecure)
assert.Empty(t, alertsGather.BearerToken)
assert.Equal(t, metricCAFile, alertsGather.CAFile)
}
})
}
}

func Test_createGatherConfig(t *testing.T) {
tests := []struct {
name string
kubeConfig *rest.Config
configHost string
token string
expectInsecure bool
}{
{
name: "without token",
kubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
APIPath: "/api",
TLSClientConfig: rest.TLSClientConfig{
CAFile: "/original/ca.crt",
CAData: []byte("original-ca-data"),
},
},
configHost: "https://prometheus.example.com:9091",
token: "",
expectInsecure: false,
},
{
name: "with token",
kubeConfig: &rest.Config{
Host: "https://api.test-cluster:6443",
APIPath: "/api",
TLSClientConfig: rest.TLSClientConfig{
CAFile: "/original/ca.crt",
CAData: []byte("original-ca-data"),
},
},
configHost: "https://prometheus.example.com:9091",
token: "test-bearer-token",
expectInsecure: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gatherConfig := createGatherConfig(tt.kubeConfig, tt.configHost, tt.token)

assert.NotNil(t, gatherConfig)
assert.Equal(t, "https://api.test-cluster:6443", tt.kubeConfig.Host)
assert.Equal(t, "/api", tt.kubeConfig.APIPath)

assert.Equal(t, tt.configHost, gatherConfig.Host)
assert.Equal(t, "/", gatherConfig.APIPath)
assert.NotNil(t, gatherConfig.GroupVersion)
assert.Equal(t, &schema.GroupVersion{}, gatherConfig.GroupVersion)
assert.Equal(t, scheme.Codecs, gatherConfig.NegotiatedSerializer)

if tt.expectInsecure {
assert.True(t, gatherConfig.Insecure)
assert.Equal(t, tt.token, gatherConfig.BearerToken)
assert.Empty(t, gatherConfig.CAFile)
assert.Empty(t, gatherConfig.CAData)
} else {
assert.False(t, gatherConfig.Insecure)
assert.Empty(t, gatherConfig.BearerToken)
assert.Equal(t, metricCAFile, gatherConfig.CAFile)
}
})
}
}
Loading