Skip to content

Commit

Permalink
Avoid calling context.CurrentConfig in integration-test
Browse files Browse the repository at this point in the history
Fix gocritic issue
  • Loading branch information
corneliusweig committed Aug 14, 2019
1 parent f504295 commit 19a7e44
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions integration/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (

"github.com/GoogleContainerTools/skaffold/integration/skaffold"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/proto"
"github.com/GoogleContainerTools/skaffold/testutil"
Expand Down Expand Up @@ -369,15 +368,11 @@ func readEventAPIStream(client proto.SkaffoldServiceClient, t *testing.T, retrie
}

func TestDev_WithKubecontextOverride(t *testing.T) {
const (
kubeconfig = "kubeconfig"
)

if testing.Short() {
t.Skip("skipping integration test")
}

testutil.Run(t, "skaffold run with kubecontext override", func(t *testutil.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

dir := "examples/getting-started"
pods := []string{"getting-started"}

Expand All @@ -389,8 +384,8 @@ func TestDev_WithKubecontextOverride(t *testing.T) {
t.Fatal(err)
}
kubeconfig := t.NewTempDir().
Write(kubeconfig, string(modifiedKubeconfig)).
Path(kubeconfig)
Write("kubeconfig", string(modifiedKubeconfig)).
Path("kubeconfig")
env := []string{fmt.Sprintf("KUBECONFIG=%s", kubeconfig)}

// n.b. for the sake of this test the namespace must not be given explicitly
Expand All @@ -404,24 +399,28 @@ func TestDev_WithKubecontextOverride(t *testing.T) {
}

func createModifiedKubeconfig(namespace string) ([]byte, string, error) {
kubeConfig, err := kubectx.CurrentConfig()
// do not use context.CurrentConfig(), because it may have cached a different config
kubeConfig, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
return nil, "", err
}

contextName := "modified-context"
if config.IsKindCluster(kubeConfig.CurrentContext) {
contextName = contextName + "@kind"
contextName += "@kind"
}

activeContext := kubeConfig.Contexts[kubeConfig.CurrentContext]
if activeContext == nil {
return nil, "", fmt.Errorf("no active kube-context set")
}
// clear the namespace in the active context
activeContext.Namespace = ""

newContext := activeContext.DeepCopy()
newContext.Namespace = namespace
kubeConfig.Contexts[contextName] = newContext

yaml, err := clientcmd.Write(kubeConfig)
yaml, err := clientcmd.Write(*kubeConfig)
return yaml, contextName, err
}

0 comments on commit 19a7e44

Please sign in to comment.