replenishment_controller_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2016 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 resourcequota
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/controller"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. "k8s.io/kubernetes/pkg/util/intstr"
  21. )
  22. // testReplenishment lets us test replenishment functions are invoked
  23. type testReplenishment struct {
  24. groupKind unversioned.GroupKind
  25. namespace string
  26. }
  27. // mock function that holds onto the last kind that was replenished
  28. func (t *testReplenishment) Replenish(groupKind unversioned.GroupKind, namespace string, object runtime.Object) {
  29. t.groupKind = groupKind
  30. t.namespace = namespace
  31. }
  32. func TestPodReplenishmentUpdateFunc(t *testing.T) {
  33. mockReplenish := &testReplenishment{}
  34. options := ReplenishmentControllerOptions{
  35. GroupKind: api.Kind("Pod"),
  36. ReplenishmentFunc: mockReplenish.Replenish,
  37. ResyncPeriod: controller.NoResyncPeriodFunc,
  38. }
  39. oldPod := &api.Pod{
  40. ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "pod"},
  41. Status: api.PodStatus{Phase: api.PodRunning},
  42. }
  43. newPod := &api.Pod{
  44. ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "pod"},
  45. Status: api.PodStatus{Phase: api.PodFailed},
  46. }
  47. updateFunc := PodReplenishmentUpdateFunc(&options)
  48. updateFunc(oldPod, newPod)
  49. if mockReplenish.groupKind != api.Kind("Pod") {
  50. t.Errorf("Unexpected group kind %v", mockReplenish.groupKind)
  51. }
  52. if mockReplenish.namespace != oldPod.Namespace {
  53. t.Errorf("Unexpected namespace %v", mockReplenish.namespace)
  54. }
  55. }
  56. func TestObjectReplenishmentDeleteFunc(t *testing.T) {
  57. mockReplenish := &testReplenishment{}
  58. options := ReplenishmentControllerOptions{
  59. GroupKind: api.Kind("Pod"),
  60. ReplenishmentFunc: mockReplenish.Replenish,
  61. ResyncPeriod: controller.NoResyncPeriodFunc,
  62. }
  63. oldPod := &api.Pod{
  64. ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "pod"},
  65. Status: api.PodStatus{Phase: api.PodRunning},
  66. }
  67. deleteFunc := ObjectReplenishmentDeleteFunc(&options)
  68. deleteFunc(oldPod)
  69. if mockReplenish.groupKind != api.Kind("Pod") {
  70. t.Errorf("Unexpected group kind %v", mockReplenish.groupKind)
  71. }
  72. if mockReplenish.namespace != oldPod.Namespace {
  73. t.Errorf("Unexpected namespace %v", mockReplenish.namespace)
  74. }
  75. }
  76. func TestServiceReplenishmentUpdateFunc(t *testing.T) {
  77. mockReplenish := &testReplenishment{}
  78. options := ReplenishmentControllerOptions{
  79. GroupKind: api.Kind("Service"),
  80. ReplenishmentFunc: mockReplenish.Replenish,
  81. ResyncPeriod: controller.NoResyncPeriodFunc,
  82. }
  83. oldService := &api.Service{
  84. ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "mysvc"},
  85. Spec: api.ServiceSpec{
  86. Type: api.ServiceTypeNodePort,
  87. Ports: []api.ServicePort{{
  88. Port: 80,
  89. TargetPort: intstr.FromInt(80),
  90. }},
  91. },
  92. }
  93. newService := &api.Service{
  94. ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "mysvc"},
  95. Spec: api.ServiceSpec{
  96. Type: api.ServiceTypeClusterIP,
  97. Ports: []api.ServicePort{{
  98. Port: 80,
  99. TargetPort: intstr.FromInt(80),
  100. }}},
  101. }
  102. updateFunc := ServiceReplenishmentUpdateFunc(&options)
  103. updateFunc(oldService, newService)
  104. if mockReplenish.groupKind != api.Kind("Service") {
  105. t.Errorf("Unexpected group kind %v", mockReplenish.groupKind)
  106. }
  107. if mockReplenish.namespace != oldService.Namespace {
  108. t.Errorf("Unexpected namespace %v", mockReplenish.namespace)
  109. }
  110. mockReplenish = &testReplenishment{}
  111. options = ReplenishmentControllerOptions{
  112. GroupKind: api.Kind("Service"),
  113. ReplenishmentFunc: mockReplenish.Replenish,
  114. ResyncPeriod: controller.NoResyncPeriodFunc,
  115. }
  116. oldService = &api.Service{
  117. ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "mysvc"},
  118. Spec: api.ServiceSpec{
  119. Type: api.ServiceTypeNodePort,
  120. Ports: []api.ServicePort{{
  121. Port: 80,
  122. TargetPort: intstr.FromInt(80),
  123. }},
  124. },
  125. }
  126. newService = &api.Service{
  127. ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "mysvc"},
  128. Spec: api.ServiceSpec{
  129. Type: api.ServiceTypeNodePort,
  130. Ports: []api.ServicePort{{
  131. Port: 81,
  132. TargetPort: intstr.FromInt(81),
  133. }}},
  134. }
  135. updateFunc = ServiceReplenishmentUpdateFunc(&options)
  136. updateFunc(oldService, newService)
  137. if mockReplenish.groupKind == api.Kind("Service") {
  138. t.Errorf("Unexpected group kind %v", mockReplenish.groupKind)
  139. }
  140. if mockReplenish.namespace == oldService.Namespace {
  141. t.Errorf("Unexpected namespace %v", mockReplenish.namespace)
  142. }
  143. }