Skip to content

Commit a8d0253

Browse files
committed
[FAB-8976] Move fixtures closer to tests
Test fixtures should typically be placed in `testdata` directories to prevent the go tooling from operating on them by default. There are a number of these fixtures that live in the incorrectly named top level `test` directory. Instead of keeping fixtures in one location, this commit moves the assets closer to the tests that use them and makes the necessary path modifications to reference them. Tests that were relying on fixtures to live on the go path, were modified to explicitly set the gopath to the testdata directory. Change-Id: I5dbc0cb5af41c63289aea925b08fea1584ca598f Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 2235b26 commit a8d0253

File tree

18 files changed

+194
-212
lines changed

18 files changed

+194
-212
lines changed

core/chaincode/platforms/golang/platform_test.go

+33-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
pb "github.com/hyperledger/fabric/protos/peer"
2424
"github.com/spf13/viper"
2525
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/require"
2627
)
2728

2829
func testerr(err error, succ bool) error {
@@ -257,32 +258,52 @@ func TestValidateSpec(t *testing.T) {
257258
}
258259
}
259260

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+
260278
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)
264282

265283
platform := &Platform{}
266284

267285
var tests = []struct {
268-
spec *pb.ChaincodeSpec
269-
succ bool
286+
gopath string
287+
spec *pb.ChaincodeSpec
288+
succ bool
270289
}{
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},
278297
}
279298

280299
for _, tst := range tests {
300+
reset := updateGopath(t, tst.gopath)
281301
_, err := platform.GetDeploymentPayload(tst.spec)
282302
t.Log(err)
283303
if err = testerr(err, tst.succ); err != nil {
284304
t.Errorf("Error validating chaincode spec: %s, %s", tst.spec.ChaincodeId.Path, err)
285305
}
306+
reset()
286307
}
287308
}
288309

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This test file is expected to contain a json formatted couchdb index, but does not. It should fail golang chaincode packaging tests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This test file is located at an unexpected metadata location under META-INF. It should fail golang chaincode packaging tests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# this is a hidden file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"index":{"fields":["docType","owner"]},"ddoc":"indexOwnerDoc", "name":"indexOwner","type":"json"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
-->
6+
<ignore>true</ignore>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a file that should not be included in java packages
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
public class Hello {
8+
public static void main(String []args) {
9+
System.out.println("Hello");
10+
System.exit(0);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package main

0 commit comments

Comments
 (0)