@@ -23,6 +23,7 @@ import (
23
23
pb "github.com/hyperledger/fabric/protos/peer"
24
24
"github.com/spf13/viper"
25
25
"github.com/stretchr/testify/assert"
26
+ "github.com/stretchr/testify/require"
26
27
)
27
28
28
29
func testerr (err error , succ bool ) error {
@@ -257,32 +258,52 @@ func TestValidateSpec(t *testing.T) {
257
258
}
258
259
}
259
260
261
+ func updateGopath (t * testing.T , path string ) func () {
262
+ initialGopath , set := os .LookupEnv ("GOPATH" )
263
+
264
+ if path == "" {
265
+ err := os .Unsetenv ("GOPATH" )
266
+ require .NoError (t , err )
267
+ } else {
268
+ err := os .Setenv ("GOPATH" , path )
269
+ require .NoError (t , err )
270
+ }
271
+
272
+ if ! set {
273
+ return func () { os .Unsetenv ("GOPATH" ) }
274
+ }
275
+ return func () { os .Setenv ("GOPATH" , initialGopath ) }
276
+ }
277
+
260
278
func TestGetDeploymentPayload (t * testing.T ) {
261
- emptyDir := fmt . Sprintf ( "pkg%d" , os . Getpid () )
262
- os . Mkdir ( emptyDir , os . ModePerm )
263
- defer os . Remove ( emptyDir )
279
+ defaultGopath := os . Getenv ( "GOPATH" )
280
+ testdataPath , err := filepath . Abs ( "testdata" )
281
+ require . NoError ( t , err )
264
282
265
283
platform := & Platform {}
266
284
267
285
var tests = []struct {
268
- spec * pb.ChaincodeSpec
269
- succ bool
286
+ gopath string
287
+ spec * pb.ChaincodeSpec
288
+ succ bool
270
289
}{
271
- {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/examples/chaincode/go/map" }}, succ : true },
272
- {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/examples/bad/go/map" }}, succ : false },
273
- {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/test/ chaincodes/BadImport" }}, succ : false },
274
- {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/test/ chaincodes/BadMetadataInvalidIndex" }}, succ : false },
275
- {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/test/ chaincodes/BadMetadataUnexpectedFolderContent" }}, succ : false },
276
- {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/test/ chaincodes/BadMetadataIgnoreHiddenFile" }}, succ : true },
277
- {spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/core/chaincode/platforms/golang/" + emptyDir }}, succ : false },
290
+ {gopath : defaultGopath , spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/examples/chaincode/go/map" }}, succ : true },
291
+ {gopath : defaultGopath , spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "github.com/hyperledger/fabric/examples/bad/go/map" }}, succ : false },
292
+ {gopath : testdataPath , spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "chaincodes/BadImport" }}, succ : false },
293
+ {gopath : testdataPath , spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "chaincodes/BadMetadataInvalidIndex" }}, succ : false },
294
+ {gopath : testdataPath , spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "chaincodes/BadMetadataUnexpectedFolderContent" }}, succ : false },
295
+ {gopath : testdataPath , spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "chaincodes/BadMetadataIgnoreHiddenFile" }}, succ : true },
296
+ {gopath : testdataPath , spec : & pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : "Test Chaincode" , Path : "chaincodes/empty/" }}, succ : false },
278
297
}
279
298
280
299
for _ , tst := range tests {
300
+ reset := updateGopath (t , tst .gopath )
281
301
_ , err := platform .GetDeploymentPayload (tst .spec )
282
302
t .Log (err )
283
303
if err = testerr (err , tst .succ ); err != nil {
284
304
t .Errorf ("Error validating chaincode spec: %s, %s" , tst .spec .ChaincodeId .Path , err )
285
305
}
306
+ reset ()
286
307
}
287
308
}
288
309
0 commit comments