volcano.sh/volcano@v1.9.0/test/e2e/vcctl/cli_util.go (about)

     1  /*
     2  Copyright 2021 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 vcctl
    18  
    19  import (
    20  	"fmt"
    21  	"os/exec"
    22  	"strings"
    23  
    24  	. "github.com/onsi/gomega"
    25  
    26  	e2eutil "volcano.sh/volcano/test/e2e/util"
    27  )
    28  
    29  // ResumeJob resumes the job in the given namespace.
    30  func ResumeJob(name string, namespace string) string {
    31  	command := []string{"job", "resume"}
    32  	Expect(name).NotTo(Equal(""), "Job name should not be empty in Resume job command")
    33  	command = append(command, "--name", name)
    34  	if namespace != "" {
    35  		command = append(command, "--namespace", namespace)
    36  	}
    37  	return RunCliCommand(command)
    38  }
    39  
    40  // SuspendJob suspends the job in the given namespace.
    41  func SuspendJob(name string, namespace string) string {
    42  	command := []string{"job", "suspend"}
    43  	Expect(name).NotTo(Equal(""), "Job name should not be empty in Suspend job command")
    44  	command = append(command, "--name", name)
    45  	if namespace != "" {
    46  		command = append(command, "--namespace", namespace)
    47  	}
    48  	return RunCliCommand(command)
    49  }
    50  
    51  // ListJobs list all the jobs in the given namespace.
    52  func ListJobs(namespace string) string {
    53  	command := []string{"job", "list"}
    54  	if namespace != "" {
    55  		command = append(command, "--namespace", namespace)
    56  	}
    57  	return RunCliCommand(command)
    58  }
    59  
    60  // DeleteJob delete the job in the given namespace.
    61  func DeleteJob(name string, namespace string) string {
    62  	command := []string{"job", "delete"}
    63  	Expect(name).NotTo(Equal(""), "Job name should not be empty in delete job command")
    64  	command = append(command, "--name", name)
    65  	if namespace != "" {
    66  		command = append(command, "--namespace", namespace)
    67  	}
    68  	return RunCliCommand(command)
    69  }
    70  
    71  // RunCliCommand runs the volcano command.
    72  func RunCliCommand(command []string) string {
    73  	if e2eutil.MasterURL() != "" {
    74  		command = append(command, "--master", e2eutil.MasterURL())
    75  	}
    76  	command = append(command, "--kubeconfig", e2eutil.KubeconfigPath(e2eutil.HomeDir()))
    77  	vcctl := e2eutil.VolcanoCliBinary()
    78  	Expect(e2eutil.FileExist(vcctl)).To(BeTrue(), fmt.Sprintf(
    79  		"vcctl binary: %s is required for E2E tests, please update VC_BIN environment", vcctl))
    80  	output, err := exec.Command(vcctl, command...).Output()
    81  	Expect(err).NotTo(HaveOccurred(),
    82  		fmt.Sprintf("Command %s failed to execute: %s", strings.Join(command, ""), err))
    83  	return string(output)
    84  }
    85  
    86  // RunCliCommandWithoutKubeConfig runs the volcano command.
    87  func RunCliCommandWithoutKubeConfig(command []string) string {
    88  	if e2eutil.MasterURL() != "" {
    89  		command = append(command, "--master", e2eutil.MasterURL())
    90  	}
    91  	vcctl := e2eutil.VolcanoCliBinary()
    92  	Expect(e2eutil.FileExist(vcctl)).To(BeTrue(), fmt.Sprintf(
    93  		"vcctl binary: %s is required for E2E tests, please update VC_BIN environment", vcctl))
    94  	output, err := exec.Command(vcctl, command...).Output()
    95  	Expect(err).NotTo(HaveOccurred(),
    96  		fmt.Sprintf("Command %s failed to execute: %s", strings.Join(command, ""), err))
    97  	return string(output)
    98  }