Skip to content

Commit 20f5006

Browse files
committed
[FAB-7412] Return error when cc src dir is empty
The peer CLI should return an error if there are no source files in src path when running the install and/or package commands. This fixes the issue for the Java and Node platforms and adds a test for the Golang platform (which already handled the error). Change-Id: Iaf732308f9f377e0e701ecac03980adeb8fea611 Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
1 parent 341159b commit 20f5006

File tree

4 files changed

+47
-347
lines changed

4 files changed

+47
-347
lines changed

core/chaincode/platforms/golang/platform_go19_test.go

Lines changed: 0 additions & 314 deletions
This file was deleted.

core/chaincode/platforms/golang/platform_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !go1.9
1+
// +build go1.9
22

33
/*
44
Copyright IBM Corp. All Rights Reserved.
@@ -14,6 +14,7 @@ import (
1414
"compress/gzip"
1515
"fmt"
1616
"os"
17+
"path/filepath"
1718
"strings"
1819
"testing"
1920
"time"
@@ -100,18 +101,15 @@ func TestValidateCDS(t *testing.T) {
100101
}
101102

102103
func TestPlatform_GoPathNotSet(t *testing.T) {
103-
p := &Platform{}
104-
spec := &pb.ChaincodeSpec{
105-
ChaincodeId: &pb.ChaincodeID{
106-
Path: "/opt/gopath/src/github.com/hyperledger/fabric",
107-
},
108-
}
109104
gopath := os.Getenv("GOPATH")
110105
defer os.Setenv("GOPATH", gopath)
111106
os.Setenv("GOPATH", "")
112107

113-
err := p.ValidateSpec(spec)
114-
assert.Contains(t, err.Error(), "invalid GOPATH environment variable value")
108+
// Go 1.9 sets GOPATH to $HOME/go if GOPATH is not set
109+
defaultGopath := filepath.Join(os.Getenv("HOME"), "go")
110+
currentGopath, err := getGopath()
111+
assert.NoError(t, err, "Expected default GOPATH")
112+
assert.Equal(t, defaultGopath, currentGopath)
115113
}
116114

117115
func Test_findSource(t *testing.T) {
@@ -225,6 +223,10 @@ func TestValidateSpec(t *testing.T) {
225223
}
226224

227225
func TestGetDeploymentPayload(t *testing.T) {
226+
emptyDir := fmt.Sprintf("pkg%d", os.Getpid())
227+
os.Mkdir(emptyDir, os.ModePerm)
228+
defer os.Remove(emptyDir)
229+
228230
platform := &Platform{}
229231

230232
var tests = []struct {
@@ -234,16 +236,30 @@ func TestGetDeploymentPayload(t *testing.T) {
234236
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map"}}, succ: true},
235237
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/examples/bad/go/map"}}, succ: false},
236238
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/test/chaincodes/BadImport"}}, succ: false},
239+
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/core/chaincode/platforms/golang/" + emptyDir}}, succ: false},
237240
}
238241

239242
for _, tst := range tests {
240243
_, err := platform.GetDeploymentPayload(tst.spec)
244+
t.Log(err)
241245
if err = testerr(err, tst.succ); err != nil {
242246
t.Errorf("Error validating chaincode spec: %s, %s", tst.spec.ChaincodeId.Path, err)
243247
}
244248
}
245249
}
246250

251+
//TestGetLDFlagsOpts tests handling of chaincode.golang.dynamicLink
252+
func TestGetLDFlagsOpts(t *testing.T) {
253+
viper.Set("chaincode.golang.dynamicLink", true)
254+
if getLDFlagsOpts() != dynamicLDFlagsOpts {
255+
t.Error("Error handling chaincode.golang.dynamicLink configuration. ldflags should be for dynamic linkink")
256+
}
257+
viper.Set("chaincode.golang.dynamicLink", false)
258+
if getLDFlagsOpts() != staticLDFlagsOpts {
259+
t.Error("Error handling chaincode.golang.dynamicLink configuration. ldflags should be for static linkink")
260+
}
261+
}
262+
247263
//TestGenerateDockerBuild goes through the functions needed to do docker build
248264
func TestGenerateDockerBuild(t *testing.T) {
249265
platform := &Platform{}

0 commit comments

Comments
 (0)