/
runner.go
81 lines (63 loc) · 1.55 KB
/
runner.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package testrunner
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"time"
"code.cloudfoundry.org/rep/cmd/rep/config"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
type Runner struct {
binPath string
Session *gexec.Session
repConfig config.RepConfig
repConfigFilePath string
}
func New(binPath string, repConfig config.RepConfig) *Runner {
return &Runner{
binPath: binPath,
repConfig: repConfig,
}
}
func (r *Runner) Start() {
if r.Session != nil && r.Session.ExitCode() == -1 {
panic("starting more than one rep!!!")
}
f, err := ioutil.TempFile("", "rep")
Expect(err).NotTo(HaveOccurred())
defer f.Close()
encoder := json.NewEncoder(f)
err = encoder.Encode(r.repConfig)
Expect(err).NotTo(HaveOccurred())
r.repConfigFilePath = f.Name()
args := []string{
"--config", r.repConfigFilePath,
}
repSession, err := gexec.Start(
exec.Command(
r.binPath,
args...,
),
gexec.NewPrefixedWriter("\x1b[32m[o]\x1b[32m[rep]\x1b[0m ", ginkgo.GinkgoWriter),
gexec.NewPrefixedWriter("\x1b[91m[e]\x1b[32m[rep]\x1b[0m ", ginkgo.GinkgoWriter),
)
Expect(err).NotTo(HaveOccurred())
r.Session = repSession
}
func (r *Runner) Stop() {
err := os.RemoveAll(r.repConfigFilePath)
Expect(err).NotTo(HaveOccurred())
if r.Session != nil {
r.Session.Interrupt().Wait(5 * time.Second)
}
}
func (r *Runner) KillWithFire() {
err := os.RemoveAll(r.repConfigFilePath)
Expect(err).NotTo(HaveOccurred())
if r.Session != nil {
r.Session.Kill().Wait(5 * time.Second)
}
}