Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions pkg/util/trainingruntime/trainingruntime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2026 The Kubeflow Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package trainingruntime

import (
"testing"

"github.com/kubeflow/trainer/v2/pkg/constants"
)

func TestIsSupportDeprecated(t *testing.T) {
cases := map[string]struct {
labels map[string]string
want bool
}{
"nil labels returns false": {
labels: nil,
want: false,
},
"empty labels returns false": {
labels: map[string]string{},
want: false,
},
"label key absent returns false": {
labels: map[string]string{
"some-other-label": "value",
},
want: false,
},
"label key present but value is not deprecated returns false": {
labels: map[string]string{
constants.LabelSupport: "supported",
},
want: false,
},
"label key present but value is empty returns false": {
labels: map[string]string{
constants.LabelSupport: "",
},
want: false,
},
"label key present with deprecated value returns true": {
labels: map[string]string{
constants.LabelSupport: constants.SupportDeprecated,
},
want: true,
},
"deprecated label alongside other labels returns true": {
labels: map[string]string{
"app": "kubeflow",
constants.LabelSupport: constants.SupportDeprecated,
"version": "v2",
},
want: true,
},
"partial match of deprecated value returns false": {
labels: map[string]string{
constants.LabelSupport: constants.SupportDeprecated + "-extra",
},
want: false,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IsSupportDeprecated(tc.labels)
if got != tc.want {
t.Errorf("IsSupportDeprecated(%v) = %v, want %v", tc.labels, got, tc.want)
}
})
}
}
125 changes: 122 additions & 3 deletions pkg/util/trainjob/trainjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package trainjob
import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

trainer "github.com/kubeflow/trainer/v2/pkg/apis/trainer/v1alpha1"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestRuntimeRefIsTrainingRuntime(t *testing.T) {
t.Run(name, func(t *testing.T) {
got := RuntimeRefIsTrainingRuntime(tc.ref)
if got != tc.want {
t.Errorf("Unexpected RuntimeRefIsTrainingRuntime()\nwant: %v\n, want: %v", got, tc.want)
t.Errorf("Unexpected RuntimeRefIsTrainingRuntime()\ngot: %v, want: %v", got, tc.want)
}
})
}
Expand All @@ -66,20 +67,138 @@ func TestRuntimeRefIsClusterTrainingRuntime(t *testing.T) {
},
want: true,
},
"runtimeRef is not ClusterTrainingRuntime": {
"runtimeRef is TrainingRuntime not ClusterTrainingRuntime": {
ref: trainer.RuntimeRef{
APIGroup: &trainer.GroupVersion.Group,
Kind: ptr.To(trainer.TrainingRuntimeKind),
},
want: false,
},
"runtimeRef has wrong APIGroup": {
ref: trainer.RuntimeRef{
APIGroup: ptr.To("other.group.io"),
Kind: ptr.To(trainer.ClusterTrainingRuntimeKind),
},
want: false,
},
"runtimeRef has nil APIGroup": {
ref: trainer.RuntimeRef{
APIGroup: nil,
Kind: ptr.To(trainer.ClusterTrainingRuntimeKind),
},
want: false,
},
"runtimeRef has nil Kind": {
ref: trainer.RuntimeRef{
APIGroup: &trainer.GroupVersion.Group,
Kind: nil,
},
want: false,
},
"runtimeRef has both nil": {
ref: trainer.RuntimeRef{},
want: false,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := RuntimeRefIsClusterTrainingRuntime(tc.ref)
if got != tc.want {
t.Errorf("Unexpected RuntimeRefIsClusterTrainingRuntime()\nwant: %v\n, want: %v", got, tc.want)
t.Errorf("RuntimeRefIsClusterTrainingRuntime(%v) = %v, want %v", tc.ref, got, tc.want)
}
})
}
}

func TestIsTrainJobFinished(t *testing.T) {
cases := map[string]struct {
trainJob *trainer.TrainJob
want bool
}{
"completed TrainJob is finished": {
trainJob: &trainer.TrainJob{
Status: trainer.TrainJobStatus{
Conditions: []metav1.Condition{
{
Type: trainer.TrainJobComplete,
Status: metav1.ConditionTrue,
},
},
},
},
want: true,
},
"failed TrainJob is finished": {
trainJob: &trainer.TrainJob{
Status: trainer.TrainJobStatus{
Conditions: []metav1.Condition{
{
Type: trainer.TrainJobFailed,
Status: metav1.ConditionTrue,
},
},
},
},
want: true,
},
"running TrainJob with no conditions is not finished": {
trainJob: &trainer.TrainJob{
Status: trainer.TrainJobStatus{
Conditions: []metav1.Condition{},
},
},
want: false,
},
"TrainJob with Complete=False is not finished": {
trainJob: &trainer.TrainJob{
Status: trainer.TrainJobStatus{
Conditions: []metav1.Condition{
{
Type: trainer.TrainJobComplete,
Status: metav1.ConditionFalse,
},
},
},
},
want: false,
},
"TrainJob with Failed=False is not finished": {
trainJob: &trainer.TrainJob{
Status: trainer.TrainJobStatus{
Conditions: []metav1.Condition{
{
Type: trainer.TrainJobFailed,
Status: metav1.ConditionFalse,
},
},
},
},
want: false,
},
"TrainJob with both Complete and Failed true is finished": {
trainJob: &trainer.TrainJob{
Status: trainer.TrainJobStatus{
Conditions: []metav1.Condition{
{
Type: trainer.TrainJobComplete,
Status: metav1.ConditionTrue,
},
{
Type: trainer.TrainJobFailed,
Status: metav1.ConditionTrue,
},
},
},
},
want: true,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IsTrainJobFinished(tc.trainJob)
if got != tc.want {
t.Errorf("IsTrainJobFinished() = %v, want %v", got, tc.want)
}
})
}
}
Loading