dockershim_checkpoint_test.go 7.3 KB

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