taint_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. for i, tc := range testCases {
  37. if tc.expectedString != tc.taint.ToString() {
  38. t.Errorf("[%v] expected taint %v converted to %s, got %s", i, tc.taint, tc.expectedString, tc.taint.ToString())
  39. }
  40. }
  41. }
  42. func TestMatchTaint(t *testing.T) {
  43. testCases := []struct {
  44. description string
  45. taint *Taint
  46. taintToMatch Taint
  47. expectMatch bool
  48. }{
  49. {
  50. description: "two taints with the same key,value,effect should match",
  51. taint: &Taint{
  52. Key: "foo",
  53. Value: "bar",
  54. Effect: TaintEffectNoSchedule,
  55. },
  56. taintToMatch: Taint{
  57. Key: "foo",
  58. Value: "bar",
  59. Effect: TaintEffectNoSchedule,
  60. },
  61. expectMatch: true,
  62. },
  63. {
  64. description: "two taints with the same key,effect but different value should match",
  65. taint: &Taint{
  66. Key: "foo",
  67. Value: "bar",
  68. Effect: TaintEffectNoSchedule,
  69. },
  70. taintToMatch: Taint{
  71. Key: "foo",
  72. Value: "different-value",
  73. Effect: TaintEffectNoSchedule,
  74. },
  75. expectMatch: true,
  76. },
  77. {
  78. description: "two taints with the different key cannot match",
  79. taint: &Taint{
  80. Key: "foo",
  81. Value: "bar",
  82. Effect: TaintEffectNoSchedule,
  83. },
  84. taintToMatch: Taint{
  85. Key: "different-key",
  86. Value: "bar",
  87. Effect: TaintEffectNoSchedule,
  88. },
  89. expectMatch: false,
  90. },
  91. {
  92. description: "two taints with the different effect cannot match",
  93. taint: &Taint{
  94. Key: "foo",
  95. Value: "bar",
  96. Effect: TaintEffectNoSchedule,
  97. },
  98. taintToMatch: Taint{
  99. Key: "foo",
  100. Value: "bar",
  101. Effect: TaintEffectPreferNoSchedule,
  102. },
  103. expectMatch: false,
  104. },
  105. }
  106. for _, tc := range testCases {
  107. if tc.expectMatch != tc.taint.MatchTaint(tc.taintToMatch) {
  108. t.Errorf("[%s] expect taint %s match taint %s", tc.description, tc.taint.ToString(), tc.taintToMatch.ToString())
  109. }
  110. }
  111. }