Skip to content

Commit

Permalink
Merge pull request #288 from sap-contributions/286-no_sha
Browse files Browse the repository at this point in the history
Use current time as version in external configuration layer
  • Loading branch information
dmikusa authored Dec 14, 2022
2 parents 31044c3 + b1d967c commit 57d247a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
10 changes: 8 additions & 2 deletions tomcat/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path"
"path/filepath"
"strings"
"time"

"github.com/paketo-buildpacks/libpak/effect"
"github.com/paketo-buildpacks/libpak/sbom"
Expand Down Expand Up @@ -123,8 +124,13 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {

var externalConfigurationDependency *libpak.BuildpackDependency
if uri, ok := cr.Resolve("BP_TOMCAT_EXT_CONF_URI"); ok {
v, _ := cr.Resolve("BP_TOMCAT_EXT_CONF_VERSION")
s, _ := cr.Resolve("BP_TOMCAT_EXT_CONF_SHA256")
v, versionExists := cr.Resolve("BP_TOMCAT_EXT_CONF_VERSION")
s, shaExists := cr.Resolve("BP_TOMCAT_EXT_CONF_SHA256")

if !versionExists && !shaExists {
v = time.Now().Format(time.RFC3339)
b.Logger.Infof(color.YellowString("WARNING: No BP_TOMCAT_EXT_CONF_VERSION or BP_TOMCAT_EXT_CONF_SHA256 provided, so no layer caching will occur."))
}

externalConfigurationDependency = &libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Expand Down
69 changes: 57 additions & 12 deletions tomcat/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/paketo-buildpacks/libpak/sbom/mocks"

Expand Down Expand Up @@ -341,18 +342,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

context("$BP_TOMCAT_EXT_CONF_URI", func() {
it.Before(func() {
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_SHA256", "test-sha256")).To(Succeed())
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_URI", "test-uri")).To(Succeed())
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_VERSION", "test-version")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_SHA256")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_URI")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_VERSION")).To(Succeed())
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI is set", func() {
Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "WEB-INF"), 0755)).To(Succeed())

ctx.Buildpack.Metadata = map[string]interface{}{
Expand Down Expand Up @@ -381,19 +370,75 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
}
ctx.StackID = "test-stack-id"

t.Setenv("BP_TOMCAT_EXT_CONF_SHA256", "test-sha256")
t.Setenv("BP_TOMCAT_EXT_CONF_URI", "test-uri")
t.Setenv("BP_TOMCAT_EXT_CONF_VERSION", "test-version")
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI, $BP_TOMCAT_EXT_CONF_VERSION and $BP_TOMCAT_EXT_CONF_SHA256 are set", func() {
result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
Expect(result.Layers[2].(tomcat.Base).ExternalConfigurationDependency).To(Equal(&libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Name: "Tomcat External Configuration",
Version: "test-version",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{ctx.StackID},
}))
sbomScanner.AssertCalled(t, "ScanLaunch", ctx.Application.Path, libcnb.SyftJSON, libcnb.CycloneDXJSON)
})

it("uses time as version if neither $BP_TOMCAT_EXT_CONF_VERSION nor $BP_TOMCAT_EXT_CONF_SHA256 is provided", func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_SHA256")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_VERSION")).To(Succeed())

result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
version := result.Layers[2].(tomcat.Base).ExternalConfigurationDependency.Version
Expect(time.Parse(time.RFC3339, version)).NotTo(BeNil())
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI and $BP_TOMCAT_EXT_CONF_VERSION are set", func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_SHA256")).To(Succeed())

result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
Expect(result.Layers[2].(tomcat.Base).ExternalConfigurationDependency).To(Equal(&libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Name: "Tomcat External Configuration",
Version: "test-version",
URI: "test-uri",
SHA256: "",
Stacks: []string{ctx.StackID},
}))
sbomScanner.AssertCalled(t, "ScanLaunch", ctx.Application.Path, libcnb.SyftJSON, libcnb.CycloneDXJSON)
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI and $BP_TOMCAT_EXT_CONF_SHA256 are set", func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_VERSION")).To(Succeed())

result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
Expect(result.Layers[2].(tomcat.Base).ExternalConfigurationDependency).To(Equal(&libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Name: "Tomcat External Configuration",
Version: "",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{ctx.StackID},
}))
sbomScanner.AssertCalled(t, "ScanLaunch", ctx.Application.Path, libcnb.SyftJSON, libcnb.CycloneDXJSON)
})

})

it("returns default context path", func() {
Expand Down

0 comments on commit 57d247a

Please sign in to comment.