This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
common.go
212 lines (175 loc) · 4.22 KB
/
common.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package acceptance
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
const (
BoshDirectorIP = "10.144.0.2"
)
func SetupDependencies(cacheDir string) {
gopaths := strings.Split(os.Getenv("GOPATH"), ":")
assets := []string{
"cfdev-efi.iso",
"cf-deps.iso",
"vpnkit",
"hyperkit",
"linuxkit",
"UEFI.fd",
"qcow-tool",
}
if runtime.GOOS == "windows" {
assets = []string{
"cfdev-efi.iso",
"cf-deps.iso",
"vpnkit.exe",
"winsw.exe",
}
}
err := os.MkdirAll(cacheDir, 0777)
Expect(err).ToNot(HaveOccurred())
for _, asset := range assets {
target := filepath.Join(cacheDir, asset)
goPath := gopaths[0]
if runtime.GOOS == "windows" {
goPath = os.Getenv("GOPATH")
}
for _, origin := range []string{filepath.Join(goPath, "output", asset), filepath.Join(goPath, "linuxkit", asset), filepath.Join(GetCfdevHome(), "cache", asset)} {
if exists, _ := FileExists(origin); exists {
Expect(os.Symlink(origin, target)).To(Succeed())
break
}
}
Expect(target).To(BeAnExistingFile())
}
}
func HttpServerIsListeningAt(url string) error {
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
resp, err := client.Get(url)
if resp != nil {
resp.Body.Close()
}
return err
}
func EventuallyProcessStops(pid int, timeoutSec int) {
EventuallyWithOffset(1, func() (bool, error) {
return processIsRunning(pid)
}, timeoutSec).Should(BeFalse())
}
func processIsRunning(pid int) (bool, error) {
proc, err := os.FindProcess(pid)
if err != nil {
return false, err
}
if err := proc.Signal(syscall.Signal(0)); err != nil {
return false, nil
}
return true, nil
}
func IsServiceRunning(label string) func() (bool, error) {
return func() (bool, error) {
switch runtime.GOOS {
case "darwin":
txt, err := exec.Command("launchctl", "list", label).CombinedOutput()
if err != nil {
if strings.Contains(string(txt), "Could not find service") {
return false, nil
}
return false, err
}
re := regexp.MustCompile(`^\s*"PID"\s*=`)
for _, line := range strings.Split(string(txt), "\n") {
if re.MatchString(line) {
return true, nil
}
}
return false, nil
case "windows":
cmd := exec.Command("powershell.exe", "-Command", fmt.Sprintf("Get-Service | Where-Object {$_.Name -eq \"%s\"}", label))
output, err := cmd.Output()
if err != nil {
return false, err
}
if strings.Contains(string(output), label) {
return true, nil
}
return false, nil
default:
cmd := exec.Command("systemctl", "is-active", label)
output, _ := cmd.Output()
return strings.TrimSpace(string(output)) == "active", nil
}
}
}
func PidFromFile(pidFile string) int {
pidBytes, _ := ioutil.ReadFile(pidFile)
pid, _ := strconv.ParseInt(string(pidBytes), 10, 64)
return int(pid)
}
func FileExists(file string) (bool, error) {
_, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func GetCfdevHome() string {
cfdevHome := os.Getenv("CFDEV_HOME")
if cfdevHome != "" {
return cfdevHome
}
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH"), ".cfdev")
} else {
return filepath.Join(os.Getenv("HOME"), ".cfdev")
}
}
func GetCfPluginPath() string {
return "cf"
}
func RemoveIPAliases(aliases ...string) {
if runtime.GOOS == "windows" {
return
}
for _, alias := range aliases {
cmd := exec.Command("sudo", "-n", "ifconfig", "lo0", "inet", alias+"/32", "remove")
writer := gexec.NewPrefixedWriter("[ifconfig] ", GinkgoWriter)
session, err := gexec.Start(cmd, writer, writer)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit())
}
}
func HasSudoPrivilege() bool {
if runtime.GOOS == "windows" {
return true
}
cmd := exec.Command("sh", "-c", "sudo -n true")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit())
if session.ExitCode() == 0 {
return true
}
return false
}