This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
helpers.go
146 lines (122 loc) · 4.43 KB
/
helpers.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package helpers
import (
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
"syscall"
"time"
"code.cloudfoundry.org/garden"
"github.com/mitchellh/go-ps"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/pivotal-golang/localip"
"github.com/tedsuo/ifrit"
"github.com/tedsuo/ifrit/ginkgomon"
"github.com/tedsuo/ifrit/grouper"
garden_runner "code.cloudfoundry.org/garden-windows/integration/runner"
"github.com/kardianos/osext"
)
func BuildContainerizer() string {
currentDir, err := osext.ExecutableFolder()
Expect(err).ShouldNot(HaveOccurred())
containerizerDir := path.Join(currentDir, "..", "..", "..", "Containerizer", "Containerizer")
windir := os.Getenv("WINDIR")
msbuild := path.Join(windir, "Microsoft.NET", "Framework64", "v4.0.30319", "MSBuild")
cmd := exec.Command(msbuild, `Containerizer.csproj`)
cmd.Dir = containerizerDir
cmd.Stdout = GinkgoWriter
cmd.Stderr = GinkgoWriter
Expect(cmd.Run()).To(Succeed(), "Cannot build containerizer. Make sure there are no compilation errors")
containerizerBin := path.Join(containerizerDir, "bin", "Containerizer.exe")
Expect(containerizerBin).To(BeAnExistingFile())
return containerizerBin
}
func BuildGarden() string {
gardenPath, err := gexec.Build("code.cloudfoundry.org/garden-windows", "-race")
Expect(err).ShouldNot(HaveOccurred())
return gardenPath
}
func StartGarden(gardenBin, containerizerBin string, maxContainerProcs int, argv ...string) (ifrit.Process, garden.Client) {
gardenPort, err := localip.LocalPort()
Expect(err).NotTo(HaveOccurred())
gardenAddr := fmt.Sprintf("127.0.0.1:%d", gardenPort)
tmpDir := os.TempDir()
// If below fails, try
// netsh advfirewall firewall add rule name="Open Port 48080" dir=in action=allow protocol=TCP localport=48080
containerizerPort, err := localip.LocalPort()
Expect(err).NotTo(HaveOccurred())
containerizerIp, err := localip.LocalIP()
Expect(err).NotTo(HaveOccurred())
containerizerArgs := []string{"--machineIp", containerizerIp, "--port", strconv.Itoa(int(containerizerPort))}
if maxContainerProcs != 0 {
containerizerArgs = append(containerizerArgs, "--activeProcessLimit", strconv.Itoa(maxContainerProcs))
}
gardenRunner := garden_runner.New("tcp4", gardenAddr, tmpDir, gardenBin, fmt.Sprintf("http://127.0.0.1:%d", containerizerPort))
cRunner := exec.Command(containerizerBin, containerizerArgs...)
cRunner.Stdout = GinkgoWriter
cRunner.Stderr = GinkgoWriter
containerizerRunner := ginkgomon.New(ginkgomon.Config{
Name: "containerizer",
Command: cRunner,
AnsiColorCode: "",
StartCheck: "containerizer.started",
StartCheckTimeout: 10 * time.Second,
Cleanup: func() {},
})
group := grouper.NewOrdered(syscall.SIGTERM, []grouper.Member{
{Name: "containerizer", Runner: containerizerRunner},
{Name: "garden", Runner: gardenRunner},
})
gardenProcess := ifrit.Invoke(group)
// wait for the processes to start before returning
<-gardenProcess.Ready()
return gardenProcess, gardenRunner.NewClient()
}
func StopGarden(process ifrit.Process, client garden.Client) {
process.Signal(syscall.SIGKILL)
Eventually(process.Wait(), 10).Should(Receive())
}
func KillAllGarden() {
killAllProcs("garden-windows")
}
func KillAllContainerizer() {
killAllProcs("containerizer")
}
func killAllProcs(matchingProcName string) {
matchingProcName = strings.ToUpper(matchingProcName)
processes, err := ps.Processes()
Expect(err).ShouldNot(HaveOccurred())
for _, proc := range processes {
procName := strings.ToUpper(proc.Executable())
if strings.Contains(procName, matchingProcName) {
parent, err := ps.FindProcess(proc.PPid())
Expect(err).ShouldNot(HaveOccurred())
if (parent == nil) || (strings.Contains(parent.Executable(), "test")) {
goProc, err := os.FindProcess(proc.Pid())
if err == nil {
goProc.Kill()
}
}
}
}
}
func AssertProcessExitsWith(expectedExitCode int, f func() (garden.Process, error)) {
process, err := f()
Expect(err).ShouldNot(HaveOccurred())
actualExitCode, err := process.Wait()
Expect(err).ShouldNot(HaveOccurred())
Expect(actualExitCode).To(Equal(expectedExitCode))
}
func AssertEventuallyProcessExitsWith(expectedExitCode int, f func() (garden.Process, error)) {
Eventually(func() int {
process, err := f()
Expect(err).ShouldNot(HaveOccurred())
actualExitCode, err := process.Wait()
Expect(err).ShouldNot(HaveOccurred())
return actualExitCode
}).Should(Equal(expectedExitCode))
}