Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an integration test for services. #1559

Merged
merged 1 commit into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 70 additions & 2 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ func podsOnMinions(c *client.Client, pods api.PodList) wait.ConditionFunc {
}
}

func endpointsSet(c *client.Client, ctx api.Context, serviceID string, endpointCount int) wait.ConditionFunc {
return func() (bool, error) {
endpoints, err := c.GetEndpoints(ctx, serviceID)
if err != nil {
return false, nil
}
return len(endpoints.Endpoints) == endpointCount, nil
}
}

func podExists(c *client.Client, ctx api.Context, podID string) wait.ConditionFunc {
return func() (bool, error) {
_, err := c.GetPod(ctx, podID)
return err == nil, nil
}
}

func runReplicationControllerTest(c *client.Client) {
ctx := api.NewDefaultContext()
data, err := ioutil.ReadFile("api/examples/controller.json")
Expand Down Expand Up @@ -289,6 +306,55 @@ func runAtomicPutTest(c *client.Client) {
glog.Info("Atomic PUTs work.")
}

func runServiceTest(client *client.Client) {
ctx := api.NewDefaultContext()
pod := api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
Containers: []api.Container{
{
Name: "c1",
Image: "foo",
Ports: []api.Port{
{ContainerPort: 1234},
},
},
},
},
},
CurrentState: api.PodState{
PodIP: "1.2.3.4",
},
Labels: map[string]string{
"name": "thisisalonglabel",
},
}
_, err := client.CreatePod(ctx, &pod)
if err != nil {
glog.Fatalf("Failed to create pod: %v, %v", pod, err)
}
if err := wait.Poll(time.Second, time.Second*20, podExists(client, ctx, pod.ID)); err != nil {
glog.Fatalf("FAILED: pod never started running %v", err)
}
svc := api.Service{
JSONBase: api.JSONBase{ID: "service1"},
Selector: map[string]string{
"name": "thisisalonglabel",
},
Port: 8080,
}
_, err = client.CreateService(ctx, &svc)
if err != nil {
glog.Fatalf("Failed to create service: %v, %v", svc, err)
}
if err := wait.Poll(time.Second, time.Second*10, endpointsSet(client, ctx, svc.ID, 1)); err != nil {
glog.Fatalf("FAILED: unexpected endpoints: %v", err)
}
glog.Info("Service test passed.")
}

type testFunc func(*client.Client)

func main() {
Expand Down Expand Up @@ -318,6 +384,7 @@ func main() {
testFuncs := []testFunc{
runReplicationControllerTest,
runAtomicPutTest,
runServiceTest,
}
var wg sync.WaitGroup
wg.Add(len(testFuncs))
Expand Down Expand Up @@ -347,8 +414,9 @@ func main() {
}
}
// We expect 5: 2 net containers + 2 pods from the replication controller +
// 1 net container + 2 pods from the URL.
if len(createdPods) != 7 {
// 1 net container + 2 pods from the URL +
// 1 net container + 1 pod from the service test.
if len(createdPods) != 9 {
glog.Fatalf("Unexpected list of created pods:\n\n%#v\n\n%#v\n\n%#v\n\n", createdPods.List(), fakeDocker1.Created, fakeDocker2.Created)
}
glog.Infof("OK - found created pods: %#v", createdPods.List())
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/endpoints_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (e *EndpointController) SyncServiceEndpoints() error {
resultErr = err
continue
}
endpoints := make([]string, len(pods.Items))
for ix, pod := range pods.Items {
endpoints := []string{}
for _, pod := range pods.Items {
port, err := findPort(&pod.DesiredState.Manifest, service.ContainerPort)
if err != nil {
glog.Errorf("Failed to find port for service: %v, %v", service, err)
Expand All @@ -73,7 +73,7 @@ func (e *EndpointController) SyncServiceEndpoints() error {
glog.Errorf("Failed to find an IP for pod: %v", pod)
continue
}
endpoints[ix] = net.JoinHostPort(pod.CurrentState.PodIP, strconv.Itoa(port))
endpoints = append(endpoints, net.JoinHostPort(pod.CurrentState.PodIP, strconv.Itoa(port)))
}
currentEndpoints, err := e.client.GetEndpoints(nsCtx, service.ID)
if err != nil {
Expand Down