Skip to content
Merged
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
31 changes: 13 additions & 18 deletions backend/cmd/headlamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
},
Comment thread
YadavAkhileshh marked this conversation as resolved.
Cache: cache,
Expand Down Expand Up @@ -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{},
},
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
7 changes: 3 additions & 4 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io/fs"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -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)
}
Comment thread
YadavAkhileshh marked this conversation as resolved.
Comment thread
YadavAkhileshh marked this conversation as resolved.

return filepath.Join(currentUser.HomeDir, ".kube", "config"), nil
return filepath.Join(homeDirectory, ".kube", "config"), nil
Comment thread
YadavAkhileshh marked this conversation as resolved.
Comment thread
YadavAkhileshh marked this conversation as resolved.
}
10 changes: 10 additions & 0 deletions backend/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion backend/pkg/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading