@@ -665,6 +665,53 @@ func TestRenameCluster(t *testing.T) {
665
665
}
666
666
}
667
667
668
+ func TestRemoveContextFromDefaultKubeConfig (t * testing.T ) {
669
+ // temp HOME directory so we don't modify the real ~/.config
670
+ tmpHome := t .TempDir ()
671
+
672
+ // Overrides HOME so os.UserHomeDir() sees tmpHome
673
+ os .Setenv ("HOME" , tmpHome )
674
+ defer os .Unsetenv ("HOME" )
675
+
676
+ // Also override XDG_CONFIG_HOME (which os.UserConfigDir() will check)
677
+ // On Linux, if XDG_CONFIG_HOME is not set, Go will default to ~/.config
678
+ xdgConfig := filepath .Join (tmpHome , ".config" )
679
+ os .Setenv ("XDG_CONFIG_HOME" , xdgConfig )
680
+
681
+ defer os .Unsetenv ("XDG_CONFIG_HOME" )
682
+
683
+ // Create the directory Headlamp expects: ~/.config/Headlamp/kubeconfigs
684
+ kubeConfigsDir := filepath .Join (xdgConfig , "Headlamp" , "kubeconfigs" )
685
+ err := os .MkdirAll (kubeConfigsDir , 0o755 )
686
+ require .NoError (t , err , "failed to create ~/.config/Headlamp/kubeconfigs" )
687
+
688
+ // Path to which removeContextFromDefaultKubeConfig will write
689
+ kubeConfigFile := filepath .Join (kubeConfigsDir , "config" )
690
+
691
+ // Read test data that has "kubedelta" context
692
+ testDataPath := filepath .Join ("headlamp_testdata" , "kubeconfig_remove" )
693
+ testData , err := os .ReadFile (testDataPath )
694
+ require .NoError (t , err , "failed to read test data file" )
695
+
696
+ // Write the test kubeconfig into ~/.config/Headlamp/kubeconfigs/config
697
+ err = os .WriteFile (kubeConfigFile , testData , 0o600 )
698
+ require .NoError (t , err , "failed to write test kubeconfig" )
699
+
700
+ // fake HTTP response writer for removeContextFromDefaultKubeConfig.
701
+ w := httptest .NewRecorder ()
702
+
703
+ // Remove the "kubedelta" context
704
+ err = removeContextFromDefaultKubeConfig (w , "kubedelta" )
705
+ require .NoError (t , err , "removeContextFromDefaultKubeConfig should succeed" )
706
+
707
+ // Read the updated file and ensure "kubedelta" is gone.
708
+ updatedData , err := os .ReadFile (kubeConfigFile )
709
+ require .NoError (t , err , "failed to read updated kubeconfig" )
710
+
711
+ require .NotContains (t , string (updatedData ), "kubedelta" ,
712
+ "Expected 'kubedelta' context to be removed from kubeconfig" )
713
+ }
714
+
668
715
func TestFileExists (t * testing.T ) {
669
716
// Test for existing file
670
717
assert .True (t , fileExists ("./headlamp_testdata/kubeconfig" ),
0 commit comments