volcano.sh/volcano@v1.9.0/pkg/cli/vsuspend/suspend_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 vsuspend 18 19 import ( 20 "encoding/json" 21 "net/http" 22 "net/http/httptest" 23 "strings" 24 "testing" 25 26 "github.com/spf13/cobra" 27 28 v1alpha1batch "volcano.sh/apis/pkg/apis/batch/v1alpha1" 29 "volcano.sh/apis/pkg/apis/bus/v1alpha1" 30 ) 31 32 func TestSuspendJobJob(t *testing.T) { 33 responsecommand := v1alpha1.Command{} 34 responsejob := v1alpha1batch.Job{} 35 36 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 37 if strings.HasSuffix(r.URL.Path, "command") { 38 w.Header().Set("Content-Type", "application/json") 39 val, err := json.Marshal(responsecommand) 40 if err == nil { 41 w.Write(val) 42 } 43 44 } else { 45 w.Header().Set("Content-Type", "application/json") 46 val, err := json.Marshal(responsejob) 47 if err == nil { 48 w.Write(val) 49 } 50 51 } 52 }) 53 54 server := httptest.NewServer(handler) 55 defer server.Close() 56 57 suspendJobFlags.Master = server.URL 58 suspendJobFlags.Namespace = "test" 59 suspendJobFlags.JobName = "testjob" 60 61 testCases := []struct { 62 Name string 63 ExpectValue error 64 }{ 65 { 66 Name: "SuspendJob", 67 ExpectValue: nil, 68 }, 69 } 70 71 for i, testcase := range testCases { 72 err := SuspendJob() 73 if err != nil { 74 t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err) 75 } 76 } 77 78 } 79 80 func TestInitSuspendFlags(t *testing.T) { 81 var cmd cobra.Command 82 InitSuspendFlags(&cmd) 83 84 if cmd.Flag("namespace") == nil { 85 t.Errorf("Could not find the flag namespace") 86 } 87 if cmd.Flag("name") == nil { 88 t.Errorf("Could not find the flag name") 89 } 90 91 }