resources_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 quota
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/resource"
  18. )
  19. func TestEquals(t *testing.T) {
  20. testCases := map[string]struct {
  21. a api.ResourceList
  22. b api.ResourceList
  23. expected bool
  24. }{
  25. "isEqual": {
  26. a: api.ResourceList{},
  27. b: api.ResourceList{},
  28. expected: true,
  29. },
  30. "isEqualWithKeys": {
  31. a: api.ResourceList{
  32. api.ResourceCPU: resource.MustParse("100m"),
  33. api.ResourceMemory: resource.MustParse("1Gi"),
  34. },
  35. b: api.ResourceList{
  36. api.ResourceCPU: resource.MustParse("100m"),
  37. api.ResourceMemory: resource.MustParse("1Gi"),
  38. },
  39. expected: true,
  40. },
  41. "isNotEqualSameKeys": {
  42. a: api.ResourceList{
  43. api.ResourceCPU: resource.MustParse("200m"),
  44. api.ResourceMemory: resource.MustParse("1Gi"),
  45. },
  46. b: api.ResourceList{
  47. api.ResourceCPU: resource.MustParse("100m"),
  48. api.ResourceMemory: resource.MustParse("1Gi"),
  49. },
  50. expected: false,
  51. },
  52. "isNotEqualDiffKeys": {
  53. a: api.ResourceList{
  54. api.ResourceCPU: resource.MustParse("100m"),
  55. api.ResourceMemory: resource.MustParse("1Gi"),
  56. },
  57. b: api.ResourceList{
  58. api.ResourceCPU: resource.MustParse("100m"),
  59. api.ResourceMemory: resource.MustParse("1Gi"),
  60. api.ResourcePods: resource.MustParse("1"),
  61. },
  62. expected: false,
  63. },
  64. }
  65. for testName, testCase := range testCases {
  66. if result := Equals(testCase.a, testCase.b); result != testCase.expected {
  67. t.Errorf("%s expected: %v, actual: %v, a=%v, b=%v", testName, testCase.expected, result, testCase.a, testCase.b)
  68. }
  69. }
  70. }
  71. func TestMax(t *testing.T) {
  72. testCases := map[string]struct {
  73. a api.ResourceList
  74. b api.ResourceList
  75. expected api.ResourceList
  76. }{
  77. "noKeys": {
  78. a: api.ResourceList{},
  79. b: api.ResourceList{},
  80. expected: api.ResourceList{},
  81. },
  82. "toEmpty": {
  83. a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  84. b: api.ResourceList{},
  85. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  86. },
  87. "matching": {
  88. a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  89. b: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
  90. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
  91. },
  92. "matching(reverse)": {
  93. a: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
  94. b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  95. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
  96. },
  97. }
  98. for testName, testCase := range testCases {
  99. sum := Max(testCase.a, testCase.b)
  100. if result := Equals(testCase.expected, sum); !result {
  101. t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sum)
  102. }
  103. }
  104. }
  105. func TestAdd(t *testing.T) {
  106. testCases := map[string]struct {
  107. a api.ResourceList
  108. b api.ResourceList
  109. expected api.ResourceList
  110. }{
  111. "noKeys": {
  112. a: api.ResourceList{},
  113. b: api.ResourceList{},
  114. expected: api.ResourceList{},
  115. },
  116. "toEmpty": {
  117. a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  118. b: api.ResourceList{},
  119. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  120. },
  121. "matching": {
  122. a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  123. b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  124. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
  125. },
  126. }
  127. for testName, testCase := range testCases {
  128. sum := Add(testCase.a, testCase.b)
  129. if result := Equals(testCase.expected, sum); !result {
  130. t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sum)
  131. }
  132. }
  133. }
  134. func TestSubtract(t *testing.T) {
  135. testCases := map[string]struct {
  136. a api.ResourceList
  137. b api.ResourceList
  138. expected api.ResourceList
  139. }{
  140. "noKeys": {
  141. a: api.ResourceList{},
  142. b: api.ResourceList{},
  143. expected: api.ResourceList{},
  144. },
  145. "value-empty": {
  146. a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  147. b: api.ResourceList{},
  148. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  149. },
  150. "empty-value": {
  151. a: api.ResourceList{},
  152. b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  153. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("-100m")},
  154. },
  155. "value-value": {
  156. a: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
  157. b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  158. expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
  159. },
  160. }
  161. for testName, testCase := range testCases {
  162. sub := Subtract(testCase.a, testCase.b)
  163. if result := Equals(testCase.expected, sub); !result {
  164. t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sub)
  165. }
  166. }
  167. }
  168. func TestResourceNames(t *testing.T) {
  169. testCases := map[string]struct {
  170. a api.ResourceList
  171. expected []api.ResourceName
  172. }{
  173. "empty": {
  174. a: api.ResourceList{},
  175. expected: []api.ResourceName{},
  176. },
  177. "values": {
  178. a: api.ResourceList{
  179. api.ResourceCPU: resource.MustParse("100m"),
  180. api.ResourceMemory: resource.MustParse("1Gi"),
  181. },
  182. expected: []api.ResourceName{api.ResourceMemory, api.ResourceCPU},
  183. },
  184. }
  185. for testName, testCase := range testCases {
  186. actualSet := ToSet(ResourceNames(testCase.a))
  187. expectedSet := ToSet(testCase.expected)
  188. if !actualSet.Equal(expectedSet) {
  189. t.Errorf("%s expected: %v, actual: %v", testName, expectedSet, actualSet)
  190. }
  191. }
  192. }
  193. func TestContains(t *testing.T) {
  194. testCases := map[string]struct {
  195. a []api.ResourceName
  196. b api.ResourceName
  197. expected bool
  198. }{
  199. "does-not-contain": {
  200. a: []api.ResourceName{api.ResourceMemory},
  201. b: api.ResourceCPU,
  202. expected: false,
  203. },
  204. "does-contain": {
  205. a: []api.ResourceName{api.ResourceMemory, api.ResourceCPU},
  206. b: api.ResourceCPU,
  207. expected: true,
  208. },
  209. }
  210. for testName, testCase := range testCases {
  211. if actual := Contains(testCase.a, testCase.b); actual != testCase.expected {
  212. t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, actual)
  213. }
  214. }
  215. }
  216. func TestIsZero(t *testing.T) {
  217. testCases := map[string]struct {
  218. a api.ResourceList
  219. expected bool
  220. }{
  221. "empty": {
  222. a: api.ResourceList{},
  223. expected: true,
  224. },
  225. "zero": {
  226. a: api.ResourceList{
  227. api.ResourceCPU: resource.MustParse("0"),
  228. api.ResourceMemory: resource.MustParse("0"),
  229. },
  230. expected: true,
  231. },
  232. "non-zero": {
  233. a: api.ResourceList{
  234. api.ResourceCPU: resource.MustParse("200m"),
  235. api.ResourceMemory: resource.MustParse("1Gi"),
  236. },
  237. expected: false,
  238. },
  239. }
  240. for testName, testCase := range testCases {
  241. if result := IsZero(testCase.a); result != testCase.expected {
  242. t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, result)
  243. }
  244. }
  245. }
  246. func TestIsNegative(t *testing.T) {
  247. testCases := map[string]struct {
  248. a api.ResourceList
  249. expected []api.ResourceName
  250. }{
  251. "empty": {
  252. a: api.ResourceList{},
  253. expected: []api.ResourceName{},
  254. },
  255. "some-negative": {
  256. a: api.ResourceList{
  257. api.ResourceCPU: resource.MustParse("-10"),
  258. api.ResourceMemory: resource.MustParse("0"),
  259. },
  260. expected: []api.ResourceName{api.ResourceCPU},
  261. },
  262. "all-negative": {
  263. a: api.ResourceList{
  264. api.ResourceCPU: resource.MustParse("-200m"),
  265. api.ResourceMemory: resource.MustParse("-1Gi"),
  266. },
  267. expected: []api.ResourceName{api.ResourceCPU, api.ResourceMemory},
  268. },
  269. }
  270. for testName, testCase := range testCases {
  271. actual := IsNegative(testCase.a)
  272. actualSet := ToSet(actual)
  273. expectedSet := ToSet(testCase.expected)
  274. if !actualSet.Equal(expectedSet) {
  275. t.Errorf("%s expected: %v, actual: %v", testName, expectedSet, actualSet)
  276. }
  277. }
  278. }