volcano.sh/volcano@v1.9.0/pkg/cli/job/run_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  	"os"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/spf13/cobra"
    28  
    29  	"volcano.sh/apis/pkg/apis/batch/v1alpha1"
    30  )
    31  
    32  func TestCreateJob(t *testing.T) {
    33  	response := v1alpha1.Job{}
    34  
    35  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    36  		w.Header().Set("Content-Type", "application/json")
    37  		val, err := json.Marshal(response)
    38  		if err == nil {
    39  			w.Write(val)
    40  		}
    41  
    42  	})
    43  
    44  	server := httptest.NewServer(handler)
    45  	defer server.Close()
    46  
    47  	fileName := time.Now().Format("20060102150405999") + "testCreateJob.yaml"
    48  	val, err := json.Marshal(response)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	err = os.WriteFile(fileName, val, os.ModePerm)
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  	defer os.Remove(fileName)
    57  
    58  	testCases := []struct {
    59  		Name        string
    60  		ExpectValue error
    61  		FileName    string
    62  	}{
    63  		{
    64  			Name:        "CreateJob",
    65  			ExpectValue: nil,
    66  		},
    67  		{
    68  			Name:        "CreateJobWithFile",
    69  			FileName:    fileName,
    70  			ExpectValue: nil,
    71  		},
    72  	}
    73  
    74  	for i, testcase := range testCases {
    75  		launchJobFlags = &runFlags{
    76  			commonFlags: commonFlags{
    77  				Master: server.URL,
    78  			},
    79  			Name:      "test",
    80  			Namespace: "test",
    81  			Requests:  "cpu=1000m,memory=100Mi",
    82  		}
    83  
    84  		err := RunJob()
    85  		if err != nil {
    86  			t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
    87  		}
    88  	}
    89  
    90  }
    91  
    92  func TestInitRunFlags(t *testing.T) {
    93  	var cmd cobra.Command
    94  	InitRunFlags(&cmd)
    95  
    96  	if cmd.Flag("namespace") == nil {
    97  		t.Errorf("Could not find the flag namespace")
    98  	}
    99  	if cmd.Flag("scheduler") == nil {
   100  		t.Errorf("Could not find the flag scheduler")
   101  	}
   102  	if cmd.Flag("image") == nil {
   103  		t.Errorf("Could not find the flag image")
   104  	}
   105  	if cmd.Flag("replicas") == nil {
   106  		t.Errorf("Could not find the flag replicas")
   107  	}
   108  	if cmd.Flag("min") == nil {
   109  		t.Errorf("Could not find the flag min")
   110  	}
   111  	if cmd.Flag("requests") == nil {
   112  		t.Errorf("Could not find the flag requests")
   113  	}
   114  	if cmd.Flag("limits") == nil {
   115  		t.Errorf("Could not find the flag limits")
   116  	}
   117  
   118  }