From 84845ffd9c26e3cd18e4e4e3253eae251563164d Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Tue, 10 Dec 2019 10:40:31 -0500 Subject: [PATCH] FAB-17170 Peer CLI should encode mdata lowercase Once upon a time, the JSON fields in the chaincode package metadata.json were upper case. The peer CLI was written to reflect this fact. When they were changed to lower-case, the peer CLI was not updated, but because of the fuzzy JSON matching rules in the golang JSON decoded it was not noticed. However, for external builders, the inconsistency is more likely to be noticed as tools like jq etc. may not default to this same fuzzy case-insensitive matching. This change simply modifies the peer CLI to encode the metadata fields as lower case. Signed-off-by: Jason Yellick --- internal/peer/lifecycle/chaincode/package.go | 6 +++--- .../peer/lifecycle/chaincode/package_test.go | 19 ++++++------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/internal/peer/lifecycle/chaincode/package.go b/internal/peer/lifecycle/chaincode/package.go index e6a53c718bc..37e5db64842 100644 --- a/internal/peer/lifecycle/chaincode/package.go +++ b/internal/peer/lifecycle/chaincode/package.go @@ -211,9 +211,9 @@ func writeBytesToPackage(tw *tar.Writer, name string, payload []byte) error { // PackageMetadata holds the path and type for a chaincode package type PackageMetadata struct { - Path string `json:"Path"` - Type string `json:"Type"` - Label string `json:"Label"` + Path string `json:"path"` + Type string `json:"type"` + Label string `json:"label"` } func toJSON(path, ccType, label string) ([]byte, error) { diff --git a/internal/peer/lifecycle/chaincode/package_test.go b/internal/peer/lifecycle/chaincode/package_test.go index df05e9e2d1c..e00a11682b3 100644 --- a/internal/peer/lifecycle/chaincode/package_test.go +++ b/internal/peer/lifecycle/chaincode/package_test.go @@ -8,9 +8,10 @@ package chaincode_test import ( "archive/tar" + "bytes" "compress/gzip" - "encoding/json" "io" + "io/ioutil" "os" "path/filepath" @@ -21,7 +22,6 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/onsi/gomega/gbytes" ) var _ = Describe("Package", func() { @@ -77,11 +77,7 @@ var _ = Describe("Package", func() { metadata, err := readMetadataFromBytes(pkgTarGzBytes) Expect(err).NotTo(HaveOccurred()) - Expect(metadata).To(Equal(&chaincode.PackageMetadata{ - Path: "normalizedPath", - Type: "testType", - Label: "testLabel", - })) + Expect(metadata).To(MatchJSON(`{"path":"normalizedPath","type":"testType","label":"testLabel"}`)) }) Context("when the path is not provided", func() { @@ -211,8 +207,8 @@ var _ = Describe("Package", func() { }) }) -func readMetadataFromBytes(pkgTarGzBytes []byte) (*chaincode.PackageMetadata, error) { - buffer := gbytes.BufferWithBytes(pkgTarGzBytes) +func readMetadataFromBytes(pkgTarGzBytes []byte) ([]byte, error) { + buffer := bytes.NewBuffer(pkgTarGzBytes) gzr, err := gzip.NewReader(buffer) Expect(err).NotTo(HaveOccurred()) defer gzr.Close() @@ -226,10 +222,7 @@ func readMetadataFromBytes(pkgTarGzBytes []byte) (*chaincode.PackageMetadata, er return nil, err } if header.Name == "metadata.json" { - jsonDecoder := json.NewDecoder(tr) - metadata := &chaincode.PackageMetadata{} - err := jsonDecoder.Decode(metadata) - return metadata, err + return ioutil.ReadAll(tr) } } return nil, errors.New("metadata.json not found")