forked from paketo-community/ubi-base-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_test.go
199 lines (157 loc) · 6.27 KB
/
metadata_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
package acceptance_test
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/uuid"
. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/packit/v2/vacation"
"github.com/sclevine/spec"
. "github.com/paketo-buildpacks/occam/matchers"
)
func testMetadata(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
tmpDir string
)
it.Before(func() {
var err error
tmpDir, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
})
it.After(func() {
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})
it("builds base stack", func() {
var (
buildReleaseDate time.Time
runReleaseDate time.Time
)
for _, imageInfo := range settings.ImagesJson.StackImages {
if !imageInfo.CreateBuildImage {
continue
}
by("confirming that the build image is correct", func() {
index, manifests, err := getImageIndexAndManifests(tmpDir, filepath.Join(root, imageInfo.OutputDir, "build.oci"))
Expect(err).NotTo(HaveOccurred())
Expect(manifests).To(HaveLen(1))
Expect(manifests[0].Platform).To(Equal(&v1.Platform{
OS: "linux",
Architecture: "amd64",
}))
image, err := index.Image(manifests[0].Digest)
Expect(err).NotTo(HaveOccurred())
file, err := image.ConfigFile()
Expect(err).NotTo(HaveOccurred())
Expect(file.Config.Labels).To(SatisfyAll(
HaveKeyWithValue("io.buildpacks.stack.id", "io.buildpacks.stacks.ubi8"),
HaveKeyWithValue("io.buildpacks.stack.description", "base build ubi8 image to support buildpacks"),
HaveKeyWithValue("io.buildpacks.stack.distro.name", "rhel"),
HaveKeyWithValue("io.buildpacks.stack.distro.version", MatchRegexp(`8\.\d+`)),
HaveKeyWithValue("io.buildpacks.stack.homepage", "https://github.com/paketo-community/ubi-base-stack"),
HaveKeyWithValue("io.buildpacks.stack.maintainer", "Paketo Community"),
HaveKeyWithValue("io.buildpacks.stack.metadata", MatchJSON("{}")),
))
buildReleaseDate, err = time.Parse(time.RFC3339, file.Config.Labels["io.buildpacks.stack.released"])
Expect(err).NotTo(HaveOccurred())
Expect(buildReleaseDate).NotTo(BeZero())
Expect(image).To(SatisfyAll(
HaveFileWithContent("/etc/group", ContainSubstring("cnb:x:1000:")),
HaveFileWithContent("/etc/passwd", ContainSubstring("cnb:x:1002:1000::/home/cnb:/bin/bash")),
HaveDirectory("/home/cnb"),
))
Expect(file.Config.User).To(Equal("1002:1000"))
Expect(file.Config.Env).To(ContainElements(
"CNB_USER_ID=1002",
"CNB_GROUP_ID=1000",
"CNB_STACK_ID=io.buildpacks.stacks.ubi8",
))
})
}
for _, imageInfo := range settings.ImagesJson.StackImages {
by(fmt.Sprintf("confirming that the run %s image is correct", imageInfo.Name), func() {
index, manifests, err := getImageIndexAndManifests(tmpDir, filepath.Join(root, imageInfo.OutputDir, "run.oci"))
Expect(err).NotTo(HaveOccurred())
Expect(manifests).To(HaveLen(1))
Expect(manifests[0].Platform).To(Equal(&v1.Platform{
OS: "linux",
Architecture: "amd64",
}))
image, err := index.Image(manifests[0].Digest)
Expect(err).NotTo(HaveOccurred())
file, err := image.ConfigFile()
Expect(err).NotTo(HaveOccurred())
Expect(file.Config.Labels).To(SatisfyAll(
HaveKeyWithValue("io.buildpacks.stack.id", "io.buildpacks.stacks.ubi8"),
HaveKeyWithValue("io.buildpacks.stack.distro.name", "rhel"),
HaveKeyWithValue("io.buildpacks.stack.distro.version", MatchRegexp(`8\.\d+`)),
HaveKeyWithValue("io.buildpacks.stack.homepage", "https://github.com/paketo-community/ubi-base-stack"),
HaveKeyWithValue("io.buildpacks.stack.maintainer", "Paketo Community"),
HaveKeyWithValue("io.buildpacks.stack.metadata", MatchJSON("{}")),
))
if imageInfo.Name == "default" {
Expect(file.Config.Labels).To(SatisfyAll(
HaveKeyWithValue("io.buildpacks.stack.description", fmt.Sprintf("base %s ubi8 image to support buildpacks", imageInfo.RunImage)),
))
} else {
Expect(file.Config.Labels).To(SatisfyAll(
HaveKeyWithValue("io.buildpacks.stack.description", fmt.Sprintf("ubi8 %s image to support buildpacks", imageInfo.Name)),
))
}
runImageReleaseDate, err := time.Parse(time.RFC3339, file.Config.Labels["io.buildpacks.stack.released"])
Expect(err).NotTo(HaveOccurred())
Expect(runImageReleaseDate).NotTo(BeZero())
// Store the release date to compare if the date is the same as the build image on later steps
if imageInfo.Name == "default" {
runReleaseDate = runImageReleaseDate
}
Expect(file.Config.User).To(Equal("1001:1000"))
Expect(image).To(SatisfyAll(
HaveFileWithContent("/etc/group", ContainSubstring("cnb:x:1000:")),
HaveFileWithContent("/etc/passwd", ContainSubstring("cnb:x:1001:1000::/home/cnb:/bin/bash")),
HaveDirectory("/home/cnb"),
))
Expect(image).To(HaveFileWithContent("/etc/os-release", SatisfyAll(
ContainLines(MatchRegexp(`PRETTY_NAME=\"Red Hat Enterprise Linux 8\.\d+ \(Ootpa\)\"`)),
ContainSubstring(`HOME_URL="https://github.com/paketo-community/ubi-base-stack"`),
ContainSubstring(`SUPPORT_URL="https://github.com/paketo-community/ubi-base-stack/blob/main/README.md"`),
ContainSubstring(`BUG_REPORT_URL="https://github.com/paketo-community/ubi-base-stack/issues/new"`),
)))
})
}
Expect(runReleaseDate).To(Equal(buildReleaseDate))
})
}
func getImageIndexAndManifests(tmpDir string, ociImageFilePath string) (index v1.ImageIndex, manifests []v1.Descriptor, err error) {
dir := filepath.Join(tmpDir, uuid.New().String())
err = os.Mkdir(dir, os.ModePerm)
if err != nil {
return nil, []v1.Descriptor{}, err
}
archive, err := os.Open(ociImageFilePath)
if err != nil {
return nil, []v1.Descriptor{}, err
}
defer archive.Close()
err = vacation.NewArchive(archive).Decompress(dir)
if err != nil {
return nil, []v1.Descriptor{}, err
}
path, err := layout.FromPath(dir)
if err != nil {
return nil, []v1.Descriptor{}, err
}
index, err = path.ImageIndex()
if err != nil {
return nil, []v1.Descriptor{}, err
}
indexManifest, err := index.IndexManifest()
if err != nil {
return nil, []v1.Descriptor{}, err
}
return index, indexManifest.Manifests, nil
}