toleration_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 core
  14. import "testing"
  15. func TestMatchToleration(t *testing.T) {
  16. tolerationSeconds := int64(5)
  17. tolerationToMatchSeconds := int64(3)
  18. testCases := []struct {
  19. description string
  20. toleration *Toleration
  21. tolerationToMatch *Toleration
  22. expectMatch bool
  23. }{
  24. {
  25. description: "two taints with the same key,operator,value,effect should match",
  26. toleration: &Toleration{
  27. Key: "foo",
  28. Operator: "Exists",
  29. Value: "bar",
  30. Effect: TaintEffectNoSchedule,
  31. },
  32. tolerationToMatch: &Toleration{
  33. Key: "foo",
  34. Operator: "Exists",
  35. Value: "bar",
  36. Effect: TaintEffectNoSchedule,
  37. },
  38. expectMatch: true,
  39. },
  40. {
  41. description: "two taints with the different key cannot match",
  42. toleration: &Toleration{
  43. Key: "foo",
  44. Operator: "Exists",
  45. Value: "bar",
  46. Effect: TaintEffectNoSchedule,
  47. },
  48. tolerationToMatch: &Toleration{
  49. Key: "different-key",
  50. Operator: "Exists",
  51. Value: "bar",
  52. Effect: TaintEffectNoSchedule,
  53. },
  54. expectMatch: false,
  55. },
  56. {
  57. description: "two taints with the different operator cannot match",
  58. toleration: &Toleration{
  59. Key: "foo",
  60. Operator: "Exists",
  61. Value: "bar",
  62. Effect: TaintEffectNoSchedule,
  63. },
  64. tolerationToMatch: &Toleration{
  65. Key: "foo",
  66. Operator: "different-operator",
  67. Value: "bar",
  68. Effect: TaintEffectNoSchedule,
  69. },
  70. expectMatch: false,
  71. },
  72. {
  73. description: "two taints with the different value cannot match",
  74. toleration: &Toleration{
  75. Key: "foo",
  76. Operator: "Exists",
  77. Value: "bar",
  78. Effect: TaintEffectNoSchedule,
  79. },
  80. tolerationToMatch: &Toleration{
  81. Key: "foo",
  82. Operator: "Exists",
  83. Value: "different-value",
  84. Effect: TaintEffectNoSchedule,
  85. },
  86. expectMatch: false,
  87. },
  88. {
  89. description: "two taints with the different effect cannot match",
  90. toleration: &Toleration{
  91. Key: "foo",
  92. Operator: "Exists",
  93. Value: "bar",
  94. Effect: TaintEffectNoSchedule,
  95. },
  96. tolerationToMatch: &Toleration{
  97. Key: "foo",
  98. Operator: "Exists",
  99. Value: "bar",
  100. Effect: TaintEffectPreferNoSchedule,
  101. },
  102. expectMatch: false,
  103. },
  104. {
  105. description: "two taints with the different tolerationSeconds should match",
  106. toleration: &Toleration{
  107. Key: "foo",
  108. Operator: "Exists",
  109. Value: "bar",
  110. Effect: TaintEffectNoSchedule,
  111. TolerationSeconds: &tolerationSeconds,
  112. },
  113. tolerationToMatch: &Toleration{
  114. Key: "foo",
  115. Operator: "Exists",
  116. Value: "bar",
  117. Effect: TaintEffectNoSchedule,
  118. TolerationSeconds: &tolerationToMatchSeconds,
  119. },
  120. expectMatch: true,
  121. },
  122. }
  123. for _, tc := range testCases {
  124. if actual := tc.toleration.MatchToleration(tc.tolerationToMatch); actual != tc.expectMatch {
  125. t.Errorf("[%s] expect: %v , got: %v", tc.description, tc.expectMatch, !tc.expectMatch)
  126. }
  127. }
  128. }