Commit 6c0191b3 authored by Dominika Hodovska's avatar Dominika Hodovska

Modify e2e test for init containers in stable

parent a856188b
...@@ -78,31 +78,42 @@ var _ = framework.KubeDescribe("InitContainer", func() { ...@@ -78,31 +78,42 @@ var _ = framework.KubeDescribe("InitContainer", func() {
}, },
}, },
} }
if err := podutil.SetInitContainersAnnotations(pod); err != nil { stable := true
Expect(err).To(BeNil()) for i := 0; i < 2; i++ {
} if !stable {
startedPod := podClient.Create(pod) framework.Logf("PodSpec: initContainers in metadata.annotation")
w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta)) if err := podutil.SetInitContainersAnnotations(pod); err != nil {
Expect(err).NotTo(HaveOccurred(), "error watching a pod") Expect(err).To(BeNil())
wr := watch.NewRecorder(w) }
event, err := watch.Until(framework.PodStartTimeout, wr, conditions.PodCompleted) } else {
Expect(err).To(BeNil()) framework.Logf("PodSpec: initContainers in spec.initContainers")
framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant) }
endPod := event.Object.(*v1.Pod) startedPod := podClient.Create(pod)
if err := podutil.SetInitContainersAndStatuses(endPod); err != nil { w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta))
Expect(err).NotTo(HaveOccurred(), "error watching a pod")
wr := watch.NewRecorder(w)
event, err := watch.Until(framework.PodStartTimeout, wr, conditions.PodCompleted)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
} framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant)
endPod := event.Object.(*v1.Pod)
if err := podutil.SetInitContainersAndStatuses(endPod); err != nil {
Expect(err).To(BeNil())
}
Expect(endPod.Status.Phase).To(Equal(v1.PodSucceeded)) Expect(endPod.Status.Phase).To(Equal(v1.PodSucceeded))
_, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized) _, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized)
Expect(init).NotTo(BeNil()) Expect(init).NotTo(BeNil())
Expect(init.Status).To(Equal(v1.ConditionTrue)) Expect(init.Status).To(Equal(v1.ConditionTrue))
Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2)) Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2))
for _, status := range endPod.Status.InitContainerStatuses { for _, status := range endPod.Status.InitContainerStatuses {
Expect(status.Ready).To(BeTrue()) Expect(status.Ready).To(BeTrue())
Expect(status.State.Terminated).NotTo(BeNil()) Expect(status.State.Terminated).NotTo(BeNil())
Expect(status.State.Terminated.ExitCode).To(BeZero()) Expect(status.State.Terminated.ExitCode).To(BeZero())
}
stable = false
name := "pod-init-" + string(uuid.NewUUID())
pod.Name = name
} }
}) })
...@@ -147,31 +158,42 @@ var _ = framework.KubeDescribe("InitContainer", func() { ...@@ -147,31 +158,42 @@ var _ = framework.KubeDescribe("InitContainer", func() {
}, },
}, },
} }
if err := podutil.SetInitContainersAnnotations(pod); err != nil { stable := true
for i := 0; i < 2; i++ {
if !stable {
framework.Logf("PodSpec: initContainers in metadata.annotation")
if err := podutil.SetInitContainersAnnotations(pod); err != nil {
Expect(err).To(BeNil())
}
} else {
framework.Logf("PodSpec: initContainers in spec.initContainers")
}
startedPod := podClient.Create(pod)
w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta))
Expect(err).NotTo(HaveOccurred(), "error watching a pod")
wr := watch.NewRecorder(w)
event, err := watch.Until(framework.PodStartTimeout, wr, conditions.PodRunning)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
} framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant)
startedPod := podClient.Create(pod) endPod := event.Object.(*v1.Pod)
w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta))
Expect(err).NotTo(HaveOccurred(), "error watching a pod")
wr := watch.NewRecorder(w)
event, err := watch.Until(framework.PodStartTimeout, wr, conditions.PodRunning)
Expect(err).To(BeNil())
framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant)
endPod := event.Object.(*v1.Pod)
Expect(endPod.Status.Phase).To(Equal(v1.PodRunning)) Expect(endPod.Status.Phase).To(Equal(v1.PodRunning))
_, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized) _, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized)
Expect(init).NotTo(BeNil()) Expect(init).NotTo(BeNil())
Expect(init.Status).To(Equal(v1.ConditionTrue)) Expect(init.Status).To(Equal(v1.ConditionTrue))
if err := podutil.SetInitContainersAndStatuses(endPod); err != nil { if err := podutil.SetInitContainersAndStatuses(endPod); err != nil {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
} }
Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2)) Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2))
for _, status := range endPod.Status.InitContainerStatuses { for _, status := range endPod.Status.InitContainerStatuses {
Expect(status.Ready).To(BeTrue()) Expect(status.Ready).To(BeTrue())
Expect(status.State.Terminated).NotTo(BeNil()) Expect(status.State.Terminated).NotTo(BeNil())
Expect(status.State.Terminated.ExitCode).To(BeZero()) Expect(status.State.Terminated.ExitCode).To(BeZero())
}
stable = false
name := "pod-init-" + string(uuid.NewUUID())
pod.Name = name
} }
}) })
...@@ -217,84 +239,95 @@ var _ = framework.KubeDescribe("InitContainer", func() { ...@@ -217,84 +239,95 @@ var _ = framework.KubeDescribe("InitContainer", func() {
}, },
}, },
} }
if err := podutil.SetInitContainersAnnotations(pod); err != nil { stable := true
Expect(err).To(BeNil()) for i := 0; i < 2; i++ {
} if !stable {
startedPod := podClient.Create(pod) framework.Logf("PodSpec: initContainers in metadata.annotation")
w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta)) if err := podutil.SetInitContainersAnnotations(pod); err != nil {
Expect(err).NotTo(HaveOccurred(), "error watching a pod") Expect(err).To(BeNil())
}
} else {
framework.Logf("PodSpec: initContainers in spec.initContainers")
}
startedPod := podClient.Create(pod)
w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta))
Expect(err).NotTo(HaveOccurred(), "error watching a pod")
wr := watch.NewRecorder(w) wr := watch.NewRecorder(w)
event, err := watch.Until( event, err := watch.Until(
framework.PodStartTimeout, wr, framework.PodStartTimeout, wr,
// check for the first container to fail at least once // check for the first container to fail at least once
func(evt watch.Event) (bool, error) { func(evt watch.Event) (bool, error) {
switch t := evt.Object.(type) { switch t := evt.Object.(type) {
case *v1.Pod: case *v1.Pod:
if err := podutil.SetInitContainersAndStatuses(t); err != nil { if err := podutil.SetInitContainersAndStatuses(t); err != nil {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
} }
for _, status := range t.Status.ContainerStatuses { for _, status := range t.Status.ContainerStatuses {
if status.State.Waiting == nil {
return false, fmt.Errorf("container %q should not be out of waiting: %#v", status.Name, status)
}
if status.State.Waiting.Reason != "PodInitializing" {
return false, fmt.Errorf("container %q should have reason PodInitializing: %#v", status.Name, status)
}
}
if len(t.Status.InitContainerStatuses) != 2 {
return false, nil
}
status := t.Status.InitContainerStatuses[1]
if status.State.Waiting == nil { if status.State.Waiting == nil {
return false, fmt.Errorf("container %q should not be out of waiting: %#v", status.Name, status) return false, fmt.Errorf("second init container should not be out of waiting: %#v", status)
} }
if status.State.Waiting.Reason != "PodInitializing" { if status.State.Waiting.Reason != "PodInitializing" {
return false, fmt.Errorf("container %q should have reason PodInitializing: %#v", status.Name, status) return false, fmt.Errorf("second init container should have reason PodInitializing: %#v", status)
} }
status = t.Status.InitContainerStatuses[0]
if status.State.Terminated != nil && status.State.Terminated.ExitCode == 0 {
return false, fmt.Errorf("first init container should have exitCode != 0: %#v", status)
}
// continue until we see an attempt to restart the pod
return status.LastTerminationState.Terminated != nil, nil
default:
return false, fmt.Errorf("unexpected object: %#v", t)
} }
if len(t.Status.InitContainerStatuses) != 2 { },
return false, nil // verify we get two restarts
} func(evt watch.Event) (bool, error) {
status := t.Status.InitContainerStatuses[1] switch t := evt.Object.(type) {
if status.State.Waiting == nil { case *v1.Pod:
return false, fmt.Errorf("second init container should not be out of waiting: %#v", status) if err := podutil.SetInitContainersAndStatuses(t); err != nil {
} Expect(err).To(BeNil())
if status.State.Waiting.Reason != "PodInitializing" { }
return false, fmt.Errorf("second init container should have reason PodInitializing: %#v", status) status := t.Status.InitContainerStatuses[0]
} if status.RestartCount < 3 {
status = t.Status.InitContainerStatuses[0] return false, nil
if status.State.Terminated != nil && status.State.Terminated.ExitCode == 0 { }
return false, fmt.Errorf("first init container should have exitCode != 0: %#v", status) framework.Logf("init container has failed twice: %#v", t)
} // TODO: more conditions
// continue until we see an attempt to restart the pod return true, nil
return status.LastTerminationState.Terminated != nil, nil default:
default: return false, fmt.Errorf("unexpected object: %#v", t)
return false, fmt.Errorf("unexpected object: %#v", t)
}
},
// verify we get two restarts
func(evt watch.Event) (bool, error) {
switch t := evt.Object.(type) {
case *v1.Pod:
if err := podutil.SetInitContainersAndStatuses(t); err != nil {
Expect(err).To(BeNil())
}
status := t.Status.InitContainerStatuses[0]
if status.RestartCount < 3 {
return false, nil
} }
framework.Logf("init container has failed twice: %#v", t) },
// TODO: more conditions )
return true, nil
default:
return false, fmt.Errorf("unexpected object: %#v", t)
}
},
)
Expect(err).To(BeNil())
framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant)
endPod := event.Object.(*v1.Pod)
if err := podutil.SetInitContainersAndStatuses(endPod); err != nil {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
} framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant)
endPod := event.Object.(*v1.Pod)
if err := podutil.SetInitContainersAndStatuses(endPod); err != nil {
Expect(err).To(BeNil())
}
Expect(endPod.Status.Phase).To(Equal(v1.PodPending)) Expect(endPod.Status.Phase).To(Equal(v1.PodPending))
_, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized) _, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized)
Expect(init).NotTo(BeNil()) Expect(init).NotTo(BeNil())
Expect(init.Status).To(Equal(v1.ConditionFalse)) Expect(init.Status).To(Equal(v1.ConditionFalse))
Expect(init.Reason).To(Equal("ContainersNotInitialized")) Expect(init.Reason).To(Equal("ContainersNotInitialized"))
Expect(init.Message).To(Equal("containers with incomplete status: [init1 init2]")) Expect(init.Message).To(Equal("containers with incomplete status: [init1 init2]"))
Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2)) Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2))
stable = false
name := "pod-init-" + string(uuid.NewUUID())
pod.Name = name
}
}) })
It("should not start app containers and fail the pod if init containers fail on a RestartNever pod", func() { It("should not start app containers and fail the pod if init containers fail on a RestartNever pod", func() {
...@@ -340,70 +373,81 @@ var _ = framework.KubeDescribe("InitContainer", func() { ...@@ -340,70 +373,81 @@ var _ = framework.KubeDescribe("InitContainer", func() {
}, },
}, },
} }
if err := podutil.SetInitContainersAnnotations(pod); err != nil { stable := true
Expect(err).To(BeNil()) for i := 0; i < 2; i++ {
} if !stable {
startedPod := podClient.Create(pod) framework.Logf("PodSpec: initContainers in metadata.annotation")
if err := podutil.SetInitContainersAnnotations(pod); err != nil {
Expect(err).To(BeNil())
}
} else {
framework.Logf("PodSpec: initContainers in spec.initContainers")
}
startedPod := podClient.Create(pod)
w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta)) w, err := podClient.Watch(metav1.SingleObject(startedPod.ObjectMeta))
Expect(err).NotTo(HaveOccurred(), "error watching a pod") Expect(err).NotTo(HaveOccurred(), "error watching a pod")
wr := watch.NewRecorder(w) wr := watch.NewRecorder(w)
event, err := watch.Until( event, err := watch.Until(
framework.PodStartTimeout, wr, framework.PodStartTimeout, wr,
// check for the second container to fail at least once // check for the second container to fail at least once
func(evt watch.Event) (bool, error) { func(evt watch.Event) (bool, error) {
switch t := evt.Object.(type) { switch t := evt.Object.(type) {
case *v1.Pod: case *v1.Pod:
if err := podutil.SetInitContainersAndStatuses(t); err != nil { if err := podutil.SetInitContainersAndStatuses(t); err != nil {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
}
for _, status := range t.Status.ContainerStatuses {
if status.State.Waiting == nil {
return false, fmt.Errorf("container %q should not be out of waiting: %#v", status.Name, status)
} }
if status.State.Waiting.Reason != "PodInitializing" { for _, status := range t.Status.ContainerStatuses {
return false, fmt.Errorf("container %q should have reason PodInitializing: %#v", status.Name, status) if status.State.Waiting == nil {
return false, fmt.Errorf("container %q should not be out of waiting: %#v", status.Name, status)
}
if status.State.Waiting.Reason != "PodInitializing" {
return false, fmt.Errorf("container %q should have reason PodInitializing: %#v", status.Name, status)
}
} }
} if len(t.Status.InitContainerStatuses) != 2 {
if len(t.Status.InitContainerStatuses) != 2 { return false, nil
return false, nil
}
status := t.Status.InitContainerStatuses[0]
if status.State.Terminated == nil {
if status.State.Waiting != nil && status.State.Waiting.Reason != "PodInitializing" {
return false, fmt.Errorf("second init container should have reason PodInitializing: %#v", status)
} }
return false, nil status := t.Status.InitContainerStatuses[0]
} if status.State.Terminated == nil {
if status.State.Terminated != nil && status.State.Terminated.ExitCode != 0 { if status.State.Waiting != nil && status.State.Waiting.Reason != "PodInitializing" {
return false, fmt.Errorf("first init container should have exitCode != 0: %#v", status) return false, fmt.Errorf("second init container should have reason PodInitializing: %#v", status)
} }
status = t.Status.InitContainerStatuses[1] return false, nil
if status.State.Terminated == nil { }
return false, nil if status.State.Terminated != nil && status.State.Terminated.ExitCode != 0 {
} return false, fmt.Errorf("first init container should have exitCode != 0: %#v", status)
if status.State.Terminated.ExitCode == 0 { }
return false, fmt.Errorf("second init container should have failed: %#v", status) status = t.Status.InitContainerStatuses[1]
if status.State.Terminated == nil {
return false, nil
}
if status.State.Terminated.ExitCode == 0 {
return false, fmt.Errorf("second init container should have failed: %#v", status)
}
return true, nil
default:
return false, fmt.Errorf("unexpected object: %#v", t)
} }
return true, nil },
default: conditions.PodCompleted,
return false, fmt.Errorf("unexpected object: %#v", t) )
} Expect(err).To(BeNil())
}, framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant)
conditions.PodCompleted, endPod := event.Object.(*v1.Pod)
)
Expect(err).To(BeNil())
framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant)
endPod := event.Object.(*v1.Pod)
Expect(endPod.Status.Phase).To(Equal(v1.PodFailed)) Expect(endPod.Status.Phase).To(Equal(v1.PodFailed))
_, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized) _, init := v1.GetPodCondition(&endPod.Status, v1.PodInitialized)
Expect(init).NotTo(BeNil()) Expect(init).NotTo(BeNil())
Expect(init.Status).To(Equal(v1.ConditionFalse)) Expect(init.Status).To(Equal(v1.ConditionFalse))
Expect(init.Reason).To(Equal("ContainersNotInitialized")) Expect(init.Reason).To(Equal("ContainersNotInitialized"))
Expect(init.Message).To(Equal("containers with incomplete status: [init2]")) Expect(init.Message).To(Equal("containers with incomplete status: [init2]"))
Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2)) Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2))
Expect(endPod.Status.ContainerStatuses[0].State.Waiting).ToNot(BeNil()) Expect(endPod.Status.ContainerStatuses[0].State.Waiting).ToNot(BeNil())
stable = false
name := "pod-init-" + string(uuid.NewUUID())
pod.Name = name
}
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment