volcano.sh/volcano@v1.9.0/pkg/cli/job/view_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 job 18 19 import ( 20 "encoding/json" 21 "math" 22 "net/http" 23 "net/http/httptest" 24 "strings" 25 "testing" 26 27 "github.com/spf13/cobra" 28 29 v1 "k8s.io/api/core/v1" 30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 31 32 "volcano.sh/apis/pkg/apis/batch/v1alpha1" 33 ) 34 35 func TestViewJob(t *testing.T) { 36 response := v1alpha1.Job{ 37 TypeMeta: metav1.TypeMeta{}, 38 ObjectMeta: metav1.ObjectMeta{ 39 Name: "testJobWithLongLongLongName", 40 Labels: map[string]string{ 41 "LabelWithLongLongLongLongName": "LongLongLongLongLabelValue", 42 }, 43 Annotations: map[string]string{ 44 "AnnotationWithLongLongLongLongName": "LongLongLongLongAnnotationValue", 45 }, 46 }, 47 Spec: v1alpha1.JobSpec{ 48 Tasks: []v1alpha1.TaskSpec{ 49 { 50 Name: "taskWithLongLongLongLongName", 51 Replicas: math.MaxInt32, 52 Template: v1.PodTemplateSpec{ 53 ObjectMeta: metav1.ObjectMeta{}, 54 Spec: v1.PodSpec{ 55 Containers: []v1.Container{ 56 { 57 Command: []string{"echo", "123"}, 58 Ports: []v1.ContainerPort{ 59 { 60 Name: "placeholder", 61 }, 62 }, 63 }, 64 }, 65 ImagePullSecrets: []v1.LocalObjectReference{ 66 { 67 Name: "imagepull-secret", 68 }, 69 }, 70 }, 71 }, 72 }, 73 }, 74 }, 75 Status: v1alpha1.JobStatus{ 76 Succeeded: 1, 77 Pending: 3, 78 Running: 1, 79 Failed: 2, 80 Unknown: 10, 81 Terminating: 4, 82 RetryCount: 5, 83 MinAvailable: 6, 84 Version: 7, 85 ControlledResources: map[string]string{ 86 "svc": "", 87 }, 88 }, 89 } 90 91 eventList := v1.EventList{ 92 Items: []v1.Event{ 93 { 94 ObjectMeta: metav1.ObjectMeta{ 95 Name: response.Name + ".123", 96 }, 97 Count: 1, 98 }, 99 { 100 ObjectMeta: metav1.ObjectMeta{ 101 Name: response.Name + ".456", 102 }, 103 Count: 2, 104 FirstTimestamp: metav1.Now(), 105 }, 106 }, 107 } 108 109 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 110 w.Header().Set("Content-Type", "application/json") 111 112 if strings.Contains(r.URL.String(), "job") { 113 val, err := json.Marshal(response) 114 if err == nil { 115 w.Write(val) 116 } 117 return 118 } 119 120 val, err := json.Marshal(eventList) 121 if err == nil { 122 w.Write(val) 123 } 124 }) 125 126 server := httptest.NewServer(handler) 127 defer server.Close() 128 129 viewJobFlags.Master = server.URL 130 viewJobFlags.Namespace = "test" 131 viewJobFlags.JobName = "testJob" 132 133 testCases := []struct { 134 Name string 135 ExpectValue error 136 }{ 137 { 138 Name: "viewJob", 139 ExpectValue: nil, 140 }, 141 } 142 143 for i, testcase := range testCases { 144 err := ViewJob() 145 if err != nil { 146 t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err) 147 } 148 } 149 150 } 151 152 func TestInitViewFlags(t *testing.T) { 153 var cmd cobra.Command 154 InitViewFlags(&cmd) 155 156 if cmd.Flag("namespace") == nil { 157 t.Errorf("Could not find the flag namespace") 158 } 159 if cmd.Flag("name") == nil { 160 t.Errorf("Could not find the flag name") 161 } 162 163 }