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"
|
2020-07-31 18:26:36 +08:00
|
|
|
"github.com/onsi/ginkgo"
|
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-09-29 19:11:18 +08:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client/config"
|
|
|
|
|
|
|
|
oamcore "github.com/crossplane/oam-kubernetes-runtime/apis/core"
|
|
|
|
k8sruntime "k8s.io/apimachinery/pkg/runtime"
|
|
|
|
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
2020-07-31 18:26:36 +08:00
|
|
|
)
|
|
|
|
|
2020-08-25 16:08:16 +08:00
|
|
|
var rudrPath = GetCliBinary()
|
2020-07-31 18:26:36 +08:00
|
|
|
|
2020-09-11 17:52:14 +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
|
|
|
}
|
|
|
|
|
|
|
|
func Exec(cli string) (string, error) {
|
|
|
|
var output []byte
|
2020-08-18 18:15:28 +08:00
|
|
|
session, err := AsyncExec(cli)
|
2020-07-31 18:26:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
2020-10-13 17:20:59 +08:00
|
|
|
s := session.Wait(30 * 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-10-29 14:02:59 +08:00
|
|
|
func ExecAndTerminate(cli string) (string, error) {
|
|
|
|
var output []byte
|
|
|
|
session, err := AsyncExec(cli)
|
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
s := session.Terminate()
|
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
|
|
|
}
|
2020-07-31 18:26:36 +08:00
|
|
|
|
2020-09-26 16:35:33 +08:00
|
|
|
func LongTimeExec(cli string, timeout time.Duration) (string, error) {
|
|
|
|
var output []byte
|
|
|
|
session, err := AsyncExec(cli)
|
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
s := session.Wait(timeout)
|
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 18:15:28 +08:00
|
|
|
func AsyncExec(cli string) (*gexec.Session, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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())
|
2020-10-13 20:02:37 +08:00
|
|
|
s := session.Wait(90 * time.Second)
|
2020-09-29 19:11:18 +08:00
|
|
|
console.Tty().Close()
|
|
|
|
<-doneC
|
|
|
|
if err != nil {
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
return string(s.Out.Contents()) + string(s.Err.Contents()), nil
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:50:16 +08:00
|
|
|
func BeforeSuit() {
|
2020-08-17 11:01:31 +08:00
|
|
|
//Without this line, will hit issue like `<string>: Error: unknown command "scale" for "vela"`
|
2020-09-10 14:57:36 +08:00
|
|
|
_, _ = Exec("vela system update")
|
2020-07-31 18:26:36 +08:00
|
|
|
}
|
2020-09-29 19:11:18 +08:00
|
|
|
|
|
|
|
func newK8sClient() (client.Client, error) {
|
|
|
|
conf, err := config.GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
scheme := k8sruntime.NewScheme()
|
|
|
|
if err := clientgoscheme.AddToScheme(scheme); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := oamcore.AddToScheme(scheme); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k8sclient, err := client.New(conf, client.Options{Scheme: scheme})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return k8sclient, nil
|
|
|
|
}
|