volcano.sh/volcano@v1.9.0/pkg/cli/queue/operate_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 TestOperateQueue(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  	operateQueueFlags.Master = server.URL
    53  	testCases := []struct {
    54  		Name        string
    55  		QueueName   string
    56  		Weight      int32
    57  		Action      string
    58  		ExpectValue error
    59  	}{
    60  		{
    61  			Name:        "Normal Case Operate Queue Succeed, Action close",
    62  			QueueName:   "normal-case-action-close",
    63  			Action:      ActionClose,
    64  			ExpectValue: nil,
    65  		},
    66  		{
    67  			Name:        "Normal Case Operate Queue Succeed, Action open",
    68  			QueueName:   "normal-case-action-open",
    69  			Action:      ActionOpen,
    70  			ExpectValue: nil,
    71  		},
    72  		{
    73  			Name:        "Normal Case Operate Queue Succeed, Update Weight",
    74  			QueueName:   "normal-case-update-weight",
    75  			Action:      ActionUpdate,
    76  			Weight:      3,
    77  			ExpectValue: nil,
    78  		},
    79  		{
    80  			Name:      "Abnormal Case Update Queue Failed For Invalid Weight",
    81  			QueueName: "abnormal-case-invalid-weight",
    82  			Action:    ActionUpdate,
    83  			ExpectValue: fmt.Errorf("when %s queue %s, weight must be specified, "+
    84  				"the value must be greater than 0", ActionUpdate, "abnormal-case-invalid-weight"),
    85  		},
    86  		{
    87  			Name:        "Abnormal Case Operate Queue Failed For Name Not Specified",
    88  			QueueName:   "",
    89  			ExpectValue: fmt.Errorf("queue name must be specified"),
    90  		},
    91  		{
    92  			Name:        "Abnormal Case Operate Queue Failed For Action null",
    93  			QueueName:   "abnormal-case-null-action",
    94  			Action:      "",
    95  			ExpectValue: fmt.Errorf("action can not be null"),
    96  		},
    97  		{
    98  			Name:      "Abnormal Case Operate Queue Failed For Action Invalid",
    99  			QueueName: "abnormal-case-invalid-action",
   100  			Action:    "invalid",
   101  			ExpectValue: fmt.Errorf("action %s invalid, valid actions are %s, %s and %s",
   102  				"invalid", ActionOpen, ActionClose, ActionUpdate),
   103  		},
   104  	}
   105  
   106  	for _, testCase := range testCases {
   107  		operateQueueFlags.Name = testCase.QueueName
   108  		operateQueueFlags.Action = testCase.Action
   109  		operateQueueFlags.Weight = testCase.Weight
   110  
   111  		err := OperateQueue()
   112  		if false == reflect.DeepEqual(err, testCase.ExpectValue) {
   113  			t.Errorf("Case '%s' failed, expected: '%v', got '%v'", testCase.Name, testCase.ExpectValue, err)
   114  		}
   115  	}
   116  }
   117  
   118  func TestInitOperateFlags(t *testing.T) {
   119  	var cmd cobra.Command
   120  	InitOperateFlags(&cmd)
   121  
   122  	if cmd.Flag("name") == nil {
   123  		t.Errorf("Could not find the flag name")
   124  	}
   125  	if cmd.Flag("weight") == nil {
   126  		t.Errorf("Could not find the flag weight")
   127  	}
   128  	if cmd.Flag("action") == nil {
   129  		t.Errorf("Could not find the flag action")
   130  	}
   131  }