From 456cd8fab87f1f8a62f27d6972b58f3c1a60fd13 Mon Sep 17 00:00:00 2001 From: Laurent Chiarello Date: Tue, 16 Jul 2024 15:40:21 +0200 Subject: [PATCH] Fix spring-cloud-bindings overriding JAVA_TOOL_OPTIONS when disabled --- helper/spring_cloud_bindings.go | 17 +++++++---------- helper/spring_cloud_bindings_test.go | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/helper/spring_cloud_bindings.go b/helper/spring_cloud_bindings.go index 8068d9c..54768e1 100644 --- a/helper/spring_cloud_bindings.go +++ b/helper/spring_cloud_bindings.go @@ -20,7 +20,6 @@ import ( "fmt" "os" "strconv" - "strings" "github.com/paketo-buildpacks/libpak/sherpa" @@ -37,7 +36,7 @@ func (s SpringCloudBindings) Execute() (map[string]string, error) { var err error enabled := true - opts := []string{} + var opt string if val, ok := os.LookupEnv("BPL_SPRING_CLOUD_BINDINGS_ENABLED"); ok { s.Logger.Infof(color.YellowString("WARNING: BPL_SPRING_CLOUD_BINDINGS_ENABLED is deprecated, support will be removed in a coming release. Use BPL_SPRING_CLOUD_BINDINGS_DISABLED instead")) @@ -48,15 +47,13 @@ func (s SpringCloudBindings) Execute() (map[string]string, error) { } // Switching from "BPL_SPRING_CLOUD_BINDINGS_ENABLED" to "BPL_SPRING_CLOUD_BINDINGS_DISABLED" which defaults to 'false' to follow convention if sherpa.ResolveBool("BPL_SPRING_CLOUD_BINDINGS_DISABLED") || !enabled { - opts = append(opts, "-Dorg.springframework.cloud.bindings.boot.enable=false") - return map[string]string{"JAVA_TOOL_OPTIONS": strings.Join(opts, " ")}, nil + opt = "-Dorg.springframework.cloud.bindings.boot.enable=false" + } else { + opt = "-Dorg.springframework.cloud.bindings.boot.enable=true" + s.Logger.Info("Spring Cloud Bindings Enabled") } - s.Logger.Info("Spring Cloud Bindings Enabled") - - opts = append(opts, "-Dorg.springframework.cloud.bindings.boot.enable=true") - - values := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", opts...) + values := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", opt) return map[string]string{"JAVA_TOOL_OPTIONS": values}, nil -} \ No newline at end of file +} diff --git a/helper/spring_cloud_bindings_test.go b/helper/spring_cloud_bindings_test.go index bf63c77..801c2d8 100644 --- a/helper/spring_cloud_bindings_test.go +++ b/helper/spring_cloud_bindings_test.go @@ -87,4 +87,21 @@ func testSpringCloudBindings(t *testing.T, context spec.G, it spec.S) { }) }) + context("$JAVA_TOOL_OPTIONS", func() { + it.Before(func() { + Expect(os.Setenv("JAVA_TOOL_OPTIONS", "test-java-tool-options"), + os.Setenv("BPL_SPRING_CLOUD_BINDINGS_DISABLED", "true")).To(Succeed()) + }) + + it.After(func() { + Expect(os.Unsetenv("JAVA_TOOL_OPTIONS"), os.Unsetenv("BPL_SPRING_CLOUD_BINDINGS_DISABLED")).To(Succeed()) + }) + + it("contributes configuration appended to existing $JAVA_TOOL_OPTIONS", func() { + Expect(s.Execute()).To(Equal(map[string]string{ + "JAVA_TOOL_OPTIONS": "test-java-tool-options -Dorg.springframework.cloud.bindings.boot.enable=false", + })) + }) + }) + }