Skip to content

Commit 77d4b81

Browse files
author
Jason Yellick
committed
FAB-10828 ValidateDepSpec to ValidateCodePackage
Part of an ongoing series to remove all references to the chaincode protos from the platforms package so that they can eventually be replaced. ValidateDeploymentSpec was only ever inspecting the code package bytes and nothing else about the spec, so changing this method to take the bytes directly. Change-Id: I897028f3bf4ea9818f685d01fba09ede5783637d Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 9e47bf2 commit 77d4b81

File tree

10 files changed

+70
-56
lines changed

10 files changed

+70
-56
lines changed

core/chaincode/platforms/car/platform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (carPlatform *Platform) ValidatePath(path string) error {
3737
return nil
3838
}
3939

40-
func (carPlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSpec) error {
40+
func (carPlatform *Platform) ValidateCodePackage(codePackage []byte) error {
4141
// CAR platform will validate the code package within chaintool
4242
return nil
4343
}

core/chaincode/platforms/golang/platform.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ func (goPlatform *Platform) ValidatePath(rawPath string) error {
120120
return nil
121121
}
122122

123-
func (goPlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSpec) error {
123+
func (goPlatform *Platform) ValidateCodePackage(code []byte) error {
124124

125-
if cds.CodePackage == nil || len(cds.CodePackage) == 0 {
125+
if len(code) == 0 {
126126
// Nothing to validate if no CodePackage was included
127127
return nil
128128
}
@@ -138,7 +138,7 @@ func (goPlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSp
138138
// resilient in enforcing constraints. However, we should still do our best to keep as much
139139
// garbage out of the system as possible.
140140
re := regexp.MustCompile(`(/)?src/.*`)
141-
is := bytes.NewReader(cds.CodePackage)
141+
is := bytes.NewReader(code)
142142
gr, err := gzip.NewReader(is)
143143
if err != nil {
144144
return fmt.Errorf("failure opening codepackage gzip stream: %s", err)

core/chaincode/platforms/golang/platform_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestValidateCDS(t *testing.T) {
9090
for _, s := range specs {
9191
cds, err := generateFakeCDS(s.CCName, s.Path, s.File, s.Mode)
9292

93-
err = platform.ValidateDeploymentSpec(cds)
93+
err = platform.ValidateCodePackage(cds.Bytes())
9494
if s.SuccessExpected == true && err != nil {
9595
t.Errorf("Unexpected failure: %s", err)
9696
}

core/chaincode/platforms/java/platform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (javaPlatform *Platform) ValidatePath(rawPath string) error {
3838
return nil
3939
}
4040

41-
func (javaPlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSpec) error {
41+
func (javaPlatform *Platform) ValidateCodePackage(code []byte) error {
4242
// FIXME: Java platform needs to implement its own validation similar to GOLANG
4343
return nil
4444
}

core/chaincode/platforms/mock/platform.go

Lines changed: 40 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/chaincode/platforms/node/platform.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ func (nodePlatform *Platform) ValidatePath(rawPath string) error {
7373
return nil
7474
}
7575

76-
func (nodePlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSpec) error {
76+
func (nodePlatform *Platform) ValidateCodePackage(code []byte) error {
7777

78-
if cds.CodePackage == nil || len(cds.CodePackage) == 0 {
78+
if len(code) == 0 {
7979
// Nothing to validate if no CodePackage was included
8080
return nil
8181
}
@@ -88,7 +88,7 @@ func (nodePlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeployment
8888
// resilient in enforcing constraints. However, we should still do our best to keep as much
8989
// garbage out of the system as possible.
9090
re := regexp.MustCompile(`(/)?src/.*`)
91-
is := bytes.NewReader(cds.CodePackage)
91+
is := bytes.NewReader(code)
9292
gr, err := gzip.NewReader(is)
9393
if err != nil {
9494
return fmt.Errorf("failure opening codepackage gzip stream: %s", err)

core/chaincode/platforms/node/platform_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func TestValidatePath(t *testing.T) {
4141

4242
}
4343

44-
func TestValidateDeploymentSpec(t *testing.T) {
45-
err := platform.ValidateDeploymentSpec(&peer.ChaincodeDeploymentSpec{CodePackage: []byte("dummy CodePackage content")})
44+
func TestValidateCodePackage(t *testing.T) {
45+
err := platform.ValidateCodePackage([]byte("dummy CodePackage content"))
4646
if err == nil {
4747
t.Fatalf("should have returned an error on an invalid chaincode package")
4848
} else if !strings.HasPrefix(err.Error(), "failure opening codepackage gzip stream") {
@@ -54,7 +54,7 @@ func TestValidateDeploymentSpec(t *testing.T) {
5454
t.Fatal(err)
5555
}
5656

57-
err = platform.ValidateDeploymentSpec(&peer.ChaincodeDeploymentSpec{CodePackage: cp})
57+
err = platform.ValidateCodePackage(cp)
5858
if err == nil {
5959
t.Fatal("should have failed to validate because file in the archive is in the root folder instead of 'src'")
6060
} else if !strings.HasPrefix(err.Error(), "illegal file detected in payload") {
@@ -66,7 +66,7 @@ func TestValidateDeploymentSpec(t *testing.T) {
6666
t.Fatal(err)
6767
}
6868

69-
err = platform.ValidateDeploymentSpec(&peer.ChaincodeDeploymentSpec{CodePackage: cp})
69+
err = platform.ValidateCodePackage(cp)
7070
if err == nil {
7171
t.Fatal("should have failed to validate because file in the archive is executable")
7272
} else if !strings.HasPrefix(err.Error(), "illegal file mode detected for file") {
@@ -78,7 +78,7 @@ func TestValidateDeploymentSpec(t *testing.T) {
7878
t.Fatal(err)
7979
}
8080

81-
err = platform.ValidateDeploymentSpec(&peer.ChaincodeDeploymentSpec{CodePackage: cp})
81+
err = platform.ValidateCodePackage(cp)
8282
if err == nil {
8383
t.Fatal("should have failed to validate because no 'package.json' found")
8484
} else if !strings.HasPrefix(err.Error(), "no package.json found at the root of the chaincode package") {
@@ -90,7 +90,7 @@ func TestValidateDeploymentSpec(t *testing.T) {
9090
t.Fatal(err)
9191
}
9292

93-
err = platform.ValidateDeploymentSpec(&peer.ChaincodeDeploymentSpec{CodePackage: cp})
93+
err = platform.ValidateCodePackage(cp)
9494
if err != nil {
9595
t.Fatalf("should have returned no errors, but got '%s'", err)
9696
}

core/chaincode/platforms/platforms.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
type Platform interface {
2727
Name() string
2828
ValidatePath(path string) error
29-
ValidateDeploymentSpec(spec *pb.ChaincodeDeploymentSpec) error
29+
ValidateCodePackage(code []byte) error
3030
GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte, error)
3131
GenerateDockerfile(spec *pb.ChaincodeDeploymentSpec) (string, error)
3232
GenerateDockerBuild(spec *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error
@@ -77,7 +77,7 @@ func (r *Registry) ValidateDeploymentSpec(spec *pb.ChaincodeDeploymentSpec) erro
7777
if !ok {
7878
return fmt.Errorf("Unknown chaincodeType: %s", spec.ChaincodeSpec.Type)
7979
}
80-
return platform.ValidateDeploymentSpec(spec)
80+
return platform.ValidateCodePackage(spec.Bytes())
8181
}
8282

8383
func (r *Registry) GetMetadataProvider(spec *pb.ChaincodeDeploymentSpec) (ccmetadata.MetadataProvider, error) {

core/chaincode/platforms/platforms_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ var _ = Describe("Platforms", func() {
6060

6161
Describe("ValidateDeploymentSpec", func() {
6262
It("returns the result of the underlying platform", func() {
63-
fakePlatform.ValidateDeploymentSpecReturns(errors.New("fake-error"))
64-
spec := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG}}
63+
fakePlatform.ValidateCodePackageReturns(errors.New("fake-error"))
64+
spec := &pb.ChaincodeDeploymentSpec{
65+
ChaincodeSpec: &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG},
66+
CodePackage: []byte("code-package"),
67+
}
6568
err := registry.ValidateDeploymentSpec(spec)
6669
Expect(err).To(MatchError(errors.New("fake-error")))
67-
Expect(fakePlatform.ValidateDeploymentSpecCallCount()).To(Equal(1))
68-
Expect(fakePlatform.ValidateDeploymentSpecArgsForCall(0)).To(Equal(spec))
70+
Expect(fakePlatform.ValidateCodePackageCallCount()).To(Equal(1))
71+
Expect(fakePlatform.ValidateCodePackageArgsForCall(0)).To(Equal([]byte("code-package")))
6972
})
7073

7174
Context("when the platform is unknown", func() {

protos/peer/chaincode.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ SPDX-License-Identifier: Apache-2.0
66

77
package peer
88

9+
// Path implements the platforms.PathDescriber interface
910
func (cs *ChaincodeSpec) Path() string {
1011
if cs.ChaincodeId == nil {
1112
return ""
1213
}
1314

1415
return cs.ChaincodeId.Path
1516
}
17+
18+
// Bytes implements the platforms.CodePackage interface
19+
func (cds *ChaincodeDeploymentSpec) Bytes() []byte {
20+
return cds.CodePackage
21+
}

0 commit comments

Comments
 (0)