github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/taint_test.go (about) 1 package api 2 3 import ( 4 "testing" 5 ) 6 7 func TestTaintString(t *testing.T) { 8 taint := Taint{ 9 Key: "key", 10 Value: "val", 11 Effect: "NoSchedule", 12 } 13 14 expected := "key=val:NoSchedule" 15 actual := taint.String() 16 if actual != expected { 17 t.Errorf("Expected taint string to be '%s', but was '%s", expected, actual) 18 } 19 } 20 21 func TestTaintValidate(t *testing.T) { 22 testCases := []struct { 23 key string 24 effect string 25 isValid bool 26 }{ 27 // Empty key 28 { 29 key: "", 30 effect: "", 31 isValid: false, 32 }, 33 34 // Invalid effect 35 { 36 key: "dedicated", 37 effect: "UnknownEffect", 38 isValid: false, 39 }, 40 41 // Valid taint 42 { 43 key: "dedicated", 44 effect: "NoSchedule", 45 isValid: true, 46 }, 47 } 48 49 for _, testCase := range testCases { 50 taint := Taint{ 51 Key: testCase.key, 52 Value: "", 53 Effect: testCase.effect, 54 } 55 56 err := taint.Validate() 57 58 if testCase.isValid && err != nil { 59 t.Errorf("Expected taint to be valid, but got error: %v", err) 60 61 } 62 if !testCase.isValid && err == nil { 63 t.Errorf("Expected taint to be invalid, but it was not") 64 } 65 } 66 } 67 68 func TestTaintsString(t *testing.T) { 69 taints := Taints([]Taint{ 70 { 71 Key: "key-1", 72 Value: "val", 73 Effect: "NoSchedule", 74 }, 75 { 76 Key: "key-2", 77 Value: "val", 78 Effect: "NoSchedule", 79 }, 80 }) 81 82 expected := "key-1=val:NoSchedule,key-2=val:NoSchedule" 83 actual := taints.String() 84 if actual != expected { 85 t.Errorf("Expected taints string to be '%s', but was '%s", expected, actual) 86 } 87 } 88 89 func TestTaintsValidate(t *testing.T) { 90 testCases := []struct { 91 taints Taints 92 isValid bool 93 }{ 94 // Unspecified key 95 { 96 taints: Taints{ 97 { 98 Key: "", 99 Effect: "NoSchedule", 100 }, 101 }, 102 isValid: false, 103 }, 104 105 // Duplicate key/effect pair 106 { 107 taints: Taints{ 108 { 109 Key: "dedicated", 110 Effect: "NoSchedule", 111 }, 112 { 113 Key: "dedicated", 114 Effect: "NoSchedule", 115 }, 116 }, 117 isValid: false, 118 }, 119 120 // Valid 121 { 122 taints: Taints{ 123 { 124 Key: "dedicated", 125 Effect: "NoSchedule", 126 }, 127 { 128 Key: "dedicated", 129 Effect: "NoExecute", 130 }, 131 }, 132 isValid: true, 133 }, 134 } 135 136 for _, testCase := range testCases { 137 err := testCase.taints.Validate() 138 139 if testCase.isValid && err != nil { 140 t.Errorf("Expected taint to be valid, but got error: %v", err) 141 } 142 143 if !testCase.isValid && err == nil { 144 t.Errorf("Expected taint to be invalid, but it was not") 145 } 146 } 147 }