Skip to content
Snippets Groups Projects
Unverified Commit ea357c73 authored by Angus Lees's avatar Angus Lees
Browse files

Add integration test for --namespace/--kubeconfig handling

This has been a frequent area of client-go bugs, so add tests that
exercise --namespace handling in various scenarios.
parent 43864466
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ package integration
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
......@@ -76,6 +77,15 @@ func deleteNsOrDie(c corev1.CoreV1Interface, ns string) {
}
}
func containsString(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
func runKubecfgWith(flags []string, input []runtime.Object) error {
tmpdir, err := ioutil.TempDir("", "kubecfg-testdata")
if err != nil {
......@@ -90,25 +100,19 @@ func runKubecfgWith(flags []string, input []runtime.Object) error {
return err
}
enc := api.Codecs.LegacyCodec(v1.SchemeGroupVersion)
for _, o := range input {
buf, err := runtime.Encode(enc, o)
if err != nil {
return err
}
fmt.Fprintf(f, "---\n")
_, err = f.Write(buf)
if err != nil {
return err
}
if err := encodeTo(f, enc, input); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
args := []string{}
if *kubeconfig != "" && !containsString(flags, "--kubeconfig") {
args = append(args, "--kubeconfig", *kubeconfig)
}
args = append(args, flags...)
args = append(args, "-f")
args = append(args, fname)
args = append(args, "-f", fname)
fmt.Fprintf(GinkgoWriter, "Running %q %q\n", *kubecfgBin, args)
cmd := exec.Command(*kubecfgBin, args...)
......@@ -122,6 +126,21 @@ func runKubecfgWith(flags []string, input []runtime.Object) error {
return nil
}
func encodeTo(w io.Writer, enc runtime.Encoder, objs []runtime.Object) error {
for _, o := range objs {
buf, err := runtime.Encode(enc, o)
if err != nil {
return err
}
fmt.Fprintf(w, "---\n")
_, err = w.Write(buf)
if err != nil {
return err
}
}
return nil
}
func TestE2e(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "kubecfg integration tests")
......
// +build integration
package integration
import (
"io/ioutil"
"os"
"os/exec"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("flags", func() {
var c corev1.CoreV1Interface
var ns string
const testName = "testobj"
var args []string
var objs []runtime.Object
var kubecfgExit *exec.ExitError
BeforeEach(func() {
c = corev1.NewForConfigOrDie(clusterConfigOrDie())
ns = createNsOrDie(c, "kubeflags")
args = []string{"update", "-vv"}
objs = []runtime.Object{
&v1.ConfigMap{
// Note: no explicit Namespace
ObjectMeta: metav1.ObjectMeta{Name: testName},
Data: map[string]string{"foo": "bar"},
},
}
})
AfterEach(func() {
deleteNsOrDie(c, ns)
})
Describe("with custom kubeconfig", func() {
var config *clientcmdapi.Config
var kubeconfigFile string
BeforeEach(func() {
// Initialise config with our --kubeconfig
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: *kubeconfig},
&clientcmd.ConfigOverrides{})
rawconf, err := clientConfig.RawConfig()
Expect(err).NotTo(HaveOccurred())
tmp, err := clientcmdlatest.Scheme.Copy(&rawconf)
Expect(err).NotTo(HaveOccurred())
config = tmp.(*clientcmdapi.Config)
})
JustBeforeEach(func() {
f, err := ioutil.TempFile("", "kubeconfig")
Expect(err).NotTo(HaveOccurred())
buf, err := runtime.Encode(clientcmdlatest.Codec, config)
Expect(err).NotTo(HaveOccurred())
_, err = f.Write(buf)
Expect(err).NotTo(HaveOccurred())
err = f.Close()
Expect(err).NotTo(HaveOccurred())
kubeconfigFile = f.Name()
args = append(args, "--kubeconfig", kubeconfigFile)
})
AfterEach(func() {
os.Remove(kubeconfigFile)
})
JustBeforeEach(func() {
kubecfgExit = nil
err := runKubecfgWith(args, objs)
if err != nil {
Expect(err).To(BeAssignableToTypeOf(&exec.ExitError{}))
kubecfgExit = err.(*exec.ExitError)
}
})
Context("with explicit namespace in config", func() {
BeforeEach(func() {
config.Contexts[config.CurrentContext].Namespace = ns
})
It("should update correct namespace", func() {
Expect(kubecfgExit).NotTo(HaveOccurred())
Expect(c.ConfigMaps(ns).Get(testName, metav1.GetOptions{})).
NotTo(BeNil())
})
})
Describe("with explicit --namespace", func() {
BeforeEach(func() {
// Test for https://github.com/kubernetes/client-go/issues/288
config.Contexts[config.CurrentContext].Namespace = "bogusNamespace"
args = append(args, "--namespace", ns)
})
It("should update correct namespace", func() {
Expect(kubecfgExit).NotTo(HaveOccurred())
Expect(c.ConfigMaps(ns).Get(testName, metav1.GetOptions{})).
NotTo(BeNil())
})
})
})
})
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment