volcano.sh/volcano@v1.9.0/pkg/cli/job/list_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  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	"volcano.sh/apis/pkg/apis/batch/v1alpha1"
    28  )
    29  
    30  func TestListJob(t *testing.T) {
    31  	response := v1alpha1.JobList{}
    32  	response.Items = append(response.Items, v1alpha1.Job{})
    33  
    34  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    35  		w.Header().Set("Content-Type", "application/json")
    36  		val, err := json.Marshal(response)
    37  		if err == nil {
    38  			w.Write(val)
    39  		}
    40  
    41  	})
    42  
    43  	server := httptest.NewServer(handler)
    44  	defer server.Close()
    45  
    46  	testCases := []struct {
    47  		Name         string
    48  		ExpectValue  error
    49  		AllNamespace bool
    50  		Selector     string
    51  	}{
    52  		{
    53  			Name:        "ListJob",
    54  			ExpectValue: nil,
    55  		},
    56  		{
    57  			Name:         "ListAllNamespaceJob",
    58  			ExpectValue:  nil,
    59  			AllNamespace: true,
    60  		},
    61  	}
    62  
    63  	for i, testcase := range testCases {
    64  		listJobFlags = &listFlags{
    65  			commonFlags: commonFlags{
    66  				Master: server.URL,
    67  			},
    68  			Namespace:    "test",
    69  			allNamespace: testcase.AllNamespace,
    70  			selector:     testcase.Selector,
    71  		}
    72  
    73  		err := ListJobs()
    74  		if err != nil {
    75  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
    76  		}
    77  	}
    78  
    79  }
    80  
    81  func TestInitListFlags(t *testing.T) {
    82  	var cmd cobra.Command
    83  	InitListFlags(&cmd)
    84  
    85  	if cmd.Flag("namespace") == nil {
    86  		t.Errorf("Could not find the flag namespace")
    87  	}
    88  	if cmd.Flag("scheduler") == nil {
    89  		t.Errorf("Could not find the flag scheduler")
    90  	}
    91  	if cmd.Flag("all-namespaces") == nil {
    92  		t.Errorf("Could not find the flag all-namespaces")
    93  	}
    94  	if cmd.Flag("selector") == nil {
    95  		t.Errorf("Could not find the flag selector")
    96  	}
    97  
    98  }