Skip to content

Commit

Permalink
fixing the integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pacostas committed Oct 27, 2023
1 parent 47e558e commit 3e8bf3d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
50 changes: 33 additions & 17 deletions integration/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {

pack occam.Pack
docker occam.Docker

pullPolicy = "never"
)

it.Before(func() {
pack = occam.NewPack().WithVerbose()
docker = occam.NewDocker()

if settings.Extensions.UbiNodejsExtension.Online != "" {
pullPolicy = "always"
}
})

context("when the buildpack is run with pack build", func() {
Expand Down Expand Up @@ -68,16 +74,21 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
)

image, logs, err = pack.WithNoColor().Build.
WithPullPolicy("never").
WithBuildpacks(buildpack, buildPlanBuildpack).
WithExtensions(
settings.Extensions.UbiNodejsExtension.Online,
).
WithBuildpacks(
settings.Buildpacks.Yarn.Online,
settings.Buildpacks.BuildPlan.Online,
).
WithSBOMOutputDir(sbomDir).
WithEnv(map[string]string{"BP_LOG_LEVEL": "DEBUG"}).
WithPullPolicy(pullPolicy).
Execute(name, source)
Expect(err).ToNot(HaveOccurred(), logs.String)

// Ensure yarn is installed correctly

container, err = docker.Container.Run.WithCommand("which yarn").Execute(image.ID)
container, err = docker.Container.Run.WithCommand("command -v yarn").Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(func() string {
Expand All @@ -87,12 +98,12 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
}).Should(ContainSubstring("yarn"))

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, buildpackInfo.Buildpack.Name)),
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Executing build process",
MatchRegexp(` Installing Yarn`),
MatchRegexp(` Completed in ([0-9]*(\.[0-9]*)?[a-z]+)+`),
"",
fmt.Sprintf(" Generating SBOM for /layers/%s/yarn", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_")),
fmt.Sprintf(" Generating SBOM for /layers/%s/yarn", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")),
MatchRegexp(` Completed in \d+(\.?\d+)*`),
"",
" Writing SBOM in the following format(s):",
Expand All @@ -103,12 +114,12 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
))

// check that all required SBOM files are present
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"), "yarn", "sbom.cdx.json")).To(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"), "yarn", "sbom.spdx.json")).To(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"), "yarn", "sbom.syft.json")).To(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"), "yarn", "sbom.cdx.json")).To(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"), "yarn", "sbom.spdx.json")).To(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"), "yarn", "sbom.syft.json")).To(BeARegularFile())

// check an SBOM file to make sure it has an entry for yarn
contents, err := os.ReadFile(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"), "yarn", "sbom.cdx.json"))
contents, err := os.ReadFile(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"), "yarn", "sbom.cdx.json"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(ContainSubstring(`"name": "Yarn"`))
Expand Down Expand Up @@ -155,8 +166,13 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
)

image, logs, err = pack.WithNoColor().Build.
WithPullPolicy("never").
WithBuildpacks(buildpack, buildPlanBuildpack).
WithExtensions(
settings.Extensions.UbiNodejsExtension.Online,
).
WithPullPolicy(pullPolicy).
WithBuildpacks(
settings.Buildpacks.Yarn.Online,
settings.Buildpacks.BuildPlan.Online).
WithSBOMOutputDir(sbomDir).
WithEnv(map[string]string{
"BP_LOG_LEVEL": "DEBUG",
Expand All @@ -166,7 +182,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
Expect(err).ToNot(HaveOccurred(), logs.String)

// Ensure yarn is installed correctly
container, err = docker.Container.Run.WithCommand("which yarn").Execute(image.ID)
container, err = docker.Container.Run.WithCommand("command -v yarn").Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(func() string {
Expand All @@ -176,7 +192,7 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
}).Should(ContainSubstring("yarn"))

Expect(logs).To(ContainLines(
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, buildpackInfo.Buildpack.Name)),
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
" Executing build process",
MatchRegexp(` Installing Yarn`),
MatchRegexp(` Completed in ([0-9]*(\.[0-9]*)?[a-z]+)+`),
Expand All @@ -185,9 +201,9 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
))

// check that SBOM files were not generated
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"), "yarn", "sbom.cdx.json")).ToNot(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"), "yarn", "sbom.spdx.json")).ToNot(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"), "yarn", "sbom.syft.json")).ToNot(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"), "yarn", "sbom.cdx.json")).ToNot(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"), "yarn", "sbom.spdx.json")).ToNot(BeARegularFile())
Expect(filepath.Join(sbomDir, "sbom", "launch", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"), "yarn", "sbom.syft.json")).ToNot(BeARegularFile())
})
})
}
23 changes: 17 additions & 6 deletions integration/layer_reuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func testRebuildLayerReuse(t *testing.T, context spec.G, it spec.S) {

name string
source string

pullPolicy = "never"
// extenderBuildStr = ""
)

it.Before(func() {
Expand All @@ -36,6 +39,11 @@ func testRebuildLayerReuse(t *testing.T, context spec.G, it spec.S) {
pack = occam.NewPack()
imageIDs = map[string]struct{}{}
containerIDs = map[string]struct{}{}

if settings.Extensions.UbiNodejsExtension.Online != "" {
pullPolicy = "always"
// extenderBuildStr = "[extender (build)] "
}
})

it.After(func() {
Expand Down Expand Up @@ -64,10 +72,13 @@ func testRebuildLayerReuse(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())

build := pack.WithNoColor().Build.
WithPullPolicy("never").
WithExtensions(
settings.Extensions.UbiNodejsExtension.Online,
).
WithPullPolicy(pullPolicy).
WithBuildpacks(
buildpack,
buildPlanBuildpack,
settings.Buildpacks.Yarn.Online,
settings.Buildpacks.BuildPlan.Online,
)

firstImage, logs, err = build.Execute(name, source)
Expand All @@ -77,7 +88,7 @@ func testRebuildLayerReuse(t *testing.T, context spec.G, it spec.S) {

Expect(firstImage.Buildpacks).To(HaveLen(2))

Expect(firstImage.Buildpacks[0].Key).To(Equal(buildpackInfo.Buildpack.ID))
Expect(firstImage.Buildpacks[0].Key).To(Equal(settings.Buildpack.ID))
Expect(firstImage.Buildpacks[0].Layers).To(HaveKey("yarn"))

Expect(logs.String()).To(ContainSubstring(" Executing build process"))
Expand All @@ -90,11 +101,11 @@ func testRebuildLayerReuse(t *testing.T, context spec.G, it spec.S) {

Expect(secondImage.Buildpacks).To(HaveLen(2))

Expect(secondImage.Buildpacks[0].Key).To(Equal(buildpackInfo.Buildpack.ID))
Expect(secondImage.Buildpacks[0].Key).To(Equal(settings.Buildpack.ID))
Expect(secondImage.Buildpacks[0].Layers).To(HaveKey("yarn"))

Expect(logs.String()).NotTo(ContainSubstring(" Executing build process"))
Expect(logs.String()).To(ContainSubstring(fmt.Sprintf(" Reusing cached layer /layers/%s/yarn", strings.ReplaceAll(buildpackInfo.Buildpack.ID, "/", "_"))))
Expect(logs.String()).To(ContainSubstring(fmt.Sprintf(" Reusing cached layer /layers/%s/yarn", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))))

Expect(secondImage.Buildpacks[0].Layers["yarn"].SHA).To(Equal(firstImage.Buildpacks[0].Layers["yarn"].SHA))
})
Expand Down
18 changes: 13 additions & 5 deletions integration/offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ func testOffline(t *testing.T, context spec.G, it spec.S) {

name string
source string

pullPolicy = "never"
)

it.Before(func() {
var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())

if settings.Extensions.UbiNodejsExtension.Online != "" {
pullPolicy = "always"
}
})

it.After(func() {
Expand All @@ -54,16 +60,18 @@ func testOffline(t *testing.T, context spec.G, it spec.S) {

var logs fmt.Stringer
image, logs, err = pack.WithNoColor().Build.
WithPullPolicy("never").
WithExtensions(
settings.Extensions.UbiNodejsExtension.Online,
).
WithBuildpacks(
offlineBuildpack,
buildPlanBuildpack).
WithNetwork("none").
settings.Buildpacks.Yarn.Offline,
settings.Buildpacks.BuildPlan.Online).
WithPullPolicy(pullPolicy).
Execute(name, source)

Expect(err).NotTo(HaveOccurred(), logs.String())

container, err = docker.Container.Run.WithCommand("which yarn").Execute(image.ID)
container, err = docker.Container.Run.WithCommand("command -v yarn").Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(func() string {
Expand Down

0 comments on commit 3e8bf3d

Please sign in to comment.