handle ctxs with ':' in the name for windows (#235)

For windows, if the ctx name for kubens state file
contains a colon ':' (EKS), we replace it with __ (both while reading/writing).

It doesn't break existing users (as it didn't work in the first place).

Other characters remain unhandled, hoping it won't happen again.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan
2020-05-31 14:24:01 -07:00
committed by GitHub
parent 401188fefd
commit 0141ee19d2
2 changed files with 47 additions and 1 deletions

View File

@@ -3,7 +3,11 @@ package main
import (
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
"github.com/ahmetb/kubectx/internal/testutil"
)
func TestNSFile(t *testing.T) {
@@ -36,3 +40,28 @@ func TestNSFile(t *testing.T) {
t.Fatalf("Load()=%q; expected=%q", v, expected)
}
}
func TestNSFile_path_windows(t *testing.T) {
defer testutil.WithEnvVar("_FORCE_GOOS", "windows")()
fp := NewNSFile("a:b:c").path()
if expected := "a__b__c"; !strings.HasSuffix(fp, expected) {
t.Fatalf("file did not have expected ending %q: %s", expected, fp)
}
}
func Test_isWindows(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("won't test this case on windows")
}
got := isWindows()
if got {
t.Fatalf("isWindows() returned true for %s", runtime.GOOS)
}
defer testutil.WithEnvVar("_FORCE_GOOS", "windows")()
if !isWindows() {
t.Fatalf("isWindows() failed to detect windows with env override.")
}
}