Skip to content

Commit

Permalink
Merge pull request #388 from paketo-buildpacks/gh-136
Browse files Browse the repository at this point in the history
Only contribute active-processor-count helper for Java versions < 17
  • Loading branch information
dmikusa authored Apr 29, 2024
2 parents 06b1aae + 6f44f29 commit 8b62860
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
7 changes: 6 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (b *Build) contributeNIK(jdkDep libpak.BuildpackDependency, nativeDep libpa
}

func (b *Build) contributeHelpers(context libcnb.BuildContext, depJRE libpak.BuildpackDependency) {
helpers := []string{"active-processor-count", "java-opts", "jvm-heap", "link-local-dns", "memory-calculator",
helpers := []string{"java-opts", "jvm-heap", "link-local-dns", "memory-calculator",
"security-providers-configurer", "jmx", "jfr", "openssl-certificate-loader"}

if IsBeforeJava9(depJRE.Version) {
Expand All @@ -283,6 +283,11 @@ func (b *Build) contributeHelpers(context libcnb.BuildContext, depJRE libpak.Bui
helpers = append(helpers, "debug-9")
helpers = append(helpers, "nmt")
}

if IsBeforeJava17(depJRE.Version) {
helpers = append(helpers, "active-processor-count")
}

found := false
for _, custom := range b.CustomHelpers {
if found {
Expand Down
69 changes: 68 additions & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
)

it.Before(func() {
t.Setenv("BP_ARCH", "amd64")
})

it("contributes JDK", func() {
ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "jdk"})
ctx.Buildpack.Metadata = map[string]interface{}{
Expand Down Expand Up @@ -123,7 +127,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers[1].(libpak.HelperLayerContributor).Names).To(Equal([]string{
"active-processor-count",
"java-opts",
"jvm-heap",
"link-local-dns",
Expand All @@ -134,6 +137,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
"openssl-certificate-loader",
"security-providers-classpath-8",
"debug-8",
"active-processor-count",
}))
})

Expand All @@ -154,7 +158,70 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers[1].(libpak.HelperLayerContributor).Names).To(Equal([]string{
"java-opts",
"jvm-heap",
"link-local-dns",
"memory-calculator",
"security-providers-configurer",
"jmx",
"jfr",
"openssl-certificate-loader",
"security-providers-classpath-9",
"debug-9",
"nmt",
"active-processor-count",
}))
})

it("contributes active-processor-count before Java 17", func() {
ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "jre", Metadata: LaunchContribution})
ctx.Buildpack.Metadata = map[string]interface{}{
"dependencies": []map[string]interface{}{
{
"id": "jre",
"version": "11.0.0",
"stacks": []interface{}{"test-stack-id"},
},
},
}
ctx.StackID = "test-stack-id"

result, err := libjvm.NewBuild(bard.NewLogger(io.Discard)).Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers[1].(libpak.HelperLayerContributor).Names).To(Equal([]string{
"java-opts",
"jvm-heap",
"link-local-dns",
"memory-calculator",
"security-providers-configurer",
"jmx",
"jfr",
"openssl-certificate-loader",
"security-providers-classpath-9",
"debug-9",
"nmt",
"active-processor-count",
}))
})

it("does not contribute active-processor-count with Java 17 and later", func() {
ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "jre", Metadata: LaunchContribution})
ctx.Buildpack.Metadata = map[string]interface{}{
"dependencies": []map[string]interface{}{
{
"id": "jre",
"version": "17.0.0",
"stacks": []interface{}{"test-stack-id"},
},
},
}
ctx.StackID = "test-stack-id"

result, err := libjvm.NewBuild(bard.NewLogger(io.Discard)).Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers[1].(libpak.HelperLayerContributor).Names).To(Equal([]string{
"java-opts",
"jvm-heap",
"link-local-dns",
Expand Down
10 changes: 10 additions & 0 deletions versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

var Java9, _ = semver.NewVersion("9")
var Java17, _ = semver.NewVersion("17")
var Java18, _ = semver.NewVersion("18")

func IsBeforeJava9(candidate string) bool {
Expand All @@ -32,6 +33,15 @@ func IsBeforeJava9(candidate string) bool {
return v.LessThan(Java9)
}

func IsBeforeJava17(candidate string) bool {
v, err := semver.NewVersion(candidate)
if err != nil {
return false
}

return v.LessThan(Java17)
}

func IsBeforeJava18(candidate string) bool {
v, err := semver.NewVersion(candidate)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,17 @@ func testVersions(t *testing.T, context spec.G, it spec.S) {
Expect(libjvm.IsBeforeJava9("")).To(BeFalse())
})

it("determins whether a version is before Java 18", func() {
Expect(libjvm.IsBeforeJava18("17.0.0")).To(BeTrue())
Expect(libjvm.IsBeforeJava18("18.0.0")).To(BeFalse())
Expect(libjvm.IsBeforeJava18("19.0.0")).To(BeFalse())
Expect(libjvm.IsBeforeJava18("")).To(BeFalse())
})

it("determines whether a version is before Java 17", func() {
Expect(libjvm.IsBeforeJava17("16.0.0")).To(BeTrue())
Expect(libjvm.IsBeforeJava17("17.0.0")).To(BeFalse())
Expect(libjvm.IsBeforeJava17("18.0.0")).To(BeFalse())
Expect(libjvm.IsBeforeJava17("")).To(BeFalse())
})
}

0 comments on commit 8b62860

Please sign in to comment.