Skip to content

Commit 5a40475

Browse files
author
Arjun Sreedharan
authored
Fallback to default port when $PORT var not passed (#3)
Previously, docker run <myrackapp> wouldn't have run without a `-e PORT=<port>`
1 parent 286f9ac commit 5a40475

4 files changed

Lines changed: 72 additions & 30 deletions

File tree

build.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ func Build(logger scribe.Logger) packit.BuildFunc {
1010
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
1111

1212
logger.Process("Writing start command")
13-
command := "bundle exec rackup -p ${PORT}"
14-
logger.Subprocess("`%s`", command)
13+
// 9292 is the default rackup port
14+
command := `bundle exec rackup -p "${PORT:-9292}"`
15+
logger.Subprocess(command)
1516

1617
return packit.BuildResult{
1718
Processes: []packit.Process{

build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
7373
Processes: []packit.Process{
7474
{
7575
Type: "web",
76-
Command: "bundle exec rackup -p ${PORT}",
76+
Command: `bundle exec rackup -p "${PORT:-9292}"`,
7777
},
7878
},
7979
}))

integration/simple_app_test.go

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,79 @@ func testSimpleApp(t *testing.T, context spec.G, it spec.S) {
4848
Expect(docker.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
4949
})
5050

51-
it("creates a working OCI image with a rackup start command", func() {
52-
var err error
53-
var logs fmt.Stringer
54-
image, logs, err = pack.WithNoColor().Build.
55-
WithBuildpacks(mriBuildpack, bundlerBuildpack, bundleInstallBuildpack, rackupBuildpack).
56-
WithNoPull().
57-
Execute(name, filepath.Join("testdata", "simple_app"))
58-
Expect(err).NotTo(HaveOccurred(), logs.String())
59-
60-
container, err = docker.Container.Run.WithEnv(map[string]string{"PORT": "9292"}).Execute(image.ID)
61-
Expect(err).NotTo(HaveOccurred())
51+
context("a container port is specified", func() {
52+
it("creates a working OCI image with a rackup start command", func() {
53+
var err error
54+
var logs fmt.Stringer
55+
image, logs, err = pack.WithNoColor().Build.
56+
WithBuildpacks(mriBuildpack, bundlerBuildpack, bundleInstallBuildpack, rackupBuildpack).
57+
WithNoPull().
58+
Execute(name, filepath.Join("testdata", "simple_app"))
59+
Expect(err).NotTo(HaveOccurred(), logs.String())
60+
61+
container, err = docker.Container.Run.WithEnv(map[string]string{"PORT": "8088"}).Execute(image.ID)
62+
Expect(err).NotTo(HaveOccurred())
63+
64+
Eventually(container).Should(BeAvailable(), ContainerLogs(container.ID))
65+
66+
_, exists := container.Ports["8088"]
67+
Expect(exists).To(BeTrue())
68+
69+
response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort()))
70+
Expect(err).NotTo(HaveOccurred())
71+
defer response.Body.Close()
72+
73+
Expect(response.StatusCode).To(Equal(http.StatusOK))
74+
75+
content, err := ioutil.ReadAll(response.Body)
76+
Expect(err).NotTo(HaveOccurred())
77+
Expect(string(content)).To(ContainSubstring("Hello world!"))
78+
79+
buildpackVersion, err := GetGitVersion()
80+
Expect(err).ToNot(HaveOccurred())
81+
82+
Expect(logs).To(ContainLines(
83+
fmt.Sprintf("Rackup Buildpack %s", buildpackVersion),
84+
" Writing start command",
85+
` bundle exec rackup -p "${PORT:-9292}"`,
86+
))
87+
})
88+
})
6289

63-
Eventually(container).Should(BeAvailable(), ContainerLogs(container.ID))
90+
context("no container port is specified", func() {
91+
it("creates a working OCI image with a rackup start command", func() {
92+
var err error
93+
var logs fmt.Stringer
94+
image, logs, err = pack.WithNoColor().Build.
95+
WithBuildpacks(mriBuildpack, bundlerBuildpack, bundleInstallBuildpack, rackupBuildpack).
96+
WithNoPull().
97+
Execute(name, filepath.Join("testdata", "simple_app"))
98+
Expect(err).NotTo(HaveOccurred(), logs.String())
6499

65-
response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort()))
66-
Expect(err).NotTo(HaveOccurred())
67-
defer response.Body.Close()
100+
container, err = docker.Container.Run.Execute(image.ID)
101+
Expect(err).NotTo(HaveOccurred())
68102

69-
Expect(response.StatusCode).To(Equal(http.StatusOK))
103+
Eventually(container).Should(BeAvailable(), ContainerLogs(container.ID))
70104

71-
content, err := ioutil.ReadAll(response.Body)
72-
Expect(err).NotTo(HaveOccurred())
73-
Expect(string(content)).To(ContainSubstring("Hello world!"))
105+
response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort()))
106+
Expect(err).NotTo(HaveOccurred())
107+
defer response.Body.Close()
108+
109+
Expect(response.StatusCode).To(Equal(http.StatusOK))
110+
111+
content, err := ioutil.ReadAll(response.Body)
112+
Expect(err).NotTo(HaveOccurred())
113+
Expect(string(content)).To(ContainSubstring("Hello world!"))
74114

75-
buildpackVersion, err := GetGitVersion()
76-
Expect(err).ToNot(HaveOccurred())
115+
buildpackVersion, err := GetGitVersion()
116+
Expect(err).ToNot(HaveOccurred())
77117

78-
Expect(logs).To(ContainLines(
79-
fmt.Sprintf("Rackup Buildpack %s", buildpackVersion),
80-
" Writing start command",
81-
" `bundle exec rackup -p ${PORT}`",
82-
))
118+
Expect(logs).To(ContainLines(
119+
fmt.Sprintf("Rackup Buildpack %s", buildpackVersion),
120+
" Writing start command",
121+
` bundle exec rackup -p "${PORT:-9292}"`,
122+
))
123+
})
83124
})
84125
})
85126
}

integration/sinatra_app_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func testSinatraApp(t *testing.T, context spec.G, it spec.S) {
8181
Expect(logs).To(ContainLines(
8282
fmt.Sprintf("Rackup Buildpack %s", buildpackVersion),
8383
" Writing start command",
84-
" `bundle exec rackup -p ${PORT}`",
84+
` bundle exec rackup -p "${PORT:-9292}"`,
8585
))
8686
})
8787
})

0 commit comments

Comments
 (0)