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

     1  /*
     2  Copyright 2021 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 tensorflow
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
    27  	pluginsinterface "volcano.sh/volcano/pkg/controllers/job/plugins/interface"
    28  )
    29  
    30  func TestTensorflow(t *testing.T) {
    31  	testjob := &batch.Job{
    32  		ObjectMeta: metav1.ObjectMeta{Name: "train-123"},
    33  		Spec: batch.JobSpec{
    34  			Tasks: []batch.TaskSpec{
    35  				{
    36  					Name:     "ps",
    37  					Replicas: 2,
    38  					Template: v1.PodTemplateSpec{},
    39  				},
    40  				{
    41  					Name:     "worker",
    42  					Replicas: 2,
    43  					Template: v1.PodTemplateSpec{},
    44  				},
    45  				{
    46  					Name:     "chief",
    47  					Replicas: 1,
    48  					Template: v1.PodTemplateSpec{},
    49  				},
    50  			},
    51  		},
    52  	}
    53  	testcases := []struct {
    54  		Name string
    55  		Job  *batch.Job
    56  		Pod  *v1.Pod
    57  	}{
    58  		{
    59  			Name: "ps case",
    60  			Job:  testjob,
    61  			Pod: &v1.Pod{
    62  				ObjectMeta: metav1.ObjectMeta{
    63  					Name: "train-123-ps-0",
    64  					Annotations: map[string]string{
    65  						batch.TaskSpecKey: "ps",
    66  					},
    67  				},
    68  				Spec: v1.PodSpec{
    69  					Containers: []v1.Container{
    70  						{
    71  							Name: "main",
    72  						},
    73  					},
    74  				},
    75  			},
    76  		},
    77  		{
    78  			Name: "worker case",
    79  			Job:  testjob,
    80  			Pod: &v1.Pod{
    81  				ObjectMeta: metav1.ObjectMeta{
    82  					Name: "train-123-worker-0",
    83  					Annotations: map[string]string{
    84  						batch.TaskSpecKey: "worker",
    85  					},
    86  				},
    87  				Spec: v1.PodSpec{
    88  					Containers: []v1.Container{
    89  						{
    90  							Name: "main",
    91  						},
    92  					},
    93  				},
    94  			},
    95  		},
    96  		{
    97  			Name: "chief case",
    98  			Job:  testjob,
    99  			Pod: &v1.Pod{
   100  				ObjectMeta: metav1.ObjectMeta{
   101  					Name: "train-123-chief-0",
   102  					Annotations: map[string]string{
   103  						batch.TaskSpecKey: "chief",
   104  					},
   105  				},
   106  				Spec: v1.PodSpec{
   107  					Containers: []v1.Container{
   108  						{
   109  							Name: "main",
   110  						},
   111  					},
   112  				},
   113  			},
   114  		},
   115  	}
   116  
   117  	for i, testcase := range testcases {
   118  		t.Run(testcase.Name, func(t *testing.T) {
   119  			tp := New(pluginsinterface.PluginClientset{}, []string{"--port=5000"})
   120  			if err := tp.OnPodCreate(testcase.Pod, testcase.Job); err != nil {
   121  				t.Errorf("Case %d (%s): expect no error, but got error %v", i, testcase.Name, err)
   122  			}
   123  			if testcase.Pod.Spec.Containers[0].Env[0].Name != "TF_CONFIG" {
   124  				t.Errorf("Case %d (%s): wrong env name, got %s", i, testcase.Name, testcase.Pod.Spec.Containers[0].Env[0].Name)
   125  			}
   126  
   127  			switch {
   128  			case strings.Contains(testcase.Pod.Name, "ps"):
   129  				if testcase.Pod.Spec.Containers[0].Env[0].Value != `{"cluster":{"ps":["train-123-ps-0.train-123:5000","train-123-ps-1.train-123:5000"],"worker":["train-123-worker-0.train-123:5000","train-123-worker-1.train-123:5000"],"chief":["train-123-chief-0.train-123:5000"]},"task":{"type":"ps","index":0}}` {
   130  					t.Errorf("Case %d (%s): wrong env value, got %s", i, testcase.Name, testcase.Pod.Spec.Containers[0].Env[0].Value)
   131  				}
   132  			case strings.Contains(testcase.Pod.Name, "worker"):
   133  				if testcase.Pod.Spec.Containers[0].Env[0].Value != `{"cluster":{"ps":["train-123-ps-0.train-123:5000","train-123-ps-1.train-123:5000"],"worker":["train-123-worker-0.train-123:5000","train-123-worker-1.train-123:5000"],"chief":["train-123-chief-0.train-123:5000"]},"task":{"type":"worker","index":0}}` {
   134  					t.Errorf("Case %d (%s): wrong env value, got %s", i, testcase.Name, testcase.Pod.Spec.Containers[0].Env[0].Value)
   135  				}
   136  			case strings.Contains(testcase.Pod.Name, "chief"):
   137  				if testcase.Pod.Spec.Containers[0].Env[0].Value != `{"cluster":{"ps":["train-123-ps-0.train-123:5000","train-123-ps-1.train-123:5000"],"worker":["train-123-worker-0.train-123:5000","train-123-worker-1.train-123:5000"],"chief":["train-123-chief-0.train-123:5000"]},"task":{"type":"chief","index":0}}` {
   138  					t.Errorf("Case %d (%s): wrong env value, got %s", i, testcase.Name, testcase.Pod.Spec.Containers[0].Env[0].Value)
   139  				}
   140  			}
   141  		})
   142  	}
   143  }