Skip to content

Commit

Permalink
Merge pull request #397 from buildpack/feature/370-inspect-image-proc…
Browse files Browse the repository at this point in the history
…esses

Include processes in inspect-image output when available
  • Loading branch information
simonjjones authored Dec 4, 2019
2 parents ce90e90 + 75645ab commit f8b3674
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 6 deletions.
8 changes: 8 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S, packFixturesDir, packP
if _, err := os.Stat(outputTemplate); err != nil {
t.Fatal(err.Error())
}
type process struct {
shell string
processType string
command string
args []string
}
expectedOutput := fillTemplate(t, outputTemplate,
map[string]interface{}{
"image_name": repoName,
Expand All @@ -382,6 +388,7 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S, packFixturesDir, packP
"run_image_local_mirror": localRunImageMirror,
"run_image_mirror": runImageMirror,
"show_reference": !lifecycleDescriptor.Info.Version.LessThan(semver.MustParse("0.5.0")),
"show_processes": !lifecycleDescriptor.Info.Version.LessThan(semver.MustParse("0.6.0")),
},
)
h.AssertEq(t, output, expectedOutput)
Expand Down Expand Up @@ -733,6 +740,7 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S, packFixturesDir, packP
"base_image_top_layer": h.TopLayerDiffID(t, runImageMirror),
"run_image_mirror": runImageMirror,
"show_reference": !lifecycleDescriptor.Info.Version.LessThan(semver.MustParse("0.5.0")),
"show_processes": !lifecycleDescriptor.Info.Version.LessThan(semver.MustParse("0.6.0")),
},
)
h.AssertEq(t, output, expectedOutput)
Expand Down
6 changes: 4 additions & 2 deletions acceptance/testdata/mock_app/run
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

set -x

port="${1-8080}"

echo "listening on port 8080"

resp=$(echo "HTTP/1.1 200 OK\n" && cat "$PWD"/*-dep /contents*.txt)
while true; do
nc -l -p 8080 -c "echo \"$resp\""
done
nc -l -p "$port" -c "echo \"$resp\""
done
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ else
fi

## adds a process
echo 'processes = [{ type = "web", command = "./run"}]' > "$launch_dir/launch.toml"
cat <<EOF > "$launch_dir/launch.toml"
[[processes]]
type = "web"
command = "./run"
args = ["8080"]
[[processes]]
type = "hello"
command = "echo"
args = ["hello", "world"]
direct = true
EOF

echo "---> Done"
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ Run Images:
Buildpacks:
ID VERSION
simple/layers simple-layers-version
{{- if .show_processes}}

Processes:
TYPE SHELL COMMAND ARGS
web (default) bash ./run 8080
hello echo hello world
{{- end }}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Run Images:
Buildpacks:
ID VERSION
simple/layers simple-layers-version
{{- if .show_processes}}

Processes:
TYPE SHELL COMMAND ARGS
web (default) bash ./run 8080
hello echo hello world
{{- end }}


LOCAL:
Expand All @@ -36,4 +43,11 @@ Run Images:
Buildpacks:
ID VERSION
simple/layers simple-layers-version
{{- if .show_processes}}

Processes:
TYPE SHELL COMMAND ARGS
web (default) bash ./run 8080
hello echo hello world
{{- end }}

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e
github.com/onsi/gomega v1.7.0
github.com/pkg/errors v0.8.1
github.com/sclevine/spec v1.2.0
github.com/sclevine/spec v1.3.0
github.com/spf13/cobra v0.0.5
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/sclevine/spec v1.0.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=
github.com/sclevine/spec v1.2.0 h1:1Jwdf9jSfDl9NVmt8ndHqbTZ7XCCPbh1jI3hkDBHVYA=
github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=
github.com/sclevine/spec v1.3.0 h1:iTB51CYlnju5oRh0/l67fg1+RlQ2nqmFecwdvN+5TrI=
github.com/sclevine/spec v1.3.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
Expand Down
22 changes: 22 additions & 0 deletions inspect_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type ImageInfo struct {
Base lifecycle.RunImageMetadata
BOM []lifecycle.BOMEntry
Stack lifecycle.StackMetadata
Processes ProcessDetails
}

type ProcessDetails struct {
DefaultProcess *lifecycle.Process
OtherProcesses []lifecycle.Process
}

func (c *Client) InspectImage(name string, daemon bool) (*ImageInfo, error) {
Expand Down Expand Up @@ -50,11 +56,27 @@ func (c *Client) InspectImage(name string, daemon bool) (*ImageInfo, error) {
return nil, err
}

defaultProcessType, err := img.Env("CNB_PROCESS_TYPE")
if err != nil || defaultProcessType == "" {
defaultProcessType = "web"
}

var processDetails ProcessDetails
for _, proc := range buildMD.Processes {
proc := proc
if proc.Type == defaultProcessType {
processDetails.DefaultProcess = &proc
continue
}
processDetails.OtherProcesses = append(processDetails.OtherProcesses, proc)
}

return &ImageInfo{
StackID: stackID,
Stack: layersMd.Stack,
Base: layersMd.RunImage,
BOM: buildMD.BOM,
Buildpacks: buildMD.Buildpacks,
Processes: processDetails,
}, nil
}
132 changes: 132 additions & 0 deletions inspect_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ func testInspectImage(t *testing.T, when spec.G, it spec.S) {
"version": "other-version"
}
],
"processes": [
{
"type": "other-process",
"command": "/other/process",
"args": ["opt", "1"],
"direct": true
},
{
"type": "web",
"command": "/start/web-process",
"args": ["-p", "1234"],
"direct": false
}
],
"launcher": {
"version": "0.5.0"
}
Expand Down Expand Up @@ -157,6 +171,124 @@ func testInspectImage(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, info.Buildpacks[1].ID, "other-buildpack")
h.AssertEq(t, info.Buildpacks[1].Version, "other-version")
})

it("returns the processes setting the web process as default", func() {
info, err := subject.InspectImage("some/image", useDaemon)
h.AssertNil(t, err)

h.AssertEq(t, info.Processes,
ProcessDetails{
DefaultProcess: &lifecycle.Process{
Type: "web",
Command: "/start/web-process",
Args: []string{"-p", "1234"},
Direct: false,
},
OtherProcesses: []lifecycle.Process{
{
Type: "other-process",
Command: "/other/process",
Args: []string{"opt", "1"},
Direct: true,
},
},
},
)
})

when("CNB_PROCESS_TYPE is set", func() {
it.Before(func() {
h.AssertNil(t, fakeImage.SetEnv("CNB_PROCESS_TYPE", "other-process"))
})

it("returns processes setting the correct default process", func() {
info, err := subject.InspectImage("some/image", useDaemon)
h.AssertNil(t, err)

h.AssertEq(t, info.Processes,
ProcessDetails{
DefaultProcess: &lifecycle.Process{
Type: "other-process",
Command: "/other/process",
Args: []string{"opt", "1"},
Direct: true,
},
OtherProcesses: []lifecycle.Process{
{
Type: "web",
Command: "/start/web-process",
Args: []string{"-p", "1234"},
Direct: false,
},
},
},
)
})
})

when("CNB_PROCESS_TYPE is set, but doesn't match an existing process", func() {
it.Before(func() {
h.AssertNil(t, fakeImage.SetEnv("CNB_PROCESS_TYPE", "missing-process"))
})

it("returns a nil default process", func() {
info, err := subject.InspectImage("some/image", useDaemon)
h.AssertNil(t, err)

h.AssertEq(t, info.Processes,
ProcessDetails{
DefaultProcess: nil,
OtherProcesses: []lifecycle.Process{
{
Type: "other-process",
Command: "/other/process",
Args: []string{"opt", "1"},
Direct: true,
},
{
Type: "web",
Command: "/start/web-process",
Args: []string{"-p", "1234"},
Direct: false,
},
},
},
)
})
})

it("returns a nil default process when CNB_PROCESS_TYPE is not set and there is no web process", func() {
h.AssertNil(t, fakeImage.SetLabel(
"io.buildpacks.build.metadata",
`{
"processes": [
{
"type": "other-process",
"command": "/other/process",
"args": ["opt", "1"],
"direct": true
}
]
}`,
))

info, err := subject.InspectImage("some/image", useDaemon)
h.AssertNil(t, err)

h.AssertEq(t, info.Processes,
ProcessDetails{
DefaultProcess: nil,
OtherProcesses: []lifecycle.Process{
{
Type: "other-process",
Command: "/other/process",
Args: []string{"opt", "1"},
Direct: true,
},
},
},
)
})
})
}
})
Expand Down
Loading

0 comments on commit f8b3674

Please sign in to comment.