volcano.sh/volcano@v1.9.0/pkg/cli/queue/delete_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes 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 queue
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    28  
    29  	"github.com/spf13/cobra"
    30  
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  )
    33  
    34  func TestDeleteQueue(t *testing.T) {
    35  	response := v1beta1.Queue{
    36  		ObjectMeta: metav1.ObjectMeta{
    37  			Name: "test-queue",
    38  		},
    39  	}
    40  
    41  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    42  		w.Header().Set("Content-Type", "application/json")
    43  		val, err := json.Marshal(response)
    44  		if err == nil {
    45  			w.Write(val)
    46  		}
    47  	})
    48  
    49  	server := httptest.NewServer(handler)
    50  	defer server.Close()
    51  
    52  	deleteQueueFlags.Master = server.URL
    53  	testCases := []struct {
    54  		Name        string
    55  		QueueName   string
    56  		ExpectValue error
    57  	}{
    58  		{
    59  			Name:        "Normal Case Delete Queue Succeed",
    60  			QueueName:   "normal-case",
    61  			ExpectValue: nil,
    62  		},
    63  		{
    64  			Name:        "Abnormal Case Delete Queue Failed For Name Not Specified",
    65  			QueueName:   "",
    66  			ExpectValue: fmt.Errorf("queue name must be specified"),
    67  		},
    68  	}
    69  
    70  	for _, testCase := range testCases {
    71  		deleteQueueFlags.Name = testCase.QueueName
    72  
    73  		err := DeleteQueue()
    74  		if false == reflect.DeepEqual(err, testCase.ExpectValue) {
    75  			t.Errorf("Case '%s' failed, expected: '%v', got '%v'", testCase.Name, testCase.ExpectValue, err)
    76  		}
    77  	}
    78  }
    79  
    80  func TestInitDeleteFlags(t *testing.T) {
    81  	var cmd cobra.Command
    82  	InitDeleteFlags(&cmd)
    83  
    84  	if cmd.Flag("name") == nil {
    85  		t.Errorf("Could not find the flag name")
    86  	}
    87  }