Skip to content

Commit

Permalink
Changes variable to BP_KEEP_FILES
Browse files Browse the repository at this point in the history
- Makes a seperate and more complicated integration test to more
thoroughly test the functionality
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Nov 11, 2020
1 parent be1dbba commit d8207ad
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 61 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ supply another value as the first argument to `package.sh`.
# compiled. The first target will be used as the start command for the image.
BP_GO_TARGETS=./cmd/web-server:./cmd/debug-server
# The BP_INCLUDE_FILES variable allows to you to specity a path list of files
# The BP_KEEP_FILES variable allows to you to specity a path list of files
# (including file globs) that you would like to appear in the workspace of the
# final image. This will allow you to perserve static assests
BP_INCLUDE_FILES=assets/*:public/*
BP_KEEP_FILES=assets/*:public/*
```

## `buildpack.yml` Configurations
Expand Down
54 changes: 0 additions & 54 deletions integration/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,60 +93,6 @@ func testDefault(t *testing.T, context spec.G, it spec.S) {
})
})

context("when building a simple app with no dependencies but perserving files in the workspace", func() {
var (
image occam.Image
container occam.Container

name string
source string
)

it.Before(func() {
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.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
Expect(docker.Image.Remove.Execute(image.ID)).To(Succeed())
Expect(os.RemoveAll(source)).To(Succeed())
})

it("builds successfully", func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "default"))
Expect(err).NotTo(HaveOccurred())

var logs fmt.Stringer
image, logs, err = pack.Build.
WithPullPolicy("never").
WithBuildpacks(
settings.Buildpacks.GoDist.Online,
settings.Buildpacks.GoBuild.Online,
).
WithEnv(map[string]string{"BP_INCLUDE_FILES": "./static-file"}).
Execute(name, source)
Expect(err).ToNot(HaveOccurred(), logs.String)

container, err = docker.Container.Run.Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(BeAvailable())

response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort()))
Expect(err).NotTo(HaveOccurred())
Expect(response.StatusCode).To(Equal(http.StatusOK))

content, err := ioutil.ReadAll(response.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("go1.15"))
Expect(string(content)).To(ContainSubstring("/workspace contents: [/workspace/static-file]"))
})
})

context("when building a simple app with no dependencies in an offline environment", func() {
var (
image occam.Image
Expand Down
1 change: 1 addition & 0 deletions integration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestIntegration(t *testing.T) {
suite("BuildFlags", testBuildFlags)
suite("Default", testDefault)
suite("ImportPath", testImportPath)
suite("KeepFiles", testKeepFiles)
suite("LayerReuse", testLayerReuse)
suite("Mod", testMod)
suite("Targets", testTargets)
Expand Down
86 changes: 86 additions & 0 deletions integration/keep_files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package integration_test

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)

func testKeepFiles(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually

pack occam.Pack
docker occam.Docker
)

it.Before(func() {
pack = occam.NewPack().WithVerbose().WithNoColor()
docker = occam.NewDocker()
})

context("when building a simple app with no dependencies but perserving files in the workspace", func() {
var (
image occam.Image
container occam.Container

name string
source string
)

it.Before(func() {
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.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
Expect(docker.Image.Remove.Execute(image.ID)).To(Succeed())
Expect(os.RemoveAll(source)).To(Succeed())
})

it("builds successfully", func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "keep_files"))
Expect(err).NotTo(HaveOccurred())

var logs fmt.Stringer
image, logs, err = pack.Build.
WithPullPolicy("never").
WithBuildpacks(
settings.Buildpacks.GoDist.Online,
settings.Buildpacks.GoBuild.Online,
).
WithEnv(map[string]string{"BP_KEEP_FILES": "./static-file:assets/*"}).
Execute(name, source)
Expect(err).ToNot(HaveOccurred(), logs.String)

container, err = docker.Container.Run.Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(BeAvailable())

response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort()))
Expect(err).NotTo(HaveOccurred())
Expect(response.StatusCode).To(Equal(http.StatusOK))

content, err := ioutil.ReadAll(response.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("go1.15"))
Expect(string(content)).To(ContainSubstring("/workspace contents: [/workspace/assets /workspace/static-file]"))
Expect(string(content)).To(ContainSubstring("file contents: Hello world!"))
})
})
}
Empty file.
1 change: 1 addition & 0 deletions integration/testdata/keep_files/assets/some-file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!
25 changes: 25 additions & 0 deletions integration/testdata/keep_files/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, runtime.Version())

paths, _ := filepath.Glob("/workspace/*")
fmt.Fprintf(w, "/workspace contents: %v\n", paths)

contents, _ := ioutil.ReadFile("./assets/some-file")
fmt.Fprintf(w, "file contents: %s\n", string(contents))
})

log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), nil))
}
1 change: 1 addition & 0 deletions integration/testdata/keep_files/static-file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content
2 changes: 1 addition & 1 deletion source_deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewSourceDeleter() SourceDeleter {

func (d SourceDeleter) Clear(path string) error {
var envGlobs []string
if val, ok := os.LookupEnv("BP_INCLUDE_FILES"); ok {
if val, ok := os.LookupEnv("BP_KEEP_FILES"); ok {
envGlobs = append(envGlobs, filepath.SplitList(val)...)
}

Expand Down
8 changes: 4 additions & 4 deletions source_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func testSourceDeleter(t *testing.T, context spec.G, it spec.S) {

context("when there are files to keep", func() {
it.Before(func() {
Expect(os.Setenv("BP_INCLUDE_FILES", `some-dir/some-other-dir/*:some-file`)).To(Succeed())
Expect(os.Setenv("BP_KEEP_FILES", `some-dir/some-other-dir/*:some-file`)).To(Succeed())
})

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

it("returns a result that deletes the contents of the working directroy except for the file that are meant to kept", func() {
Expand All @@ -71,11 +71,11 @@ func testSourceDeleter(t *testing.T, context spec.G, it spec.S) {
context("failure cases", func() {
context("when the path is malformed", func() {
it.Before(func() {
Expect(os.Setenv("BP_INCLUDE_FILES", `\`)).To(Succeed())
Expect(os.Setenv("BP_KEEP_FILES", `\`)).To(Succeed())
})

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

it("returns an error", func() {
Expand Down

0 comments on commit d8207ad

Please sign in to comment.