-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildpack_integration_test.go
226 lines (184 loc) · 5.74 KB
/
buildpack_integration_test.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package acceptance_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/uuid"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/occam"
. "github.com/paketo-buildpacks/occam/matchers"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/vacation"
)
const RegistryName = "registry:2"
func testBuildpackIntegration(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually
buildPlanBuildpack string
goDistBuildpack string
builderConfigFilepath string
pack occam.Pack
docker occam.Docker
source string
name string
builder string
image occam.Image
registry occam.Container
container occam.Container
)
it.Before(func() {
var err error
pack = occam.NewPack().WithVerbose()
docker = occam.NewDocker()
// A registry is needed in order to build and push the multi-arch stack images
registry, err = docker.Container.Run.
WithPublish(fmt.Sprintf("%d:5000", localRegistryPort)).
Execute(RegistryName)
Expect(err).NotTo(HaveOccurred())
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())
buildPlanBuildpack = "index.docker.io/paketocommunity/build-plan"
goDistBuildpack = "gcr.io/paketo-buildpacks/go-dist"
source, err = occam.Source(filepath.Join("integration", "testdata", "simple_app"))
Expect(err).NotTo(HaveOccurred())
builderConfigFile, err := os.CreateTemp("", "builder.toml")
Expect(err).NotTo(HaveOccurred())
builderConfigFilepath = builderConfigFile.Name()
_, err = fmt.Fprintf(builderConfigFile, `
[stack]
build-image = "%s:latest"
id = "io.buildpacks.stacks.noble.tiny"
run-image = "%s:latest"
`,
stack.BuildImageID,
stack.RunImageID,
)
Expect(err).NotTo(HaveOccurred())
Expect(archiveToDaemon(stack.BuildArchive, stack.BuildImageID)).To(Succeed())
Expect(archiveToDaemon(stack.RunArchive, stack.RunImageID)).To(Succeed())
builder = fmt.Sprintf("builder-%s", uuid.NewString())
logs, err := createBuilder(builderConfigFilepath, builder)
Expect(err).NotTo(HaveOccurred(), logs)
})
it.After(func() {
Expect(docker.Container.Remove.Execute(registry.ID)).To(Succeed())
Expect(docker.Container.Remove.Execute(container.ID)).To(Succeed())
Expect(docker.Image.Remove.Execute(image.ID)).To(Succeed())
Expect(docker.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
lifecycleVersion, err := getLifecycleVersion(builder)
Expect(err).NotTo(HaveOccurred())
Expect(docker.Image.Remove.Execute(builder)).To(Succeed())
Expect(os.RemoveAll(builderConfigFilepath)).To(Succeed())
Expect(docker.Image.Remove.Execute(stack.BuildImageID)).To(Succeed())
Expect(docker.Image.Remove.Execute(stack.RunImageID)).To(Succeed())
Expect(docker.Image.Remove.Execute(RegistryName)).To(Succeed())
Expect(docker.Image.Remove.Execute(fmt.Sprintf("buildpacksio/lifecycle:%s", lifecycleVersion))).To(Succeed())
Expect(os.RemoveAll(source)).To(Succeed())
})
it("builds an app with a buildpack", func() {
var err error
var logs fmt.Stringer
image, logs, err = pack.WithNoColor().Build.
WithBuildpacks(
goDistBuildpack,
buildPlanBuildpack,
).
WithEnv(map[string]string{
"BP_LOG_LEVEL": "DEBUG",
}).
WithPullPolicy("if-not-present").
WithBuilder(builder).
Execute(name, source)
Expect(err).ToNot(HaveOccurred(), logs.String)
container, err = docker.Container.Run.
WithDirect().
WithCommand("go").
WithCommandArgs([]string{"run", "main.go"}).
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
WithPublishAll().
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())
Eventually(container).Should(BeAvailable())
Eventually(container).Should(Serve(MatchRegexp(`go1.*`)).OnPort(8080))
})
}
func archiveToDaemon(path, id string) error {
tmpDir := os.TempDir()
tarReader, err := os.Open(path)
if err != nil {
return fmt.Errorf("unable to open tar: %w", err)
}
err = vacation.NewTarArchive(tarReader).Decompress(tmpDir)
if err != nil {
return fmt.Errorf("unable to extract files: %w", err)
}
pathLayout, err := layout.FromPath(tmpDir)
if err != nil {
return fmt.Errorf("unable to load image from path %s: %w", tmpDir, err)
}
imageIndex, err := pathLayout.ImageIndex()
if err != nil {
return fmt.Errorf("unable to read image index: %w", err)
}
ref, err := name.ParseReference(id)
if err != nil {
return fmt.Errorf("unable to parse reference from %s: %w", id, err)
}
return remote.WriteIndex(ref, imageIndex, remote.WithAuthFromKeychain(authn.DefaultKeychain))
}
func createBuilder(config string, name string) (string, error) {
buf := bytes.NewBuffer(nil)
pack := pexec.NewExecutable("pack")
err := pack.Execute(pexec.Execution{
Stdout: buf,
Stderr: buf,
Args: []string{
"builder",
"create",
name,
fmt.Sprintf("--config=%s", config),
},
})
return buf.String(), err
}
type Builder struct {
LocalInfo struct {
Lifecycle struct {
Version string `json:"version"`
} `json:"lifecycle"`
} `json:"local_info"`
}
func getLifecycleVersion(builderID string) (string, error) {
buf := bytes.NewBuffer(nil)
pack := pexec.NewExecutable("pack")
err := pack.Execute(pexec.Execution{
Stdout: buf,
Stderr: buf,
Args: []string{
"builder",
"inspect",
builderID,
"-o",
"json",
},
})
if err != nil {
return "", err
}
var builder Builder
err = json.Unmarshal((buf.Bytes()), &builder)
if err != nil {
return "", err
}
return builder.LocalInfo.Lifecycle.Version, nil
}