Converted TestClusterDNS to native ginkgo syntax #4162

This commit is contained in:
Robert Rati
2015-02-05 08:50:07 -05:00
parent de848cc3f0
commit d44c1d5b84

View File

@@ -23,132 +23,135 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// TestClusterDNS checks that cluster DNS works.
func TestClusterDNS(c *client.Client) bool {
if testContext.provider == "vagrant" {
glog.Infof("Skipping test which is broken for vagrant (See https://github.com/GoogleCloudPlatform/kubernetes/issues/3580)")
return true
}
var _ = Describe("Services", func() {
var c *client.Client
podClient := c.Pods(api.NamespaceDefault)
BeforeEach(func() {
c = loadClientOrDie()
})
//TODO: Wait for skyDNS
It("should provide DNS for the cluster", func() {
if testContext.provider == "vagrant" {
By("Skipping test which is broken for vagrant (See https://github.com/GoogleCloudPlatform/kubernetes/issues/3580)")
} else {
podClient := c.Pods(api.NamespaceDefault)
// All the names we need to be able to resolve.
namesToResolve := []string{
"kubernetes-ro",
"kubernetes-ro.default",
"kubernetes-ro.default.kubernetes.local",
"google.com",
}
//TODO: Wait for skyDNS
probeCmd := "for i in `seq 1 600`; do "
for _, name := range namesToResolve {
probeCmd += fmt.Sprintf("wget -O /dev/null %s && echo OK > /results/%s;", name, name)
}
probeCmd += "sleep 1; done"
// Run a pod which probes DNS and exposes the results by HTTP.
pod := &api.Pod{
TypeMeta: api.TypeMeta{
Kind: "Pod",
APIVersion: "v1beta1",
},
ObjectMeta: api.ObjectMeta{
Name: "dns-test-" + string(util.NewUUID()),
},
Spec: api.PodSpec{
Volumes: []api.Volume{
{
Name: "results",
Source: api.VolumeSource{
EmptyDir: &api.EmptyDir{},
},
},
},
Containers: []api.Container{
{
Name: "webserver",
Image: "kubernetes/test-webserver",
VolumeMounts: []api.VolumeMount{
{
Name: "results",
MountPath: "/results",
},
},
},
{
Name: "pinger",
Image: "busybox",
Command: []string{"sh", "-c", probeCmd},
VolumeMounts: []api.VolumeMount{
{
Name: "results",
MountPath: "/results",
},
},
},
},
},
}
_, err := podClient.Create(pod)
if err != nil {
glog.Errorf("Failed to create %s pod: %v", pod.Name, err)
return false
}
defer podClient.Delete(pod.Name)
waitForPodRunning(c, pod.Name)
pod, err = podClient.Get(pod.Name)
if err != nil {
glog.Errorf("Failed to get pod %s: %v", pod.Name, err)
return false
}
// Try to find results for each expected name.
var failed []string
for try := 1; try < 100; try++ {
failed = []string{}
for _, name := range namesToResolve {
_, err := c.Get().
Prefix("proxy").
Resource("pods").
Namespace("default").
Name(pod.Name).
Suffix("results", name).
Do().Raw()
if err != nil {
failed = append(failed, name)
glog.V(4).Infof("Lookup using %s for %s failed: %v", pod.Name, name, err)
// All the names we need to be able to resolve.
namesToResolve := []string{
"kubernetes-ro",
"kubernetes-ro.default",
"kubernetes-ro.default.kubernetes.local",
"google.com",
}
probeCmd := "for i in `seq 1 600`; do "
for _, name := range namesToResolve {
probeCmd += fmt.Sprintf("wget -O /dev/null %s && echo OK > /results/%s;", name, name)
}
probeCmd += "sleep 1; done"
// Run a pod which probes DNS and exposes the results by HTTP.
By("creating a pod to probe DNS")
pod := &api.Pod{
TypeMeta: api.TypeMeta{
Kind: "Pod",
APIVersion: "v1beta1",
},
ObjectMeta: api.ObjectMeta{
Name: "dns-test-" + string(util.NewUUID()),
},
Spec: api.PodSpec{
Volumes: []api.Volume{
{
Name: "results",
Source: api.VolumeSource{
EmptyDir: &api.EmptyDir{},
},
},
},
Containers: []api.Container{
{
Name: "webserver",
Image: "kubernetes/test-webserver",
VolumeMounts: []api.VolumeMount{
{
Name: "results",
MountPath: "/results",
},
},
},
{
Name: "pinger",
Image: "busybox",
Command: []string{"sh", "-c", probeCmd},
VolumeMounts: []api.VolumeMount{
{
Name: "results",
MountPath: "/results",
},
},
},
},
},
}
By("submitting the pod to kuberenetes")
_, err := podClient.Create(pod)
if err != nil {
Fail(fmt.Sprintf("Failed to create %s pod: %v", pod.Name, err))
}
defer func() {
By("deleting the pod")
defer GinkgoRecover()
podClient.Delete(pod.Name)
}()
By("waiting for the pod to start running")
waitForPodRunning(c, pod.Name)
By("retrieving the pod")
pod, err = podClient.Get(pod.Name)
if err != nil {
Fail(fmt.Sprintf("Failed to get pod %s: %v", pod.Name, err))
}
// Try to find results for each expected name.
By("looking for the results for each expected name")
var failed []string
for try := 1; try < 100; try++ {
failed = []string{}
for _, name := range namesToResolve {
_, err := c.Get().
Prefix("proxy").
Resource("pods").
Namespace("default").
Name(pod.Name).
Suffix("results", name).
Do().Raw()
if err != nil {
failed = append(failed, name)
fmt.Printf("Lookup using %s for %s failed: %v\n", pod.Name, name, err)
}
}
if len(failed) == 0 {
break
}
fmt.Printf("lookups using %s failed for: %v\n", pod.Name, failed)
time.Sleep(10 * time.Second)
}
Expect(len(failed)).To(Equal(0))
// TODO: probe from the host, too.
fmt.Printf("DNS probes using %s succeeded\n", pod.Name)
}
if len(failed) == 0 {
break
}
glog.Infof("lookups using %s failed for: %v", pod.Name, failed)
time.Sleep(10 * time.Second)
}
if len(failed) != 0 {
glog.Errorf("DNS using %s failed for: %v", pod.Name, failed)
return false
}
// TODO: probe from the host, too.
glog.Infof("DNS probes using %s succeeded", pod.Name)
return true
}
var _ = Describe("TestClusterDNS", func() {
It("should pass", func() {
// TODO: Instead of OrDie, client should Fail the test if there's a problem.
// In general tests should Fail() instead of glog.Fatalf().
Expect(TestClusterDNS(loadClientOrDie())).To(BeTrue())
})
})