diff --git a/backend/cmd/headlamp_test.go b/backend/cmd/headlamp_test.go index 662df5bda60..0b41e7f2992 100644 --- a/backend/cmd/headlamp_test.go +++ b/backend/cmd/headlamp_test.go @@ -108,19 +108,6 @@ func getResponseFromRestrictedEndpoint(handler http.Handler, method, url string, return rr, nil } -// getDefaultKubeConfigPathForTest is a test helper that wraps config.GetDefaultKubeConfigPath -// and fails the test immediately if retrieving the default path returns an error. -func getDefaultKubeConfigPathForTest(t *testing.T) string { - t.Helper() - - path, err := config.GetDefaultKubeConfigPath() - if err != nil { - t.Fatalf("failed to get default kubeconfig path: %v", err) - } - - return path -} - func TestGetConfigIncludesDefaultPodDebugImage(t *testing.T) { c := &HeadlampConfig{ HeadlampConfig: &headlampconfig.HeadlampConfig{ @@ -593,13 +580,15 @@ func TestDrainAndCordonNode(t *testing.T) { //nolint:funlen cache := cache.New[interface{}]() kubeConfigStore := kubeconfig.NewContextStore() + kubeConfigPath := filepath.Join("headlamp_testdata", "kubeconfig") + tests := []test{ { handler: createHeadlampHandler(context.Background(), &HeadlampConfig{ HeadlampConfig: &headlampconfig.HeadlampConfig{ HeadlampCFG: &headlampconfig.HeadlampCFG{ UseInCluster: false, - KubeConfigPath: getDefaultKubeConfigPathForTest(t), + KubeConfigPath: kubeConfigPath, KubeConfigStore: kubeConfigStore, }, Cache: cache, @@ -829,16 +818,20 @@ func TestDeletePlugin(t *testing.T) { cache := cache.New[interface{}]() kubeConfigStore := kubeconfig.NewContextStore() + kubeConfigPath := filepath.Join("headlamp_testdata", "kubeconfig") + c := HeadlampConfig{ HeadlampConfig: &headlampconfig.HeadlampConfig{ HeadlampCFG: &headlampconfig.HeadlampCFG{ UseInCluster: false, - KubeConfigPath: getDefaultKubeConfigPathForTest(t), + KubeConfigPath: kubeConfigPath, PluginDir: devPluginDir, UserPluginDir: userPluginDir, KubeConfigStore: kubeConfigStore, }, - Cache: cache, + Cache: cache, + TelemetryConfig: GetDefaultTestTelemetryConfig(), + TelemetryHandler: &telemetry.RequestHandler{}, }, } @@ -884,11 +877,13 @@ func TestHandleClusterAPI_XForwardedHost(t *testing.T) { cache := cache.New[interface{}]() + kubeConfigPath := filepath.Join("headlamp_testdata", "kubeconfig") + c := HeadlampConfig{ HeadlampConfig: &headlampconfig.HeadlampConfig{ HeadlampCFG: &headlampconfig.HeadlampCFG{ UseInCluster: false, - KubeConfigPath: getDefaultKubeConfigPathForTest(t), + KubeConfigPath: kubeConfigPath, KubeConfigStore: kubeConfigStore, }, Cache: cache, @@ -2059,7 +2054,7 @@ func newRealK8sHeadlampConfig(t *testing.T) (*HeadlampConfig, string) { kubeConfigPath, err = config.GetDefaultKubeConfigPath() if err != nil { - t.Fatalf("failed to get default kubeconfig path: %v", err) + t.Skipf("unable to determine default kubeconfig path, skipping real K8s integration test: %v", err) } } diff --git a/backend/pkg/config/config.go b/backend/pkg/config/config.go index 24af32b79ff..67bab0e9988 100644 --- a/backend/pkg/config/config.go +++ b/backend/pkg/config/config.go @@ -7,7 +7,6 @@ import ( "fmt" "io/fs" "os" - "os/user" "path/filepath" "runtime" "strings" @@ -610,10 +609,10 @@ func defaultUserPluginDir() string { } func GetDefaultKubeConfigPath() (string, error) { - currentUser, err := user.Current() + homeDirectory, err := os.UserHomeDir() if err != nil { - return "", fmt.Errorf("getting current user for kubeconfig path: %w", err) + return "", fmt.Errorf("failed to determine user home directory: %w", err) } - return filepath.Join(currentUser.HomeDir, ".kube", "config"), nil + return filepath.Join(homeDirectory, ".kube", "config"), nil } diff --git a/backend/pkg/config/config_test.go b/backend/pkg/config/config_test.go index 1baed7199ae..f50eace975e 100644 --- a/backend/pkg/config/config_test.go +++ b/backend/pkg/config/config_test.go @@ -763,3 +763,13 @@ func TestProxyAuthFlagOverridesEnv(t *testing.T) { assert.Equal(t, "X-Flag-Token", conf.ProxyAuthTokenHeader) }) } + +func TestGetDefaultKubeConfigPath(t *testing.T) { + tmpDir := t.TempDir() + t.Setenv("HOME", tmpDir) + t.Setenv("USERPROFILE", tmpDir) + + path, err := config.GetDefaultKubeConfigPath() + require.NoError(t, err) + assert.Equal(t, filepath.Join(tmpDir, ".kube", "config"), path) +} diff --git a/backend/pkg/kubeconfig/kubeconfig_test.go b/backend/pkg/kubeconfig/kubeconfig_test.go index f54cc4b03ab..3abe3faaeed 100644 --- a/backend/pkg/kubeconfig/kubeconfig_test.go +++ b/backend/pkg/kubeconfig/kubeconfig_test.go @@ -330,7 +330,9 @@ func TestContext(t *testing.T) { } kubeConfigFile, err := config.GetDefaultKubeConfigPath() - require.NoError(t, err) + if err != nil { + t.Skipf("Skipping test: failed to resolve default kubeconfig path: %v", err) + } configStore := kubeconfig.NewContextStore()