Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Antonin Stefanutti <[email protected]>
  • Loading branch information
astefanutti committed Feb 14, 2025
1 parent b3a865e commit 4d7f02a
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 46 deletions.
22 changes: 15 additions & 7 deletions pkg/runtime/core/clustertrainingruntime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"k8s.io/apimachinery/pkg/runtime"
schedulerpluginsv1alpha1 "sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"

trainer "github.com/kubeflow/trainer/pkg/apis/trainer/v1alpha1"
Expand All @@ -41,7 +41,7 @@ func TestClusterTrainingRuntimeNewObjects(t *testing.T) {
cases := map[string]struct {
trainJob *trainer.TrainJob
clusterTrainingRuntime *trainer.ClusterTrainingRuntime
wantObjs []client.Object
wantObjs []runtime.Object
wantError error
}{
"succeeded to build PodGroup and JobSet with NumNodes from the Runtime and container from the Trainer.": {
Expand All @@ -63,7 +63,7 @@ func TestClusterTrainingRuntimeNewObjects(t *testing.T) {
Obj(),
).
Obj(),
wantObjs: []client.Object{
wantObjs: []runtime.Object{
testingutil.MakeJobSetWrapper(metav1.NamespaceDefault, "test-job").
InitContainerDatasetModelInitializer("test:runtime", []string{"runtime"}, []string{"runtime"}, resRequests).
NumNodes(100).
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestClusterTrainingRuntimeNewObjects(t *testing.T) {
},
}
cmpOpts := []cmp.Option{
cmpopts.SortSlices(func(a, b client.Object) bool {
cmpopts.SortSlices(func(a, b runtime.Object) bool {
return a.GetObjectKind().GroupVersionKind().String() < b.GetObjectKind().GroupVersionKind().String()
}),
cmpopts.EquateEmpty(),
Expand All @@ -109,8 +109,9 @@ func TestClusterTrainingRuntimeNewObjects(t *testing.T) {
if tc.clusterTrainingRuntime != nil {
clientBuilder.WithObjects(tc.clusterTrainingRuntime)
}
c := clientBuilder.Build()

trainingRuntime, err := NewTrainingRuntime(ctx, clientBuilder.Build(), testingutil.AsIndex(clientBuilder))
trainingRuntime, err := NewTrainingRuntime(ctx, c, testingutil.AsIndex(clientBuilder))
if err != nil {
t.Fatal(err)
}
Expand All @@ -120,15 +121,22 @@ func TestClusterTrainingRuntimeNewObjects(t *testing.T) {
t.Fatal("Failed type assertion from Runtime interface to TrainingRuntime")
}

clTrainingRuntime, err := NewClusterTrainingRuntime(ctx, clientBuilder.Build(), testingutil.AsIndex(clientBuilder))
clTrainingRuntime, err := NewClusterTrainingRuntime(ctx, c, testingutil.AsIndex(clientBuilder))
if err != nil {
t.Fatal(err)
}

objs, err := clTrainingRuntime.NewObjects(ctx, tc.trainJob)
if diff := cmp.Diff(tc.wantError, err, cmpopts.EquateErrors()); len(diff) != 0 {
t.Errorf("Unexpected error (-want,+got):\n%s", diff)
}
if diff := cmp.Diff(tc.wantObjs, objs, cmpOpts...); len(diff) != 0 {

resultObjs, err := testingutil.UnstructuredToObject(c.Scheme(), objs...)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.wantObjs, resultObjs, cmpOpts...); len(diff) != 0 {
t.Errorf("Unexpected objects (-want,+got):\n%s", diff)
}
})
Expand Down
31 changes: 21 additions & 10 deletions pkg/runtime/core/trainingruntime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"k8s.io/apimachinery/pkg/runtime"
schedulerpluginsv1alpha1 "sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"

trainer "github.com/kubeflow/trainer/pkg/apis/trainer/v1alpha1"
Expand All @@ -44,7 +44,7 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
cases := map[string]struct {
trainingRuntime *trainer.TrainingRuntime
trainJob *trainer.TrainJob
wantObjs []client.Object
wantObjs []runtime.Object
wantError error
}{
// Test cases for the PlainML MLPolicy.
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
Obj(),
).
Obj(),
wantObjs: []client.Object{
wantObjs: []runtime.Object{
testingutil.MakeJobSetWrapper(metav1.NamespaceDefault, "test-job").
InitContainerDatasetModelInitializer("test:runtime", []string{"runtime"}, []string{"runtime"}, resRequests).
NumNodes(30).
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
Obj(),
).
Obj(),
wantObjs: []client.Object{
wantObjs: []runtime.Object{
testingutil.MakeJobSetWrapper(metav1.NamespaceDefault, "test-job").
NumNodes(100).
ContainerTrainer("test:trainjob", []string{"trainjob"}, []string{"trainjob"}, resRequests).
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
Obj(),
).
Obj(),
wantObjs: []client.Object{
wantObjs: []runtime.Object{
testingutil.MakeJobSetWrapper(metav1.NamespaceDefault, "test-job").
NumNodes(100).
ContainerTrainer("test:runtime", []string{"runtime"}, []string{"runtime"}, resRequests).
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
Obj(),
).
Obj(),
wantObjs: []client.Object{
wantObjs: []runtime.Object{
testingutil.MakeJobSetWrapper(metav1.NamespaceDefault, "test-job").
NumNodes(30).
ContainerTrainer("test:runtime", []string{"runtime"}, []string{"runtime"}, resRequests).
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
Obj(),
).
Obj(),
wantObjs: []client.Object{
wantObjs: []runtime.Object{
testingutil.MakeJobSetWrapper(metav1.NamespaceDefault, "test-job").
NumNodes(100).
ContainerTrainer("test:trainjob", []string{"trainjob"}, []string{"trainjob"}, resRequests).
Expand Down Expand Up @@ -417,9 +417,12 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
},
}
cmpOpts := []cmp.Option{
cmpopts.SortSlices(func(a, b client.Object) bool {
cmpopts.SortSlices(func(a, b runtime.Object) bool {
return a.GetObjectKind().GroupVersionKind().String() < b.GetObjectKind().GroupVersionKind().String()
}),
cmpopts.SortSlices(func(a, b corev1.EnvVar) bool {
return a.Name < b.Name
}),
cmpopts.EquateEmpty(),
cmpopts.SortMaps(func(a, b string) bool { return a < b }),
}
Expand All @@ -431,16 +434,24 @@ func TestTrainingRuntimeNewObjects(t *testing.T) {
if tc.trainingRuntime != nil {
clientBuilder.WithObjects(tc.trainingRuntime)
}
c := clientBuilder.Build()

trainingRuntime, err := NewTrainingRuntime(ctx, clientBuilder.Build(), testingutil.AsIndex(clientBuilder))
trainingRuntime, err := NewTrainingRuntime(ctx, c, testingutil.AsIndex(clientBuilder))
if err != nil {
t.Fatal(err)
}

objs, err := trainingRuntime.NewObjects(ctx, tc.trainJob)
if diff := cmp.Diff(tc.wantError, err, cmpopts.EquateErrors()); len(diff) != 0 {
t.Errorf("Unexpected error (-want,+got):\n%s", diff)
}
if diff := cmp.Diff(tc.wantObjs, objs, cmpOpts...); len(diff) != 0 {

resultObjs, err := testingutil.UnstructuredToObject(c.Scheme(), objs...)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.wantObjs, resultObjs, cmpOpts...); len(diff) != 0 {
t.Errorf("Unexpected objects (-want,+got):\n%s", diff)
}
})
Expand Down
47 changes: 33 additions & 14 deletions pkg/runtime/framework/core/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiruntime "k8s.io/apimachinery/pkg/runtime"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -387,17 +388,17 @@ func TestRunComponentBuilderPlugins(t *testing.T) {
}

cases := map[string]struct {
registry fwkplugins.Registry
runtimeInfo *runtime.Info
trainJob *trainer.TrainJob
runtimeJobTemplate client.Object
wantRuntimeInfo *runtime.Info
wantObjs []client.Object
wantError error
registry fwkplugins.Registry
runtimeInfo *runtime.Info
trainingRuntime *trainer.TrainingRuntime
trainJob *trainer.TrainJob
wantRuntimeInfo *runtime.Info
wantObjs []k8sruntime.Object
wantError error
}{
"succeeded to build PodGroup and JobSet with NumNodes from TrainJob": {
registry: fwkplugins.NewRegistry(),
runtimeJobTemplate: testingutil.MakeJobSetWrapper(metav1.NamespaceDefault, "test-job").DeepCopy(),
registry: fwkplugins.NewRegistry(),
trainingRuntime: testingutil.MakeTrainingRuntimeWrapper(metav1.NamespaceDefault, "test-runtime").DeepCopy(),
runtimeInfo: &runtime.Info{
RuntimePolicy: runtime.RuntimePolicy{
MLPolicy: &trainer.MLPolicy{
Expand Down Expand Up @@ -429,6 +430,7 @@ func TestRunComponentBuilderPlugins(t *testing.T) {
},
trainJob: testingutil.MakeTrainJobWrapper(metav1.NamespaceDefault, "test-job").
UID("uid").
RuntimeRef(trainer.SchemeGroupVersion.WithKind(trainer.TrainingRuntimeKind), "test-runtime").
Trainer(
testingutil.MakeTrainJobTrainerWrapper().
NumNodes(100).
Expand Down Expand Up @@ -466,7 +468,7 @@ func TestRunComponentBuilderPlugins(t *testing.T) {
},
},
},
wantObjs: []client.Object{
wantObjs: []k8sruntime.Object{
testingutil.MakeSchedulerPluginsPodGroup(metav1.NamespaceDefault, "test-job").
SchedulingTimeout(300).
MinMember(101). // 101 replicas = 100 Trainer nodes + 1 Initializer.
Expand All @@ -487,7 +489,7 @@ func TestRunComponentBuilderPlugins(t *testing.T) {
"an empty registry": {},
}
cmpOpts := []cmp.Option{
cmpopts.SortSlices(func(a, b client.Object) bool {
cmpopts.SortSlices(func(a, b k8sruntime.Object) bool {
return a.GetObjectKind().GroupVersionKind().String() < b.GetObjectKind().GroupVersionKind().String()
}),
cmpopts.EquateEmpty(),
Expand All @@ -496,9 +498,14 @@ func TestRunComponentBuilderPlugins(t *testing.T) {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

clientBuilder := testingutil.NewClientBuilder()
if tc.trainingRuntime != nil {
clientBuilder.WithObjects(tc.trainingRuntime)
}
c := clientBuilder.Build()

fwk, err := New(ctx, clientBuilder.Build(), tc.registry, testingutil.AsIndex(clientBuilder))
fwk, err := New(ctx, c, tc.registry, testingutil.AsIndex(clientBuilder))
if err != nil {
t.Fatal(err)
}
Expand All @@ -510,13 +517,21 @@ func TestRunComponentBuilderPlugins(t *testing.T) {
t.Fatal(err)
}
objs, err := fwk.RunComponentBuilderPlugins(ctx, tc.runtimeInfo, tc.trainJob)

if diff := cmp.Diff(tc.wantError, err, cmpopts.EquateErrors()); len(diff) != 0 {
t.Errorf("Unexpected errors (-want,+got):\n%s", diff)
}

if diff := cmp.Diff(tc.wantRuntimeInfo, tc.runtimeInfo); len(diff) != 0 {
t.Errorf("Unexpected runtime.Info (-want,+got)\n%s", diff)
}
if diff := cmp.Diff(tc.wantObjs, objs, cmpOpts...); len(diff) != 0 {

resultObjs, err := testingutil.UnstructuredToObject(c.Scheme(), objs...)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.wantObjs, resultObjs, cmpOpts...); len(diff) != 0 {
t.Errorf("Unexpected objects (-want,+got):\n%s", diff)
}
})
Expand Down Expand Up @@ -651,14 +666,18 @@ func TestTerminalConditionPlugins(t *testing.T) {
if tc.jobSet != nil {
clientBuilder = clientBuilder.WithObjects(tc.jobSet)
}
fwk, err := New(ctx, clientBuilder.Build(), tc.registry, testingutil.AsIndex(clientBuilder))
c := clientBuilder.Build()

fwk, err := New(ctx, c, tc.registry, testingutil.AsIndex(clientBuilder))
if err != nil {
t.Fatal(err)
}

gotCond, gotErr := fwk.RunTerminalConditionPlugins(ctx, tc.trainJob)
if diff := cmp.Diff(tc.wantError, gotErr, cmpopts.EquateErrors()); len(diff) != 0 {
t.Errorf("Unexpected error (-want,+got):\n%s", diff)
}

if diff := cmp.Diff(tc.wantCondition, gotCond); len(diff) != 0 {
t.Errorf("Unexpected terminal condition (-want,+got):\n%s", diff)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/runtime/framework/plugins/coscheduling/coscheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import (
"fmt"
"slices"

trainer "github.com/kubeflow/trainer/pkg/apis/trainer/v1alpha1"
"github.com/kubeflow/trainer/pkg/runtime"
"github.com/kubeflow/trainer/pkg/runtime/framework"
runtimeindexer "github.com/kubeflow/trainer/pkg/runtime/indexer"
corev1 "k8s.io/api/core/v1"
nodev1 "k8s.io/api/node/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -45,6 +41,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
schedulerpluginsv1alpha1 "sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"
schedulerpluginsv1alpha1ac "sigs.k8s.io/scheduler-plugins/pkg/generated/applyconfiguration/scheduling/v1alpha1"

trainer "github.com/kubeflow/trainer/pkg/apis/trainer/v1alpha1"
"github.com/kubeflow/trainer/pkg/runtime"
"github.com/kubeflow/trainer/pkg/runtime/framework"
runtimeindexer "github.com/kubeflow/trainer/pkg/runtime/indexer"
)

type CoScheduling struct {
Expand Down
63 changes: 63 additions & 0 deletions pkg/util/testing/unstructured.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2024 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 testing

import (
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

func UnstructuredToObject(s *runtime.Scheme, objects ...*unstructured.Unstructured) ([]runtime.Object, error) {
var objs []runtime.Object
for _, obj := range objects {
if o, err := toObject(s, obj); err != nil {
return nil, err
} else {
objs = append(objs, o)
}
}
return objs, nil
}

func toObject(s *runtime.Scheme, obj runtime.Object) (runtime.Object, error) {
u, isUnstructured := obj.(runtime.Unstructured)
if !isUnstructured {
return obj, nil
}
gvk := obj.GetObjectKind().GroupVersionKind()
if !s.Recognizes(gvk) {
return obj, nil
}

typed, err := s.New(gvk)
if err != nil {
return nil, fmt.Errorf("scheme recognizes %s but failed to produce an object for it: %w", gvk, err)
}

raw, err := json.Marshal(u)
if err != nil {
return nil, fmt.Errorf("failed to serialize %T: %w", raw, err)
}
if err := json.Unmarshal(raw, typed); err != nil {
return nil, fmt.Errorf("failed to unmarshal the content of %T into %T: %w", u, typed, err)
}

return typed, nil
}
Loading

0 comments on commit 4d7f02a

Please sign in to comment.