123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package e2enode
- import (
- "context"
- "fmt"
- "strings"
- "time"
- "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/kubernetes/test/e2e/framework"
- e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
- imageutils "k8s.io/kubernetes/test/utils/image"
- "github.com/onsi/ginkgo"
- "github.com/onsi/gomega"
- )
- var _ = framework.KubeDescribe("Docker features [Feature:Docker][Legacy:Docker]", func() {
- f := framework.NewDefaultFramework("docker-feature-test")
- ginkgo.BeforeEach(func() {
- e2eskipper.RunIfContainerRuntimeIs("docker")
- })
- ginkgo.Context("when live-restore is enabled [Serial] [Slow] [Disruptive]", func() {
- ginkgo.It("containers should not be disrupted when the daemon shuts down and restarts", func() {
- const (
- podName = "live-restore-test-pod"
- containerName = "live-restore-test-container"
- )
- isSupported, err := isDockerLiveRestoreSupported()
- framework.ExpectNoError(err)
- if !isSupported {
- e2eskipper.Skipf("Docker live-restore is not supported.")
- }
- isEnabled, err := isDockerLiveRestoreEnabled()
- framework.ExpectNoError(err)
- if !isEnabled {
- e2eskipper.Skipf("Docker live-restore is not enabled.")
- }
- ginkgo.By("Create the test pod.")
- pod := f.PodClient().CreateSync(&v1.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: podName},
- Spec: v1.PodSpec{
- Containers: []v1.Container{{
- Name: containerName,
- Image: imageutils.GetE2EImage(imageutils.Nginx),
- }},
- },
- })
- ginkgo.By("Ensure that the container is running before Docker is down.")
- gomega.Eventually(func() bool {
- return isContainerRunning(pod.Status.PodIP)
- }).Should(gomega.BeTrue())
- startTime1, err := getContainerStartTime(f, podName, containerName)
- framework.ExpectNoError(err)
- ginkgo.By("Stop Docker daemon.")
- framework.ExpectNoError(stopDockerDaemon())
- isDockerDown := true
- defer func() {
- if isDockerDown {
- ginkgo.By("Start Docker daemon.")
- framework.ExpectNoError(startDockerDaemon())
- }
- }()
- ginkgo.By("Ensure that the container is running after Docker is down.")
- gomega.Consistently(func() bool {
- return isContainerRunning(pod.Status.PodIP)
- }).Should(gomega.BeTrue())
- ginkgo.By("Start Docker daemon.")
- framework.ExpectNoError(startDockerDaemon())
- isDockerDown = false
- ginkgo.By("Ensure that the container is running after Docker has restarted.")
- gomega.Consistently(func() bool {
- return isContainerRunning(pod.Status.PodIP)
- }).Should(gomega.BeTrue())
- ginkgo.By("Ensure that the container has not been restarted after Docker is restarted.")
- gomega.Consistently(func() bool {
- startTime2, err := getContainerStartTime(f, podName, containerName)
- framework.ExpectNoError(err)
- return startTime1 == startTime2
- }, 3*time.Second, time.Second).Should(gomega.BeTrue())
- })
- })
- })
- func isContainerRunning(podIP string) bool {
- output, err := runCommand("curl", podIP)
- if err != nil {
- return false
- }
- return strings.Contains(output, "Welcome to nginx!")
- }
- func getContainerStartTime(f *framework.Framework, podName, containerName string) (time.Time, error) {
- pod, err := f.PodClient().Get(context.TODO(), podName, metav1.GetOptions{})
- if err != nil {
- return time.Time{}, fmt.Errorf("failed to get pod %q: %v", podName, err)
- }
- for _, status := range pod.Status.ContainerStatuses {
- if status.Name != containerName {
- continue
- }
- if status.State.Running == nil {
- return time.Time{}, fmt.Errorf("%v/%v is not running", podName, containerName)
- }
- return status.State.Running.StartedAt.Time, nil
- }
- return time.Time{}, fmt.Errorf("failed to find %v/%v", podName, containerName)
- }
|