...

Source file src/k8s.io/api/core/v1/taint_test.go

Documentation: k8s.io/api/core/v1

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

View as plain text