volcano.sh/volcano@v1.9.0/pkg/controllers/job/plugins/distributed-framework/mpi/mpi_test.go (about)

     1  /*
     2  Copyright 2022 The Volcano 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 mpi
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	"volcano.sh/apis/pkg/apis/batch/v1alpha1"
    26  	pluginsinterface "volcano.sh/volcano/pkg/controllers/job/plugins/interface"
    27  )
    28  
    29  func TestMpi(t *testing.T) {
    30  	plugins := make(map[string][]string)
    31  	plugins[MPIPluginName] = []string{"--port=5000"}
    32  	testjob5000 := &v1alpha1.Job{
    33  		ObjectMeta: metav1.ObjectMeta{Name: "test-mpi-1"},
    34  		Spec: v1alpha1.JobSpec{
    35  			Plugins: plugins,
    36  			Tasks: []v1alpha1.TaskSpec{
    37  				{
    38  					Name:     "fakeMaster",
    39  					Replicas: 1,
    40  					Template: v1.PodTemplateSpec{},
    41  				},
    42  				{
    43  					Name:     "fakeWorker",
    44  					Replicas: 2,
    45  					Template: v1.PodTemplateSpec{},
    46  				},
    47  			},
    48  		},
    49  	}
    50  
    51  	testcases := []struct {
    52  		Name string
    53  		Job  *v1alpha1.Job
    54  		Pod  *v1.Pod
    55  		port int
    56  	}{
    57  		{
    58  			Name: "add port",
    59  			Job: &v1alpha1.Job{
    60  				ObjectMeta: metav1.ObjectMeta{Name: "test-mpi-0"},
    61  				Spec: v1alpha1.JobSpec{
    62  					Tasks: []v1alpha1.TaskSpec{
    63  						{
    64  							Name:     "fakeMaster",
    65  							Replicas: 1,
    66  							Template: v1.PodTemplateSpec{},
    67  						},
    68  						{
    69  							Name:     "fakeWorker",
    70  							Replicas: 2,
    71  							Template: v1.PodTemplateSpec{},
    72  						},
    73  					},
    74  				},
    75  			},
    76  			Pod: &v1.Pod{
    77  				ObjectMeta: metav1.ObjectMeta{
    78  					Name: "test-mpi-fakeMaster-0",
    79  				},
    80  				Spec: v1.PodSpec{
    81  					Containers: []v1.Container{
    82  						{
    83  							Name: "master",
    84  						},
    85  					},
    86  				},
    87  			},
    88  			port: DefaultPort,
    89  		},
    90  		{
    91  			Name: "add 5000 port",
    92  			Job:  testjob5000,
    93  			Pod: &v1.Pod{
    94  				ObjectMeta: metav1.ObjectMeta{
    95  					Name: "test-mpi-fakeMaster-0",
    96  				},
    97  				Spec: v1.PodSpec{
    98  					Containers: []v1.Container{
    99  						{
   100  							Name: "master",
   101  						},
   102  					},
   103  				},
   104  			},
   105  			port: 5000,
   106  		},
   107  	}
   108  
   109  	for index, testcase := range testcases {
   110  		t.Run(testcase.Name, func(t *testing.T) {
   111  			mp := New(pluginsinterface.PluginClientset{}, testcase.Job.Spec.Plugins[MPIPluginName])
   112  			if err := mp.OnPodCreate(testcase.Pod, testcase.Job); err != nil {
   113  				t.Errorf("Case %d (%s): expect no error, but got error %v", index, testcase.Name, err)
   114  			}
   115  			if testcase.Pod.Spec.Containers[0].Ports == nil || testcase.Pod.Spec.Containers[0].Ports[0].ContainerPort != int32(testcase.port) {
   116  				t.Errorf("Case %d (%s): wrong port, got %d", index, testcase.Name, testcase.Pod.Spec.Containers[0].Ports[0].ContainerPort)
   117  			}
   118  		})
   119  	}
   120  }