feat(e2e): add test scenario

pull/606/head
Adam Reese 10 years ago
parent 60bce48631
commit 4c30e6aecf

@ -9,6 +9,11 @@ import (
"testing" "testing"
) )
const (
namespace = "dm"
apiProxy = "/api/v1/proxy/namespaces/" + namespace + "/services/manager-service:manager/"
)
type HelmContext struct { type HelmContext struct {
t *testing.T t *testing.T
Path string Path string
@ -44,12 +49,21 @@ type HelmCmd struct {
} }
func (h *HelmCmd) exec(args ...string) error { func (h *HelmCmd) exec(args ...string) error {
args = append([]string{"--host", h.ctx.Host}, args...)
cmd := exec.Command(h.ctx.Path, args...) cmd := exec.Command(h.ctx.Path, args...)
h.stdout.Reset() h.stdout.Reset()
h.stderr.Reset() h.stderr.Reset()
cmd.Stdout = &h.stdout cmd.Stdout = &h.stdout
cmd.Stderr = &h.stderr cmd.Stderr = &h.stderr
status := cmd.Run() 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 h.ran = true
return status return status
} }

@ -1,34 +1,58 @@
package e2e package e2e
import ( import (
"os/exec" "fmt"
"math/rand"
"strings" "strings"
"testing" "testing"
"time"
) )
func init() {
rand.Seed(time.Now().Unix())
}
var chart = "gs://areese-charts/replicatedservice-3.tgz"
func TestHelm(t *testing.T) { 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") t.Fatal("Not connected to kubernetes")
} }
helm := NewHelmContext(t)
if !helmRunning(helm) { if !helmRunning(helm) {
t.Fatal("Helm is not installed") 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 { func helmRunning(h *HelmContext) bool {
out := h.Run("server", "status").Stdout() out := h.Run("server", "status").Stdout()
return strings.Count(out, "Running") == 5 return strings.Count(out, "Running") == 5
} }
func kubeRunning() bool {
_, err := exec.Command("kubectl", "cluster-info").CombinedOutput()
return err == nil
}

@ -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
}
Loading…
Cancel
Save