Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ut for pkg/controllers/job/plugins #3422

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
53 changes: 46 additions & 7 deletions pkg/controllers/job/plugins/distributed-framework/mpi/mpi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"testing"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"

"volcano.sh/apis/pkg/apis/batch/v1alpha1"
pluginsinterface "volcano.sh/volcano/pkg/controllers/job/plugins/interface"
Expand All @@ -46,13 +48,17 @@ func TestMpi(t *testing.T) {
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
}

testcases := []struct {
Name string
Job *v1alpha1.Job
Pod *v1.Pod
port int
Name string
Job *v1alpha1.Job
Pod *v1.Pod
port int
wantErr error
}{
{
Name: "add port",
Expand All @@ -61,26 +67,47 @@ func TestMpi(t *testing.T) {
Spec: v1alpha1.JobSpec{
Tasks: []v1alpha1.TaskSpec{
{
Name: "fakeMaster",
Name: "master",
Replicas: 1,
Template: v1.PodTemplateSpec{},
},
{
Name: "fakeWorker",
Name: "worker",
Replicas: 2,
Template: v1.PodTemplateSpec{},
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
},
Pod: &v1.Pod{
googs1025 marked this conversation as resolved.
Show resolved Hide resolved
ObjectMeta: metav1.ObjectMeta{
Name: "test-mpi-fakeMaster-0",
Name: "test-mpi-fakeMaster-0",
Annotations: map[string]string{batch.TaskSpecKey: DefaultMaster},
},
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{
Name: "master",
Ports: []v1.ContainerPort{
{
Name: "test",
ContainerPort: DefaultPort,
},
},
},
},
Containers: []v1.Container{
{
Name: "master",
Ports: []v1.ContainerPort{
{
Name: "test",
ContainerPort: DefaultPort,
},
},
},
},
},
Expand Down Expand Up @@ -115,6 +142,18 @@ func TestMpi(t *testing.T) {
if testcase.Pod.Spec.Containers[0].Ports == nil || testcase.Pod.Spec.Containers[0].Ports[0].ContainerPort != int32(testcase.port) {
t.Errorf("Case %d (%s): wrong port, got %d", index, testcase.Name, testcase.Pod.Spec.Containers[0].Ports[0].ContainerPort)
}
err := mp.OnJobAdd(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobUpdate(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobDelete(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobDelete error = %v, wantErr %v", err, testcase.wantErr)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ func TestPytorch(t *testing.T) {
plugins[PytorchPluginName] = []string{"--port=5000"}

testcases := []struct {
Name string
Job *v1alpha1.Job
Pod *v1.Pod
port int
envs []v1.EnvVar
Name string
Job *v1alpha1.Job
Pod *v1.Pod
port int
envs []v1.EnvVar
wantErr error
}{
{
Name: "test pod without master",
Expand All @@ -36,6 +37,9 @@ func TestPytorch(t *testing.T) {
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
},
Pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -70,6 +74,9 @@ func TestPytorch(t *testing.T) {
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
},
Pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -124,6 +131,9 @@ func TestPytorch(t *testing.T) {
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
},
Pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -184,6 +194,9 @@ func TestPytorch(t *testing.T) {
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
},
Pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -244,6 +257,9 @@ func TestPytorch(t *testing.T) {
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
},
Pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -304,6 +320,9 @@ func TestPytorch(t *testing.T) {
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
},
Pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -368,6 +387,19 @@ func TestPytorch(t *testing.T) {
if !equality.Semantic.DeepEqual(testcase.Pod.Spec.Containers[0].Env, testcase.envs) {
t.Errorf("Case %d (%s): wrong envs, got %v, expected %v", index, testcase.Name, testcase.Pod.Spec.Containers[0].Env, testcase.envs)
}

err := mp.OnJobAdd(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobUpdate(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobDelete(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobDelete error = %v, wantErr %v", err, testcase.wantErr)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
Expand Down Expand Up @@ -49,11 +50,15 @@ func TestTensorflow(t *testing.T) {
},
},
},
Status: batch.JobStatus{
ControlledResources: map[string]string{},
},
}
testcases := []struct {
Name string
Job *batch.Job
Pod *v1.Pod
Name string
Job *batch.Job
Pod *v1.Pod
wantErr error
}{
{
Name: "ps case",
Expand Down Expand Up @@ -138,6 +143,18 @@ func TestTensorflow(t *testing.T) {
t.Errorf("Case %d (%s): wrong env value, got %s", i, testcase.Name, testcase.Pod.Spec.Containers[0].Env[0].Value)
}
}
err := tp.OnJobAdd(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", err, testcase.wantErr)
}
err = tp.OnJobUpdate(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", err, testcase.wantErr)
}
err = tp.OnJobDelete(testcase.Job)
if !equality.Semantic.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobDelete error = %v, wantErr %v", err, testcase.wantErr)
}
})
}
}
100 changes: 100 additions & 0 deletions pkg/controllers/job/plugins/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright 2024 The Volcano 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 env

import (
"testing"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

"volcano.sh/apis/pkg/apis/batch/v1alpha1"
pluginsinterface "volcano.sh/volcano/pkg/controllers/job/plugins/interface"
)

func TestENVPlugin(t *testing.T) {
var (
job = &v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{Name: "test-pytorch"},
Spec: v1alpha1.JobSpec{
Tasks: []v1alpha1.TaskSpec{
{
Name: "worker",
Replicas: 1,
Template: v1.PodTemplateSpec{},
},
},
},
Status: v1alpha1.JobStatus{
ControlledResources: map[string]string{},
},
}
pod = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pytorch-worker-0",
},
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{
Name: "worker",
},
},
Containers: []v1.Container{
{
Name: "worker",
},
},
},
}
)
tests := []struct {
name string
pluginArguments []string
wantErr error
}{
{
name: "no params specified",
pluginArguments: []string{"test1", "test2"},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pluginInterface := New(pluginsinterface.PluginClientset{KubeClients: fake.NewSimpleClientset(pod)}, test.pluginArguments)
plugin := pluginInterface.(*envPlugin)

gotErr := plugin.OnPodCreate(pod, job)
if !equality.Semantic.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnPodCreate error = %v, wantErr %v", gotErr, test.wantErr)
}
gotErr = plugin.OnJobAdd(job)
if !equality.Semantic.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", gotErr, test.wantErr)
}
gotErr = plugin.OnJobUpdate(job)
if !equality.Semantic.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", gotErr, test.wantErr)
}
gotErr = plugin.OnJobDelete(job)
if !equality.Semantic.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnJobDelete error = %v, wantErr %v", gotErr, test.wantErr)
}
})
}
}
Loading
Loading