Skip to content

Commit

Permalink
[FAB-4063] core/chaincode/platforms test coverage
Browse files Browse the repository at this point in the history
This change brings the test coverage of
core/chaincode/platforms up to 100%

Change-Id: I38ea0f69fff1c125473fef899b293071650a3bdb
Signed-off-by: Liam Grace <liamgrace.896@gmail.com>
  • Loading branch information
liam-grace authored and mastersingh24 committed Sep 6, 2017
1 parent 651f497 commit 6df0d50
Show file tree
Hide file tree
Showing 2 changed files with 431 additions and 13 deletions.
35 changes: 22 additions & 13 deletions core/chaincode/platforms/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ type Platform interface {

var logger = flogging.MustGetLogger("chaincode-platform")

// Added for unit testing purposes
var _Find = Find
var _GetPath = config.GetPath
var _VGetBool = viper.GetBool
var _OSStat = os.Stat
var _IOUtilReadFile = ioutil.ReadFile
var _CUtilWriteBytesToPackage = cutil.WriteBytesToPackage
var _getPeerTLSCert = getPeerTLSCert
var _generateDockerfile = generateDockerfile
var _generateDockerBuild = generateDockerBuild

// Find returns the platform interface for the given platform type
func Find(chaincodeType pb.ChaincodeSpec_Type) (Platform, error) {

Expand All @@ -70,7 +81,7 @@ func Find(chaincodeType pb.ChaincodeSpec_Type) (Platform, error) {
}

func GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte, error) {
platform, err := Find(spec.Type)
platform, err := _Find(spec.Type)
if err != nil {
return nil, err
}
Expand All @@ -80,23 +91,23 @@ func GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte, error) {

func getPeerTLSCert() ([]byte, error) {

if viper.GetBool("peer.tls.enabled") == false {
if _VGetBool("peer.tls.enabled") == false {
// no need for certificates if TLS is not enabled
return nil, nil
}
var path string
// first we check for the rootcert
path = config.GetPath("peer.tls.rootcert.file")
path = _GetPath("peer.tls.rootcert.file")
if path == "" {
// check for tls cert
path = config.GetPath("peer.tls.cert.file")
path = _GetPath("peer.tls.cert.file")
}
// this should not happen if the peer is running with TLS enabled
if _, err := os.Stat(path); err != nil {
if _, err := _OSStat(path); err != nil {
return nil, err
}
// FIXME: FAB-2037 - ensure we sanely resolve relative paths specified in the yaml
return ioutil.ReadFile(path)
return _IOUtilReadFile(path)
}

func generateDockerfile(platform Platform, cds *pb.ChaincodeDeploymentSpec, tls bool) ([]byte, error) {
Expand All @@ -110,7 +121,6 @@ func generateDockerfile(platform Platform, cds *pb.ChaincodeDeploymentSpec, tls
if err != nil {
return nil, fmt.Errorf("Failed to generate platform-specific Dockerfile: %s", err)
}

buf = append(buf, base)

// ----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -154,7 +164,7 @@ func generateDockerBuild(platform Platform, cds *pb.ChaincodeDeploymentSpec, inp
// First stream out our static inputFiles
// ----------------------------------------------------------------------------------------------------
for name, data := range inputFiles {
err = cutil.WriteBytesToPackage(name, data, tw)
err = _CUtilWriteBytesToPackage(name, data, tw)
if err != nil {
return fmt.Errorf("Failed to inject \"%s\": %s", name, err)
}
Expand All @@ -178,7 +188,7 @@ func GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec) (io.Reader, error) {
// ----------------------------------------------------------------------------------------------------
// Determine our platform driver from the spec
// ----------------------------------------------------------------------------------------------------
platform, err := Find(cds.ChaincodeSpec.Type)
platform, err := _Find(cds.ChaincodeSpec.Type)
if err != nil {
return nil, fmt.Errorf("Failed to determine platform type: %s", err)
}
Expand All @@ -190,7 +200,7 @@ func GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec) (io.Reader, error) {
// found, regardless of whether TLS is enabled or not. The main implication is that if the administrator
// updates the peer cert, the chaincode containers will need to be invalidated and rebuilt.
// We will manage enabling or disabling TLS at container run time via CORE_PEER_TLS_ENABLED
cert, err := getPeerTLSCert()
cert, err := _getPeerTLSCert()
if err != nil {
return nil, fmt.Errorf("Failed to read the TLS certificate: %s", err)
}
Expand All @@ -200,7 +210,7 @@ func GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec) (io.Reader, error) {
// ----------------------------------------------------------------------------------------------------
// Generate the Dockerfile specific to our context
// ----------------------------------------------------------------------------------------------------
dockerFile, err := generateDockerfile(platform, cds, cert != nil)
dockerFile, err := _generateDockerfile(platform, cds, cert != nil)
if err != nil {
return nil, fmt.Errorf("Failed to generate a Dockerfile: %s", err)
}
Expand All @@ -215,8 +225,7 @@ func GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec) (io.Reader, error) {
go func() {
gw := gzip.NewWriter(output)
tw := tar.NewWriter(gw)

err := generateDockerBuild(platform, cds, inputFiles, tw)
err := _generateDockerBuild(platform, cds, inputFiles, tw)
if err != nil {
logger.Error(err)
}
Expand Down
Loading

0 comments on commit 6df0d50

Please sign in to comment.