Skip to content

Commit

Permalink
[FAB-8976] Move fixtures closer to tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sykesm committed Mar 20, 2018
1 parent 2235b26 commit a8d0253
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 212 deletions.
45 changes: 33 additions & 12 deletions core/chaincode/platforms/golang/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func testerr(err error, succ bool) error {
Expand Down Expand Up @@ -257,32 +258,52 @@ func TestValidateSpec(t *testing.T) {
}
}

func updateGopath(t *testing.T, path string) func() {
initialGopath, set := os.LookupEnv("GOPATH")

if path == "" {
err := os.Unsetenv("GOPATH")
require.NoError(t, err)
} else {
err := os.Setenv("GOPATH", path)
require.NoError(t, err)
}

if !set {
return func() { os.Unsetenv("GOPATH") }
}
return func() { os.Setenv("GOPATH", initialGopath) }
}

func TestGetDeploymentPayload(t *testing.T) {
emptyDir := fmt.Sprintf("pkg%d", os.Getpid())
os.Mkdir(emptyDir, os.ModePerm)
defer os.Remove(emptyDir)
defaultGopath := os.Getenv("GOPATH")
testdataPath, err := filepath.Abs("testdata")
require.NoError(t, err)

platform := &Platform{}

var tests = []struct {
spec *pb.ChaincodeSpec
succ bool
gopath string
spec *pb.ChaincodeSpec
succ bool
}{
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map"}}, succ: true},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/examples/bad/go/map"}}, succ: false},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/test/chaincodes/BadImport"}}, succ: false},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/test/chaincodes/BadMetadataInvalidIndex"}}, succ: false},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/test/chaincodes/BadMetadataUnexpectedFolderContent"}}, succ: false},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/test/chaincodes/BadMetadataIgnoreHiddenFile"}}, succ: true},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/core/chaincode/platforms/golang/" + emptyDir}}, succ: false},
{gopath: defaultGopath, spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map"}}, succ: true},
{gopath: defaultGopath, spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/examples/bad/go/map"}}, succ: false},
{gopath: testdataPath, spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "chaincodes/BadImport"}}, succ: false},
{gopath: testdataPath, spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "chaincodes/BadMetadataInvalidIndex"}}, succ: false},
{gopath: testdataPath, spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "chaincodes/BadMetadataUnexpectedFolderContent"}}, succ: false},
{gopath: testdataPath, spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "chaincodes/BadMetadataIgnoreHiddenFile"}}, succ: true},
{gopath: testdataPath, spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "chaincodes/empty/"}}, succ: false},
}

for _, tst := range tests {
reset := updateGopath(t, tst.gopath)
_, err := platform.GetDeploymentPayload(tst.spec)
t.Log(err)
if err = testerr(err, tst.succ); err != nil {
t.Errorf("Error validating chaincode spec: %s, %s", tst.spec.ChaincodeId.Path, err)
}
reset()
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +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 number Diff line number Diff line change
@@ -0,0 +1 @@
This test file is located at an unexpected metadata location under META-INF. It should fail golang chaincode packaging tests.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package main
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# this is a hidden file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"index":{"fields":["docType","owner"]},"ddoc":"indexOwnerDoc", "name":"indexOwner","type":"json"}
6 changes: 6 additions & 0 deletions core/container/util/testdata/sourcefiles/artifact.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<ignore>true</ignore>
1 change: 1 addition & 0 deletions core/container/util/testdata/sourcefiles/src/Hello.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a file that should not be included in java packages
12 changes: 12 additions & 0 deletions core/container/util/testdata/sourcefiles/src/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

public class Hello {
public static void main(String []args) {
System.out.println("Hello");
System.exit(0);
}
}
7 changes: 7 additions & 0 deletions core/container/util/testdata/sourcefiles/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package main
Loading

0 comments on commit a8d0253

Please sign in to comment.