-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BPL_DEBUG_ENABLED to set --inspect
Signed-off-by: Ralf Pannemans <[email protected]> Signed-off-by: Pavel Busko <[email protected]> Co-authored-by: Ralf Pannemans <[email protected]>
- Loading branch information
1 parent
be44298
commit a311946
Showing
21 changed files
with
292 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package internal_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
) | ||
|
||
func TestUnitInspector(t *testing.T) { | ||
suite := spec.New("cmd/inspector/internal", spec.Report(report.Terminal{})) | ||
suite("Run", testRun) | ||
suite.Run(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
func Run(environment map[string]string, output io.Writer, root string) error { | ||
variables := map[string]string{} | ||
if debug, ok := environment["BPL_DEBUG_ENABLED"]; ok && debug == "true" { | ||
option := "--inspect" | ||
if debugPort, ok := environment["BPL_DEBUG_PORT"]; ok { | ||
option = fmt.Sprintf("%s=127.0.0.1:%s", option, debugPort) | ||
} | ||
|
||
if nodeOpts, ok := environment["NODE_OPTIONS"]; ok { | ||
if strings.Contains(nodeOpts, "--inspect") { | ||
return nil | ||
} | ||
|
||
option = strings.Join([]string{nodeOpts, option}, " ") | ||
} | ||
|
||
variables["NODE_OPTIONS"] = option | ||
} | ||
|
||
return toml.NewEncoder(output).Encode(variables) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package internal_test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/paketo-buildpacks/node-engine/cmd/inspector/internal" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
. "github.com/paketo-buildpacks/packit/v2/matchers" | ||
) | ||
|
||
func testRun(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
|
||
environment map[string]string | ||
root string | ||
) | ||
|
||
it.Before(func() { | ||
environment = map[string]string{} | ||
}) | ||
|
||
it.After(func() { | ||
Expect(os.RemoveAll(root)).To(Succeed()) | ||
}) | ||
|
||
context("when $BPL_DEBUG_ENABLE is set", func() { | ||
it.Before(func() { | ||
environment["BPL_DEBUG_ENABLED"] = "true" | ||
}) | ||
|
||
it("--inspect is added to NODE_OPTIONS", func() { | ||
environment["NODE_OPTIONS"] = "--existing" | ||
buffer := bytes.NewBuffer(nil) | ||
|
||
err := internal.Run(environment, buffer, root) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(buffer.String()).To(MatchTOML(` | ||
NODE_OPTIONS = "--existing --inspect" | ||
`)) | ||
}) | ||
|
||
context("when $BPL_DEBUG_PORT is set", func() { | ||
it.Before(func() { | ||
environment["BPL_DEBUG_PORT"] = "1111" | ||
}) | ||
|
||
it("sets the inspector port", func() { | ||
buffer := bytes.NewBuffer(nil) | ||
|
||
err := internal.Run(environment, buffer, root) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(buffer.String()).To(MatchTOML(` | ||
NODE_OPTIONS = "--inspect=127.0.0.1:1111" | ||
`)) | ||
}) | ||
}) | ||
|
||
context("when $NODE_OPTIONS contains --inspect flag", func() { | ||
it.Before(func() { | ||
environment["NODE_OPTIONS"] = "--inspect=0.0.0.0:8888" | ||
}) | ||
|
||
it("does not change it", func() { | ||
buffer := bytes.NewBuffer(nil) | ||
|
||
err := internal.Run(environment, buffer, root) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(buffer.String()).To(BeEmpty()) | ||
}) | ||
}) | ||
}) | ||
|
||
context("when $BPL_DEBUG_ENABLED is not set", func() { | ||
it("NODE_OPTIONS are not changed", func() { | ||
environment["NODE_OPTIONS"] = "--existing" | ||
|
||
buffer := bytes.NewBuffer(nil) | ||
err := internal.Run(environment, buffer, root) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buffer.String()).To(BeEmpty()) | ||
}) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/paketo-buildpacks/node-engine/cmd/inspector/internal" | ||
"github.com/paketo-buildpacks/node-engine/cmd/util" | ||
) | ||
|
||
func main() { | ||
err := internal.Run(util.LoadEnvironmentMap(os.Environ()), os.NewFile(3, "/dev/fd/3"), "/") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...timize-memory/internal/environment_map.go → cmd/util/environment_map.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package internal | ||
package util | ||
|
||
import "strings" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
) | ||
|
||
func TestUnitUtils(t *testing.T) { | ||
suite := spec.New("cmd/util", spec.Report(report.Terminal{})) | ||
suite("EnvironmentMap", testEnvironmentMap) | ||
suite.Run(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/paketo-buildpacks/occam" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
. "github.com/paketo-buildpacks/occam/matchers" | ||
) | ||
|
||
func testInspector(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
Eventually = NewWithT(t).Eventually | ||
|
||
docker occam.Docker | ||
pack occam.Pack | ||
|
||
image occam.Image | ||
container occam.Container | ||
name string | ||
source string | ||
) | ||
|
||
it.Before(func() { | ||
docker = occam.NewDocker() | ||
pack = occam.NewPack() | ||
|
||
var err error | ||
name, err = occam.RandomName() | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
it.After(func() { | ||
Expect(docker.Container.Remove.Execute(container.ID)).To(Succeed()) | ||
Expect(docker.Image.Remove.Execute(image.ID)).To(Succeed()) | ||
Expect(docker.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed()) | ||
Expect(os.RemoveAll(source)).To(Succeed()) | ||
}) | ||
|
||
it("sets --inspect if set with env variable BPL_DEBUG_ENABLED", func() { | ||
var err error | ||
source, err = occam.Source(filepath.Join("testdata", "simple_app")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
var logs fmt.Stringer | ||
image, logs, err = pack.WithNoColor().Build. | ||
WithPullPolicy("never"). | ||
WithBuildpacks( | ||
settings.Buildpacks.NodeEngine.Online, | ||
settings.Buildpacks.Processes.Online, | ||
). | ||
Execute(name, source) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(logs).To(ContainLines( | ||
" Writing exec.d/1-inspector", | ||
)) | ||
|
||
container, err = docker.Container.Run. | ||
WithMemory("128m"). | ||
WithPublish("8080"). | ||
WithEnv(map[string]string{"BPL_DEBUG_ENABLED": "true", "BPL_DEBUG_PORT": "9000", "NODE_OPTIONS": "--no-warnings"}). | ||
Execute(image.ID) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Eventually(container).Should(BeAvailable()) | ||
Eventually(container).Should(Serve(ContainSubstring("NodeOptions: --no-warnings --inspect=127.0.0.1:9000")).OnPort(8080).WithEndpoint("/node-options")) | ||
|
||
}) | ||
} |
Oops, something went wrong.