...

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

View as plain text