Skip to content

Commit

Permalink
test(manager): parseToleration
Browse files Browse the repository at this point in the history
Ref: 57, Longhorn-5614

Signed-off-by: Chin-Ya Huang <chin-ya.huang@suse.com>
  • Loading branch information
c3y1huang committed Mar 24, 2023
1 parent 365bc8d commit 385efcc
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package manager

import (
"fmt"
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
)

func TestParseToleration(t *testing.T) {
type testCase struct {
input string

expectedToleration *corev1.Toleration
expectError bool
}
testCases := map[string]testCase{
"valid key:NoSchedule": {
input: "key:NoSchedule",
expectedToleration: &corev1.Toleration{
Key: "key",
Value: "",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
},
expectError: false,
},
"valid key=value:NoExecute": {
input: "key=value:NoExecute",
expectedToleration: &corev1.Toleration{
Key: "key",
Value: "value",
Operator: corev1.TolerationOpEqual,
Effect: corev1.TaintEffectNoExecute,
},
expectError: false,
},
"valid key=value:PreferNoSchedule": {
input: "key=value:PreferNoSchedule",
expectedToleration: &corev1.Toleration{
Key: "key",
Value: "value",
Operator: corev1.TolerationOpEqual,
Effect: corev1.TaintEffectPreferNoSchedule,
},
expectError: false,
},
"invalid key:InvalidEffect": {
input: "key:InvalidEffect",
expectedToleration: nil,
expectError: true,
},
"invalid key=value=NoSchedule": {
input: "key=value=NoSchedule",
expectedToleration: nil,
expectError: true,
},
}

for name, test := range testCases {
fmt.Printf("testing %v\n", name)

toleration, err := parseToleration(test.input)
if !reflect.DeepEqual(toleration, test.expectedToleration) {
t.Errorf("unexpected toleration:\nGot: %v\nWant: %v", toleration, test.expectedToleration)
}

if test.expectError && err == nil {
t.Errorf("unexpected error: %v", err)
}
}
}

0 comments on commit 385efcc

Please sign in to comment.