Merge pull request #4256 from zmerlynn/port_endpoints

Port endpoints.go test to new-style Gingko test
This commit is contained in:
Satnam Singh 2015-02-09 12:56:54 -08:00
commit 5f3bc33d01
2 changed files with 128 additions and 196 deletions

View File

@ -1,196 +0,0 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"net"
"time"
"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"
)
func validateIPs(c *client.Client, ns, expectedPort string, expectedEndpoints []string, endpoints *api.Endpoints) bool {
ips := util.StringSet{}
for _, spec := range endpoints.Endpoints {
host, port, err := net.SplitHostPort(spec)
if err != nil {
glog.Warningf("invalid endpoint spec: %s", spec)
return false
}
if port != expectedPort {
glog.Warningf("invalid port, expected %s, got %s", expectedPort, port)
return false
}
ips.Insert(host)
}
for _, name := range expectedEndpoints {
pod, err := c.Pods(ns).Get(name)
if err != nil {
glog.Warningf("failed to get pod %s, that's pretty weird. validation failed.", name)
return false
}
if !ips.Has(pod.Status.PodIP) {
glog.Warningf("ip validation failed, expected: %v, saw: %v", ips, pod.Status.PodIP)
return false
}
}
return true
}
func validateEndpoints(c *client.Client, ns, serviceName, expectedPort string, expectedEndpoints []string) bool {
done := make(chan bool)
running := true
go func() {
ok := true
for running {
endpoints, err := c.Endpoints(ns).Get(serviceName)
if err == nil {
if len(endpoints.Endpoints) != len(expectedEndpoints) {
glog.Warningf("Unexpected endpoints: %#v, expected %v", endpoints, expectedEndpoints)
} else if validateIPs(c, ns, expectedPort, expectedEndpoints, endpoints) {
break
}
} else {
glog.Warningf("Failed to get endpoints: %v", err)
}
time.Sleep(time.Second)
}
done <- ok
}()
select {
case result := <-done:
return result
case <-time.After(60 * time.Second):
glog.Errorf("Timed out waiting for endpoints.")
running = false
return false
}
}
func addPod(c *client.Client, ns, name string, labels map[string]string) error {
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "test",
Image: "kubernetes/pause",
Ports: []api.Port{{ContainerPort: 80}},
},
},
},
}
_, err := c.Pods(ns).Create(pod)
return err
}
func TestEndpoints(c *client.Client) bool {
serviceName := "endpoint-test"
ns := api.NamespaceDefault
labels := map[string]string{
"foo": "bar",
"baz": "blah",
}
defer func() {
err := c.Services(ns).Delete(serviceName)
if err != nil {
glog.Errorf("Failed to delete service: %v (%v)", serviceName, err)
}
}()
service := &api.Service{
ObjectMeta: api.ObjectMeta{
Name: serviceName,
},
Spec: api.ServiceSpec{
Port: 80,
Selector: labels,
ContainerPort: util.NewIntOrStringFromInt(80),
},
}
if _, err := c.Services(ns).Create(service); err != nil {
glog.Errorf("Failed to create endpoint test service: %v", err)
return false
}
expectedPort := "80"
if !validateEndpoints(c, ns, serviceName, expectedPort, []string{}) {
return false
}
name1 := "test1"
if err := addPod(c, ns, name1, labels); err != nil {
glog.Errorf("Failed to create pod: %v", err)
return false
}
names := []string{name1}
defer func() {
for _, name := range names {
err := c.Pods(ns).Delete(name)
if err != nil {
glog.Errorf("Failed to delete pod: %v (%v)", name, err)
}
}
}()
if !validateEndpoints(c, ns, serviceName, expectedPort, names) {
return false
}
name2 := "test2"
if err := addPod(c, ns, name2, labels); err != nil {
glog.Errorf("Failed to create pod: %v", err)
return false
}
names = append(names, name2)
if !validateEndpoints(c, ns, serviceName, expectedPort, names) {
return false
}
if err := c.Pods(ns).Delete(name1); err != nil {
glog.Errorf("Failed to delete pod: %s", name1)
return false
}
names = []string{name2}
if !validateEndpoints(c, ns, serviceName, expectedPort, names) {
return false
}
return true
}
var _ = Describe("TestEndpoints", func() {
It("should pass", func() {
c, err := loadClient()
Expect(err).NotTo(HaveOccurred())
Expect(TestEndpoints(c)).To(BeTrue())
})
})

View File

@ -18,6 +18,7 @@ package e2e
import (
"fmt"
"net"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -180,4 +181,131 @@ var _ = Describe("Services", func() {
Expect(foundRW).To(Equal(true))
Expect(foundRO).To(Equal(true))
})
It("should serve basic a endpoint from pods", func(done Done) {
serviceName := "endpoint-test"
ns := api.NamespaceDefault
labels := map[string]string{
"foo": "bar",
"baz": "blah",
}
defer func() {
err := c.Services(ns).Delete(serviceName)
Expect(err).NotTo(HaveOccurred())
}()
service := &api.Service{
ObjectMeta: api.ObjectMeta{
Name: serviceName,
},
Spec: api.ServiceSpec{
Port: 80,
Selector: labels,
ContainerPort: util.NewIntOrStringFromInt(80),
},
}
_, err := c.Services(ns).Create(service)
Expect(err).NotTo(HaveOccurred())
expectedPort := "80"
validateEndpointsOrFail(c, ns, serviceName, expectedPort, []string{})
name1 := "test1"
addEndpointPodOrFail(c, ns, name1, labels)
names := []string{name1}
defer func() {
for _, name := range names {
err := c.Pods(ns).Delete(name)
Expect(err).NotTo(HaveOccurred())
}
}()
validateEndpointsOrFail(c, ns, serviceName, expectedPort, names)
name2 := "test2"
addEndpointPodOrFail(c, ns, name2, labels)
names = append(names, name2)
validateEndpointsOrFail(c, ns, serviceName, expectedPort, names)
err = c.Pods(ns).Delete(name1)
Expect(err).NotTo(HaveOccurred())
names = []string{name2}
validateEndpointsOrFail(c, ns, serviceName, expectedPort, names)
err = c.Pods(ns).Delete(name2)
Expect(err).NotTo(HaveOccurred())
names = []string{}
validateEndpointsOrFail(c, ns, serviceName, expectedPort, names)
// We deferred Gingko pieces that may Fail, we aren't done.
defer func() {
close(done)
}()
}, 120.0)
})
func validateIPsOrFail(c *client.Client, ns, expectedPort string, expectedEndpoints []string, endpoints *api.Endpoints) {
ips := util.StringSet{}
for _, spec := range endpoints.Endpoints {
host, port, err := net.SplitHostPort(spec)
if err != nil {
Fail(fmt.Sprintf("invalid endpoint spec: %s (%v)", spec, err))
}
if port != expectedPort {
Fail(fmt.Sprintf("invalid port, expected %s, got %s", expectedPort, port))
}
ips.Insert(host)
}
for _, name := range expectedEndpoints {
pod, err := c.Pods(ns).Get(name)
if err != nil {
Fail(fmt.Sprintf("failed to get pod %s, that's pretty weird. validation failed: %s", name, err))
}
if !ips.Has(pod.Status.PodIP) {
Fail(fmt.Sprintf("ip validation failed, expected: %v, saw: %v", ips, pod.Status.PodIP))
}
}
}
func validateEndpointsOrFail(c *client.Client, ns, serviceName, expectedPort string, expectedEndpoints []string) {
for {
endpoints, err := c.Endpoints(ns).Get(serviceName)
if err == nil {
if len(endpoints.Endpoints) == len(expectedEndpoints) {
validateIPsOrFail(c, ns, expectedPort, expectedEndpoints, endpoints)
return
} else {
By(fmt.Sprintf("Unexpected endpoints: %v, expected %v", endpoints.Endpoints, expectedEndpoints))
}
} else {
By(fmt.Sprintf("Failed to get endpoints: %v (ignoring for 1s)", err))
}
time.Sleep(time.Second)
}
}
func addEndpointPodOrFail(c *client.Client, ns, name string, labels map[string]string) {
By(fmt.Sprintf("Adding pod %v", name))
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "test",
Image: "kubernetes/pause",
Ports: []api.Port{{ContainerPort: 80}},
},
},
},
}
_, err := c.Pods(ns).Create(pod)
Expect(err).NotTo(HaveOccurred())
}