util_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright 2015 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 volume
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/errors"
  20. "k8s.io/kubernetes/pkg/api/resource"
  21. )
  22. func TestRecyclerSuccess(t *testing.T) {
  23. client := &mockRecyclerClient{}
  24. recycler := &api.Pod{
  25. ObjectMeta: api.ObjectMeta{
  26. Namespace: api.NamespaceDefault,
  27. },
  28. Status: api.PodStatus{
  29. Phase: api.PodSucceeded,
  30. },
  31. }
  32. err := internalRecycleVolumeByWatchingPodUntilCompletion("pv-name", recycler, client)
  33. if err != nil {
  34. t.Errorf("Unexpected error watching recycler pod: %+v", err)
  35. }
  36. if !client.deletedCalled {
  37. t.Errorf("Expected deferred client.Delete to be called on recycler pod")
  38. }
  39. }
  40. func TestRecyclerFailure(t *testing.T) {
  41. client := &mockRecyclerClient{}
  42. recycler := &api.Pod{
  43. ObjectMeta: api.ObjectMeta{
  44. Namespace: api.NamespaceDefault,
  45. },
  46. Status: api.PodStatus{
  47. Phase: api.PodFailed,
  48. Message: "foo",
  49. },
  50. }
  51. err := internalRecycleVolumeByWatchingPodUntilCompletion("pv-name", recycler, client)
  52. if err == nil {
  53. t.Fatalf("Expected pod failure but got nil error returned")
  54. }
  55. if err != nil {
  56. if !strings.Contains(err.Error(), "foo") {
  57. t.Errorf("Expected pod.Status.Message %s but got %s", recycler.Status.Message, err)
  58. }
  59. }
  60. if !client.deletedCalled {
  61. t.Errorf("Expected deferred client.Delete to be called on recycler pod")
  62. }
  63. }
  64. func TestRecyclerAlreadyExists(t *testing.T) {
  65. // Test that internalRecycleVolumeByWatchingPodUntilCompletion does not
  66. // start a new recycler when an old one is already running.
  67. // Old recycler is running and fails with "foo" error message
  68. oldRecycler := &api.Pod{
  69. ObjectMeta: api.ObjectMeta{
  70. Name: "recycler-test",
  71. Namespace: api.NamespaceDefault,
  72. },
  73. Status: api.PodStatus{
  74. Phase: api.PodFailed,
  75. Message: "foo",
  76. },
  77. }
  78. // New recycler _would_ succeed if it was run
  79. newRecycler := &api.Pod{
  80. ObjectMeta: api.ObjectMeta{
  81. Name: "recycler-test",
  82. Namespace: api.NamespaceDefault,
  83. },
  84. Status: api.PodStatus{
  85. Phase: api.PodSucceeded,
  86. Message: "bar",
  87. },
  88. }
  89. client := &mockRecyclerClient{
  90. pod: oldRecycler,
  91. }
  92. err := internalRecycleVolumeByWatchingPodUntilCompletion("pv-name", newRecycler, client)
  93. if err == nil {
  94. t.Fatalf("Expected pod failure but got nil error returned")
  95. }
  96. // Check the recycler failed with "foo" error message, i.e. it was the
  97. // old recycler that finished and not the new one.
  98. if err != nil {
  99. if !strings.Contains(err.Error(), "foo") {
  100. t.Errorf("Expected pod.Status.Message %s but got %s", oldRecycler.Status.Message, err)
  101. }
  102. }
  103. if !client.deletedCalled {
  104. t.Errorf("Expected deferred client.Delete to be called on recycler pod")
  105. }
  106. }
  107. type mockRecyclerClient struct {
  108. pod *api.Pod
  109. deletedCalled bool
  110. }
  111. func (c *mockRecyclerClient) CreatePod(pod *api.Pod) (*api.Pod, error) {
  112. if c.pod == nil {
  113. c.pod = pod
  114. return c.pod, nil
  115. }
  116. // Simulate "already exists" error
  117. return nil, errors.NewAlreadyExists(api.Resource("pods"), pod.Name)
  118. }
  119. func (c *mockRecyclerClient) GetPod(name, namespace string) (*api.Pod, error) {
  120. if c.pod != nil {
  121. return c.pod, nil
  122. } else {
  123. return nil, fmt.Errorf("pod does not exist")
  124. }
  125. }
  126. func (c *mockRecyclerClient) DeletePod(name, namespace string) error {
  127. c.deletedCalled = true
  128. return nil
  129. }
  130. func (c *mockRecyclerClient) WatchPod(name, namespace string, stopChannel chan struct{}) func() *api.Pod {
  131. return func() *api.Pod {
  132. return c.pod
  133. }
  134. }
  135. func TestCalculateTimeoutForVolume(t *testing.T) {
  136. pv := &api.PersistentVolume{
  137. Spec: api.PersistentVolumeSpec{
  138. Capacity: api.ResourceList{
  139. api.ResourceName(api.ResourceStorage): resource.MustParse("500M"),
  140. },
  141. },
  142. }
  143. timeout := CalculateTimeoutForVolume(50, 30, pv)
  144. if timeout != 50 {
  145. t.Errorf("Expected 50 for timeout but got %v", timeout)
  146. }
  147. pv.Spec.Capacity[api.ResourceStorage] = resource.MustParse("2Gi")
  148. timeout = CalculateTimeoutForVolume(50, 30, pv)
  149. if timeout != 60 {
  150. t.Errorf("Expected 60 for timeout but got %v", timeout)
  151. }
  152. pv.Spec.Capacity[api.ResourceStorage] = resource.MustParse("150Gi")
  153. timeout = CalculateTimeoutForVolume(50, 30, pv)
  154. if timeout != 4500 {
  155. t.Errorf("Expected 4500 for timeout but got %v", timeout)
  156. }
  157. }
  158. func TestGenerateVolumeName(t *testing.T) {
  159. // Normal operation, no truncate
  160. v1 := GenerateVolumeName("kubernetes", "pv-cinder-abcde", 255)
  161. if v1 != "kubernetes-dynamic-pv-cinder-abcde" {
  162. t.Errorf("Expected kubernetes-dynamic-pv-cinder-abcde, got %s", v1)
  163. }
  164. // Truncate trailing "6789-dynamic"
  165. prefix := strings.Repeat("0123456789", 9) // 90 characters prefix + 8 chars. of "-dynamic"
  166. v2 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
  167. expect := prefix[:84] + "-pv-cinder-abcde"
  168. if v2 != expect {
  169. t.Errorf("Expected %s, got %s", expect, v2)
  170. }
  171. // Truncate really long cluster name
  172. prefix = strings.Repeat("0123456789", 1000) // 10000 characters prefix
  173. v3 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
  174. if v3 != expect {
  175. t.Errorf("Expected %s, got %s", expect, v3)
  176. }
  177. }