github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/qos/oom_priority_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 qos
    18  
    19  import (
    20  	"testing"
    21  
    22  	apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
    23  )
    24  
    25  func intPtr(x int) *int {
    26  	return &x
    27  }
    28  
    29  func TestAlignOOMPriority(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	type fields struct {
    33  		qosLevel           string
    34  		userSpecifiedScore *int
    35  	}
    36  
    37  	tests := []struct {
    38  		name   string
    39  		fields fields
    40  		want   int
    41  	}{
    42  		{
    43  			name: "reclaimed_cores without user specified score",
    44  			fields: fields{
    45  				qosLevel:           apiconsts.PodAnnotationQoSLevelReclaimedCores,
    46  				userSpecifiedScore: nil,
    47  			},
    48  			want: -100,
    49  		},
    50  		{
    51  			name: "shared_cores without user specified score",
    52  			fields: fields{
    53  				qosLevel:           apiconsts.PodAnnotationQoSLevelSharedCores,
    54  				userSpecifiedScore: nil,
    55  			},
    56  			want: 0,
    57  		},
    58  		{
    59  			name: "dedicated_cores without user specified score",
    60  			fields: fields{
    61  				qosLevel:           apiconsts.PodAnnotationQoSLevelDedicatedCores,
    62  				userSpecifiedScore: nil,
    63  			},
    64  			want: 100,
    65  		},
    66  		{
    67  			name: "system_cores without user specified score",
    68  			fields: fields{
    69  				qosLevel:           apiconsts.PodAnnotationQoSLevelSystemCores,
    70  				userSpecifiedScore: nil,
    71  			},
    72  			want: 200,
    73  		},
    74  		{
    75  			name: "invalid qosLevel without user specified score",
    76  			fields: fields{
    77  				qosLevel:           "",
    78  				userSpecifiedScore: nil,
    79  			},
    80  			want: -100,
    81  		},
    82  		{
    83  			name: "reclaimed_cores with userSpecifiedScore in the correct range",
    84  			fields: fields{
    85  				qosLevel:           apiconsts.PodAnnotationQoSLevelReclaimedCores,
    86  				userSpecifiedScore: intPtr(-50),
    87  			},
    88  			want: -50,
    89  		},
    90  		{
    91  			name: "reclaimed_cores with userSpecifiedScore less than the correct range",
    92  			fields: fields{
    93  				qosLevel:           apiconsts.PodAnnotationQoSLevelReclaimedCores,
    94  				userSpecifiedScore: intPtr(-150),
    95  			},
    96  			want: -100,
    97  		},
    98  		{
    99  			name: "reclaimed_cores with userSpecifiedScore large than the correct range",
   100  			fields: fields{
   101  				qosLevel:           apiconsts.PodAnnotationQoSLevelReclaimedCores,
   102  				userSpecifiedScore: intPtr(100),
   103  			},
   104  			want: -1,
   105  		},
   106  		{
   107  			name: "reclaimed_cores with top userSpecifiedScore",
   108  			fields: fields{
   109  				qosLevel:           apiconsts.PodAnnotationQoSLevelReclaimedCores,
   110  				userSpecifiedScore: intPtr(300),
   111  			},
   112  			want: 300,
   113  		},
   114  		{
   115  			name: "shared_cores with userSpecifiedScore in the correct range",
   116  			fields: fields{
   117  				qosLevel:           apiconsts.PodAnnotationQoSLevelSharedCores,
   118  				userSpecifiedScore: intPtr(39),
   119  			},
   120  			want: 39,
   121  		},
   122  		{
   123  			name: "shared_cores with userSpecifiedScore less than the correct range",
   124  			fields: fields{
   125  				qosLevel:           apiconsts.PodAnnotationQoSLevelSharedCores,
   126  				userSpecifiedScore: intPtr(-100),
   127  			},
   128  			want: 0,
   129  		},
   130  		{
   131  			name: "shared_cores with userSpecifiedScore large than the correct range",
   132  			fields: fields{
   133  				qosLevel:           apiconsts.PodAnnotationQoSLevelSharedCores,
   134  				userSpecifiedScore: intPtr(210),
   135  			},
   136  			want: 99,
   137  		},
   138  		{
   139  			name: "shared_cores with top userSpecifiedScore",
   140  			fields: fields{
   141  				qosLevel:           apiconsts.PodAnnotationQoSLevelSharedCores,
   142  				userSpecifiedScore: intPtr(300),
   143  			},
   144  			want: 300,
   145  		},
   146  		{
   147  			name: "dedicated_cores with userSpecifiedScore in the correct range",
   148  			fields: fields{
   149  				qosLevel:           apiconsts.PodAnnotationQoSLevelDedicatedCores,
   150  				userSpecifiedScore: intPtr(142),
   151  			},
   152  			want: 142,
   153  		},
   154  		{
   155  			name: "dedicated_cores with userSpecifiedScore less than the correct range",
   156  			fields: fields{
   157  				qosLevel:           apiconsts.PodAnnotationQoSLevelDedicatedCores,
   158  				userSpecifiedScore: intPtr(-100),
   159  			},
   160  			want: 100,
   161  		},
   162  		{
   163  			name: "dedicated_cores with userSpecifiedScore large than the correct range",
   164  			fields: fields{
   165  				qosLevel:           apiconsts.PodAnnotationQoSLevelDedicatedCores,
   166  				userSpecifiedScore: intPtr(250),
   167  			},
   168  			want: 199,
   169  		},
   170  		{
   171  			name: "dedicated_cores with top userSpecifiedScore",
   172  			fields: fields{
   173  				qosLevel:           apiconsts.PodAnnotationQoSLevelDedicatedCores,
   174  				userSpecifiedScore: intPtr(300),
   175  			},
   176  			want: 300,
   177  		},
   178  		{
   179  			name: "system_cores with userSpecifiedScore in the correct range",
   180  			fields: fields{
   181  				qosLevel:           apiconsts.PodAnnotationQoSLevelSystemCores,
   182  				userSpecifiedScore: intPtr(221),
   183  			},
   184  			want: 221,
   185  		},
   186  		{
   187  			name: "system_cores with userSpecifiedScore less than the correct range",
   188  			fields: fields{
   189  				qosLevel:           apiconsts.PodAnnotationQoSLevelSystemCores,
   190  				userSpecifiedScore: intPtr(121),
   191  			},
   192  			want: 200,
   193  		},
   194  		{
   195  			name: "system_cores with userSpecifiedScore large than the correct range",
   196  			fields: fields{
   197  				qosLevel:           apiconsts.PodAnnotationQoSLevelSystemCores,
   198  				userSpecifiedScore: intPtr(322),
   199  			},
   200  			want: 299,
   201  		},
   202  		{
   203  			name: "system_cores with top userSpecifiedScore",
   204  			fields: fields{
   205  				qosLevel:           apiconsts.PodAnnotationQoSLevelSystemCores,
   206  				userSpecifiedScore: intPtr(300),
   207  			},
   208  			want: 300,
   209  		},
   210  	}
   211  
   212  	for _, tt := range tests {
   213  		tt := tt
   214  		t.Run(tt.name, func(t *testing.T) {
   215  			t.Parallel()
   216  			if got := AlignOOMPriority(tt.fields.qosLevel, tt.fields.userSpecifiedScore); got != tt.want {
   217  				t.Errorf("AlignOOMPriority() = %v, want %v", got, tt.want)
   218  			}
   219  		})
   220  	}
   221  }