taint_test.go 2.9 KB

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