checkpoint_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 checkpoint
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "reflect"
  18. "testing"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/kubernetes/pkg/apis/core"
  22. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
  23. )
  24. // TestWriteLoadDeletePods validates all combinations of write, load, and delete
  25. func TestWriteLoadDeletePods(t *testing.T) {
  26. testPods := []struct {
  27. pod *v1.Pod
  28. written bool
  29. }{
  30. {
  31. pod: &v1.Pod{
  32. ObjectMeta: metav1.ObjectMeta{
  33. Name: "Foo",
  34. Annotations: map[string]string{core.BootstrapCheckpointAnnotationKey: "true"},
  35. UID: "1",
  36. },
  37. },
  38. written: true,
  39. },
  40. {
  41. pod: &v1.Pod{
  42. ObjectMeta: metav1.ObjectMeta{
  43. Name: "Foo2",
  44. Annotations: map[string]string{core.BootstrapCheckpointAnnotationKey: "true"},
  45. UID: "2",
  46. },
  47. },
  48. written: true,
  49. },
  50. {
  51. pod: &v1.Pod{
  52. ObjectMeta: metav1.ObjectMeta{
  53. Name: "Bar",
  54. UID: "3",
  55. },
  56. },
  57. written: false,
  58. },
  59. }
  60. dir, err := ioutil.TempDir("", "checkpoint")
  61. if err != nil {
  62. t.Errorf("Failed to allocate temp directory for TestWriteLoadDeletePods error=%v", err)
  63. }
  64. defer os.RemoveAll(dir)
  65. cpm, err := checkpointmanager.NewCheckpointManager(dir)
  66. if err != nil {
  67. t.Errorf("Failed to initialize checkpoint manager error=%v", err)
  68. }
  69. for _, p := range testPods {
  70. // Write pods should always pass unless there is an fs error
  71. if err := WritePod(cpm, p.pod); err != nil {
  72. t.Errorf("Failed to Write Pod: %v", err)
  73. }
  74. }
  75. // verify the correct written files are loaded from disk
  76. pods, err := LoadPods(cpm)
  77. if err != nil {
  78. t.Errorf("Failed to Load Pods: %v", err)
  79. }
  80. // loop through contents and check make sure
  81. // what was loaded matched the expected results.
  82. for _, p := range testPods {
  83. pname := p.pod.GetName()
  84. var lpod *v1.Pod
  85. for _, check := range pods {
  86. if check.GetName() == pname {
  87. lpod = check
  88. break
  89. }
  90. }
  91. if p.written {
  92. if lpod != nil {
  93. if !reflect.DeepEqual(p.pod, lpod) {
  94. t.Errorf("expected %#v, \ngot %#v", p.pod, lpod)
  95. }
  96. } else {
  97. t.Errorf("Got unexpected result for %v, should have been loaded", pname)
  98. }
  99. } else if lpod != nil {
  100. t.Errorf("Got unexpected result for %v, should not have been loaded", pname)
  101. }
  102. err = DeletePod(cpm, p.pod)
  103. if err != nil {
  104. t.Errorf("Failed to delete pod %v", pname)
  105. }
  106. }
  107. // finally validate the contents of the directory is empty.
  108. files, err := ioutil.ReadDir(dir)
  109. if err != nil {
  110. t.Errorf("Failed to read directory %v", dir)
  111. }
  112. if len(files) > 0 {
  113. t.Errorf("Directory %v should be empty but found %#v", dir, files)
  114. }
  115. }