-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.go
82 lines (65 loc) · 2.05 KB
/
utils.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
package integration
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"code.cloudfoundry.org/grootfs/groot"
"code.cloudfoundry.org/grootfs/integration/runner"
"code.cloudfoundry.org/lager"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
func CreateBundle(grootFSBin, storePath, draxBin, imagePath, id string, diskLimit int64) groot.Bundle {
spec := groot.CreateSpec{
ID: id,
Image: imagePath,
DiskLimit: diskLimit,
}
bundle, err := CreateBundleWSpec(grootFSBin, storePath, draxBin, spec)
Expect(err).NotTo(HaveOccurred())
return bundle
}
func CreateBundleWSpec(grootFSBin, storePath, draxBin string, spec groot.CreateSpec) (groot.Bundle, error) {
runner := &runner.Runner{
GrootFSBin: grootFSBin,
StorePath: storePath,
DraxBin: draxBin,
LogLevel: lager.DEBUG,
Stderr: GinkgoWriter,
}
return runner.Create(spec)
}
func FindUID(user string) uint32 {
sess, err := gexec.Start(exec.Command("id", "-u", user), nil, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(sess).Should(gexec.Exit(0))
i, err := strconv.ParseInt(strings.TrimSpace(string(sess.Out.Contents())), 10, 32)
Expect(err).NotTo(HaveOccurred())
return uint32(i)
}
func FindGID(group string) uint32 {
sess, err := gexec.Start(exec.Command("id", "-g", group), nil, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(sess).Should(gexec.Exit(0))
i, err := strconv.ParseInt(strings.TrimSpace(string(sess.Out.Contents())), 10, 32)
Expect(err).NotTo(HaveOccurred())
return uint32(i)
}
func ImagePathToVolumeID(imagePath string) string {
stat, err := os.Stat(imagePath)
Expect(err).ToNot(HaveOccurred())
imagePathSha := sha256.Sum256([]byte(imagePath))
return fmt.Sprintf("%s-%d", hex.EncodeToString(imagePathSha[:32]), stat.ModTime().UnixNano())
}
type CustomRoundTripper struct {
RoundTripFn func(req *http.Request) (*http.Response, error)
}
func (r *CustomRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return r.RoundTripFn(req)
}