From 4c30e6aecffd9ee418b43b68245fd2e2e36100a8 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Thu, 31 Mar 2016 22:41:20 -0700 Subject: [PATCH] feat(e2e): add test scenario --- test/e2e/helm.go | 14 ++++++++ test/e2e/helm_test.go | 46 +++++++++++++++++++------- test/e2e/kubernetes.go | 74 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 test/e2e/kubernetes.go diff --git a/test/e2e/helm.go b/test/e2e/helm.go index 7b0e39fa9..aa527de77 100644 --- a/test/e2e/helm.go +++ b/test/e2e/helm.go @@ -9,6 +9,11 @@ import ( "testing" ) +const ( + namespace = "dm" + apiProxy = "/api/v1/proxy/namespaces/" + namespace + "/services/manager-service:manager/" +) + type HelmContext struct { t *testing.T Path string @@ -44,12 +49,21 @@ type HelmCmd struct { } func (h *HelmCmd) exec(args ...string) error { + args = append([]string{"--host", h.ctx.Host}, args...) cmd := exec.Command(h.ctx.Path, args...) h.stdout.Reset() h.stderr.Reset() cmd.Stdout = &h.stdout cmd.Stderr = &h.stderr status := cmd.Run() + if h.stdout.Len() > 0 { + h.ctx.t.Log("standard output:") + h.ctx.t.Log(h.stdout.String()) + } + if h.stderr.Len() > 0 { + h.ctx.t.Log("standard error:") + h.ctx.t.Log(h.stderr.String()) + } h.ran = true return status } diff --git a/test/e2e/helm_test.go b/test/e2e/helm_test.go index 5c474796d..ce4f40aee 100644 --- a/test/e2e/helm_test.go +++ b/test/e2e/helm_test.go @@ -1,34 +1,58 @@ package e2e import ( - "os/exec" + "fmt" + "math/rand" "strings" "testing" + "time" ) +func init() { + rand.Seed(time.Now().Unix()) +} + +var chart = "gs://areese-charts/replicatedservice-3.tgz" + func TestHelm(t *testing.T) { - if !kubeRunning() { + kube := NewKubeContext() + helm := NewHelmContext(t) + + t.Log(kube.CurrentContext()) + t.Log(kube.Cluster()) + t.Log(kube.Server()) + + if !kube.Running() { t.Fatal("Not connected to kubernetes") } - helm := NewHelmContext(t) if !helmRunning(helm) { t.Fatal("Helm is not installed") } - // Setup helm host + helm.Host = fmt.Sprintf("%s%s", kube.Server(), apiProxy) + t.Logf("Using host: %v", helm.Host) - // Run deploy + t.Log("Executing deployment list") + helm.Run("deployment", "list") - // Test deployment + deploymentName := genName() + + t.Log("Executing deploy") + helm.Run("deploy", "--name", deploymentName, chart) + + t.Log("Executing deployment list") + helm.Run("deployment", "list") + + t.Log("Executing deployment delete") + helm.Run("deployment", "delete", deploymentName) +} + +func genName() string { + return fmt.Sprintf("%d", rand.Uint32()) } func helmRunning(h *HelmContext) bool { out := h.Run("server", "status").Stdout() return strings.Count(out, "Running") == 5 } - -func kubeRunning() bool { - _, err := exec.Command("kubectl", "cluster-info").CombinedOutput() - return err == nil -} diff --git a/test/e2e/kubernetes.go b/test/e2e/kubernetes.go new file mode 100644 index 000000000..bb15e839b --- /dev/null +++ b/test/e2e/kubernetes.go @@ -0,0 +1,74 @@ +package e2e + +import ( + "encoding/json" + "fmt" + "os/exec" + "strings" +) + +const defaultKubectlPath = "kubectl" + +type KubeContext struct { + Path string + Config *Config +} + +func NewKubeContext() *KubeContext { + return &KubeContext{ + Path: defaultKubectlPath, + } +} + +type Config struct { + Clusters []struct { + // Name is the nickname for this Cluster + Name string `json:"name"` + // Cluster holds the cluster information + Cluster struct { + // Server is the address of the kubernetes cluster (https://hostname:port). + Server string `json:"server"` + // APIVersion is the preferred api version for communicating with the kubernetes cluster (v1, v2, etc). + APIVersion string `json:"api-version,omitempty"` + // InsecureSkipTLSVerify skips the validity check for the server's certificate. This will make your HTTPS connections insecure. + InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify,omitempty"` + // CertificateAuthority is the path to a cert file for the certificate authority. + CertificateAuthority string `json:"certificate-authority,omitempty"` + // CertificateAuthorityData contains PEM-encoded certificate authority certificates. Overrides CertificateAuthority + CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"` + // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields + Extensions []struct { + // Name is the nickname for this Extension + Name string `json:"name"` + } `json:"extensions,omitempty"` + } `json:"cluster"` + } +} + +func (k *KubeContext) ParseConfig() { + out, _ := exec.Command(k.Path, "config", "view", "--flatten=true", "--minify=true", "-o=json").Output() + err := json.Unmarshal(out, &k.Config) + if err != nil { + fmt.Println(err.Error()) + } +} + +func (k *KubeContext) Cluster() string { + out, _ := exec.Command(k.Path, "config", "view", "--flatten=true", "--minify=true", "-o", "jsonpath='{.clusters[0].name}'").Output() + return string(out) +} + +func (k *KubeContext) Server() string { + out, _ := exec.Command(k.Path, "config", "view", "--flatten=true", "--minify=true", "-o", "jsonpath='{.clusters[0].cluster.server}'").Output() + return strings.Replace(string(out), "'", "", -1) +} + +func (k *KubeContext) CurrentContext() string { + out, _ := exec.Command(k.Path, "config", "view", "--flatten=true", "--minify=true", "-o", "jsonpath='{.current-context}'").Output() + return string(out) +} + +func (k *KubeContext) Running() bool { + _, err := exec.Command(k.Path, "cluster-info").CombinedOutput() + return err == nil +}