Skip to content

Commit

Permalink
[FAB-9466] separate dev/test config from prod code
Browse files Browse the repository at this point in the history
Remove fallback to fabric sample configuration on the GOPATH from the
core viper initialization and separate dev/test related config functions
from production code packages.

Change-Id: I053d0a3fb2d479aa57223cfe6ebcbadaa2f8f290
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed Apr 12, 2018
1 parent 752752f commit e5d4303
Show file tree
Hide file tree
Showing 29 changed files with 244 additions and 348 deletions.
4 changes: 2 additions & 2 deletions common/channelconfig/msp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ package channelconfig
import (
"testing"

"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/core/config/configtest"
"github.com/hyperledger/fabric/msp"
mspprotos "github.com/hyperledger/fabric/protos/msp"
"github.com/stretchr/testify/assert"
)

func TestMSPConfigManager(t *testing.T) {
mspDir, err := config.GetDevMspDir()
mspDir, err := configtest.GetDevMspDir()
assert.NoError(t, err)
conf, err := msp.GetLocalMspConfig(mspDir, nil, "SampleOrg")
assert.NoError(t, err)
Expand Down
17 changes: 4 additions & 13 deletions common/localmsp/signer_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package localmsp
Expand All @@ -22,11 +12,12 @@ import (

"github.com/hyperledger/fabric/common/crypto"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
msptesttools "github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
if err := mspmgmt.LoadDevMsp(); err != nil {
if err := msptesttools.LoadDevMsp(); err != nil {
os.Exit(-1)
}

Expand Down
4 changes: 2 additions & 2 deletions common/metrics/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/core/config/configtest"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -230,7 +230,7 @@ func setupTestConfig() {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

err := config.AddDevConfigPath(nil)
err := configtest.AddDevConfigPath(nil)
if err != nil {
panic(fmt.Errorf("Fatal error adding dev dir: %s \n", err))
}
Expand Down
4 changes: 2 additions & 2 deletions common/tools/configtxgen/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/localmsp"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
msptesttools "github.com/hyperledger/fabric/msp/mgmt/testtools"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestNegativeChannelCreateConfigUpdate(t *testing.T) {
func TestMakeChannelCreationTransactionWithSigner(t *testing.T) {
channelID := "foo"

mspmgmt.LoadDevMsp()
msptesttools.LoadDevMsp()
signer := localmsp.NewSigner()

cct, err := MakeChannelCreationTransaction(channelID, signer, nil, genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile))
Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/platforms/golang/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"
"time"

"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/core/config/configtest"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestGenerateDockerBuild(t *testing.T) {
func TestMain(m *testing.M) {
viper.SetConfigName("core")
viper.SetEnvPrefix("CORE")
config.AddDevConfigPath(nil)
configtest.AddDevConfigPath(nil)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/platforms/node/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"
"testing"

"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/core/config/configtest"
"github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -290,7 +290,7 @@ func writeFileToPackage(localpath string, packagepath string, tw *tar.Writer, mo
func TestMain(m *testing.M) {
viper.SetConfigName("core")
viper.SetEnvPrefix("CORE")
config.AddDevConfigPath(nil)
configtest.AddDevConfigPath(nil)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
Expand Down
20 changes: 5 additions & 15 deletions core/chaincode/platforms/util/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package util
Expand All @@ -31,7 +21,7 @@ import (
"time"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/core/config/configtest"
cutil "github.com/hyperledger/fabric/core/container/util"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -246,7 +236,7 @@ func TestHashBadWriter(t *testing.T) {
"HashFilesInDir invoked with closed writer, should have failed")
}

// TestHashNonExistentDir tests HashFilesInDir with non existant directory
// TestHashNonExistentDir tests HashFilesInDir with non existent directory
func TestHashNonExistentDir(t *testing.T) {
if testing.Short() {
t.Skip("Skipping TestHashNonExistentDir")
Expand Down Expand Up @@ -375,7 +365,7 @@ func TestDockerPull(t *testing.T) {
func TestMain(m *testing.M) {
viper.SetConfigName("core")
viper.SetEnvPrefix("CORE")
config.AddDevConfigPath(nil)
configtest.AddDevConfigPath(nil)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
Expand Down
83 changes: 7 additions & 76 deletions core/config/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
/*
Copyright Greg Haskins <gregory.haskins@gmail.com> 2017, All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package config
Expand All @@ -25,8 +16,11 @@ import (
)

func dirExists(path string) bool {
_, err := os.Stat(path)
return err == nil
fi, err := os.Stat(path)
if err != nil {
return false
}
return fi.IsDir()
}

func addConfigPath(v *viper.Viper, p string) {
Expand All @@ -37,48 +31,6 @@ func addConfigPath(v *viper.Viper, p string) {
}
}

//----------------------------------------------------------------------------------
// GetDevConfigDir()
//----------------------------------------------------------------------------------
// Returns the path to the default configuration that is maintained with the source
// tree. Only valid to call from a test/development context.
//----------------------------------------------------------------------------------
func GetDevConfigDir() (string, error) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
return "", fmt.Errorf("GOPATH not set")
}

for _, p := range filepath.SplitList(gopath) {
devPath := filepath.Join(p, "src/github.com/hyperledger/fabric/sampleconfig")
if !dirExists(devPath) {
continue
}

return devPath, nil
}

return "", fmt.Errorf("DevConfigDir not found in %s", gopath)
}

//----------------------------------------------------------------------------------
// GetDevMspDir()
//----------------------------------------------------------------------------------
// Builds upon GetDevConfigDir to return the path to our sampleconfig/msp that is
// maintained with the source tree. Only valid to call from a test/development
// context. Runtime environment should use configuration elements such as
//
// GetPath("peer.mspConfigDir")
//----------------------------------------------------------------------------------
func GetDevMspDir() (string, error) {
devDir, err := GetDevConfigDir()
if err != nil {
return "", fmt.Errorf("Error obtaining DevConfigDir: %s", devDir)
}

return filepath.Join(devDir, "msp"), nil
}

//----------------------------------------------------------------------------------
// TranslatePath()
//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -150,16 +102,11 @@ func InitViper(v *viper.Viper, configName string) error {
// If we get here, we should use the default paths in priority order:
//
// *) CWD
// *) The $GOPATH based development tree
// *) /etc/hyperledger/fabric
//

// CWD
addConfigPath(v, "./")

// DevConfigPath
AddDevConfigPath(v)

// And finally, the official path
if dirExists(OfficialPath) {
addConfigPath(v, OfficialPath)
Expand All @@ -175,19 +122,3 @@ func InitViper(v *viper.Viper, configName string) error {

return nil
}

//----------------------------------------------------------------------------------
// AddDevConfigPath()
//----------------------------------------------------------------------------------
// Helper utility that automatically adds our DevConfigDir to the viper path
//----------------------------------------------------------------------------------
func AddDevConfigPath(v *viper.Viper) error {
devPath, err := GetDevConfigDir()
if err != nil {
return err
}

addConfigPath(v, devPath)

return nil
}
57 changes: 4 additions & 53 deletions core/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*
*/
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package config

import (
"io/ioutil"
"os"
"testing"
"time"
Expand All @@ -30,24 +28,6 @@ func TestConfig_dirExists(t *testing.T) {
tmpF)
}

func TestConfig_AddDevConfigPath(t *testing.T) {
// Case 1: use viper instance to call AddDevConfigPath
v := viper.New()
err := AddDevConfigPath(v)
assert.NoError(t, err, "Error while adding dev config path to viper")

// Case 2: default viper instance to call AddDevConfigPath
err = AddDevConfigPath(nil)
assert.NoError(t, err, "Error while adding dev config path to default viper")

// Error case: GOPATH is empty
gopath := os.Getenv("GOPATH")
os.Setenv("GOPATH", "")
defer os.Setenv("GOPATH", gopath)
err = AddDevConfigPath(v)
assert.Error(t, err, "GOPATH is empty, expected error from AddDevConfigPath")
}

func TestConfig_InitViper(t *testing.T) {
// Case 1: use viper instance to call InitViper
v := viper.New()
Expand Down Expand Up @@ -81,32 +61,3 @@ func TestConfig_TranslatePathInPlace(t *testing.T) {
TranslatePathInPlace(OfficialPath, &p)
assert.Equal(t, "/foo", p, "TranslatePathInPlace failed to translate path %s", p)
}

func TestConfig_GetDevMspDir(t *testing.T) {
// Success case
_, err := GetDevMspDir()
assert.NoError(t, err)

// Error case: GOPATH is empty
gopath := os.Getenv("GOPATH")
os.Setenv("GOPATH", "")
defer os.Setenv("GOPATH", gopath)
_, err = GetDevMspDir()
assert.Error(t, err, "GOPATH is empty, expected error from GetDevMspDir")

// Error case: GOPATH is set to temp dir
dir, err1 := ioutil.TempDir("/tmp", "devmspdir")
assert.NoError(t, err1)
defer os.RemoveAll(dir)
os.Setenv("GOPATH", dir)
_, err = GetDevMspDir()
assert.Error(t, err, "GOPATH is set to temp dir, expected error from GetDevMspDir")
}

func TestConfig_GetDevConfigDir(t *testing.T) {
gopath := os.Getenv("GOPATH")
os.Setenv("GOPATH", "")
defer os.Setenv("GOPATH", gopath)
_, err := GetDevConfigDir()
assert.Error(t, err, "GOPATH is empty, expected error from GetDevConfigDir")
}
Loading

0 comments on commit e5d4303

Please sign in to comment.