Skip to content

Commit

Permalink
[FAB-3382] more test cov. for platforms/golang
Browse files Browse the repository at this point in the history
Improves the test coverage from 17% to 82%. Some test coverage
also comes from chaincode tests outside of this package.

Change-Id: I7ba1858aa6549e45380732d4d2ab98017cabc0c4
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Apr 26, 2017
1 parent c5c60c3 commit 45bf901
Showing 1 changed file with 121 additions and 6 deletions.
127 changes: 121 additions & 6 deletions core/chaincode/platforms/golang/platform_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package golang

import (
"fmt"
"os"
"strings"
"testing"

"archive/tar"
"bytes"
"compress/gzip"
"time"

"github.com/spf13/viper"

"github.com/hyperledger/fabric/core/config"
pb "github.com/hyperledger/fabric/protos/peer"
)

func testerr(err error, succ bool) error {
if succ && err != nil {
return fmt.Errorf("Expected success but got %s", err)
} else if !succ && err == nil {
return fmt.Errorf("Expected failer but succeeded")
}
return nil
}

func writeBytesToPackage(name string, payload []byte, mode int64, tw *tar.Writer) error {
//Make headers identical by using zero time
var zeroTime time.Time
Expand All @@ -20,7 +35,7 @@ func writeBytesToPackage(name string, payload []byte, mode int64, tw *tar.Writer
return nil
}

func generateFakeCDS(path, file string, mode int64) (*pb.ChaincodeDeploymentSpec, error) {
func generateFakeCDS(ccname, path, file string, mode int64) (*pb.ChaincodeDeploymentSpec, error) {
codePackage := bytes.NewBuffer(nil)
gw := gzip.NewWriter(codePackage)
tw := tar.NewWriter(gw)
Expand All @@ -37,7 +52,7 @@ func generateFakeCDS(path, file string, mode int64) (*pb.ChaincodeDeploymentSpec
cds := &pb.ChaincodeDeploymentSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
ChaincodeId: &pb.ChaincodeID{
Name: "Bad Code",
Name: ccname,
Path: path,
},
},
Expand All @@ -48,21 +63,23 @@ func generateFakeCDS(path, file string, mode int64) (*pb.ChaincodeDeploymentSpec
}

type spec struct {
CCName string
Path, File string
Mode int64
SuccessExpected bool
RealGen bool
}

func TestValidateCDS(t *testing.T) {
platform := &Platform{}

specs := make([]spec, 0)
specs = append(specs, spec{Path: "path/to/nowhere", File: "/bin/warez", Mode: 0100400, SuccessExpected: false})
specs = append(specs, spec{Path: "path/to/somewhere", File: "/src/path/to/somewhere/main.go", Mode: 0100400, SuccessExpected: true})
specs = append(specs, spec{Path: "path/to/somewhere", File: "/src/path/to/somewhere/warez", Mode: 0100555, SuccessExpected: false})
specs = append(specs, spec{CCName: "NoCode", Path: "path/to/nowhere", File: "/bin/warez", Mode: 0100400, SuccessExpected: false})
specs = append(specs, spec{CCName: "NoCode", Path: "path/to/somewhere", File: "/src/path/to/somewhere/main.go", Mode: 0100400, SuccessExpected: true})
specs = append(specs, spec{CCName: "NoCode", Path: "path/to/somewhere", File: "/src/path/to/somewhere/warez", Mode: 0100555, SuccessExpected: false})

for _, s := range specs {
cds, err := generateFakeCDS(s.Path, s.File, s.Mode)
cds, err := generateFakeCDS(s.CCName, s.Path, s.File, s.Mode)

err = platform.ValidateDeploymentSpec(cds)
if s.SuccessExpected == true && err != nil {
Expand Down Expand Up @@ -123,3 +140,101 @@ func Test_decodeUrl(t *testing.T) {
t.Logf("Error to decodeUrl successfully with invalid path: %s", cs.ChaincodeId.Path)
}
}

func TestValidChaincodeSpec(t *testing.T) {
platform := &Platform{}

var tests = []struct {
spec *pb.ChaincodeSpec
succ bool
}{
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "http://github.com/hyperledger/fabric/examples/chaincode/go/map"}}, succ: true},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "https://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/chaincode/go/map"}}, succ: true},
{spec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "Test Chaincode", Path: "github.com/hyperledger/fabric/bad/chaincode/go/map"}}, succ: false},
}

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

func TestGetDeploymentPayload(t *testing.T) {
platform := &Platform{}

var tests = []struct {
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},
}

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

//TestGenerateDockerBuild goes through the functions needed to do docker build
func TestGenerateDockerBuild(t *testing.T) {
platform := &Platform{}

specs := make([]spec, 0)
specs = append(specs, spec{CCName: "NoCode", Path: "path/to/nowhere", File: "/bin/warez", Mode: 0100400, SuccessExpected: false})
specs = append(specs, spec{CCName: "invalidhttp", Path: "https://not/a/valid/path", File: "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go", Mode: 0100400, SuccessExpected: false, RealGen: true})
specs = append(specs, spec{CCName: "map", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map", File: "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go", Mode: 0100400, SuccessExpected: true, RealGen: true})
specs = append(specs, spec{CCName: "mapBadPath", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map", File: "/src/github.com/hyperledger/fabric/examples/bad/path/to/map.go", Mode: 0100400, SuccessExpected: false})
specs = append(specs, spec{CCName: "mapBadMode", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map", File: "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go", Mode: 0100555, SuccessExpected: false})

var err error
for _, tst := range specs {
inputbuf := bytes.NewBuffer(nil)
tw := tar.NewWriter(inputbuf)

var cds *pb.ChaincodeDeploymentSpec
if tst.RealGen {
cds = &pb.ChaincodeDeploymentSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
ChaincodeId: &pb.ChaincodeID{
Name: tst.CCName,
Path: tst.Path,
Version: "0",
},
},
}
cds.CodePackage, err = platform.GetDeploymentPayload(cds.ChaincodeSpec)
if err = testerr(err, tst.SuccessExpected); err != nil {
t.Errorf("test failed in GetDeploymentPayload: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
}
} else {
cds, err = generateFakeCDS(tst.CCName, tst.Path, tst.File, tst.Mode)
}

if _, err = platform.GenerateDockerfile(cds); err != nil {
t.Errorf("could not generate docker file for a valid spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
}
err = platform.GenerateDockerBuild(cds, tw)
if err = testerr(err, tst.SuccessExpected); err != nil {
t.Errorf("Error to validating chaincode spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
}
}
}

func TestMain(m *testing.M) {
viper.SetConfigName("core")
viper.SetEnvPrefix("CORE")
config.AddDevConfigPath(nil)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("could not read config %s\n", err)
os.Exit(-1)
}
os.Exit(m.Run())
}

0 comments on commit 45bf901

Please sign in to comment.