docker_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2enode
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "time"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  23. imageutils "k8s.io/kubernetes/test/utils/image"
  24. "github.com/onsi/ginkgo"
  25. "github.com/onsi/gomega"
  26. )
  27. var _ = framework.KubeDescribe("Docker features [Feature:Docker][Legacy:Docker]", func() {
  28. f := framework.NewDefaultFramework("docker-feature-test")
  29. ginkgo.BeforeEach(func() {
  30. e2eskipper.RunIfContainerRuntimeIs("docker")
  31. })
  32. ginkgo.Context("when live-restore is enabled [Serial] [Slow] [Disruptive]", func() {
  33. ginkgo.It("containers should not be disrupted when the daemon shuts down and restarts", func() {
  34. const (
  35. podName = "live-restore-test-pod"
  36. containerName = "live-restore-test-container"
  37. )
  38. isSupported, err := isDockerLiveRestoreSupported()
  39. framework.ExpectNoError(err)
  40. if !isSupported {
  41. e2eskipper.Skipf("Docker live-restore is not supported.")
  42. }
  43. isEnabled, err := isDockerLiveRestoreEnabled()
  44. framework.ExpectNoError(err)
  45. if !isEnabled {
  46. e2eskipper.Skipf("Docker live-restore is not enabled.")
  47. }
  48. ginkgo.By("Create the test pod.")
  49. pod := f.PodClient().CreateSync(&v1.Pod{
  50. ObjectMeta: metav1.ObjectMeta{Name: podName},
  51. Spec: v1.PodSpec{
  52. Containers: []v1.Container{{
  53. Name: containerName,
  54. Image: imageutils.GetE2EImage(imageutils.Nginx),
  55. }},
  56. },
  57. })
  58. ginkgo.By("Ensure that the container is running before Docker is down.")
  59. gomega.Eventually(func() bool {
  60. return isContainerRunning(pod.Status.PodIP)
  61. }).Should(gomega.BeTrue())
  62. startTime1, err := getContainerStartTime(f, podName, containerName)
  63. framework.ExpectNoError(err)
  64. ginkgo.By("Stop Docker daemon.")
  65. framework.ExpectNoError(stopDockerDaemon())
  66. isDockerDown := true
  67. defer func() {
  68. if isDockerDown {
  69. ginkgo.By("Start Docker daemon.")
  70. framework.ExpectNoError(startDockerDaemon())
  71. }
  72. }()
  73. ginkgo.By("Ensure that the container is running after Docker is down.")
  74. gomega.Consistently(func() bool {
  75. return isContainerRunning(pod.Status.PodIP)
  76. }).Should(gomega.BeTrue())
  77. ginkgo.By("Start Docker daemon.")
  78. framework.ExpectNoError(startDockerDaemon())
  79. isDockerDown = false
  80. ginkgo.By("Ensure that the container is running after Docker has restarted.")
  81. gomega.Consistently(func() bool {
  82. return isContainerRunning(pod.Status.PodIP)
  83. }).Should(gomega.BeTrue())
  84. ginkgo.By("Ensure that the container has not been restarted after Docker is restarted.")
  85. gomega.Consistently(func() bool {
  86. startTime2, err := getContainerStartTime(f, podName, containerName)
  87. framework.ExpectNoError(err)
  88. return startTime1 == startTime2
  89. }, 3*time.Second, time.Second).Should(gomega.BeTrue())
  90. })
  91. })
  92. })
  93. // isContainerRunning returns true if the container is running by checking
  94. // whether the server is responding, and false otherwise.
  95. func isContainerRunning(podIP string) bool {
  96. output, err := runCommand("curl", podIP)
  97. if err != nil {
  98. return false
  99. }
  100. return strings.Contains(output, "Welcome to nginx!")
  101. }
  102. // getContainerStartTime returns the start time of the container with the
  103. // containerName of the pod having the podName.
  104. func getContainerStartTime(f *framework.Framework, podName, containerName string) (time.Time, error) {
  105. pod, err := f.PodClient().Get(context.TODO(), podName, metav1.GetOptions{})
  106. if err != nil {
  107. return time.Time{}, fmt.Errorf("failed to get pod %q: %v", podName, err)
  108. }
  109. for _, status := range pod.Status.ContainerStatuses {
  110. if status.Name != containerName {
  111. continue
  112. }
  113. if status.State.Running == nil {
  114. return time.Time{}, fmt.Errorf("%v/%v is not running", podName, containerName)
  115. }
  116. return status.State.Running.StartedAt.Time, nil
  117. }
  118. return time.Time{}, fmt.Errorf("failed to find %v/%v", podName, containerName)
  119. }