github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/orm/topology/policy_none_test.go (about)

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