From e5d43039133464066d98b98a1101e950f322fd28 Mon Sep 17 00:00:00 2001 From: Matthew Sykes Date: Wed, 11 Apr 2018 12:51:14 -0400 Subject: [PATCH] [FAB-9466] separate dev/test config from prod code 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 --- common/channelconfig/msp_test.go | 4 +- common/localmsp/signer_test.go | 17 +--- common/metrics/server_test.go | 4 +- .../tools/configtxgen/encoder/encoder_test.go | 4 +- .../platforms/golang/platform_test.go | 4 +- .../chaincode/platforms/node/platform_test.go | 4 +- core/chaincode/platforms/util/utils_test.go | 20 ++--- core/config/config.go | 83 ++----------------- core/config/config_test.go | 57 +------------ core/config/configtest/config.go | 72 ++++++++++++++++ core/config/configtest/config_test.go | 64 ++++++++++++++ core/ledger/testutil/test_util.go | 18 +--- core/testutil/config.go | 29 ++----- examples/ccchecker/init.go | 20 ++--- gossip/comm/comm_test.go | 4 +- gossip/discovery/discovery_test.go | 4 +- gossip/election/election_test.go | 4 +- gossip/gossip/algo/pull_test.go | 4 +- gossip/integration/integration_test.go | 4 +- msp/configbuilder_test.go | 20 ++--- msp/mgmt/deserializer_test.go | 18 +--- msp/mgmt/mgmt.go | 25 +----- msp/mgmt/mgmt_test.go | 18 +--- msp/mgmt/peermsp_test.go | 26 +++--- msp/mgmt/testtools/config.go | 28 +++---- msp/mgmt/testtools/config_test.go | 14 +--- msp/msp_test.go | 12 +-- orderer/common/server/main_test.go | 6 +- peer/common/common_test.go | 5 +- 29 files changed, 244 insertions(+), 348 deletions(-) create mode 100644 core/config/configtest/config.go create mode 100644 core/config/configtest/config_test.go diff --git a/common/channelconfig/msp_test.go b/common/channelconfig/msp_test.go index e7041a3eaef..3dceca6801f 100644 --- a/common/channelconfig/msp_test.go +++ b/common/channelconfig/msp_test.go @@ -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) diff --git a/common/localmsp/signer_test.go b/common/localmsp/signer_test.go index b7038f0120b..f2e16d22034 100644 --- a/common/localmsp/signer_test.go +++ b/common/localmsp/signer_test.go @@ -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 @@ -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) } diff --git a/common/metrics/server_test.go b/common/metrics/server_test.go index ee0f6f7107a..e36e7221e4f 100644 --- a/common/metrics/server_test.go +++ b/common/metrics/server_test.go @@ -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" ) @@ -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)) } diff --git a/common/tools/configtxgen/encoder/encoder_test.go b/common/tools/configtxgen/encoder/encoder_test.go index 266ad4fafa5..0a4840a687e 100644 --- a/common/tools/configtxgen/encoder/encoder_test.go +++ b/common/tools/configtxgen/encoder/encoder_test.go @@ -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" @@ -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)) diff --git a/core/chaincode/platforms/golang/platform_test.go b/core/chaincode/platforms/golang/platform_test.go index 4814ba7624a..f6046191ac2 100644 --- a/core/chaincode/platforms/golang/platform_test.go +++ b/core/chaincode/platforms/golang/platform_test.go @@ -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" @@ -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 { diff --git a/core/chaincode/platforms/node/platform_test.go b/core/chaincode/platforms/node/platform_test.go index 8a4d4dfd589..44c01cb1512 100644 --- a/core/chaincode/platforms/node/platform_test.go +++ b/core/chaincode/platforms/node/platform_test.go @@ -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" ) @@ -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 { diff --git a/core/chaincode/platforms/util/utils_test.go b/core/chaincode/platforms/util/utils_test.go index 17c459ec31e..5243f19b8ca 100644 --- a/core/chaincode/platforms/util/utils_test.go +++ b/core/chaincode/platforms/util/utils_test.go @@ -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 @@ -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" @@ -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") @@ -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 { diff --git a/core/config/config.go b/core/config/config.go index 7619b424220..f1e1002dd08 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -1,17 +1,8 @@ /* Copyright Greg Haskins 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 @@ -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) { @@ -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() //---------------------------------------------------------------------------------- @@ -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) @@ -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 -} diff --git a/core/config/config_test.go b/core/config/config_test.go index e46ecb766a0..726592106fb 100644 --- a/core/config/config_test.go +++ b/core/config/config_test.go @@ -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" @@ -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() @@ -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") -} diff --git a/core/config/configtest/config.go b/core/config/configtest/config.go new file mode 100644 index 00000000000..fcbb57c7f82 --- /dev/null +++ b/core/config/configtest/config.go @@ -0,0 +1,72 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package configtest + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/spf13/viper" +) + +// AddDevConfigPath adds the DevConfigDir to the viper path. +func AddDevConfigPath(v *viper.Viper) error { + devPath, err := GetDevConfigDir() + if err != nil { + return err + } + + if v != nil { + v.AddConfigPath(devPath) + } else { + viper.AddConfigPath(devPath) + } + + return nil +} + +func dirExists(path string) bool { + fi, err := os.Stat(path) + if err != nil { + return false + } + return fi.IsDir() +} + +// GetDevConfigDir gets the path to the default configuration that is +// maintained with the source tree. This should only be used in 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 gets the path to the sampleconfig/msp treethat is maintained +// with the source tree. This should only be used in a test/development +// context. +func GetDevMspDir() (string, error) { + devDir, err := GetDevConfigDir() + if err != nil { + return "", fmt.Errorf("Error obtaining DevConfigDir: %s", devDir) + } + + return filepath.Join(devDir, "msp"), nil +} diff --git a/core/config/configtest/config_test.go b/core/config/configtest/config_test.go new file mode 100644 index 00000000000..d5ee8bd8a77 --- /dev/null +++ b/core/config/configtest/config_test.go @@ -0,0 +1,64 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package configtest_test + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/hyperledger/fabric/core/config/configtest" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestConfig_AddDevConfigPath(t *testing.T) { + // Case 1: use viper instance to call AddDevConfigPath + v := viper.New() + err := configtest.AddDevConfigPath(v) + assert.NoError(t, err, "Error while adding dev config path to viper") + + // Case 2: default viper instance to call AddDevConfigPath + err = configtest.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 = configtest.AddDevConfigPath(v) + assert.Error(t, err, "GOPATH is empty, expected error from AddDevConfigPath") +} + +func TestConfig_GetDevMspDir(t *testing.T) { + // Success case + _, err := configtest.GetDevMspDir() + assert.NoError(t, err) + + // Error case: GOPATH is empty + gopath := os.Getenv("GOPATH") + os.Setenv("GOPATH", "") + defer os.Setenv("GOPATH", gopath) + _, err = configtest.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 = configtest.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 := configtest.GetDevConfigDir() + assert.Error(t, err, "GOPATH is empty, expected error from GetDevConfigDir") +} diff --git a/core/ledger/testutil/test_util.go b/core/ledger/testutil/test_util.go index 15d584c1fd9..9c1e8a501ae 100644 --- a/core/ledger/testutil/test_util.go +++ b/core/ledger/testutil/test_util.go @@ -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 testutil @@ -24,7 +14,7 @@ import ( "strings" "time" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/op/go-logging" "github.com/spf13/viper" ) @@ -75,7 +65,7 @@ func SetupCoreYAMLConfig() { 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)) } diff --git a/core/testutil/config.go b/core/testutil/config.go index 4e1d88ace42..a855a76b262 100644 --- a/core/testutil/config.go +++ b/core/testutil/config.go @@ -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 testutil @@ -27,22 +17,13 @@ import ( "github.com/spf13/viper" "github.com/hyperledger/fabric/bccsp/factory" - "github.com/hyperledger/fabric/core/config" - "github.com/hyperledger/fabric/msp" - "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/core/config/configtest" + "github.com/hyperledger/fabric/msp" ) -// Config the config wrapper structure -type Config struct { -} - var configLogger = flogging.MustGetLogger("config") -func init() { - -} - // SetupTestLogging setup the logging during test execution func SetupTestLogging() { level, err := logging.LogLevel(viper.GetString("logging.level")) @@ -69,7 +50,7 @@ func SetupTestConfig() { replacer := strings.NewReplacer(".", "_") viper.SetEnvKeyReplacer(replacer) viper.SetConfigName("core") // name of config file (without extension) - err := config.AddDevConfigPath(nil) + err := configtest.AddDevConfigPath(nil) if err != nil { panic(fmt.Errorf("Fatal error adding DevConfigPath: %s \n", err)) } diff --git a/examples/ccchecker/init.go b/examples/ccchecker/init.go index 8a20a945200..69c145e9fa8 100644 --- a/examples/ccchecker/init.go +++ b/examples/ccchecker/init.go @@ -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 main @@ -24,7 +14,7 @@ import ( "github.com/spf13/pflag" "github.com/spf13/viper" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/peer/common" ) @@ -45,7 +35,7 @@ func initCCCheckerParams(mainFlags *pflag.FlagSet) { //read yaml file from -y . Defaults to ../../peer func initYaml(mainFlags *pflag.FlagSet) { - defaultConfigPath, err := config.GetDevConfigDir() + defaultConfigPath, err := configtest.GetDevConfigDir() if err != nil { fmt.Printf("Fatal error when getting DevConfigDir: %s\n", err) os.Exit(2) @@ -72,7 +62,7 @@ func initYaml(mainFlags *pflag.FlagSet) { //initialize MSP from -m . Defaults to ../../sampleconfig/msp func initMSP(mainFlags *pflag.FlagSet) { - defaultMspDir, err := config.GetDevMspDir() + defaultMspDir, err := configtest.GetDevMspDir() if err != nil { panic(err.Error()) } diff --git a/gossip/comm/comm_test.go b/gossip/comm/comm_test.go index 59eece50350..cb64e6ff20a 100644 --- a/gossip/comm/comm_test.go +++ b/gossip/comm/comm_test.go @@ -21,7 +21,7 @@ import ( "time" "github.com/hyperledger/fabric/bccsp/factory" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/gossip/api" "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/identity" @@ -188,7 +188,7 @@ func handshaker(endpoint string, comm Comm, t *testing.T, connMutator msgMutator func TestViperConfig(t *testing.T) { viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - config.AddDevConfigPath(nil) + configtest.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/discovery/discovery_test.go b/gossip/discovery/discovery_test.go index 9b1b2a4289b..3686efbab78 100644 --- a/gossip/discovery/discovery_test.go +++ b/gossip/discovery/discovery_test.go @@ -20,7 +20,7 @@ import ( "testing" "time" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/util" proto "github.com/hyperledger/fabric/protos/gossip" @@ -877,7 +877,7 @@ func TestConfigFromFile(t *testing.T) { aliveExpirationCheckInterval = 0 * time.Second viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - config.AddDevConfigPath(nil) + configtest.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/election/election_test.go b/gossip/election/election_test.go index ec4c3369edd..21807c63d39 100644 --- a/gossip/election/election_test.go +++ b/gossip/election/election_test.go @@ -14,7 +14,7 @@ import ( "testing" "time" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/gossip/util" "github.com/spf13/viper" "github.com/stretchr/testify/assert" @@ -443,7 +443,7 @@ func TestConfigFromFile(t *testing.T) { viper.Reset() viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - config.AddDevConfigPath(nil) + configtest.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/gossip/algo/pull_test.go b/gossip/gossip/algo/pull_test.go index 3d2b2e0e5cb..3285c232170 100644 --- a/gossip/gossip/algo/pull_test.go +++ b/gossip/gossip/algo/pull_test.go @@ -15,7 +15,7 @@ import ( "testing" "time" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/gossip/util" "github.com/spf13/viper" "github.com/stretchr/testify/assert" @@ -562,7 +562,7 @@ func TestDefaultConfig(t *testing.T) { viper.Reset() viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - config.AddDevConfigPath(nil) + configtest.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/integration/integration_test.go b/gossip/integration/integration_test.go index afa6b78b45b..ae4fe442f0b 100644 --- a/gossip/integration/integration_test.go +++ b/gossip/integration/integration_test.go @@ -14,7 +14,7 @@ import ( "time" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/gossip/api" "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/util" @@ -73,7 +73,7 @@ func TestNewGossipCryptoService(t *testing.T) { func setupTestEnv() { viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - config.AddDevConfigPath(nil) + configtest.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/msp/configbuilder_test.go b/msp/configbuilder_test.go index 840df11a428..d5bc94e4e25 100644 --- a/msp/configbuilder_test.go +++ b/msp/configbuilder_test.go @@ -1,17 +1,7 @@ /* -Copyright IBM Corp. 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 msp @@ -22,12 +12,12 @@ import ( "path/filepath" "testing" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/stretchr/testify/assert" ) func TestGetLocalMspConfig(t *testing.T) { - mspDir, err := config.GetDevMspDir() + mspDir, err := configtest.GetDevMspDir() assert.NoError(t, err) _, err = GetLocalMspConfig(mspDir, nil, "SampleOrg") assert.NoError(t, err) @@ -50,7 +40,7 @@ func TestGetPemMaterialFromDirWithFile(t *testing.T) { } func TestGetPemMaterialFromDirWithSymlinks(t *testing.T) { - mspDir, err := config.GetDevMspDir() + mspDir, err := configtest.GetDevMspDir() assert.NoError(t, err) tempDir, err := ioutil.TempDir("", "fabric-msp-test") diff --git a/msp/mgmt/deserializer_test.go b/msp/mgmt/deserializer_test.go index 70a8ede2473..a3d912f37b1 100644 --- a/msp/mgmt/deserializer_test.go +++ b/msp/mgmt/deserializer_test.go @@ -1,17 +1,7 @@ /* -Copyright IBM Corp. 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 mgmt @@ -21,7 +11,7 @@ import ( "os" "testing" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/msp" "github.com/stretchr/testify/assert" ) @@ -68,7 +58,7 @@ func TestMspDeserializersManager_GetLocalDeserializer(t *testing.T) { func TestMain(m *testing.M) { - mspDir, err := config.GetDevMspDir() + mspDir, err := configtest.GetDevMspDir() if err != nil { fmt.Printf("Error getting DevMspDir: %s", err) os.Exit(-1) diff --git a/msp/mgmt/mgmt.go b/msp/mgmt/mgmt.go index c860c609613..195b6dcca9d 100644 --- a/msp/mgmt/mgmt.go +++ b/msp/mgmt/mgmt.go @@ -1,17 +1,7 @@ /* -Copyright IBM Corp. 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 mgmt @@ -22,7 +12,6 @@ import ( "github.com/hyperledger/fabric/bccsp/factory" "github.com/hyperledger/fabric/common/flogging" - "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/msp/cache" "github.com/pkg/errors" @@ -57,16 +46,6 @@ func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) er return GetLocalMSP().Setup(conf) } -// Loads the development local MSP for use in testing. Not valid for production/runtime context -func LoadDevMsp() error { - mspDir, err := config.GetDevMspDir() - if err != nil { - return err - } - - return LoadLocalMsp(mspDir, nil, "SampleOrg") -} - // FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE, // THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE // OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT; diff --git a/msp/mgmt/mgmt_test.go b/msp/mgmt/mgmt_test.go index 496bed7ca2d..d2c846e7b3e 100644 --- a/msp/mgmt/mgmt_test.go +++ b/msp/mgmt/mgmt_test.go @@ -1,17 +1,7 @@ /* -Copyright IBM Corp. 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 mgmt @@ -20,7 +10,7 @@ import ( "testing" "github.com/hyperledger/fabric/common/util" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/msp" "github.com/stretchr/testify/assert" ) @@ -123,7 +113,7 @@ func TestNewMSPMgmtMgr(t *testing.T) { } func LoadMSPSetupForTesting() error { - dir, err := config.GetDevMspDir() + dir, err := configtest.GetDevMspDir() if err != nil { return err } diff --git a/msp/mgmt/peermsp_test.go b/msp/mgmt/peermsp_test.go index 8995095dbea..f9b10b62473 100644 --- a/msp/mgmt/peermsp_test.go +++ b/msp/mgmt/peermsp_test.go @@ -1,26 +1,24 @@ /* -Copyright IBM Corp. 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 mgmt -import "testing" +import ( + "testing" + + "github.com/hyperledger/fabric/core/config/configtest" +) func TestLocalMSP(t *testing.T) { - err := LoadDevMsp() + mspDir, err := configtest.GetDevMspDir() + if err != nil { + t.Fatalf("GetDevMspDir failed, err %s", err) + } + err = LoadLocalMsp(mspDir, nil, "SampleOrg") if err != nil { t.Fatalf("LoadLocalMsp failed, err %s", err) } diff --git a/msp/mgmt/testtools/config.go b/msp/mgmt/testtools/config.go index 3d27ed403a7..db8a77829a2 100644 --- a/msp/mgmt/testtools/config.go +++ b/msp/mgmt/testtools/config.go @@ -1,24 +1,14 @@ /* -Copyright IBM Corp. 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 msptesttools import ( "github.com/hyperledger/fabric/common/util" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/msp/mgmt" ) @@ -26,7 +16,7 @@ import ( // LoadTestMSPSetup sets up the local MSP // and a chain MSP for the default chain func LoadMSPSetupForTesting() error { - dir, err := config.GetDevMspDir() + dir, err := configtest.GetDevMspDir() if err != nil { return err } @@ -47,3 +37,13 @@ func LoadMSPSetupForTesting() error { return nil } + +// Loads the development local MSP for use in testing. Not valid for production/runtime context +func LoadDevMsp() error { + mspDir, err := configtest.GetDevMspDir() + if err != nil { + return err + } + + return mgmt.LoadLocalMsp(mspDir, nil, "SampleOrg") +} diff --git a/msp/mgmt/testtools/config_test.go b/msp/mgmt/testtools/config_test.go index 19cbd514ae2..852c6bb7d02 100644 --- a/msp/mgmt/testtools/config_test.go +++ b/msp/mgmt/testtools/config_test.go @@ -1,17 +1,7 @@ /* -Copyright IBM Corp. 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 msptesttools diff --git a/msp/msp_test.go b/msp/msp_test.go index 9f3a20d9167..878d9301de8 100644 --- a/msp/msp_test.go +++ b/msp/msp_test.go @@ -24,7 +24,7 @@ import ( "github.com/hyperledger/fabric/bccsp" "github.com/hyperledger/fabric/bccsp/sw" "github.com/hyperledger/fabric/bccsp/utils" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/protos/msp" "github.com/stretchr/testify/assert" ) @@ -61,7 +61,7 @@ func TestMSPParsers(t *testing.T) { } func TestMSPSetupNoCryptoConf(t *testing.T) { - mspDir, err := config.GetDevMspDir() + mspDir, err := configtest.GetDevMspDir() if err != nil { fmt.Printf("Errog getting DevMspDir: %s", err) os.Exit(-1) @@ -152,7 +152,7 @@ func (*bccspNoKeyLookupKS) GetKey(ski []byte) (k bccsp.Key, err error) { } func TestNotFoundInBCCSP(t *testing.T) { - dir, err := config.GetDevMspDir() + dir, err := configtest.GetDevMspDir() assert.NoError(t, err) conf, err := GetLocalMspConfig(dir, nil, "SampleOrg") @@ -197,7 +197,7 @@ func TestDeserializeIdentityFails(t *testing.T) { } func TestGetSigningIdentityFromVerifyingMSP(t *testing.T) { - mspDir, err := config.GetDevMspDir() + mspDir, err := configtest.GetDevMspDir() if err != nil { fmt.Printf("Errog getting DevMspDir: %s", err) os.Exit(-1) @@ -930,7 +930,7 @@ var mspMgr MSPManager func TestMain(m *testing.M) { var err error - mspDir, err := config.GetDevMspDir() + mspDir, err := configtest.GetDevMspDir() if err != nil { fmt.Printf("Errog getting DevMspDir: %s", err) os.Exit(-1) @@ -995,7 +995,7 @@ func TestMain(m *testing.M) { } func getIdentity(t *testing.T, path string) Identity { - mspDir, err := config.GetDevMspDir() + mspDir, err := configtest.GetDevMspDir() assert.NoError(t, err) pems, err := getPemMaterialFromDir(filepath.Join(mspDir, path)) diff --git a/orderer/common/server/main_test.go b/orderer/common/server/main_test.go index 82e247af912..64bed15aafd 100644 --- a/orderer/common/server/main_test.go +++ b/orderer/common/server/main_test.go @@ -24,7 +24,7 @@ import ( "github.com/hyperledger/fabric/common/localmsp" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" "github.com/hyperledger/fabric/core/comm" - coreconfig "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/orderer/common/localconfig" "github.com/op/go-logging" "github.com/stretchr/testify/assert" @@ -198,7 +198,7 @@ func TestInitializeBootstrapChannel(t *testing.T) { func TestInitializeLocalMsp(t *testing.T) { t.Run("Happy", func(t *testing.T) { assert.NotPanics(t, func() { - localMSPDir, _ := coreconfig.GetDevMspDir() + localMSPDir, _ := configtest.GetDevMspDir() initializeLocalMsp( &config.TopLevel{ General: config.General{ @@ -339,7 +339,7 @@ func TestUpdateTrustedRoots(t *testing.T) { func genesisConfig(t *testing.T) *config.TopLevel { t.Helper() - localMSPDir, _ := coreconfig.GetDevMspDir() + localMSPDir, _ := configtest.GetDevMspDir() return &config.TopLevel{ General: config.General{ LedgerType: "ram", diff --git a/peer/common/common_test.go b/peer/common/common_test.go index 5091d6fdd2f..33b2b6eda66 100644 --- a/peer/common/common_test.go +++ b/peer/common/common_test.go @@ -13,7 +13,7 @@ import ( "testing" "github.com/hyperledger/fabric/common/util" - "github.com/hyperledger/fabric/core/config" + "github.com/hyperledger/fabric/core/config/configtest" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/peer/common" "github.com/spf13/viper" @@ -62,8 +62,7 @@ func TestInitCryptoMissingDir(t *testing.T) { } func TestInitCrypto(t *testing.T) { - - mspConfigPath, err := config.GetDevMspDir() + mspConfigPath, err := configtest.GetDevMspDir() localMspId := "SampleOrg" err = common.InitCrypto(mspConfigPath, localMspId, msp.ProviderTypeToString(msp.FABRIC)) assert.NoError(t, err, "Unexpected error [%s] calling InitCrypto()", err)