sigs.k8s.io/kueue@v0.6.2/pkg/controller/jobframework/workload_names_test.go (about)

     1  /*
     2  Copyright 2023 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 jobframework
    18  
    19  import (
    20  	"errors"
    21  	"strings"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  func TestGetWorkloadNameForOwner(t *testing.T) {
    28  	testCases := map[string]struct {
    29  		owner            *metav1.OwnerReference
    30  		wantWorkloadName string
    31  		wantErr          error
    32  	}{
    33  		"simple name": {
    34  			owner:            &metav1.OwnerReference{Kind: "Job", APIVersion: "batch/v1", Name: "myjob"},
    35  			wantWorkloadName: "job-myjob-2f79a",
    36  		},
    37  		"lengthy name": {
    38  			owner:            &metav1.OwnerReference{Kind: "Job", APIVersion: "batch/v1", Name: strings.Repeat("a", 252)},
    39  			wantWorkloadName: "job-" + strings.Repeat("a", 253-4-6) + "-f1c14",
    40  		},
    41  		"simple MPIJob name for v2beta1": {
    42  			owner:            &metav1.OwnerReference{Kind: "MPIJob", APIVersion: "kubeflow.org/v2beta1", Name: "myjob"},
    43  			wantWorkloadName: "mpijob-myjob-98672",
    44  		},
    45  		"simple MPIJob name for v2; should be as for v2beta1": {
    46  			owner:            &metav1.OwnerReference{Kind: "MPIJob", APIVersion: "kubeflow.org/v2", Name: "myjob"},
    47  			wantWorkloadName: "mpijob-myjob-98672",
    48  		},
    49  		"invalid APIVersion": {
    50  			owner:   &metav1.OwnerReference{Kind: "Job", APIVersion: "batch/v1/beta1", Name: "myjob"},
    51  			wantErr: errors.New("unexpected GroupVersion string: batch/v1/beta1"),
    52  		},
    53  	}
    54  
    55  	for name, tc := range testCases {
    56  		t.Run(name, func(t *testing.T) {
    57  			gotWorkloadName, gotErr := GetWorkloadNameForOwnerRef(tc.owner)
    58  			if tc.wantWorkloadName != gotWorkloadName {
    59  				t.Errorf("Unexpected workload name. want=%s, got=%s", tc.wantWorkloadName, gotWorkloadName)
    60  			}
    61  			if gotErr != nil && tc.wantErr == nil {
    62  				t.Errorf("Unexpected error present: %s", gotErr.Error())
    63  			} else if gotErr == nil && tc.wantErr != nil {
    64  				t.Errorf("Missing expected error: %s", tc.wantErr.Error())
    65  			} else if gotErr != nil && tc.wantErr != nil && gotErr.Error() != tc.wantErr.Error() {
    66  				t.Errorf("Unexpected error. want: %s, got: %s.", tc.wantErr.Error(), gotErr.Error())
    67  			}
    68  		})
    69  	}
    70  }