Skip to content

Commit

Permalink
Merge pull request #280 from sap-contributions/274-debug-already-present
Browse files Browse the repository at this point in the history
Do not add debug options if already available in JAVA_TOOL_OPTIONS
  • Loading branch information
dmikusa authored Apr 21, 2023
2 parents 42d22a2 + 7188912 commit 1c0e73b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
11 changes: 10 additions & 1 deletion helper/debug_8.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package helper

import (
"fmt"
"strings"

"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/sherpa"
Expand All @@ -33,6 +34,14 @@ func (d Debug8) Execute() (map[string]string, error) {
return nil, nil
}

opts := sherpa.GetEnvWithDefault("JAVA_TOOL_OPTIONS", "")
debugAlreadyExists := strings.Contains(opts, "-agentlib:jdwp=")

if debugAlreadyExists {
d.Logger.Info("Java agent 'jdwp' already configured")
return nil, nil
}

port := sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000")

suspend := sherpa.ResolveBool("BPL_DEBUG_SUSPEND")
Expand All @@ -49,7 +58,7 @@ func (d Debug8) Execute() (map[string]string, error) {
s = "n"
}

opts := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))
opts = sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))

return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil
}
14 changes: 14 additions & 0 deletions helper/debug_8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ func testDebug8(t *testing.T, context spec.G, it spec.S) {
}))
})

context("jdwp agent already configured", func() {
it.Before(func() {
Expect(os.Setenv("JAVA_TOOL_OPTIONS", "-agentlib:jdwp=something")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("JAVA_TOOL_OPTIONS")).To(Succeed())
})

it("does not update JAVA_TOOL_OPTIONS", func() {
Expect(d.Execute()).To(BeEmpty())
})
})

context("$BPL_DEBUG_PORT", func() {
it.Before(func() {
Expect(os.Setenv("BPL_DEBUG_PORT", "8001")).To(Succeed())
Expand Down
11 changes: 10 additions & 1 deletion helper/debug_9.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package helper

import (
"fmt"
"strings"

"github.com/paketo-buildpacks/libpak/sherpa"

Expand All @@ -34,6 +35,14 @@ func (d Debug9) Execute() (map[string]string, error) {
return nil, nil
}

opts := sherpa.GetEnvWithDefault("JAVA_TOOL_OPTIONS", "")
debugAlreadyExists := strings.Contains(opts, "-agentlib:jdwp=")

if debugAlreadyExists {
d.Logger.Info("Java agent 'jdwp' already configured")
return nil, nil
}

port := "*:" + sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000")

suspend := sherpa.ResolveBool("BPL_DEBUG_SUSPEND")
Expand All @@ -50,7 +59,7 @@ func (d Debug9) Execute() (map[string]string, error) {
s = "n"
}

opts := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))
opts = sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))

return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil
}
14 changes: 14 additions & 0 deletions helper/debug_9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ func testDebug9(t *testing.T, context spec.G, it spec.S) {
}))
})

context("jdwp agent already configured", func() {
it.Before(func() {
Expect(os.Setenv("JAVA_TOOL_OPTIONS", "-agentlib:jdwp=something")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("JAVA_TOOL_OPTIONS")).To(Succeed())
})

it("does not update JAVA_TOOL_OPTIONS", func() {
Expect(d.Execute()).To(BeEmpty())
})
})

context("$BPL_DEBUG_PORT", func() {
it.Before(func() {
Expect(os.Setenv("BPL_DEBUG_PORT", "8001")).To(Succeed())
Expand Down

0 comments on commit 1c0e73b

Please sign in to comment.