sigs.k8s.io/kueue@v0.6.2/pkg/queue/cluster_queue_best_effort_fifo_test.go (about)

     1  /*
     2  Copyright 2022 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 queue
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	corev1 "k8s.io/api/core/v1"
    24  
    25  	config "sigs.k8s.io/kueue/apis/config/v1beta1"
    26  	kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
    27  	utiltesting "sigs.k8s.io/kueue/pkg/util/testing"
    28  	"sigs.k8s.io/kueue/pkg/workload"
    29  )
    30  
    31  func TestBestEffortFIFORequeueIfNotPresent(t *testing.T) {
    32  	tests := map[string]struct {
    33  		reason           RequeueReason
    34  		lastAssignment   *workload.AssigmentClusterQueueState
    35  		wantInadmissible bool
    36  	}{
    37  		"failure after nomination": {
    38  			reason:           RequeueReasonFailedAfterNomination,
    39  			wantInadmissible: false,
    40  		},
    41  		"namespace doesn't match": {
    42  			reason:           RequeueReasonNamespaceMismatch,
    43  			wantInadmissible: true,
    44  		},
    45  		"didn't fit and no pending flavors": {
    46  			reason: RequeueReasonGeneric,
    47  			lastAssignment: &workload.AssigmentClusterQueueState{
    48  				LastTriedFlavorIdx: []map[corev1.ResourceName]int{
    49  					{
    50  						corev1.ResourceMemory: -1,
    51  					},
    52  					{
    53  						corev1.ResourceCPU:    -1,
    54  						corev1.ResourceMemory: -1,
    55  					},
    56  				},
    57  			},
    58  			wantInadmissible: true,
    59  		},
    60  		"didn't fit but pending flavors": {
    61  			reason: RequeueReasonGeneric,
    62  			lastAssignment: &workload.AssigmentClusterQueueState{
    63  				LastTriedFlavorIdx: []map[corev1.ResourceName]int{
    64  					{
    65  						corev1.ResourceCPU:    -1,
    66  						corev1.ResourceMemory: 0,
    67  					},
    68  					{
    69  						corev1.ResourceMemory: 1,
    70  					},
    71  				},
    72  			},
    73  			wantInadmissible: false,
    74  		},
    75  	}
    76  
    77  	for name, tc := range tests {
    78  		t.Run(name, func(t *testing.T) {
    79  			cq, _ := newClusterQueueBestEffortFIFO(
    80  				&kueue.ClusterQueue{
    81  					Spec: kueue.ClusterQueueSpec{
    82  						QueueingStrategy: kueue.StrictFIFO,
    83  					},
    84  				},
    85  				workload.Ordering{PodsReadyRequeuingTimestamp: config.EvictionTimestamp},
    86  			)
    87  			wl := utiltesting.MakeWorkload("workload-1", defaultNamespace).Obj()
    88  			info := workload.NewInfo(wl)
    89  			info.LastAssignment = tc.lastAssignment
    90  			if ok := cq.RequeueIfNotPresent(info, tc.reason); !ok {
    91  				t.Error("failed to requeue nonexistent workload")
    92  			}
    93  
    94  			_, gotInadmissible := cq.(*ClusterQueueBestEffortFIFO).inadmissibleWorkloads[workload.Key(wl)]
    95  			if diff := cmp.Diff(tc.wantInadmissible, gotInadmissible); diff != "" {
    96  				t.Errorf("Unexpected inadmissible status (-want,+got):\n%s", diff)
    97  			}
    98  
    99  			if ok := cq.RequeueIfNotPresent(workload.NewInfo(wl), tc.reason); ok {
   100  				t.Error("Re-queued a workload that was already present")
   101  			}
   102  		})
   103  	}
   104  }