volcano.sh/volcano@v1.9.0/pkg/controllers/apis/job_info_test.go (about)

     1  /*
     2  Copyright 2019 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 apis
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  
    27  	vcbatchv1 "volcano.sh/apis/pkg/apis/batch/v1alpha1"
    28  	vcbus "volcano.sh/apis/pkg/apis/bus/v1alpha1"
    29  )
    30  
    31  func TestAddPod(t *testing.T) {
    32  	namespace := "test"
    33  	name := "pod1"
    34  
    35  	testCases := []struct {
    36  		Name        string
    37  		jobinfo     JobInfo
    38  		pod         *v1.Pod
    39  		ExpectValue bool
    40  		ExpectErr   string
    41  	}{
    42  		{
    43  			Name: "AddPod",
    44  			pod: &v1.Pod{
    45  				ObjectMeta: metav1.ObjectMeta{
    46  					UID:       types.UID(fmt.Sprintf("%v-%v", namespace, name)),
    47  					Name:      name,
    48  					Namespace: namespace,
    49  					Labels:    nil,
    50  					Annotations: map[string]string{vcbatchv1.JobNameKey: "job1",
    51  						vcbatchv1.JobVersion:  "0",
    52  						vcbatchv1.TaskSpecKey: "task1"},
    53  				},
    54  				Status: v1.PodStatus{
    55  					Phase: v1.PodRunning,
    56  				},
    57  				Spec: v1.PodSpec{
    58  					Containers: []v1.Container{
    59  						{
    60  							Name:  "nginx",
    61  							Image: "nginx:latest",
    62  						},
    63  					},
    64  				},
    65  			},
    66  			jobinfo: JobInfo{
    67  				Pods: make(map[string]map[string]*v1.Pod),
    68  			},
    69  			ExpectValue: true,
    70  			ExpectErr:   "duplicated pod",
    71  		},
    72  	}
    73  
    74  	for i, testcase := range testCases {
    75  		err := testcase.jobinfo.AddPod(testcase.pod)
    76  		if err != nil {
    77  			t.Fatalf("AddPod() error: %v", err)
    78  		}
    79  
    80  		if _, ok := testcase.jobinfo.Pods["task1"][testcase.pod.Name]; ok != testcase.ExpectValue {
    81  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok)
    82  		}
    83  
    84  		err = testcase.jobinfo.AddPod(testcase.pod)
    85  
    86  		if err == nil {
    87  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectErr, nil)
    88  		}
    89  	}
    90  
    91  }
    92  
    93  func TestDeletePod(t *testing.T) {
    94  	namespace := "test"
    95  	name := "pod1"
    96  
    97  	testCases := []struct {
    98  		Name        string
    99  		jobinfo     JobInfo
   100  		pod         *v1.Pod
   101  		ExpectValue bool
   102  	}{
   103  		{
   104  			Name: "DeletePod",
   105  			pod: &v1.Pod{
   106  				ObjectMeta: metav1.ObjectMeta{
   107  					UID:       types.UID(fmt.Sprintf("%v-%v", namespace, name)),
   108  					Name:      name,
   109  					Namespace: namespace,
   110  					Labels:    nil,
   111  					Annotations: map[string]string{vcbatchv1.JobNameKey: "job1",
   112  						vcbatchv1.JobVersion:  "0",
   113  						vcbatchv1.TaskSpecKey: "task1"},
   114  				},
   115  				Status: v1.PodStatus{
   116  					Phase: v1.PodRunning,
   117  				},
   118  				Spec: v1.PodSpec{
   119  					Containers: []v1.Container{
   120  						{
   121  							Name:  "nginx",
   122  							Image: "nginx:latest",
   123  						},
   124  					},
   125  				},
   126  			},
   127  			jobinfo: JobInfo{
   128  				Pods: make(map[string]map[string]*v1.Pod),
   129  			},
   130  			ExpectValue: false,
   131  		},
   132  	}
   133  
   134  	for i, testcase := range testCases {
   135  
   136  		testcase.jobinfo.Pods["task1"] = make(map[string]*v1.Pod)
   137  		testcase.jobinfo.Pods["task1"][testcase.pod.Name] = testcase.pod
   138  
   139  		err := testcase.jobinfo.DeletePod(testcase.pod)
   140  		if err != nil {
   141  			t.Fatalf("DeletePod() error: %v", err)
   142  		}
   143  		if _, ok := testcase.jobinfo.Pods["task1"][testcase.pod.Name]; ok != testcase.ExpectValue {
   144  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok)
   145  		}
   146  	}
   147  }
   148  
   149  func TestUpdatePod(t *testing.T) {
   150  	namespace := "test"
   151  	name := "pod1"
   152  
   153  	testCases := []struct {
   154  		Name        string
   155  		jobinfo     JobInfo
   156  		oldpod      *v1.Pod
   157  		newpod      *v1.Pod
   158  		ExpectValue v1.PodPhase
   159  	}{
   160  		{
   161  			Name: "UpdatePod",
   162  			oldpod: &v1.Pod{
   163  				ObjectMeta: metav1.ObjectMeta{
   164  					UID:       types.UID(fmt.Sprintf("%v-%v", namespace, name)),
   165  					Name:      name,
   166  					Namespace: namespace,
   167  					Labels:    nil,
   168  					Annotations: map[string]string{vcbatchv1.JobNameKey: "job1",
   169  						vcbatchv1.JobVersion:  "0",
   170  						vcbatchv1.TaskSpecKey: "task1"},
   171  				},
   172  				Status: v1.PodStatus{
   173  					Phase: v1.PodRunning,
   174  				},
   175  				Spec: v1.PodSpec{
   176  					Containers: []v1.Container{
   177  						{
   178  							Name:  "nginx",
   179  							Image: "nginx:latest",
   180  						},
   181  					},
   182  				},
   183  			},
   184  			newpod: &v1.Pod{
   185  				ObjectMeta: metav1.ObjectMeta{
   186  					UID:       types.UID(fmt.Sprintf("%v-%v", namespace, name)),
   187  					Name:      name,
   188  					Namespace: namespace,
   189  					Labels:    nil,
   190  					Annotations: map[string]string{vcbatchv1.JobNameKey: "job1",
   191  						vcbatchv1.JobVersion:  "0",
   192  						vcbatchv1.TaskSpecKey: "task1"},
   193  				},
   194  				Status: v1.PodStatus{
   195  					Phase: v1.PodSucceeded,
   196  				},
   197  				Spec: v1.PodSpec{
   198  					Containers: []v1.Container{
   199  						{
   200  							Name:  "nginx",
   201  							Image: "nginx:latest",
   202  						},
   203  					},
   204  				},
   205  			},
   206  			jobinfo: JobInfo{
   207  				Pods: make(map[string]map[string]*v1.Pod),
   208  			},
   209  			ExpectValue: v1.PodSucceeded,
   210  		},
   211  	}
   212  
   213  	for i, testcase := range testCases {
   214  
   215  		testcase.jobinfo.Pods["task1"] = make(map[string]*v1.Pod)
   216  		testcase.jobinfo.Pods["task1"][testcase.oldpod.Name] = testcase.oldpod
   217  
   218  		err := testcase.jobinfo.UpdatePod(testcase.newpod)
   219  		if err != nil {
   220  			t.Fatalf("UpdatePod() error: %v", err)
   221  		}
   222  		if val, ok := testcase.jobinfo.Pods["task1"][testcase.newpod.Name]; ok != true {
   223  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, true, ok)
   224  		} else if val.Status.Phase != v1.PodSucceeded {
   225  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, val.Status.Phase)
   226  		}
   227  	}
   228  }
   229  
   230  func TestClone(t *testing.T) {
   231  
   232  	testCases := []struct {
   233  		Name    string
   234  		jobinfo JobInfo
   235  
   236  		ExpectValue v1.PodPhase
   237  	}{
   238  		{
   239  			Name: "Clone",
   240  			jobinfo: JobInfo{
   241  				Name: "testjobInfo",
   242  				Pods: make(map[string]map[string]*v1.Pod),
   243  			},
   244  		},
   245  	}
   246  
   247  	for i, testcase := range testCases {
   248  		newjobinfo := testcase.jobinfo.Clone()
   249  
   250  		if newjobinfo.Name != testcase.jobinfo.Name {
   251  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.jobinfo.Name, newjobinfo.Name)
   252  		}
   253  	}
   254  }
   255  
   256  func TestSetJob(t *testing.T) {
   257  
   258  	testCases := []struct {
   259  		Name    string
   260  		job     vcbatchv1.Job
   261  		jobinfo JobInfo
   262  
   263  		ExpectValue v1.PodPhase
   264  	}{
   265  		{
   266  			Name: "Clone",
   267  			jobinfo: JobInfo{
   268  				Name: "testjobInfo",
   269  				Pods: make(map[string]map[string]*v1.Pod),
   270  			},
   271  			job: vcbatchv1.Job{
   272  				ObjectMeta: metav1.ObjectMeta{
   273  					Name: "testjob",
   274  				},
   275  			},
   276  		},
   277  	}
   278  
   279  	for i, testcase := range testCases {
   280  		testcase.jobinfo.SetJob(&testcase.job)
   281  
   282  		if testcase.jobinfo.Job.Name != testcase.jobinfo.Name {
   283  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.job.Name, testcase.jobinfo.Job.Name)
   284  		}
   285  	}
   286  }
   287  
   288  func TestRequest_String(t *testing.T) {
   289  	testCases := []struct {
   290  		Name          string
   291  		req           Request
   292  		ExpectedValue string
   293  	}{
   294  		{
   295  			Name: "RequestToString",
   296  			req: Request{
   297  				Namespace:  "testnamespace",
   298  				JobName:    "testjobname",
   299  				QueueName:  "testqueuename",
   300  				TaskName:   "testtaskname",
   301  				Event:      vcbus.AnyEvent,
   302  				ExitCode:   0,
   303  				Action:     vcbus.SyncJobAction,
   304  				JobVersion: 0,
   305  			},
   306  			ExpectedValue: "Queue: testqueuename, Job: testnamespace/testjobname, Task:testtaskname, Event:*, ExitCode:0, Action:SyncJob, JobVersion: 0",
   307  		},
   308  	}
   309  
   310  	for i, testcase := range testCases {
   311  		reqString := testcase.req.String()
   312  
   313  		if reqString != testcase.ExpectedValue {
   314  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectedValue, reqString)
   315  		}
   316  	}
   317  
   318  }