2015-01-13 02:11:27 +00:00
|
|
|
/*
|
2015-01-15 23:24:58 +00:00
|
|
|
Copyright 2015 Google Inc. All rights reserved.
|
2015-01-13 02:11:27 +00:00
|
|
|
|
|
|
|
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 (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-01-16 21:11:42 +00:00
|
|
|
"time"
|
2015-01-13 02:11:27 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2015-01-16 21:11:42 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2015-01-28 00:38:48 +00:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2015-01-13 02:11:27 +00:00
|
|
|
)
|
|
|
|
|
2015-02-10 00:15:04 +00:00
|
|
|
var _ = Describe("ReplicationController", func() {
|
|
|
|
var c *client.Client
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
var err error
|
|
|
|
c, err = loadClient()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should serve a basic image on each replica with a public image", func() {
|
|
|
|
ServeImageOrFail(c, "basic", "kubernetes/serve_hostname:1.1")
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should serve a basic image on each replica with a private image", func() {
|
|
|
|
switch testContext.provider {
|
|
|
|
case "gce", "gke", "aws":
|
|
|
|
ServeImageOrFail(c, "private", "gcr.io/_b_k8s_test/serve_hostname:1.0")
|
|
|
|
default:
|
|
|
|
By(fmt.Sprintf("Skipping private variant, which is only supported for providers gce, gke and aws (not %s)",
|
|
|
|
testContext.provider))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-01-15 23:24:58 +00:00
|
|
|
// A basic test to check the deployment of an image using
|
|
|
|
// a replication controller. The image serves its hostname
|
|
|
|
// which is checked for each replica.
|
2015-02-10 00:15:04 +00:00
|
|
|
func ServeImageOrFail(c *client.Client, test string, image string) {
|
2015-01-13 02:11:27 +00:00
|
|
|
ns := api.NamespaceDefault
|
2015-01-16 21:11:42 +00:00
|
|
|
name := "my-hostname-" + test + "-" + string(util.NewUUID())
|
2015-01-13 02:11:27 +00:00
|
|
|
replicas := 2
|
|
|
|
|
2015-01-16 21:11:42 +00:00
|
|
|
// Create a replication controller for a service
|
2015-01-13 02:11:27 +00:00
|
|
|
// that serves its hostname on port 8080.
|
|
|
|
// The source for the Docker containter kubernetes/serve_hostname is
|
|
|
|
// in contrib/for-demos/serve_hostname
|
2015-02-10 00:15:04 +00:00
|
|
|
By(fmt.Sprintf("Creating replication controller %s", name))
|
2015-01-13 02:11:27 +00:00
|
|
|
controller, err := c.ReplicationControllers(ns).Create(&api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: replicas,
|
|
|
|
Selector: map[string]string{
|
|
|
|
"name": name,
|
|
|
|
},
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"name": name},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: name,
|
2015-01-15 23:24:58 +00:00
|
|
|
Image: image,
|
2015-01-13 02:11:27 +00:00
|
|
|
Ports: []api.Port{{ContainerPort: 9376, HostPort: 8080}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-02-10 00:15:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-16 21:11:42 +00:00
|
|
|
// Cleanup the replication controller when we are done.
|
|
|
|
defer func() {
|
|
|
|
// Resize the replication controller to zero to get rid of pods.
|
|
|
|
controller.Spec.Replicas = 0
|
|
|
|
if _, err = c.ReplicationControllers(ns).Update(controller); err != nil {
|
2015-02-10 00:15:04 +00:00
|
|
|
By(fmt.Sprintf("Failed to resize replication controller %s to zero: %v", name, err))
|
2015-01-16 21:11:42 +00:00
|
|
|
}
|
2015-01-13 02:11:27 +00:00
|
|
|
|
2015-01-16 21:11:42 +00:00
|
|
|
// Delete the replication controller.
|
|
|
|
if err = c.ReplicationControllers(ns).Delete(name); err != nil {
|
2015-02-10 00:15:04 +00:00
|
|
|
By(fmt.Sprintf("Failed to delete replication controller %s: %v", name, err))
|
2015-01-16 21:11:42 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// List the pods, making sure we observe all the replicas.
|
|
|
|
listTimeout := time.Minute
|
|
|
|
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
|
|
|
|
pods, err := c.Pods(ns).List(label)
|
2015-02-10 00:15:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-16 21:11:42 +00:00
|
|
|
t := time.Now()
|
|
|
|
for {
|
2015-02-10 00:15:04 +00:00
|
|
|
By(fmt.Sprintf("Controller %s: Found %d pods out of %d", name, len(pods.Items), replicas))
|
2015-01-16 21:11:42 +00:00
|
|
|
if len(pods.Items) == replicas {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if time.Since(t) > listTimeout {
|
2015-02-10 00:15:04 +00:00
|
|
|
Fail(fmt.Sprintf(
|
|
|
|
"Controller %s: Gave up waiting for %d pods to come up after seeing only %d pods after %v seconds",
|
|
|
|
name, replicas, len(pods.Items), time.Since(t).Seconds()))
|
2015-01-16 21:11:42 +00:00
|
|
|
}
|
2015-02-10 00:15:04 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2015-01-16 21:11:42 +00:00
|
|
|
pods, err = c.Pods(ns).List(label)
|
2015-02-10 00:15:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-13 02:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the pods to enter the running state. Waiting loops until the pods
|
|
|
|
// are running so non-running pods cause a timeout for this test.
|
|
|
|
for _, pod := range pods.Items {
|
2015-02-08 15:57:48 +00:00
|
|
|
err = waitForPodRunning(c, pod.Name, 300*time.Second)
|
2015-02-10 00:15:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-13 02:11:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 21:11:42 +00:00
|
|
|
// Try to make sure we get a hostIP for each pod.
|
|
|
|
hostIPTimeout := 2 * time.Minute
|
|
|
|
t = time.Now()
|
|
|
|
for i, pod := range pods.Items {
|
|
|
|
for {
|
|
|
|
p, err := c.Pods(ns).Get(pod.Name)
|
2015-02-10 00:15:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-16 21:11:42 +00:00
|
|
|
if p.Status.HostIP != "" {
|
2015-02-10 00:15:04 +00:00
|
|
|
By(fmt.Sprintf("Controller %s: Replica %d has hostIP: %s", name, i+1, p.Status.HostIP))
|
2015-01-16 21:11:42 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if time.Since(t) >= hostIPTimeout {
|
2015-02-10 00:15:04 +00:00
|
|
|
Fail(fmt.Sprintf("Controller %s: Gave up waiting for hostIP of replica %d after %v seconds",
|
|
|
|
name, i, time.Since(t).Seconds()))
|
2015-01-16 21:11:42 +00:00
|
|
|
}
|
2015-02-10 00:15:04 +00:00
|
|
|
By(fmt.Sprintf("Controller %s: Retrying to get the hostIP of replica %d", name, i+1))
|
2015-01-16 21:11:42 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-fetch the pod information to update the host port information.
|
|
|
|
pods, err = c.Pods(ns).List(label)
|
2015-02-10 00:15:04 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-13 02:11:27 +00:00
|
|
|
|
|
|
|
// Verify that something is listening.
|
|
|
|
for i, pod := range pods.Items {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://%s:8080", pod.Status.HostIP))
|
|
|
|
if err != nil {
|
2015-02-10 00:15:04 +00:00
|
|
|
Fail(fmt.Sprintf("Controller %s: Failed to GET from replica %d: %v", name, i+1, err))
|
2015-01-13 02:11:27 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-02-10 00:15:04 +00:00
|
|
|
Fail(fmt.Sprintf("Controller %s: Expected OK status code for replica %d but got %d", name, i+1, resp.StatusCode))
|
2015-01-13 02:11:27 +00:00
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2015-02-10 00:15:04 +00:00
|
|
|
Fail(fmt.Sprintf("Controller %s: Failed to read the body of the GET response from replica %d: %v",
|
|
|
|
name, i+1, err))
|
2015-01-13 02:11:27 +00:00
|
|
|
}
|
2015-01-16 21:11:42 +00:00
|
|
|
// The body should be the pod name.
|
|
|
|
if string(body) != pod.Name {
|
2015-02-10 00:15:04 +00:00
|
|
|
Fail(fmt.Sprintf("Controller %s: Replica %d expected response %s but got %s", name, i+1, pod.Name, string(body)))
|
2015-01-13 02:11:27 +00:00
|
|
|
}
|
2015-02-10 00:15:04 +00:00
|
|
|
By(fmt.Sprintf("Controller %s: Got expected result from replica %d: %s", name, i+1, string(body)))
|
2015-01-13 02:11:27 +00:00
|
|
|
}
|
2015-01-15 23:24:58 +00:00
|
|
|
}
|