Skip to content

Commit

Permalink
Merge "[FAB-4855]Print error if MSP config folder missing"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed Jun 19, 2017
2 parents b10b124 + 1cc0e2c commit f20846c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
17 changes: 13 additions & 4 deletions peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package common

import (
"fmt"
"os"

"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/configtx"
Expand All @@ -33,7 +34,7 @@ import (
pcommon "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
logging "github.com/op/go-logging"
"github.com/op/go-logging"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -85,16 +86,24 @@ func InitConfig(cmdRoot string) error {

//InitCrypto initializes crypto for this peer
func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
var err error
// Check whenever msp folder exists
_, err = os.Stat(mspMgrConfigDir)
if os.IsNotExist(err) {
// No need to try to load MSP from folder which is not available
return fmt.Errorf("cannot init crypto, missing %s folder", mspMgrConfigDir)
}

// Init the BCCSP
var bccspConfig *factory.FactoryOpts
err := viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
err = viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
if err != nil {
return fmt.Errorf("Could not parse YAML config [%s]", err)
return fmt.Errorf("could not parse YAML config [%s]", err)
}

err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
if err != nil {
return fmt.Errorf("Error when setting up MSP from directory %s: err %s", mspMgrConfigDir, err)
return fmt.Errorf("error when setting up MSP from directory %s: err %s", mspMgrConfigDir, err)
}

return nil
Expand Down
10 changes: 10 additions & 0 deletions peer/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ SPDX-License-Identifier: Apache-2.0
package common_test

import (
"fmt"
"os"
"testing"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/peer/common"
Expand Down Expand Up @@ -50,6 +53,13 @@ func TestInitConfig(t *testing.T) {
}
}

func TestINitCryptoMissingDir(t *testing.T) {
dir := os.TempDir() + "/" + util.GenerateUUID()
err := common.InitCrypto(dir, "DEFAULT")
assert.Error(t, err, "Should be able to initialize crypto with non-existing directory")
assert.Contains(t, err.Error(), fmt.Sprintf("missing %s folder", dir))
}

func TestInitCrypto(t *testing.T) {

mspConfigPath, err := config.GetDevMspDir()
Expand Down
3 changes: 2 additions & 1 deletion peer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func main() {
var mspID = viper.GetString("peer.localMspId")
err = common.InitCrypto(mspMgrConfigDir, mspID)
if err != nil { // Handle errors reading the config file
panic(err.Error())
logger.Errorf("Cannot run peer because %s", err.Error())
os.Exit(1)
}
// On failure Cobra prints the usage message and error string, so we only
// need to exit with a non-0 status
Expand Down

0 comments on commit f20846c

Please sign in to comment.