policy_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 qos
  14. import (
  15. "strconv"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/resource"
  19. )
  20. const (
  21. standardMemoryAmount = 8000000000
  22. )
  23. var (
  24. cpuLimit = api.Pod{
  25. Spec: api.PodSpec{
  26. Containers: []api.Container{
  27. {
  28. Resources: api.ResourceRequirements{
  29. Limits: api.ResourceList{
  30. api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
  31. },
  32. },
  33. },
  34. },
  35. },
  36. }
  37. memoryLimitCPURequest = api.Pod{
  38. Spec: api.PodSpec{
  39. Containers: []api.Container{
  40. {
  41. Resources: api.ResourceRequirements{
  42. Requests: api.ResourceList{
  43. api.ResourceName(api.ResourceCPU): resource.MustParse("0"),
  44. },
  45. Limits: api.ResourceList{
  46. api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
  47. },
  48. },
  49. },
  50. },
  51. },
  52. }
  53. zeroMemoryLimit = api.Pod{
  54. Spec: api.PodSpec{
  55. Containers: []api.Container{
  56. {
  57. Resources: api.ResourceRequirements{
  58. Limits: api.ResourceList{
  59. api.ResourceName(api.ResourceMemory): resource.MustParse("0"),
  60. },
  61. },
  62. },
  63. },
  64. },
  65. }
  66. noRequestLimit = api.Pod{
  67. Spec: api.PodSpec{
  68. Containers: []api.Container{
  69. {
  70. Resources: api.ResourceRequirements{},
  71. },
  72. },
  73. },
  74. }
  75. equalRequestLimitCPUMemory = api.Pod{
  76. Spec: api.PodSpec{
  77. Containers: []api.Container{
  78. {
  79. Resources: api.ResourceRequirements{
  80. Requests: api.ResourceList{
  81. api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
  82. api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
  83. },
  84. Limits: api.ResourceList{
  85. api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
  86. api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
  87. },
  88. },
  89. },
  90. },
  91. },
  92. }
  93. cpuUnlimitedMemoryLimitedWithRequests = api.Pod{
  94. Spec: api.PodSpec{
  95. Containers: []api.Container{
  96. {
  97. Resources: api.ResourceRequirements{
  98. Requests: api.ResourceList{
  99. api.ResourceName(api.ResourceMemory): resource.MustParse(strconv.Itoa(standardMemoryAmount / 2)),
  100. api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
  101. },
  102. Limits: api.ResourceList{
  103. api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
  104. },
  105. },
  106. },
  107. },
  108. },
  109. }
  110. requestNoLimit = api.Pod{
  111. Spec: api.PodSpec{
  112. Containers: []api.Container{
  113. {
  114. Resources: api.ResourceRequirements{
  115. Requests: api.ResourceList{
  116. api.ResourceName(api.ResourceMemory): resource.MustParse(strconv.Itoa(standardMemoryAmount - 1)),
  117. api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
  118. },
  119. },
  120. },
  121. },
  122. },
  123. }
  124. )
  125. type oomTest struct {
  126. pod *api.Pod
  127. memoryCapacity int64
  128. lowOOMScoreAdj int // The max oom_score_adj score the container should be assigned.
  129. highOOMScoreAdj int // The min oom_score_adj score the container should be assigned.
  130. }
  131. func TestGetContainerOOMScoreAdjust(t *testing.T) {
  132. oomTests := []oomTest{
  133. {
  134. pod: &cpuLimit,
  135. memoryCapacity: 4000000000,
  136. lowOOMScoreAdj: 999,
  137. highOOMScoreAdj: 999,
  138. },
  139. {
  140. pod: &memoryLimitCPURequest,
  141. memoryCapacity: 8000000000,
  142. lowOOMScoreAdj: 999,
  143. highOOMScoreAdj: 999,
  144. },
  145. {
  146. pod: &zeroMemoryLimit,
  147. memoryCapacity: 7230457451,
  148. lowOOMScoreAdj: 1000,
  149. highOOMScoreAdj: 1000,
  150. },
  151. {
  152. pod: &noRequestLimit,
  153. memoryCapacity: 4000000000,
  154. lowOOMScoreAdj: 1000,
  155. highOOMScoreAdj: 1000,
  156. },
  157. {
  158. pod: &equalRequestLimitCPUMemory,
  159. memoryCapacity: 123456789,
  160. lowOOMScoreAdj: -998,
  161. highOOMScoreAdj: -998,
  162. },
  163. {
  164. pod: &cpuUnlimitedMemoryLimitedWithRequests,
  165. memoryCapacity: standardMemoryAmount,
  166. lowOOMScoreAdj: 495,
  167. highOOMScoreAdj: 505,
  168. },
  169. {
  170. pod: &requestNoLimit,
  171. memoryCapacity: standardMemoryAmount,
  172. lowOOMScoreAdj: 2,
  173. highOOMScoreAdj: 2,
  174. },
  175. }
  176. for _, test := range oomTests {
  177. oomScoreAdj := GetContainerOOMScoreAdjust(test.pod, &test.pod.Spec.Containers[0], test.memoryCapacity)
  178. if oomScoreAdj < test.lowOOMScoreAdj || oomScoreAdj > test.highOOMScoreAdj {
  179. t.Errorf("oom_score_adj should be between %d and %d, but was %d", test.lowOOMScoreAdj, test.highOOMScoreAdj, oomScoreAdj)
  180. }
  181. }
  182. }