docker_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 e2e_node
  14. import (
  15. "fmt"
  16. "strings"
  17. "time"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. imageutils "k8s.io/kubernetes/test/utils/image"
  22. . "github.com/onsi/ginkgo"
  23. . "github.com/onsi/gomega"
  24. )
  25. var _ = framework.KubeDescribe("Docker features [Feature:Docker][Legacy:Docker]", func() {
  26. f := framework.NewDefaultFramework("docker-feature-test")
  27. BeforeEach(func() {
  28. framework.RunIfContainerRuntimeIs("docker")
  29. })
  30. Context("when live-restore is enabled [Serial] [Slow] [Disruptive]", func() {
  31. It("containers should not be disrupted when the daemon shuts down and restarts", func() {
  32. const (
  33. podName = "live-restore-test-pod"
  34. containerName = "live-restore-test-container"
  35. )
  36. isSupported, err := isDockerLiveRestoreSupported()
  37. framework.ExpectNoError(err)
  38. if !isSupported {
  39. framework.Skipf("Docker live-restore is not supported.")
  40. }
  41. isEnabled, err := isDockerLiveRestoreEnabled()
  42. framework.ExpectNoError(err)
  43. if !isEnabled {
  44. framework.Skipf("Docker live-restore is not enabled.")
  45. }
  46. By("Create the test pod.")
  47. pod := f.PodClient().CreateSync(&v1.Pod{
  48. ObjectMeta: metav1.ObjectMeta{Name: podName},
  49. Spec: v1.PodSpec{
  50. Containers: []v1.Container{{
  51. Name: containerName,
  52. Image: imageutils.GetE2EImage(imageutils.Nginx),
  53. }},
  54. },
  55. })
  56. By("Ensure that the container is running before Docker is down.")
  57. Eventually(func() bool {
  58. return isContainerRunning(pod.Status.PodIP)
  59. }).Should(BeTrue())
  60. startTime1, err := getContainerStartTime(f, podName, containerName)
  61. framework.ExpectNoError(err)
  62. By("Stop Docker daemon.")
  63. framework.ExpectNoError(stopDockerDaemon())
  64. isDockerDown := true
  65. defer func() {
  66. if isDockerDown {
  67. By("Start Docker daemon.")
  68. framework.ExpectNoError(startDockerDaemon())
  69. }
  70. }()
  71. By("Ensure that the container is running after Docker is down.")
  72. Consistently(func() bool {
  73. return isContainerRunning(pod.Status.PodIP)
  74. }).Should(BeTrue())
  75. By("Start Docker daemon.")
  76. framework.ExpectNoError(startDockerDaemon())
  77. isDockerDown = false
  78. By("Ensure that the container is running after Docker has restarted.")
  79. Consistently(func() bool {
  80. return isContainerRunning(pod.Status.PodIP)
  81. }).Should(BeTrue())
  82. By("Ensure that the container has not been restarted after Docker is restarted.")
  83. Consistently(func() bool {
  84. startTime2, err := getContainerStartTime(f, podName, containerName)
  85. framework.ExpectNoError(err)
  86. return startTime1 == startTime2
  87. }, 3*time.Second, time.Second).Should(BeTrue())
  88. })
  89. })
  90. })
  91. // isContainerRunning returns true if the container is running by checking
  92. // whether the server is responding, and false otherwise.
  93. func isContainerRunning(podIP string) bool {
  94. output, err := runCommand("curl", podIP)
  95. if err != nil {
  96. return false
  97. }
  98. return strings.Contains(output, "Welcome to nginx!")
  99. }
  100. // getContainerStartTime returns the start time of the container with the
  101. // containerName of the pod having the podName.
  102. func getContainerStartTime(f *framework.Framework, podName, containerName string) (time.Time, error) {
  103. pod, err := f.PodClient().Get(podName, metav1.GetOptions{})
  104. if err != nil {
  105. return time.Time{}, fmt.Errorf("failed to get pod %q: %v", podName, err)
  106. }
  107. for _, status := range pod.Status.ContainerStatuses {
  108. if status.Name != containerName {
  109. continue
  110. }
  111. if status.State.Running == nil {
  112. return time.Time{}, fmt.Errorf("%v/%v is not running", podName, containerName)
  113. }
  114. return status.State.Running.StartedAt.Time, nil
  115. }
  116. return time.Time{}, fmt.Errorf("failed to find %v/%v", podName, containerName)
  117. }