forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildpack.go
128 lines (109 loc) · 3.75 KB
/
buildpack.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
package helpers
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)
// MakeBuildpackArchive makes a simple buildpack zip for a given stack.
func MakeBuildpackArchive(stackName string) string {
archiveFile, err := ioutil.TempFile("", "buildpack-archive-file-")
Expect(err).ToNot(HaveOccurred())
err = archiveFile.Close()
Expect(err).ToNot(HaveOccurred())
err = os.RemoveAll(archiveFile.Name())
Expect(err).ToNot(HaveOccurred())
archiveName := archiveFile.Name() + ".zip"
dir, err := ioutil.TempDir("", "buildpack-dir-")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
manifest := filepath.Join(dir, "manifest.yml")
err = ioutil.WriteFile(manifest, []byte(fmt.Sprintf("stack: %s", stackName)), 0666)
Expect(err).ToNot(HaveOccurred())
err = Zipit(dir, archiveName, "")
Expect(err).ToNot(HaveOccurred())
return archiveName
}
// BuildpackWithStack makes a simple buildpack for the given stack (using
// MakeBuildpackArchive) and yields it to the given function, removing the zip
// at the end.
func BuildpackWithStack(f func(buildpackArchive string), stackName string) {
buildpackZip := MakeBuildpackArchive(stackName)
defer os.Remove(buildpackZip)
f(buildpackZip)
}
// BuildpackWithoutStack makes a simple buildpack without a stack (using
// MakeBuildpackArchive) and yields it to the given function, removing the zip
// at the end.
func BuildpackWithoutStack(f func(buildpackArchive string)) {
BuildpackWithStack(f, "")
}
// SetupBuildpackWithStack makes and uploads a buildpack for the given stack.
func SetupBuildpackWithStack(buildpackName, stack string) {
BuildpackWithStack(func(buildpackPath string) {
session := CF("create-buildpack", buildpackName, buildpackPath, "99")
Eventually(session).Should(Say("OK"))
Eventually(session).Should(Say("OK"))
Eventually(session).Should(Exit(0))
}, stack)
}
// SetupBuildpackWithoutStack makes and uploads a buildpack without a stack.
func SetupBuildpackWithoutStack(buildpackName string) {
SetupBuildpackWithStack(buildpackName, "")
}
// BuildpackFields represents a buildpack, displayed in the 'cf buildpacks'
// command.
type BuildpackFields struct {
Position string
Name string
Enabled string
Locked string
Filename string
Stack string
}
// DeleteBuildpackIfOnOldCCAPI deletes the buildpack if the CC API targeted
// by the current test run is <= 2.80.0. Before this version, some entities
// would receive and invalid next_url in paginated requests. Since our test run
// now generally creates more than 50 buildpacks, we need to delete test buildpacks
// after use if we are targeting an older CC API.
// see https://github.com/cloudfoundry/capi-release/releases/tag/1.45.0
func DeleteBuildpackIfOnOldCCAPI(buildpackName string) {
minVersion := "2.99.0"
if !IsVersionMet(minVersion) {
deleteSessions := CF("delete-buildpack", buildpackName, "-f")
Eventually(deleteSessions).Should(Exit())
}
}
type Buildpack struct {
GUID string `json:"guid"`
Name string `json:"name"`
Stack string `json:"stack"`
}
type BuildpackList struct {
Buildpacks []Buildpack `json:"resources"`
}
func BuildpackGUIDByNameAndStack(buildpackName string, stackName string) string {
url := "/v3/buildpacks?names=" + buildpackName
if stackName != "" {
url += "&stacks=" + stackName
}
session := CF("curl", url)
bytes := session.Wait().Out.Contents()
buildpacks := BuildpackList{}
err := json.Unmarshal(bytes, &buildpacks)
Expect(err).ToNot(HaveOccurred())
Expect(len(buildpacks.Buildpacks)).To(BeNumerically(">", 0))
if stackName != "" {
return buildpacks.Buildpacks[0].GUID
}
for _, buildpack := range buildpacks.Buildpacks {
if buildpack.Stack == "" {
return buildpack.GUID
}
}
return ""
}