2021-03-26 15:24:19 +08:00
|
|
|
/*
|
|
|
|
Copyright 2021 The KubeVela Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-08-03 15:51:43 +08:00
|
|
|
package e2e
|
2020-07-31 18:26:36 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-09-29 19:11:18 +08:00
|
|
|
"github.com/Netflix/go-expect"
|
|
|
|
"github.com/hinshun/vt10x"
|
2023-05-15 16:07:51 +08:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-09-26 12:58:30 +08:00
|
|
|
"github.com/onsi/gomega"
|
2020-07-31 18:26:36 +08:00
|
|
|
"github.com/onsi/gomega/gexec"
|
|
|
|
)
|
|
|
|
|
2020-08-25 16:08:16 +08:00
|
|
|
var rudrPath = GetCliBinary()
|
2020-07-31 18:26:36 +08:00
|
|
|
|
2022-09-28 10:19:28 +08:00
|
|
|
// GetCliBinary is to build kubevela binary.
|
2020-08-25 16:08:16 +08:00
|
|
|
func GetCliBinary() string {
|
2020-07-31 18:26:36 +08:00
|
|
|
cwd, _ := os.Getwd()
|
2020-08-25 16:08:16 +08:00
|
|
|
return path.Join(cwd, "../..", "./bin")
|
2020-07-31 18:26:36 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 11:10:42 +08:00
|
|
|
// Exec executes a command
|
2020-07-31 18:26:36 +08:00
|
|
|
func Exec(cli string) (string, error) {
|
|
|
|
var output []byte
|
2020-12-10 11:10:42 +08:00
|
|
|
session, err := asyncExec(cli)
|
2020-07-31 18:26:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
2021-05-15 11:34:33 +08:00
|
|
|
s := session.Wait(60 * time.Second)
|
2020-07-31 18:26:36 +08:00
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
2020-08-14 09:50:16 +08:00
|
|
|
}
|
2020-12-10 11:10:42 +08:00
|
|
|
|
|
|
|
// ExecAndTerminate executes a long-running command and terminate it after 3s
|
2020-10-29 14:02:59 +08:00
|
|
|
func ExecAndTerminate(cli string) (string, error) {
|
|
|
|
var output []byte
|
2020-12-10 11:10:42 +08:00
|
|
|
session, err := asyncExec(cli)
|
2020-10-29 14:02:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
2025-02-03 11:09:28 +08:00
|
|
|
time.Sleep(10 * time.Second)
|
2020-10-29 14:02:59 +08:00
|
|
|
s := session.Terminate()
|
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
|
|
|
}
|
2020-07-31 18:26:36 +08:00
|
|
|
|
2020-12-10 11:10:42 +08:00
|
|
|
// LongTimeExec executes a long-running command and wait it exits by itself
|
2020-09-26 16:35:33 +08:00
|
|
|
func LongTimeExec(cli string, timeout time.Duration) (string, error) {
|
|
|
|
var output []byte
|
2020-12-10 11:10:42 +08:00
|
|
|
session, err := asyncExec(cli)
|
2020-09-26 16:35:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
s := session.Wait(timeout)
|
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:10:42 +08:00
|
|
|
func asyncExec(cli string) (*gexec.Session, error) {
|
2020-08-18 18:15:28 +08:00
|
|
|
c := strings.Fields(cli)
|
|
|
|
commandName := path.Join(rudrPath, c[0])
|
|
|
|
command := exec.Command(commandName, c[1:]...)
|
|
|
|
session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
|
2022-02-18 17:23:50 +08:00
|
|
|
func LongTimeExecWithEnv(cli string, timeout time.Duration, env []string) (string, error) {
|
|
|
|
var output []byte
|
|
|
|
c := strings.Fields(cli)
|
|
|
|
commandName := path.Join(rudrPath, c[0])
|
|
|
|
command := exec.Command(commandName, c[1:]...)
|
|
|
|
command.Env = os.Environ()
|
|
|
|
command.Env = append(command.Env, env...)
|
|
|
|
|
|
|
|
session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
|
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
s := session.Wait(timeout)
|
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:10:42 +08:00
|
|
|
// InteractiveExec executes a command with interactive input
|
2020-09-29 19:11:18 +08:00
|
|
|
func InteractiveExec(cli string, consoleFn func(*expect.Console)) (string, error) {
|
|
|
|
var output []byte
|
|
|
|
console, _, err := vt10x.NewVT10XConsole(expect.WithStdout(ginkgo.GinkgoWriter))
|
|
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
|
|
defer console.Close()
|
|
|
|
doneC := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer ginkgo.GinkgoRecover()
|
|
|
|
defer close(doneC)
|
|
|
|
consoleFn(console)
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := strings.Fields(cli)
|
|
|
|
commandName := path.Join(rudrPath, c[0])
|
|
|
|
command := exec.Command(commandName, c[1:]...)
|
|
|
|
command.Stdin = console.Tty()
|
|
|
|
|
|
|
|
session, err := gexec.Start(command, console.Tty(), console.Tty())
|
2021-10-12 14:36:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
2021-01-15 19:04:12 +08:00
|
|
|
s := session.Wait(300 * time.Second)
|
2021-10-12 14:36:09 +08:00
|
|
|
err = console.Tty().Close()
|
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
2020-09-29 19:11:18 +08:00
|
|
|
<-doneC
|
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
|
|
|
}
|
|
|
|
|
2021-01-15 19:04:12 +08:00
|
|
|
func BeforeSuit() {
|
|
|
|
// sync capabilities from cluster into local
|
|
|
|
_, _ = Exec("vela workloads")
|
|
|
|
}
|