...

Source file src/k8s.io/kubernetes/pkg/apis/core/toleration_test.go

Documentation: k8s.io/kubernetes/pkg/apis/core

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package core
    18  
    19  import "testing"
    20  
    21  func TestMatchToleration(t *testing.T) {
    22  
    23  	tolerationSeconds := int64(5)
    24  	tolerationToMatchSeconds := int64(3)
    25  	testCases := []struct {
    26  		description       string
    27  		toleration        *Toleration
    28  		tolerationToMatch *Toleration
    29  		expectMatch       bool
    30  	}{
    31  		{
    32  			description: "two taints with the same key,operator,value,effect should match",
    33  			toleration: &Toleration{
    34  				Key:      "foo",
    35  				Operator: "Exists",
    36  				Value:    "bar",
    37  				Effect:   TaintEffectNoSchedule,
    38  			},
    39  			tolerationToMatch: &Toleration{
    40  				Key:      "foo",
    41  				Operator: "Exists",
    42  				Value:    "bar",
    43  				Effect:   TaintEffectNoSchedule,
    44  			},
    45  			expectMatch: true,
    46  		},
    47  		{
    48  			description: "two taints with the different key cannot match",
    49  			toleration: &Toleration{
    50  				Key:      "foo",
    51  				Operator: "Exists",
    52  				Value:    "bar",
    53  				Effect:   TaintEffectNoSchedule,
    54  			},
    55  			tolerationToMatch: &Toleration{
    56  				Key:      "different-key",
    57  				Operator: "Exists",
    58  				Value:    "bar",
    59  				Effect:   TaintEffectNoSchedule,
    60  			},
    61  			expectMatch: false,
    62  		},
    63  		{
    64  			description: "two taints with the different operator cannot match",
    65  			toleration: &Toleration{
    66  				Key:      "foo",
    67  				Operator: "Exists",
    68  				Value:    "bar",
    69  				Effect:   TaintEffectNoSchedule,
    70  			},
    71  			tolerationToMatch: &Toleration{
    72  				Key:      "foo",
    73  				Operator: "different-operator",
    74  				Value:    "bar",
    75  				Effect:   TaintEffectNoSchedule,
    76  			},
    77  			expectMatch: false,
    78  		},
    79  		{
    80  			description: "two taints with the different value cannot match",
    81  			toleration: &Toleration{
    82  				Key:      "foo",
    83  				Operator: "Exists",
    84  				Value:    "bar",
    85  				Effect:   TaintEffectNoSchedule,
    86  			},
    87  			tolerationToMatch: &Toleration{
    88  				Key:      "foo",
    89  				Operator: "Exists",
    90  				Value:    "different-value",
    91  				Effect:   TaintEffectNoSchedule,
    92  			},
    93  			expectMatch: false,
    94  		},
    95  		{
    96  			description: "two taints with the different effect cannot match",
    97  			toleration: &Toleration{
    98  				Key:      "foo",
    99  				Operator: "Exists",
   100  				Value:    "bar",
   101  				Effect:   TaintEffectNoSchedule,
   102  			},
   103  			tolerationToMatch: &Toleration{
   104  				Key:      "foo",
   105  				Operator: "Exists",
   106  				Value:    "bar",
   107  				Effect:   TaintEffectPreferNoSchedule,
   108  			},
   109  			expectMatch: false,
   110  		},
   111  		{
   112  			description: "two taints with the different tolerationSeconds should match",
   113  			toleration: &Toleration{
   114  				Key:               "foo",
   115  				Operator:          "Exists",
   116  				Value:             "bar",
   117  				Effect:            TaintEffectNoSchedule,
   118  				TolerationSeconds: &tolerationSeconds,
   119  			},
   120  			tolerationToMatch: &Toleration{
   121  				Key:               "foo",
   122  				Operator:          "Exists",
   123  				Value:             "bar",
   124  				Effect:            TaintEffectNoSchedule,
   125  				TolerationSeconds: &tolerationToMatchSeconds,
   126  			},
   127  			expectMatch: true,
   128  		},
   129  	}
   130  
   131  	for _, tc := range testCases {
   132  		if actual := tc.toleration.MatchToleration(tc.tolerationToMatch); actual != tc.expectMatch {
   133  			t.Errorf("[%s] expect: %v , got:  %v", tc.description, tc.expectMatch, !tc.expectMatch)
   134  		}
   135  	}
   136  }
   137  

View as plain text