...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/policy_none_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/topologymanager

     1  /*
     2  Copyright 2019 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 topologymanager
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestPolicyNoneName(t *testing.T) {
    24  	tcases := []struct {
    25  		name     string
    26  		expected string
    27  	}{
    28  		{
    29  			name:     "New None Policy",
    30  			expected: "none",
    31  		},
    32  	}
    33  	for _, tc := range tcases {
    34  		policy := NewNonePolicy()
    35  		if policy.Name() != tc.expected {
    36  			t.Errorf("Expected Policy Name to be %s, got %s", tc.expected, policy.Name())
    37  		}
    38  	}
    39  }
    40  
    41  func TestPolicyNoneCanAdmitPodResult(t *testing.T) {
    42  	tcases := []struct {
    43  		name     string
    44  		hint     TopologyHint
    45  		expected bool
    46  	}{
    47  		{
    48  			name:     "Preferred is set to false in topology hints",
    49  			hint:     TopologyHint{nil, false},
    50  			expected: true,
    51  		},
    52  		{
    53  			name:     "Preferred is set to true in topology hints",
    54  			hint:     TopologyHint{nil, true},
    55  			expected: true,
    56  		},
    57  	}
    58  
    59  	for _, tc := range tcases {
    60  		policy := NewNonePolicy()
    61  		result := policy.(*nonePolicy).canAdmitPodResult(&tc.hint)
    62  
    63  		if result != tc.expected {
    64  			t.Errorf("Expected result to be %t, got %t", tc.expected, result)
    65  		}
    66  	}
    67  }
    68  
    69  func TestPolicyNoneMerge(t *testing.T) {
    70  	tcases := []struct {
    71  		name           string
    72  		providersHints []map[string][]TopologyHint
    73  		expectedHint   TopologyHint
    74  		expectedAdmit  bool
    75  	}{
    76  		{
    77  			name:           "merged empty providers hints",
    78  			providersHints: []map[string][]TopologyHint{},
    79  			expectedHint:   TopologyHint{},
    80  			expectedAdmit:  true,
    81  		},
    82  		{
    83  			name: "merge with a single provider with a single preferred resource",
    84  			providersHints: []map[string][]TopologyHint{
    85  				{
    86  					"resource": {{NUMANodeAffinity: NewTestBitMask(0, 1), Preferred: true}},
    87  				},
    88  			},
    89  			expectedHint:  TopologyHint{},
    90  			expectedAdmit: true,
    91  		},
    92  		{
    93  			name: "merge with a single provider with a single non-preferred resource",
    94  			providersHints: []map[string][]TopologyHint{
    95  				{
    96  					"resource": {{NUMANodeAffinity: NewTestBitMask(0, 1), Preferred: false}},
    97  				},
    98  			},
    99  			expectedHint:  TopologyHint{},
   100  			expectedAdmit: true,
   101  		},
   102  	}
   103  
   104  	for _, tc := range tcases {
   105  		policy := NewNonePolicy()
   106  		result, admit := policy.Merge(tc.providersHints)
   107  		if !result.IsEqual(tc.expectedHint) || admit != tc.expectedAdmit {
   108  			t.Errorf("Test Case: %s: Expected merge hint to be %v, got %v", tc.name, tc.expectedHint, result)
   109  		}
   110  	}
   111  }
   112  

View as plain text