Skip to content

Commit

Permalink
add ut for pkg/controllers/job/plugins
Browse files Browse the repository at this point in the history
Signed-off-by: googs1025 <[email protected]>
  • Loading branch information
googs1025 committed May 7, 2024
1 parent d6d29f9 commit 403bd6c
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 21 deletions.
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 @@ -17,10 +17,12 @@ limitations under the License.
package mpi

import (
"reflect"
"testing"

v1 "k8s.io/api/core/v1"
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{
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 !reflect.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobUpdate(testcase.Job)
if !reflect.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobDelete(testcase.Job)
if !reflect.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 @@ -366,7 +385,19 @@ func TestPytorch(t *testing.T) {
}

if !reflect.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)
t.Fatalf("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 !reflect.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobUpdate(testcase.Job)
if !reflect.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", err, testcase.wantErr)
}
err = mp.OnJobDelete(testcase.Job)
if !reflect.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobDelete error = %v, wantErr %v", err, testcase.wantErr)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package tensorflow

import (
"reflect"
"strings"
"testing"

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 !reflect.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", err, testcase.wantErr)
}
err = tp.OnJobUpdate(testcase.Job)
if !reflect.DeepEqual(err, testcase.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", err, testcase.wantErr)
}
err = tp.OnJobDelete(testcase.Job)
if !reflect.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 (
"reflect"
"testing"

v1 "k8s.io/api/core/v1"
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 !reflect.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnPodCreate error = %v, wantErr %v", gotErr, test.wantErr)
}
gotErr = plugin.OnJobAdd(job)
if !reflect.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnJobAdd error = %v, wantErr %v", gotErr, test.wantErr)
}
gotErr = plugin.OnJobUpdate(job)
if !reflect.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnJobUpdate error = %v, wantErr %v", gotErr, test.wantErr)
}
gotErr = plugin.OnJobDelete(job)
if !reflect.DeepEqual(gotErr, test.wantErr) {
t.Fatalf("OnJobDelete error = %v, wantErr %v", gotErr, test.wantErr)
}
})
}
}
Loading

0 comments on commit 403bd6c

Please sign in to comment.