...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/policy_single_numa_node_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  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestPolicySingleNumaNodeCanAdmitPodResult(t *testing.T) {
    25  	tcases := []struct {
    26  		name     string
    27  		hint     TopologyHint
    28  		expected bool
    29  	}{
    30  		{
    31  			name:     "Preferred is set to false in topology hints",
    32  			hint:     TopologyHint{nil, false},
    33  			expected: false,
    34  		},
    35  	}
    36  	numaInfo := commonNUMAInfoTwoNodes()
    37  
    38  	for _, tc := range tcases {
    39  		policy := singleNumaNodePolicy{numaInfo: numaInfo, opts: PolicyOptions{}}
    40  		result := policy.canAdmitPodResult(&tc.hint)
    41  
    42  		if result != tc.expected {
    43  			t.Errorf("Expected result to be %t, got %t", tc.expected, result)
    44  		}
    45  	}
    46  }
    47  
    48  func TestPolicySingleNumaNodeFilterHints(t *testing.T) {
    49  	tcases := []struct {
    50  		name              string
    51  		allResources      [][]TopologyHint
    52  		expectedResources [][]TopologyHint
    53  	}{
    54  		{
    55  			name:              "filter empty resources",
    56  			allResources:      [][]TopologyHint{},
    57  			expectedResources: [][]TopologyHint(nil),
    58  		},
    59  		{
    60  			name: "filter hints with nil socket mask 1/2",
    61  			allResources: [][]TopologyHint{
    62  				{
    63  					{NUMANodeAffinity: nil, Preferred: false},
    64  				},
    65  				{
    66  					{NUMANodeAffinity: nil, Preferred: true},
    67  				},
    68  			},
    69  			expectedResources: [][]TopologyHint{
    70  				[]TopologyHint(nil),
    71  				{
    72  					{NUMANodeAffinity: nil, Preferred: true},
    73  				},
    74  			},
    75  		},
    76  		{
    77  			name: "filter hints with nil socket mask 2/2",
    78  			allResources: [][]TopologyHint{
    79  				{
    80  					{NUMANodeAffinity: NewTestBitMask(0), Preferred: true},
    81  					{NUMANodeAffinity: nil, Preferred: false},
    82  				},
    83  				{
    84  					{NUMANodeAffinity: NewTestBitMask(1), Preferred: true},
    85  					{NUMANodeAffinity: nil, Preferred: true},
    86  				},
    87  			},
    88  			expectedResources: [][]TopologyHint{
    89  				{
    90  					{NUMANodeAffinity: NewTestBitMask(0), Preferred: true},
    91  				},
    92  				{
    93  					{NUMANodeAffinity: NewTestBitMask(1), Preferred: true},
    94  					{NUMANodeAffinity: nil, Preferred: true},
    95  				},
    96  			},
    97  		},
    98  		{
    99  			name: "filter hints with empty resource socket mask",
   100  			allResources: [][]TopologyHint{
   101  				{
   102  					{NUMANodeAffinity: NewTestBitMask(1), Preferred: true},
   103  					{NUMANodeAffinity: NewTestBitMask(0), Preferred: true},
   104  					{NUMANodeAffinity: nil, Preferred: false},
   105  				},
   106  				{},
   107  			},
   108  			expectedResources: [][]TopologyHint{
   109  				{
   110  					{NUMANodeAffinity: NewTestBitMask(1), Preferred: true},
   111  					{NUMANodeAffinity: NewTestBitMask(0), Preferred: true},
   112  				},
   113  				[]TopologyHint(nil),
   114  			},
   115  		},
   116  		{
   117  			name: "filter hints with wide sockemask",
   118  			allResources: [][]TopologyHint{
   119  				{
   120  					{NUMANodeAffinity: NewTestBitMask(0), Preferred: true},
   121  					{NUMANodeAffinity: NewTestBitMask(1), Preferred: true},
   122  					{NUMANodeAffinity: NewTestBitMask(1, 2), Preferred: false},
   123  					{NUMANodeAffinity: NewTestBitMask(0, 1, 2), Preferred: false},
   124  					{NUMANodeAffinity: nil, Preferred: false},
   125  				},
   126  				{
   127  					{NUMANodeAffinity: NewTestBitMask(1, 2), Preferred: false},
   128  					{NUMANodeAffinity: NewTestBitMask(0, 1, 2), Preferred: false},
   129  					{NUMANodeAffinity: NewTestBitMask(0, 2), Preferred: false},
   130  					{NUMANodeAffinity: NewTestBitMask(3), Preferred: false},
   131  				},
   132  				{
   133  					{NUMANodeAffinity: NewTestBitMask(1, 2), Preferred: false},
   134  					{NUMANodeAffinity: NewTestBitMask(0, 1, 2), Preferred: false},
   135  					{NUMANodeAffinity: NewTestBitMask(0, 2), Preferred: false},
   136  				},
   137  			},
   138  			expectedResources: [][]TopologyHint{
   139  				{
   140  					{NUMANodeAffinity: NewTestBitMask(0), Preferred: true},
   141  					{NUMANodeAffinity: NewTestBitMask(1), Preferred: true},
   142  				},
   143  				[]TopologyHint(nil),
   144  				[]TopologyHint(nil),
   145  			},
   146  		},
   147  	}
   148  
   149  	for _, tc := range tcases {
   150  		actual := filterSingleNumaHints(tc.allResources)
   151  		if !reflect.DeepEqual(tc.expectedResources, actual) {
   152  			t.Errorf("Test Case: %s", tc.name)
   153  			t.Errorf("Expected result to be %v, got %v", tc.expectedResources, actual)
   154  		}
   155  	}
   156  }
   157  
   158  func TestPolicySingleNumaNodeMerge(t *testing.T) {
   159  	numaInfo := commonNUMAInfoFourNodes()
   160  	policy := singleNumaNodePolicy{numaInfo: numaInfo, opts: PolicyOptions{}}
   161  
   162  	tcases := commonPolicyMergeTestCases(numaInfo.Nodes)
   163  	tcases = append(tcases, policy.mergeTestCases(numaInfo.Nodes)...)
   164  
   165  	testPolicyMerge(&policy, tcases, t)
   166  }
   167  

View as plain text