2015-06-16 01:02:30 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-06-16 01:02:30 +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 (
|
2015-12-05 00:43:38 +00:00
|
|
|
"fmt"
|
2015-06-16 01:02:30 +00:00
|
|
|
"time"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-08-13 19:01:50 +00:00
|
|
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-11-10 06:28:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/intstr"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
2016-04-07 17:21:31 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2015-06-16 01:02:30 +00:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2015-12-05 00:43:38 +00:00
|
|
|
const (
|
2016-06-28 17:08:06 +00:00
|
|
|
probTestContainerName = "test-webserver"
|
2015-12-05 00:43:38 +00:00
|
|
|
probTestInitialDelaySeconds = 30
|
|
|
|
)
|
|
|
|
|
2016-04-07 17:21:31 +00:00
|
|
|
var _ = framework.KubeDescribe("Probing container", func() {
|
|
|
|
f := framework.NewDefaultFramework("container-probe")
|
2015-06-16 01:02:30 +00:00
|
|
|
var podClient client.PodInterface
|
2015-07-06 16:11:10 +00:00
|
|
|
probe := webserverProbeBuilder{}
|
2015-06-16 01:02:30 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2016-04-07 17:21:31 +00:00
|
|
|
podClient = f.Client.Pods(f.Namespace.Name)
|
2015-06-16 01:02:30 +00:00
|
|
|
})
|
|
|
|
|
2015-10-07 14:09:24 +00:00
|
|
|
It("with readiness probe should not be ready before initial delay and never restart [Conformance]", func() {
|
2015-06-16 01:02:30 +00:00
|
|
|
p, err := podClient.Create(makePodSpec(probe.withInitialDelay().build(), nil))
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.ExpectNoError(err)
|
2015-06-16 01:02:30 +00:00
|
|
|
|
2016-04-07 17:21:31 +00:00
|
|
|
Expect(wait.Poll(framework.Poll, 240*time.Second, func() (bool, error) {
|
2015-06-16 01:02:30 +00:00
|
|
|
p, err := podClient.Get(p.Name)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-07-06 16:11:10 +00:00
|
|
|
ready := api.IsPodReady(p)
|
|
|
|
if !ready {
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.Logf("pod is not yet ready; pod has phase %q.", p.Status.Phase)
|
2015-07-06 16:11:10 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})).NotTo(HaveOccurred(), "pod never became ready")
|
2015-06-16 01:02:30 +00:00
|
|
|
|
|
|
|
p, err = podClient.Get(p.Name)
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.ExpectNoError(err)
|
|
|
|
isReady, err := framework.PodRunningReady(p)
|
|
|
|
framework.ExpectNoError(err)
|
2015-07-06 16:11:10 +00:00
|
|
|
Expect(isReady).To(BeTrue(), "pod should be ready")
|
2015-06-16 01:02:30 +00:00
|
|
|
|
2015-12-05 00:43:38 +00:00
|
|
|
// We assume the pod became ready when the container became ready. This
|
|
|
|
// is true for a single container pod.
|
|
|
|
readyTime, err := getTransitionTimeForReadyCondition(p)
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.ExpectNoError(err)
|
2015-12-05 00:43:38 +00:00
|
|
|
startedTime, err := getContainerStartedTime(p, probTestContainerName)
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.ExpectNoError(err)
|
2015-12-05 00:43:38 +00:00
|
|
|
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.Logf("Container started at %v, pod became ready at %v", startedTime, readyTime)
|
2015-12-05 00:43:38 +00:00
|
|
|
initialDelay := probTestInitialDelaySeconds * time.Second
|
|
|
|
if readyTime.Sub(startedTime) < initialDelay {
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.Failf("Pod became ready before it's %v initial delay", initialDelay)
|
2015-12-05 00:43:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 16:11:10 +00:00
|
|
|
restartCount := getRestartCount(p)
|
|
|
|
Expect(restartCount == 0).To(BeTrue(), "pod should have a restart count of 0 but got %v", restartCount)
|
2015-06-16 01:02:30 +00:00
|
|
|
})
|
|
|
|
|
2015-10-07 14:09:24 +00:00
|
|
|
It("with readiness probe that fails should never be ready and never restart [Conformance]", func() {
|
2015-06-16 01:02:30 +00:00
|
|
|
p, err := podClient.Create(makePodSpec(probe.withFailing().build(), nil))
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.ExpectNoError(err)
|
2015-06-16 01:02:30 +00:00
|
|
|
|
2016-04-07 17:21:31 +00:00
|
|
|
err = wait.Poll(framework.Poll, 180*time.Second, func() (bool, error) {
|
2015-06-16 01:02:30 +00:00
|
|
|
p, err := podClient.Get(p.Name)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return api.IsPodReady(p), nil
|
|
|
|
})
|
|
|
|
if err != wait.ErrWaitTimeout {
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.Failf("expecting wait timeout error but got: %v", err)
|
2015-06-16 01:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p, err = podClient.Get(p.Name)
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.ExpectNoError(err)
|
2015-06-16 01:02:30 +00:00
|
|
|
|
2016-04-07 17:21:31 +00:00
|
|
|
isReady, err := framework.PodRunningReady(p)
|
2015-07-06 16:11:10 +00:00
|
|
|
Expect(isReady).NotTo(BeTrue(), "pod should be not ready")
|
2015-06-16 01:02:30 +00:00
|
|
|
|
2015-07-06 16:11:10 +00:00
|
|
|
restartCount := getRestartCount(p)
|
|
|
|
Expect(restartCount == 0).To(BeTrue(), "pod should have a restart count of 0 but got %v", restartCount)
|
2015-06-16 01:02:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2015-12-05 00:43:38 +00:00
|
|
|
func getContainerStartedTime(p *api.Pod, containerName string) (time.Time, error) {
|
|
|
|
for _, status := range p.Status.ContainerStatuses {
|
|
|
|
if status.Name != containerName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status.State.Running == nil {
|
|
|
|
return time.Time{}, fmt.Errorf("Container is not running")
|
|
|
|
}
|
|
|
|
return status.State.Running.StartedAt.Time, nil
|
|
|
|
}
|
|
|
|
return time.Time{}, fmt.Errorf("cannot find container named %q", containerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTransitionTimeForReadyCondition(p *api.Pod) (time.Time, error) {
|
|
|
|
for _, cond := range p.Status.Conditions {
|
|
|
|
if cond.Type == api.PodReady {
|
|
|
|
return cond.LastTransitionTime.Time, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Time{}, fmt.Errorf("No ready condition can be found for pod")
|
|
|
|
}
|
|
|
|
|
2015-06-16 01:02:30 +00:00
|
|
|
func getRestartCount(p *api.Pod) int {
|
|
|
|
count := 0
|
|
|
|
for _, containerStatus := range p.Status.ContainerStatuses {
|
2016-04-27 04:35:14 +00:00
|
|
|
count += int(containerStatus.RestartCount)
|
2015-06-16 01:02:30 +00:00
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePodSpec(readinessProbe, livenessProbe *api.Probe) *api.Pod {
|
|
|
|
pod := &api.Pod{
|
2015-07-06 16:11:10 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test-webserver-" + string(util.NewUUID())},
|
2015-06-16 01:02:30 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
2015-12-05 00:43:38 +00:00
|
|
|
Name: probTestContainerName,
|
2016-02-20 04:22:49 +00:00
|
|
|
Image: "gcr.io/google_containers/test-webserver:e2e",
|
2015-06-16 01:02:30 +00:00
|
|
|
LivenessProbe: livenessProbe,
|
|
|
|
ReadinessProbe: readinessProbe,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return pod
|
|
|
|
}
|
|
|
|
|
2015-07-06 16:11:10 +00:00
|
|
|
type webserverProbeBuilder struct {
|
2015-06-16 01:02:30 +00:00
|
|
|
failing bool
|
|
|
|
initialDelay bool
|
|
|
|
}
|
|
|
|
|
2015-07-06 16:11:10 +00:00
|
|
|
func (b webserverProbeBuilder) withFailing() webserverProbeBuilder {
|
2015-06-16 01:02:30 +00:00
|
|
|
b.failing = true
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-07-06 16:11:10 +00:00
|
|
|
func (b webserverProbeBuilder) withInitialDelay() webserverProbeBuilder {
|
2015-06-16 01:02:30 +00:00
|
|
|
b.initialDelay = true
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-07-06 16:11:10 +00:00
|
|
|
func (b webserverProbeBuilder) build() *api.Probe {
|
2015-06-16 01:02:30 +00:00
|
|
|
probe := &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
HTTPGet: &api.HTTPGetAction{
|
2015-11-10 06:28:45 +00:00
|
|
|
Port: intstr.FromInt(80),
|
2015-06-16 01:02:30 +00:00
|
|
|
Path: "/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if b.initialDelay {
|
2015-12-05 00:43:38 +00:00
|
|
|
probe.InitialDelaySeconds = probTestInitialDelaySeconds
|
2015-06-16 01:02:30 +00:00
|
|
|
}
|
|
|
|
if b.failing {
|
2015-11-10 06:28:45 +00:00
|
|
|
probe.HTTPGet.Port = intstr.FromInt(81)
|
2015-06-16 01:02:30 +00:00
|
|
|
}
|
|
|
|
return probe
|
|
|
|
}
|