diff --git a/core/chaincode/platforms/golang/platform_test.go b/core/chaincode/platforms/golang/platform_test.go index 124ffb9d568..fce56025e0f 100644 --- a/core/chaincode/platforms/golang/platform_test.go +++ b/core/chaincode/platforms/golang/platform_test.go @@ -1,6 +1,9 @@ package golang import ( + "fmt" + "os" + "strings" "testing" "archive/tar" @@ -8,9 +11,21 @@ import ( "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 @@ -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) @@ -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, }, }, @@ -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 { @@ -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()) +}