-
Notifications
You must be signed in to change notification settings - Fork 53
/
testing.go
62 lines (55 loc) · 1.06 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package testutil
import (
"errors"
"os/exec"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega/gexec"
"github.com/rancher/opni/pkg/test/testruntime"
)
type Session interface {
G() (*gexec.Session, bool)
Wait() error
}
type sessionWrapper struct {
g *gexec.Session
cmd *exec.Cmd
}
func (s *sessionWrapper) G() (*gexec.Session, bool) {
if s.g != nil {
return s.g, true
}
return nil, false
}
func (s *sessionWrapper) Wait() error {
if s == nil {
return nil
}
if s.g != nil {
ws := s.g.Wait()
if ws.ExitCode() != 0 {
return errors.New(string(ws.Err.Contents()))
}
return nil
}
return s.cmd.Wait()
}
func StartCmd(cmd *exec.Cmd) (Session, error) {
if testruntime.IsTesting {
session, err := gexec.Start(cmd, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
if err != nil {
return nil, err
}
return &sessionWrapper{
g: session,
cmd: cmd,
}, nil
}
cmd.Stdout = testruntime.StdoutWriter
cmd.Stderr = testruntime.StderrWriter
if err := cmd.Start(); err != nil {
return nil, err
}
return &sessionWrapper{
cmd: cmd,
}, nil
}