PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test/e2e_node/dockershim_checkpoint_test.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 229 lines | 189 code | 22 blank | 18 comment | 30 complexity | e49c8f563ba076a1a7161b0013edeba8 MD5 | raw file
  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. "crypto/md5"
  16. "fmt"
  17. "os"
  18. "os/exec"
  19. "path"
  20. "regexp"
  21. "strings"
  22. "time"
  23. . "github.com/onsi/ginkgo"
  24. . "github.com/onsi/gomega"
  25. "k8s.io/api/core/v1"
  26. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "k8s.io/apimachinery/pkg/util/uuid"
  28. "k8s.io/apimachinery/pkg/util/wait"
  29. "k8s.io/kubernetes/test/e2e/framework"
  30. imageutils "k8s.io/kubernetes/test/utils/image"
  31. )
  32. const (
  33. testCheckpoint = "checkpoint-test"
  34. // Container GC Period is 1 minute
  35. gcTimeout = 3 * time.Minute
  36. testCheckpointContent = `{"version":"v1","name":"fluentd-gcp-v2.0-vmnqx","namespace":"kube-system","data":{},"checksum":1799154314}`
  37. )
  38. var _ = SIGDescribe("Dockershim [Serial] [Disruptive] [Feature:Docker]", func() {
  39. f := framework.NewDefaultFramework("dockerhism-checkpoint-test")
  40. BeforeEach(func() {
  41. framework.RunIfContainerRuntimeIs("docker")
  42. })
  43. It("should clean up pod sandbox checkpoint after pod deletion", func() {
  44. podName := "pod-checkpoint-no-disrupt"
  45. runPodCheckpointTest(f, podName, func() {
  46. checkpoints := findCheckpoints(podName)
  47. if len(checkpoints) == 0 {
  48. framework.Failf("No checkpoint for the pod was found")
  49. }
  50. })
  51. })
  52. It("should remove dangling checkpoint file", func() {
  53. filename := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%s/%s", testCheckpoint, f.Namespace.Name))))
  54. fullpath := path.Join(framework.TestContext.DockershimCheckpointDir, filename)
  55. By(fmt.Sprintf("Write a file at %q", fullpath))
  56. err := writeFileAndSync(fullpath, []byte(testCheckpointContent))
  57. framework.ExpectNoError(err, "Failed to create file %q", fullpath)
  58. By("Check if file is removed")
  59. Eventually(func() bool {
  60. if _, err := os.Stat(fullpath); os.IsNotExist(err) {
  61. return true
  62. }
  63. return false
  64. }, gcTimeout, 10*time.Second).Should(BeTrue())
  65. })
  66. Context("When pod sandbox checkpoint is missing", func() {
  67. It("should complete pod sandbox clean up", func() {
  68. podName := "pod-checkpoint-missing"
  69. runPodCheckpointTest(f, podName, func() {
  70. checkpoints := findCheckpoints(podName)
  71. if len(checkpoints) == 0 {
  72. framework.Failf("No checkpoint for the pod was found")
  73. }
  74. By("Removing checkpoint of test pod")
  75. for _, filename := range checkpoints {
  76. if len(filename) == 0 {
  77. continue
  78. }
  79. framework.Logf("Removing checkpoint %q", filename)
  80. _, err := exec.Command("sudo", "rm", filename).CombinedOutput()
  81. framework.ExpectNoError(err, "Failed to remove checkpoint file %q: %v", string(filename), err)
  82. }
  83. })
  84. })
  85. })
  86. Context("When all containers in pod are missing", func() {
  87. It("should complete pod sandbox clean up based on the information in sandbox checkpoint", func() {
  88. runPodCheckpointTest(f, "pod-containers-missing", func() {
  89. By("Gathering pod container ids")
  90. stdout, err := exec.Command("sudo", "docker", "ps", "-q", "-f",
  91. fmt.Sprintf("name=%s", f.Namespace.Name)).CombinedOutput()
  92. framework.ExpectNoError(err, "Failed to run docker ps: %v", err)
  93. lines := strings.Split(string(stdout), "\n")
  94. ids := []string{}
  95. for _, id := range lines {
  96. id = cleanString(id)
  97. if len(id) > 0 {
  98. ids = append(ids, id)
  99. }
  100. }
  101. By("Stop and remove pod containers")
  102. dockerStopCmd := append([]string{"docker", "stop"}, ids...)
  103. _, err = exec.Command("sudo", dockerStopCmd...).CombinedOutput()
  104. framework.ExpectNoError(err, "Failed to run command %v: %v", dockerStopCmd, err)
  105. dockerRmCmd := append([]string{"docker", "rm"}, ids...)
  106. _, err = exec.Command("sudo", dockerRmCmd...).CombinedOutput()
  107. framework.ExpectNoError(err, "Failed to run command %v: %v", dockerRmCmd, err)
  108. })
  109. })
  110. })
  111. Context("When checkpoint file is corrupted", func() {
  112. It("should complete pod sandbox clean up", func() {
  113. podName := "pod-checkpoint-corrupted"
  114. runPodCheckpointTest(f, podName, func() {
  115. By("Corrupt checkpoint file")
  116. checkpoints := findCheckpoints(podName)
  117. if len(checkpoints) == 0 {
  118. framework.Failf("No checkpoint for the pod was found")
  119. }
  120. for _, file := range checkpoints {
  121. f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND, 0644)
  122. framework.ExpectNoError(err, "Failed to open file %q", file)
  123. _, err = f.WriteString("blabblab")
  124. framework.ExpectNoError(err, "Failed to write to file %q", file)
  125. f.Sync()
  126. f.Close()
  127. }
  128. })
  129. })
  130. })
  131. })
  132. func runPodCheckpointTest(f *framework.Framework, podName string, twist func()) {
  133. podName = podName + string(uuid.NewUUID())
  134. By(fmt.Sprintf("Creating test pod: %s", podName))
  135. f.PodClient().CreateSync(&v1.Pod{
  136. ObjectMeta: metav1.ObjectMeta{Name: podName},
  137. Spec: v1.PodSpec{
  138. Containers: []v1.Container{
  139. {
  140. Image: imageutils.GetPauseImageName(),
  141. Name: "pause-container",
  142. },
  143. },
  144. },
  145. })
  146. By("Performing disruptive operations")
  147. twist()
  148. By("Remove test pod")
  149. f.PodClient().DeleteSync(podName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  150. By("Waiting for checkpoint to be removed")
  151. if err := wait.PollImmediate(10*time.Second, gcTimeout, func() (bool, error) {
  152. checkpoints := findCheckpoints(podName)
  153. if len(checkpoints) == 0 {
  154. return true, nil
  155. }
  156. framework.Logf("Checkpoint of %q still exists: %v", podName, checkpoints)
  157. return false, nil
  158. }); err != nil {
  159. framework.Failf("Failed to observe checkpoint being removed within timeout: %v", err)
  160. }
  161. }
  162. // cleanString cleans up any trailing spaces and new line character for the input string
  163. func cleanString(output string) string {
  164. processed := strings.TrimSpace(string(output))
  165. regex := regexp.MustCompile(`\r?\n`)
  166. processed = regex.ReplaceAllString(processed, "")
  167. return processed
  168. }
  169. func writeFileAndSync(path string, data []byte) error {
  170. f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  171. if err != nil {
  172. return err
  173. }
  174. _, err = f.Write(data)
  175. if err != nil {
  176. return err
  177. }
  178. f.Sync()
  179. if err1 := f.Close(); err == nil {
  180. err = err1
  181. }
  182. return err
  183. }
  184. // findCheckpoints returns all checkpoint files containing input string
  185. func findCheckpoints(match string) []string {
  186. By(fmt.Sprintf("Search checkpoints containing %q", match))
  187. checkpoints := []string{}
  188. stdout, err := exec.Command("sudo", "grep", "-rl", match, framework.TestContext.DockershimCheckpointDir).CombinedOutput()
  189. if err != nil {
  190. framework.Logf("grep from dockershim checkpoint directory returns error: %v", err)
  191. }
  192. if stdout == nil {
  193. return checkpoints
  194. }
  195. files := strings.Split(string(stdout), "\n")
  196. for _, file := range files {
  197. cleaned := cleanString(file)
  198. if len(cleaned) == 0 {
  199. continue
  200. }
  201. checkpoints = append(checkpoints, cleaned)
  202. }
  203. return checkpoints
  204. }