Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes comparison of layer metadata vs expected wrt deprecation_date #261

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"reflect"
"time"

"github.com/BurntSushi/toml"
"github.com/heroku/color"
Expand Down Expand Up @@ -118,7 +119,37 @@ func (l *LayerContributor) checkIfMetadataMatches(layer libcnb.Layer) (map[strin
l.Logger.Debugf("Expected metadata: %+v", expected)
l.Logger.Debugf("Actual metadata: %+v", layer.Metadata)

return expected, reflect.DeepEqual(expected, layer.Metadata), nil
match, err := l.Equals(expected,layer.Metadata)
if err != nil {
return map[string]interface{}{}, false, fmt.Errorf("unable to compare metadata\n%w", err)
}
return expected, match, nil
}

func (l *LayerContributor) Equals(expectedM map[string]interface{}, layerM map[string]interface{}) (bool, error) {
if dep, ok := expectedM["dependency"].(map[string]interface{}); ok {
for k, v := range dep {
if k == "deprecation_date" {
deprecationDate := v.(time.Time).Truncate(time.Second).In(time.UTC)
dep["deprecation_date"] = deprecationDate
break
}
}
}
if dep, ok := layerM["dependency"].(map[string]interface{}); ok {
for k, v := range dep {
if k == "deprecation_date" {
deprecationDate, err := time.Parse(time.RFC3339, v.(string))
if err != nil {
return false, fmt.Errorf("unable to parse deprecation_date %s", v.(string))
}
deprecationDate = deprecationDate.Truncate(time.Second).In(time.UTC)
dep["deprecation_date"] = deprecationDate
break
}
}
}
return reflect.DeepEqual(expectedM, layerM), nil
}

func (l *LayerContributor) checkIfLayerRestored(layer libcnb.Layer) (bool, error) {
Expand Down
101 changes: 101 additions & 0 deletions layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,107 @@ func testLayer(t *testing.T, context spec.G, it spec.S) {
Expect(called).To(BeFalse())
})

it("does not call function with non-matching deprecation_date format", func() {
dependency = libpak.BuildpackDependency{
ID: "test-id",
Name: "test-name",
Version: "1.1.1",
URI: fmt.Sprintf("%s/test-path", server.URL()),
SHA256: "576dd8416de5619ea001d9662291d62444d1292a38e96956bc4651c01f14bca1",
Stacks: []string{"test-stack"},
Licenses: []libpak.BuildpackDependencyLicense{
{
Type: "test-type",
URI: "test-uri",
},
},
CPEs: []string{"cpe:2.3:a:some:jre:11.0.2:*:*:*:*:*:*:*"},
PURL: "pkg:generic/some-java11@11.0.2?arch=amd64",
DeprecationDate: dependency.DeprecationDate, // parsed as '2021-04-01 00:00:00 +0000 UTC'
anthonydahanne marked this conversation as resolved.
Show resolved Hide resolved
}
dlc.ExpectedMetadata = map[string]interface{}{ "dependency":dependency}

layer.Metadata = map[string]interface{}{ "dependency": map[string]interface{}{
"id": dependency.ID,
"name": dependency.Name,
"version": dependency.Version,
"uri": dependency.URI,
"sha256": dependency.SHA256,
"stacks": []interface{}{dependency.Stacks[0]},
"licenses": []map[string]interface{}{
{
"type": dependency.Licenses[0].Type,
"uri": dependency.Licenses[0].URI,
},
},
"cpes": []interface{}{"cpe:2.3:a:some:jre:11.0.2:*:*:*:*:*:*:*"},
"purl": "pkg:generic/some-java11@11.0.2?arch=amd64",
"deprecation_date": "2021-04-01T00:00:00Z", // does not match without truncation
}}

var called bool

_, err := dlc.Contribute(layer, func(artifact *os.File) (libcnb.Layer, error) {
defer artifact.Close()

called = true
return layer, nil
})
Expect(err).NotTo(HaveOccurred())

Expect(called).To(BeFalse())
})

it("does not call function with missing deprecation_date", func() {
dependency = libpak.BuildpackDependency{
ID: "test-id",
Name: "test-name",
Version: "1.1.1",
URI: fmt.Sprintf("%s/test-path", server.URL()),
SHA256: "576dd8416de5619ea001d9662291d62444d1292a38e96956bc4651c01f14bca1",
Stacks: []string{"test-stack"},
Licenses: []libpak.BuildpackDependencyLicense{
{
Type: "test-type",
URI: "test-uri",
},
},
CPEs: []string{"cpe:2.3:a:some:jre:11.0.2:*:*:*:*:*:*:*"},
PURL: "pkg:generic/some-java11@11.0.2?arch=amd64",
}
dlc.ExpectedMetadata = map[string]interface{}{ "dependency":dependency}

layer.Metadata = map[string]interface{}{ "dependency": map[string]interface{}{
"id": dependency.ID,
"name": dependency.Name,
"version": dependency.Version,
"uri": dependency.URI,
"sha256": dependency.SHA256,
"stacks": []interface{}{dependency.Stacks[0]},
"licenses": []map[string]interface{}{
{
"type": dependency.Licenses[0].Type,
"uri": dependency.Licenses[0].URI,
},
},
"cpes": []interface{}{"cpe:2.3:a:some:jre:11.0.2:*:*:*:*:*:*:*"},
"purl": "pkg:generic/some-java11@11.0.2?arch=amd64",
"deprecation_date": "0001-01-01T00:00:00Z",
}}

var called bool

_, err := dlc.Contribute(layer, func(artifact *os.File) (libcnb.Layer, error) {
defer artifact.Close()

called = true
return layer, nil
})
Expect(err).NotTo(HaveOccurred())

Expect(called).To(BeFalse())
})

it("returns function error", func() {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "test-fixture"))

Expand Down