diff --git a/api/apiconfig/configprovider.go b/api/apiconfig/configprovider.go index 3e2d9ecdf3..4a3eb5b2ae 100644 --- a/api/apiconfig/configprovider.go +++ b/api/apiconfig/configprovider.go @@ -52,6 +52,9 @@ type Config interface { TLSClientCerts() ([]tls.Certificate, error) } +// ConfigProvider enables creation of a Config instance +type ConfigProvider func() (Config, error) + // TimeoutType enumerates the different types of outgoing connections type TimeoutType int diff --git a/def/fabapi/deprecated.go b/def/fabapi/deprecated.go index 3a12de0ef2..97a9df5386 100644 --- a/def/fabapi/deprecated.go +++ b/def/fabapi/deprecated.go @@ -45,7 +45,7 @@ type StateStoreOpts struct { Path string } -func configFromOptions(options *Options) (apiconfig.Config, error) { +func configFromOptions(options *Options) apiconfig.ConfigProvider { if options.ConfigByte != nil { return config.FromRaw(options.ConfigByte, options.ConfigType) } @@ -54,19 +54,15 @@ func configFromOptions(options *Options) (apiconfig.Config, error) { return config.FromFile(options.ConfigFile) } - return nil, errors.New("No configuration provided") + return func() (apiconfig.Config, error) { + return nil, errors.New("No configuration provided") + } } // NewSDK wraps the NewSDK func moved to the pkg folder. // Notice: this wrapper is deprecated and will be removed. func NewSDK(options Options) (*fabsdk.FabricSDK, error) { - configProvider, err := configFromOptions(&options) - if err != nil { - return nil, err - } - - sdk, err := fabsdk.New( - configProvider, + sdk, err := fabsdk.New(configFromOptions(&options), sdkOptionsFromDeprecatedOptions(options)...) if err != nil { diff --git a/def/factory/defclient/orgfactory_test.go b/def/factory/defclient/orgfactory_test.go index 0e53e7763c..82aa5ff6e5 100644 --- a/def/factory/defclient/orgfactory_test.go +++ b/def/factory/defclient/orgfactory_test.go @@ -41,7 +41,7 @@ func TestNewMSPClient(t *testing.T) { func TestNewCredentialManager(t *testing.T) { factory := NewOrgClientFactory() - config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml") + config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatalf(err.Error()) } diff --git a/def/factory/defclient/sessfactory_test.go b/def/factory/defclient/sessfactory_test.go index f0a283f319..d3b7864fb8 100644 --- a/def/factory/defclient/sessfactory_test.go +++ b/def/factory/defclient/sessfactory_test.go @@ -17,6 +17,7 @@ import ( "github.com/hyperledger/fabric-sdk-go/api/apicryptosuite" "github.com/hyperledger/fabric-sdk-go/api/apifabclient" "github.com/hyperledger/fabric-sdk-go/def/factory/defcore" + "github.com/hyperledger/fabric-sdk-go/pkg/config" "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/channel" fabmocks "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks" chImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/chclient" @@ -187,10 +188,7 @@ func newMockProviders(t *testing.T) *mockProviders { coreFactory := defcore.NewProviderFactory() svcFactory := defsvc.NewProviderFactory() - opts := defcore.ConfigOpts{ - FileName: "../../../test/fixtures/config/config_test.yaml", - } - config, err := coreFactory.NewConfigProvider(opts) + config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatalf(err.Error()) } diff --git a/def/factory/defcore/corefactory.go b/def/factory/defcore/corefactory.go index 17abf8e861..4e40849727 100644 --- a/def/factory/defcore/corefactory.go +++ b/def/factory/defcore/corefactory.go @@ -14,7 +14,6 @@ import ( "github.com/hyperledger/fabric-sdk-go/api/apilogging" "github.com/hyperledger/fabric-sdk-go/def/provider/fabpvdr" - configImpl "github.com/hyperledger/fabric-sdk-go/pkg/config" cryptosuiteimpl "github.com/hyperledger/fabric-sdk-go/pkg/cryptosuite/bccsp/sw" "github.com/hyperledger/fabric-sdk-go/pkg/errors" kvs "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/keyvaluestore" @@ -35,26 +34,6 @@ func NewProviderFactory() *ProviderFactory { return &f } -// ConfigOpts provides bootstrap setup -type ConfigOpts struct { - //FileName to load from a predefined path - FileName string - //Raw to load from an bytes array - Raw []byte - //Format to specify the type of the config (mainly used with ConfigBytes as ConfigFile has a file extension to specify the type) - // valid values: yaml, json, etc. - Format string -} - -// NewConfigProvider creates a Config using the SDK's default implementation -func (f *ProviderFactory) NewConfigProvider(opts ConfigOpts) (apiconfig.Config, error) { - // configBytes takes precedence over configFile - if opts.Raw != nil && len(opts.Raw) > 0 { - return configImpl.FromRaw(opts.Raw, opts.Format) - } - return configImpl.FromFile(opts.FileName) -} - // NewStateStoreProvider creates a KeyValueStore using the SDK's default implementation func (f *ProviderFactory) NewStateStoreProvider(config apiconfig.Config) (fab.KeyValueStore, error) { diff --git a/def/factory/defcore/corefactory_test.go b/def/factory/defcore/corefactory_test.go index 0e1e31ede6..f067ae7f27 100644 --- a/def/factory/defcore/corefactory_test.go +++ b/def/factory/defcore/corefactory_test.go @@ -15,7 +15,6 @@ import ( "github.com/hyperledger/fabric-sdk-go/api/apiconfig/mocks" "github.com/hyperledger/fabric-sdk-go/api/apifabclient" "github.com/hyperledger/fabric-sdk-go/def/provider/fabpvdr" - configImpl "github.com/hyperledger/fabric-sdk-go/pkg/config" cryptosuitewrapper "github.com/hyperledger/fabric-sdk-go/pkg/cryptosuite/bccsp/wrapper" kvs "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/keyvaluestore" "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks" @@ -23,24 +22,6 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/logging/modlog" ) -func TestNewConfigProvider(t *testing.T) { - factory := NewProviderFactory() - - opts := ConfigOpts{ - FileName: "../../../test/fixtures/config/config_test.yaml", - } - - config, err := factory.NewConfigProvider(opts) - if err != nil { - t.Fatalf("Unexpected error creating config provider %v", err) - } - - _, ok := config.(*configImpl.Config) - if !ok { - t.Fatalf("Unexpected config provider created") - } -} - func TestNewStateStoreProvider(t *testing.T) { factory := NewProviderFactory() diff --git a/pkg/config/config.go b/pkg/config/config.go index 382ba35edb..0450d50d5f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -61,56 +61,55 @@ type Option func(opts *options) error // FromReader loads configuration from in. // configType can be "json" or "yaml". -func FromReader(in io.Reader, configType string, opts ...Option) (*Config, error) { - c, err := newConfig(opts...) - if err != nil { - return nil, err - } +func FromReader(in io.Reader, configType string, opts ...Option) apiconfig.ConfigProvider { + return func() (apiconfig.Config, error) { + c, err := newConfig(opts...) + if err != nil { + return nil, err + } - if configType == "" { - return nil, errors.New("empty config type") - } + if configType == "" { + return nil, errors.New("empty config type") + } - // read config from bytes array, but must set ConfigType - // for viper to properly unmarshal the bytes array - c.configViper.SetConfigType(configType) - c.configViper.MergeConfig(in) + // read config from bytes array, but must set ConfigType + // for viper to properly unmarshal the bytes array + c.configViper.SetConfigType(configType) + c.configViper.MergeConfig(in) - return initConfig(c) + return initConfig(c) + } } // FromFile reads from named config file -func FromFile(name string, opts ...Option) (*Config, error) { - c, err := newConfig(opts...) - if err != nil { - return nil, err - } +func FromFile(name string, opts ...Option) apiconfig.ConfigProvider { + return func() (apiconfig.Config, error) { + c, err := newConfig(opts...) + if err != nil { + return nil, err + } - if name == "" { - return nil, errors.New("filename is required") - } + if name == "" { + return nil, errors.New("filename is required") + } - // create new viper - c.configViper.SetConfigFile(name) + // create new viper + c.configViper.SetConfigFile(name) - // If a config file is found, read it in. - err = c.configViper.MergeInConfig() - if err == nil { - logger.Debugf("Using config file: %s", c.configViper.ConfigFileUsed()) - } else { - return nil, errors.Wrap(err, "loading config file failed") - } + // If a config file is found, read it in. + err = c.configViper.MergeInConfig() + if err == nil { + logger.Debugf("Using config file: %s", c.configViper.ConfigFileUsed()) + } else { + return nil, errors.Wrap(err, "loading config file failed") + } - return initConfig(c) + return initConfig(c) + } } // FromRaw will initialize the configs from a byte array -func FromRaw(configBytes []byte, configType string, opts ...Option) (*Config, error) { - - if len(configBytes) == 0 { - return nil, errors.New("empty config byte array") - } - +func FromRaw(configBytes []byte, configType string, opts ...Option) apiconfig.ConfigProvider { buf := bytes.NewBuffer(configBytes) logger.Debugf("config.FromRaw buf Len is %d, Cap is %d: %s", buf.Len(), buf.Cap(), buf) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 4d92945b61..70ceedf2d3 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -179,7 +179,11 @@ func TestCAConfig(t *testing.T) { func TestCAConfigFailsByNetworkConfig(t *testing.T) { //Tamper 'client.network' value and use a new config to avoid conflicting with other tests - sampleConfig, err := FromFile(configTestFilePath) + configProvider, err := FromFile(configTestFilePath)() + if err != nil { + t.Fatalf("Unexpected error reading config: %v", err) + } + sampleConfig := configProvider.(*Config) sampleConfig.configViper.Set("client", "INVALID") sampleConfig.configViper.Set("peers", "INVALID") sampleConfig.configViper.Set("organizations", "INVALID") @@ -314,7 +318,7 @@ func TestTLSCAConfig(t *testing.T) { } func TestTLSCAConfigFromPems(t *testing.T) { - c, err := FromFile(configEmbeddedUsersTestFilePath) + c, err := FromFile(configEmbeddedUsersTestFilePath)() if err != nil { t.Fatal(err) } @@ -517,7 +521,7 @@ func TestFromRawSuccess(t *testing.T) { cBytes, err := loadConfigBytesFromFile(t, configTestFilePath) // test init config from bytes - _, err = FromRaw(cBytes, configType) + _, err = FromRaw(cBytes, configType)() if err != nil { t.Fatalf("Failed to initialize config from bytes array. Error: %s", err) } @@ -529,14 +533,14 @@ func TestFromReaderSuccess(t *testing.T) { buf := bytes.NewBuffer(cBytes) // test init config from bytes - _, err = FromReader(buf, configType) + _, err = FromReader(buf, configType)() if err != nil { t.Fatalf("Failed to initialize config from bytes array. Error: %s", err) } } func TestFromFileEmptyFilename(t *testing.T) { - _, err := FromFile("") + _, err := FromFile("")() if err == nil { t.Fatalf("Expected error when passing empty string to FromFile") } @@ -565,23 +569,10 @@ func loadConfigBytesFromFile(t *testing.T, filePath string) ([]byte, error) { return cBytes, err } -func TestInitConfigFromRawEmpty(t *testing.T) { - // test init config from an empty bytes - _, err := FromRaw([]byte{}, configType) - if err == nil { - t.Fatalf("Expected to fail initialize config with empty bytes array.") - } - // test init config with an empty configType - _, err = FromRaw([]byte("test config"), "") - if err == nil { - t.Fatalf("Expected to fail initialize config with empty config type.") - } -} - func TestInitConfigSuccess(t *testing.T) { //Test init config //...Positive case - _, err := FromFile(configTestFilePath) + _, err := FromFile(configTestFilePath)() if err != nil { t.Fatalf("Failed to initialize config. Error: %s", err) } @@ -595,11 +586,13 @@ func TestInitConfigWithCmdRoot(t *testing.T) { logger.Infof("fileLoc is %s", fileLoc) logger.Infof("fileLoc right before calling InitConfigWithCmdRoot is %s", fileLoc) - config, err := FromFile(fileLoc, WithEnvPrefix(cmdRoot)) + configProvider, err := FromFile(fileLoc, WithEnvPrefix(cmdRoot))() if err != nil { t.Fatalf("Failed to initialize config with cmd root. Error: %s", err) } + config := configProvider.(*Config) + //Test if Viper is initialized after calling init config if config.configViper.GetString("client.BCCSP.security.hashAlgorithm") != configImpl.SecurityAlgorithm() { t.Fatal("Config initialized with incorrect viper configuration") @@ -620,12 +613,12 @@ func TestInitConfigPanic(t *testing.T) { } }() - FromFile(configTestFilePath) + FromFile(configTestFilePath)() } func TestInitConfigInvalidLocation(t *testing.T) { //...Negative case - _, err := FromFile("invalid file location") + _, err := FromFile("invalid file location")() if err == nil { t.Fatalf("Config file initialization is supposed to fail. Error: %s", err) } @@ -645,11 +638,13 @@ func TestMultipleVipers(t *testing.T) { t.Fatalf("Expected testValue before config initialization got: %s", testValue1) } // initialize go sdk - config, err := FromFile(configTestFilePath) + configProvider, err := FromFile(configTestFilePath)() if err != nil { t.Log(err.Error()) } + config := configProvider.(*Config) + // Make sure initial value is unaffected testValue2 := viper.GetString("test.testkey") if testValue2 != "testvalue" { @@ -694,11 +689,12 @@ func TestEnvironmentVariablesSpecificCmdRoot(t *testing.T) { t.Log(err.Error()) } - config, err := FromFile(configTestFilePath, WithEnvPrefix("test_root")) + configProvider, err := FromFile(configTestFilePath, WithEnvPrefix("test_root"))() if err != nil { t.Log(err.Error()) } + config := configProvider.(*Config) testValue = config.configViper.GetString("env.test") if testValue != "456" { t.Fatalf("Expected environment variable value but got: %s", testValue) @@ -732,10 +728,11 @@ func TestMain(m *testing.M) { func setUp(m *testing.M) { // do any test setup here... var err error - configImpl, err = FromFile(configTestFilePath) + configProvider, err := FromFile(configTestFilePath)() if err != nil { fmt.Println(err.Error()) } + configImpl = configProvider.(*Config) } func teardown() { @@ -763,11 +760,13 @@ func TestInterfaces(t *testing.T) { func TestSystemCertPoolDisabled(t *testing.T) { // get a config file with pool disabled - c, err := FromFile(configTestFilePath) + configProvider, err := FromFile(configTestFilePath)() if err != nil { t.Fatal(err) } + c := configProvider.(*Config) + // cert pool should be empty if len(c.tlsCertPool.Subjects()) > 0 { t.Fatal("Expecting empty tls cert pool due to disabled system cert pool") @@ -777,11 +776,13 @@ func TestSystemCertPoolDisabled(t *testing.T) { func TestSystemCertPoolEnabled(t *testing.T) { // get a config file with pool enabled - c, err := FromFile(configPemTestFilePath) + configProvider, err := FromFile(configPemTestFilePath)() if err != nil { t.Fatal(err) } + c := configProvider.(*Config) + if len(c.tlsCertPool.Subjects()) == 0 { t.Fatal("System Cert Pool not loaded even though it is enabled") } @@ -807,7 +808,7 @@ func TestInitConfigFromRawWithPem(t *testing.T) { } // test init config from bytes - c, err := FromRaw(cBytes, configType) + c, err := FromRaw(cBytes, configType)() if err != nil { t.Fatalf("Failed to initialize config from bytes array. Error: %s", err) } @@ -921,7 +922,7 @@ O94CDp7l2k7hMQI0zQ== func TestLoadConfigWithEmbeddedUsersWithPems(t *testing.T) { // get a config file with embedded users - c, err := FromFile(configEmbeddedUsersTestFilePath) + c, err := FromFile(configEmbeddedUsersTestFilePath)() if err != nil { t.Fatal(err) } @@ -951,7 +952,7 @@ func TestLoadConfigWithEmbeddedUsersWithPems(t *testing.T) { func TestLoadConfigWithEmbeddedUsersWithPaths(t *testing.T) { // get a config file with embedded users - c, err := FromFile(configEmbeddedUsersTestFilePath) + c, err := FromFile(configEmbeddedUsersTestFilePath)() if err != nil { t.Fatal(err) } @@ -987,13 +988,13 @@ func TestInitConfigFromRawWrongType(t *testing.T) { } // test init config with empty type - c, err := FromRaw(cBytes, "") + c, err := FromRaw(cBytes, "")() if err == nil { t.Fatalf("Expected error when initializing config with wrong config type but got no error.") } // test init config with wrong type - c, err = FromRaw(cBytes, "json") + c, err = FromRaw(cBytes, "json")() if err != nil { t.Fatalf("Failed to initialize config from bytes array. Error: %s", err) } @@ -1234,7 +1235,7 @@ func TestTLSClientCertsNoCerts(t *testing.T) { } func TestNewGoodOpt(t *testing.T) { - _, err := FromFile("../../test/fixtures/config/config_test.yaml", goodOpt()) + _, err := FromFile("../../test/fixtures/config/config_test.yaml", goodOpt())() if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -1246,12 +1247,12 @@ func TestNewGoodOpt(t *testing.T) { buf := bytes.NewBuffer(cBytes) - _, err = FromReader(buf, configType, goodOpt()) + _, err = FromReader(buf, configType, goodOpt())() if err != nil { t.Fatalf("Unexpected error from FromReader: %v", err) } - _, err = FromRaw(cBytes, configType, goodOpt()) + _, err = FromRaw(cBytes, configType, goodOpt())() if err != nil { t.Fatalf("Unexpected error from FromRaw %v", err) } @@ -1277,7 +1278,7 @@ func goodOpt() Option { } func TestNewBadOpt(t *testing.T) { - _, err := FromFile("../../test/fixtures/config/config_test.yaml", badOpt()) + _, err := FromFile("../../test/fixtures/config/config_test.yaml", badOpt())() if err == nil { t.Fatalf("Expected error from FromFile") } @@ -1289,12 +1290,12 @@ func TestNewBadOpt(t *testing.T) { buf := bytes.NewBuffer(cBytes) - _, err = FromReader(buf, configType, badOpt()) + _, err = FromReader(buf, configType, badOpt())() if err == nil { t.Fatalf("Expected error from FromReader") } - _, err = FromRaw(cBytes, configType, badOpt()) + _, err = FromRaw(cBytes, configType, badOpt())() if err == nil { t.Fatalf("Expected error from FromRaw") } diff --git a/pkg/fabric-client/credentialmgr/credentialmgr_test.go b/pkg/fabric-client/credentialmgr/credentialmgr_test.go index a2382a5edd..e920d42a8a 100644 --- a/pkg/fabric-client/credentialmgr/credentialmgr_test.go +++ b/pkg/fabric-client/credentialmgr/credentialmgr_test.go @@ -15,7 +15,7 @@ import ( func TestCredentialManager(t *testing.T) { - config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml") + config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatalf(err.Error()) } @@ -44,7 +44,7 @@ func TestCredentialManager(t *testing.T) { func TestInvalidOrgCredentialManager(t *testing.T) { - config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml") + config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatalf(err.Error()) } @@ -68,7 +68,7 @@ func TestInvalidOrgCredentialManager(t *testing.T) { } func TestCredentialManagerFromEmbeddedCryptoConfig(t *testing.T) { - config, err := config.FromFile("../../../test/fixtures/config/config_test_embedded_pems.yaml") + config, err := config.FromFile("../../../test/fixtures/config/config_test_embedded_pems.yaml")() if err != nil { t.Fatalf(err.Error()) diff --git a/pkg/fabric-txn/chmgmtclient/chmgmt_test.go b/pkg/fabric-txn/chmgmtclient/chmgmt_test.go index 164316ec69..e603a75d59 100644 --- a/pkg/fabric-txn/chmgmtclient/chmgmt_test.go +++ b/pkg/fabric-txn/chmgmtclient/chmgmt_test.go @@ -9,6 +9,7 @@ package chmgmtclient import ( "testing" + "github.com/hyperledger/fabric-sdk-go/api/apiconfig" "github.com/hyperledger/fabric-sdk-go/api/apitxn/chmgmtclient" "github.com/hyperledger/fabric-sdk-go/pkg/config" @@ -139,8 +140,8 @@ func setupTestClient() *fcmocks.MockClient { return client } -func getNetworkConfig(t *testing.T) *config.Config { - config, err := config.FromFile(networkCfg) +func getNetworkConfig(t *testing.T) apiconfig.Config { + config, err := config.FromFile(networkCfg)() if err != nil { t.Fatal(err) } diff --git a/pkg/fabric-txn/discovery/staticdiscovery/staticdiscovery_test.go b/pkg/fabric-txn/discovery/staticdiscovery/staticdiscovery_test.go index 635a126393..7ad62bd4f2 100644 --- a/pkg/fabric-txn/discovery/staticdiscovery/staticdiscovery_test.go +++ b/pkg/fabric-txn/discovery/staticdiscovery/staticdiscovery_test.go @@ -14,7 +14,7 @@ import ( func TestStaticDiscovery(t *testing.T) { - config, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml") + config, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatalf(err.Error()) } diff --git a/pkg/fabric-txn/resmgmtclient/resmgmt_test.go b/pkg/fabric-txn/resmgmtclient/resmgmt_test.go index 8d10c2feee..fe0687ea2d 100644 --- a/pkg/fabric-txn/resmgmtclient/resmgmt_test.go +++ b/pkg/fabric-txn/resmgmtclient/resmgmt_test.go @@ -15,6 +15,7 @@ import ( "google.golang.org/grpc" + "github.com/hyperledger/fabric-sdk-go/api/apiconfig" fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient" resmgmt "github.com/hyperledger/fabric-sdk-go/api/apitxn/resmgmtclient" "github.com/hyperledger/fabric-sdk-go/pkg/errors" @@ -210,7 +211,7 @@ func TestJoinChannelNoOrdererConfig(t *testing.T) { client := setupTestClient("test", "Org1MSP") // No channel orderer, no global orderer - noOrdererConfig, err := config.FromFile("./testdata/noorderer_test.yaml") + noOrdererConfig, err := config.FromFile("./testdata/noorderer_test.yaml")() if err != nil { t.Fatal(err) } @@ -222,7 +223,7 @@ func TestJoinChannelNoOrdererConfig(t *testing.T) { } // Misconfigured channel orderer - invalidChOrdererConfig, err := config.FromFile("./testdata/invalidchorderer_test.yaml") + invalidChOrdererConfig, err := config.FromFile("./testdata/invalidchorderer_test.yaml")() if err != nil { t.Fatal(err) } @@ -234,7 +235,7 @@ func TestJoinChannelNoOrdererConfig(t *testing.T) { } // Misconfigured global orderer (cert cannot be loaded) - invalidOrdererConfig, err := config.FromFile("./testdata/invalidorderer_test.yaml") + invalidOrdererConfig, err := config.FromFile("./testdata/invalidorderer_test.yaml")() if err != nil { t.Fatal(err) } @@ -921,7 +922,7 @@ func TestCCProposal(t *testing.T) { client.SetChannel("mychannel", channel) // Setup resource management client - cfg, err := config.FromFile("./testdata/ccproposal_test.yaml") + cfg, err := config.FromFile("./testdata/ccproposal_test.yaml")() if err != nil { t.Fatal(err) } @@ -989,7 +990,7 @@ func TestCCProposal(t *testing.T) { } // Test no event source in config - cfg, err = config.FromFile("./testdata/event_source_missing_test.yaml") + cfg, err = config.FromFile("./testdata/event_source_missing_test.yaml")() if err != nil { t.Fatal(err) } @@ -1017,8 +1018,8 @@ func setupTestDiscovery(discErr error, peers []fab.Peer) (fab.DiscoveryProvider, return mockDiscovery, nil } -func getNetworkConfig(t *testing.T) *config.Config { - config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml") +func getNetworkConfig(t *testing.T) apiconfig.Config { + config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatal(err) } @@ -1032,7 +1033,7 @@ func setupDefaultResMgmtClient(t *testing.T) *ResourceMgmtClient { return setupResMgmtClient(client, nil, network, t) } -func setupResMgmtClient(client *fcmocks.MockClient, discErr error, config *config.Config, t *testing.T) *ResourceMgmtClient { +func setupResMgmtClient(client *fcmocks.MockClient, discErr error, config apiconfig.Config, t *testing.T) *ResourceMgmtClient { discovery, err := setupTestDiscovery(discErr, nil) if err != nil { diff --git a/pkg/fabric-txn/selection/dynamicselection/ccpolicyprovider_test.go b/pkg/fabric-txn/selection/dynamicselection/ccpolicyprovider_test.go index 408ceafa85..6dee25552f 100644 --- a/pkg/fabric-txn/selection/dynamicselection/ccpolicyprovider_test.go +++ b/pkg/fabric-txn/selection/dynamicselection/ccpolicyprovider_test.go @@ -15,12 +15,7 @@ import ( func TestCCPolicyProvider(t *testing.T) { // Create SDK setup for channel client with dynamic selection - c, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml") - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - sdk, err := fabsdk.New(c) + sdk, err := fabsdk.New(config.FromFile("../../../../test/fixtures/config/config_test.yaml")) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) } diff --git a/pkg/fabric-txn/selection/dynamicselection/dynamicselection_test.go b/pkg/fabric-txn/selection/dynamicselection/dynamicselection_test.go index 892dd380ae..a7084eb48d 100644 --- a/pkg/fabric-txn/selection/dynamicselection/dynamicselection_test.go +++ b/pkg/fabric-txn/selection/dynamicselection/dynamicselection_test.go @@ -325,14 +325,14 @@ func toString(peers []apifabclient.Peer) string { func TestDynamicSelection(t *testing.T) { - config, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml") + c, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatalf(err.Error()) } mychannelUser := ChannelUser{ChannelID: "mychannel", UserName: "User1", OrgName: "Org1"} - selectionProvider, err := NewSelectionProvider(config, []ChannelUser{mychannelUser}, nil) + selectionProvider, err := NewSelectionProvider(c, []ChannelUser{mychannelUser}, nil) if err != nil { t.Fatalf("Failed to setup selection provider: %s", err) } @@ -348,7 +348,9 @@ func TestDynamicSelection(t *testing.T) { } // Create SDK setup for channel client with dynamic selection - sdk, err := fabsdk.New(config, fabsdk.WithServicePkg(&DynamicSelectionProviderFactory{ChannelUsers: []ChannelUser{mychannelUser}})) + sdk, err := fabsdk.New( + config.FromFile("../../../../test/fixtures/config/config_test.yaml"), + fabsdk.WithServicePkg(&DynamicSelectionProviderFactory{ChannelUsers: []ChannelUser{mychannelUser}})) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) } @@ -390,7 +392,7 @@ func TestDynamicSelection(t *testing.T) { } // Test custom load balancer - selectionProvider, err = NewSelectionProvider(config, []ChannelUser{mychannelUser}, newCustomLBP()) + selectionProvider, err = NewSelectionProvider(c, []ChannelUser{mychannelUser}, newCustomLBP()) if err != nil { t.Fatalf("Failed to setup selection provider: %s", err) } diff --git a/pkg/fabric-txn/selection/staticselection/staticselection_test.go b/pkg/fabric-txn/selection/staticselection/staticselection_test.go index afca327a6a..82bc2d8116 100644 --- a/pkg/fabric-txn/selection/staticselection/staticselection_test.go +++ b/pkg/fabric-txn/selection/staticselection/staticselection_test.go @@ -14,7 +14,7 @@ import ( func TestStaticSelection(t *testing.T) { - config, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml") + config, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml")() if err != nil { t.Fatalf(err.Error()) } diff --git a/pkg/fabsdk/client_test.go b/pkg/fabsdk/client_test.go index cf3863c7c4..7dbcb8c8f4 100644 --- a/pkg/fabsdk/client_test.go +++ b/pkg/fabsdk/client_test.go @@ -23,12 +23,24 @@ const ( ) func TestNewGoodClientOpt(t *testing.T) { - c, err := configImpl.FromFile(clientConfigFile) + sdk, err := New(configImpl.FromFile(clientConfigFile)) + if err != nil { + t.Fatalf("Expected no error from New, but got %v", err) + } + + _, err = sdk.NewClient(WithUser(clientValidUser), goodClientOpt()) + if err != nil { + t.Fatalf("Expected no error from Client, but got %v", err) + } +} + +func TestFromConfigGoodClientOpt(t *testing.T) { + c, err := configImpl.FromFile(clientConfigFile)() if err != nil { t.Fatalf("Unexpected error from config: %v", err) } - sdk, err := New(c) + sdk, err := fromConfig(c) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -46,12 +58,7 @@ func goodClientOpt() ClientOption { } func TestNewBadClientOpt(t *testing.T) { - c, err := configImpl.FromFile(clientConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(clientConfigFile)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -69,12 +76,7 @@ func badClientOpt() ClientOption { } func TestClient(t *testing.T) { - c, err := configImpl.FromFile(clientConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(clientConfigFile)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -86,12 +88,7 @@ func TestClient(t *testing.T) { } func TestWithOrg(t *testing.T) { - c, err := configImpl.FromFile(clientConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(clientConfigFile)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -123,7 +120,7 @@ func TestWithFilter(t *testing.T) { } func TestWithConfig(t *testing.T) { - c, err := configImpl.FromFile(clientConfigFile) + c, err := configImpl.FromFile(clientConfigFile)() if err != nil { t.Fatalf("Unexpected error from config: %v", err) } @@ -141,12 +138,7 @@ func TestWithConfig(t *testing.T) { } func TestNoIdentity(t *testing.T) { - c, err := configImpl.FromFile(clientConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(clientConfigFile)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -158,12 +150,7 @@ func TestNoIdentity(t *testing.T) { } func TestNewChannelMgmtClient(t *testing.T) { - c, err := configImpl.FromFile("../../test/fixtures/config/config_test.yaml") - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile("../../test/fixtures/config/config_test.yaml")) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -182,12 +169,7 @@ func TestNewChannelMgmtClient(t *testing.T) { } func TestNewResourceMgmtClient(t *testing.T) { - c, err := configImpl.FromFile("../../test/fixtures/config/config_test.yaml") - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile("../../test/fixtures/config/config_test.yaml")) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } diff --git a/pkg/fabsdk/deprecated_test.go b/pkg/fabsdk/deprecated_test.go index fee6ff3635..0babc40a1a 100644 --- a/pkg/fabsdk/deprecated_test.go +++ b/pkg/fabsdk/deprecated_test.go @@ -20,12 +20,7 @@ const ( ) func TestNewChannelMgmtWithOptsClient(t *testing.T) { - c, err := configImpl.FromFile("../../test/fixtures/config/config_test.yaml") - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile("../../test/fixtures/config/config_test.yaml")) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -57,12 +52,7 @@ func TestNewChannelMgmtWithOptsClient(t *testing.T) { } func TestNewResourceMgmtWithOptsClient(t *testing.T) { - c, err := configImpl.FromFile("../../test/fixtures/config/config_test.yaml") - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile("../../test/fixtures/config/config_test.yaml")) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -93,12 +83,7 @@ func TestNewResourceMgmtWithOptsClient(t *testing.T) { } func TestNewPreEnrolledUserSession(t *testing.T) { - c, err := configImpl.FromFile("../../test/fixtures/config/config_test.yaml") - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile("../../test/fixtures/config/config_test.yaml")) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } diff --git a/pkg/fabsdk/fabsdk.go b/pkg/fabsdk/fabsdk.go index 7bd7fd02a4..b740f72e72 100644 --- a/pkg/fabsdk/fabsdk.go +++ b/pkg/fabsdk/fabsdk.go @@ -51,9 +51,20 @@ type ProviderInit interface { // New initializes the SDK based on the set of options provided. // configProvider provides the application configuration and is required. -func New(configProvider apiconfig.Config, opts ...Option) (*FabricSDK, error) { +func New(cp apiconfig.ConfigProvider, opts ...Option) (*FabricSDK, error) { pkgSuite := defPkgSuite{} - return fromPkgSuite(configProvider, &pkgSuite, opts...) + config, err := cp() + if err != nil { + return nil, errors.New("unable to load configuration") + } + return fromPkgSuite(config, &pkgSuite, opts...) +} + +// fromConfig initializes the SDK based on the set of options provided. +// configProvider provides the application configuration and is required. +func fromConfig(config apiconfig.Config, opts ...Option) (*FabricSDK, error) { + pkgSuite := defPkgSuite{} + return fromPkgSuite(config, &pkgSuite, opts...) } // fromPkgSuite creates an SDK based on the implementations in the provided pkg suite. diff --git a/pkg/fabsdk/fabsdk_test.go b/pkg/fabsdk/fabsdk_test.go index 7d2a670789..741bd75308 100644 --- a/pkg/fabsdk/fabsdk_test.go +++ b/pkg/fabsdk/fabsdk_test.go @@ -24,12 +24,8 @@ const ( ) func TestNewGoodOpt(t *testing.T) { - c, err := configImpl.FromFile(sdkConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - _, err = New(c, goodOpt()) + _, err := New(configImpl.FromFile(sdkConfigFile), + goodOpt()) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -42,12 +38,8 @@ func goodOpt() Option { } func TestNewBadOpt(t *testing.T) { - c, err := configImpl.FromFile(sdkConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - _, err = New(c, badOpt()) + _, err := New(configImpl.FromFile(sdkConfigFile), + badOpt()) if err == nil { t.Fatalf("Expected error from New") } @@ -61,12 +53,7 @@ func badOpt() Option { func TestNewDefaultSDK(t *testing.T) { // Test New SDK with valid config file - c, err := configImpl.FromFile(sdkConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(sdkConfigFile)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -103,12 +90,12 @@ func TestNewDefaultSDK(t *testing.T) { func TestWithCorePkg(t *testing.T) { // Test New SDK with valid config file - c, err := configImpl.FromFile(sdkConfigFile) + c, err := configImpl.FromFile(sdkConfigFile)() if err != nil { t.Fatalf("Unexpected error from config: %v", err) } - _, err = New(c) + _, err = fromConfig(c) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -122,7 +109,7 @@ func TestWithCorePkg(t *testing.T) { factory.EXPECT().NewSigningManager(nil, c).Return(nil, nil) factory.EXPECT().NewFabricProvider(c, nil, nil, nil).Return(nil, nil) - _, err = New(c, WithCorePkg(factory)) + _, err = fromConfig(c, WithCorePkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -130,12 +117,12 @@ func TestWithCorePkg(t *testing.T) { func TestWithServicePkg(t *testing.T) { // Test New SDK with valid config file - c, err := configImpl.FromFile(sdkConfigFile) + c, err := configImpl.FromFile(sdkConfigFile)() if err != nil { t.Fatalf("Unexpected error from config: %v", err) } - _, err = New(c) + _, err = fromConfig(c) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -147,7 +134,7 @@ func TestWithServicePkg(t *testing.T) { factory.EXPECT().NewDiscoveryProvider(c).Return(nil, nil) factory.EXPECT().NewSelectionProvider(c).Return(nil, nil) - _, err = New(c, WithServicePkg(factory)) + _, err = fromConfig(c, WithServicePkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -155,7 +142,7 @@ func TestWithServicePkg(t *testing.T) { func TestWithContextPkg(t *testing.T) { // Test New SDK with valid config file - c, err := configImpl.FromFile(sdkConfigFile) + c, err := configImpl.FromFile(sdkConfigFile)() if err != nil { t.Fatalf("Unexpected error from config: %v", err) } @@ -165,7 +152,7 @@ func TestWithContextPkg(t *testing.T) { t.Fatalf("Error initializing core factory: %s", err) } - _, err = New(c) + _, err = fromConfig(c) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -189,7 +176,7 @@ func TestWithContextPkg(t *testing.T) { factory.EXPECT().NewCredentialManager(sdkValidClientOrg1, c, core.cryptoSuite).Return(cm, nil) - sdk, err := New(c, WithCorePkg(core), WithContextPkg(factory)) + sdk, err := fromConfig(c, WithCorePkg(core), WithContextPkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -203,7 +190,7 @@ func TestWithContextPkg(t *testing.T) { func TestWithSessionPkg(t *testing.T) { // Test New SDK with valid config file - c, err := configImpl.FromFile(sdkConfigFile) + c, err := configImpl.FromFile(sdkConfigFile)() if err != nil { t.Fatalf("Unexpected error from config: %v", err) } @@ -213,7 +200,7 @@ func TestWithSessionPkg(t *testing.T) { t.Fatalf("Error initializing core factory: %s", err) } - _, err = New(c) + _, err = fromConfig(c) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -223,7 +210,7 @@ func TestWithSessionPkg(t *testing.T) { defer mockCtrl.Finish() factory := mockapisdk.NewMockSessionClientFactory(mockCtrl) - sdk, err := New(c, WithCorePkg(core), WithSessionPkg(factory)) + sdk, err := fromConfig(c, WithCorePkg(core), WithSessionPkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -263,7 +250,7 @@ func TestWithSessionPkg(t *testing.T) { func TestErrPkgSuite(t *testing.T) { ps := mockPkgSuite{} - c, err := configImpl.FromFile(sdkConfigFile) + c, err := configImpl.FromFile(sdkConfigFile)() if err != nil { t.Fatalf("Unexpected error from config: %v", err) } @@ -310,22 +297,12 @@ func TestErrPkgSuite(t *testing.T) { } func TestNewDefaultTwoValidSDK(t *testing.T) { - c1, err := configImpl.FromFile(sdkConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk1, err := New(c1) + sdk1, err := New(configImpl.FromFile(sdkConfigFile)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } - c2, err := configImpl.FromFile("./testdata/test.yaml") - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk2, err := New(c2) + sdk2, err := New(configImpl.FromFile("./testdata/test.yaml")) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -389,12 +366,7 @@ func TestNewDefaultSDKFromByte(t *testing.T) { t.Fatalf("Failed to load sample bytes from File. Error: %s", err) } - c1, err := configImpl.FromRaw(cBytes, "yaml") - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c1) + sdk, err := New(configImpl.FromRaw(cBytes, "yaml")) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -426,3 +398,26 @@ func loadConfigBytesFromFile(t *testing.T, filePath string) ([]byte, error) { } return cBytes, err } + +func TestWithConfigSuccess(t *testing.T) { + sdk, err := New(configImpl.FromFile(sdkConfigFile)) + if err != nil { + t.Fatalf("Error initializing SDK: %s", err) + } + + client1, err := sdk.configProvider.Client() + if err != nil { + t.Fatalf("Error getting client from config: %s", err) + } + + if client1.Organization != sdkValidClientOrg1 { + t.Fatalf("Unexpected org in config: %s", client1.Organization) + } +} + +func TestWithConfigFailure(t *testing.T) { + _, err := New(configImpl.FromFile("notarealfile")) + if err == nil { + t.Fatal("Expected failure due to invalid config") + } +} diff --git a/pkg/fabsdk/session_test.go b/pkg/fabsdk/session_test.go index b768f1f3e2..899ba2814e 100644 --- a/pkg/fabsdk/session_test.go +++ b/pkg/fabsdk/session_test.go @@ -19,12 +19,7 @@ const ( ) func TestWithUserValid(t *testing.T) { - c, err := configImpl.FromFile(identityOptConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(identityOptConfigFile)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -41,12 +36,7 @@ func TestWithUserValid(t *testing.T) { } func TestWithUserInvalid(t *testing.T) { - c, err := configImpl.FromFile(identityOptConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(identityOptConfigFile)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } @@ -63,12 +53,7 @@ func TestWithUserInvalid(t *testing.T) { } func TestWithIdentity(t *testing.T) { - c, err := configImpl.FromFile(identityOptConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - sdk, err := New(c) + sdk, err := New(configImpl.FromFile(identityOptConfigFile)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } diff --git a/pkg/fabsdk/singleton_test.go b/pkg/fabsdk/singleton_test.go index dddd3653bf..3b91c703aa 100644 --- a/pkg/fabsdk/singleton_test.go +++ b/pkg/fabsdk/singleton_test.go @@ -21,12 +21,7 @@ func TestDefLoggerFactory(t *testing.T) { // Cleanup logging singleton logging.UnsafeReset() - c, err := configImpl.FromFile(sdkConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - _, err = New(c) + _, err := New(configImpl.FromFile(sdkConfigFile)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -64,12 +59,8 @@ func TestOptLoggerFactory(t *testing.T) { lf := NewMockLoggerFactory() - c, err := configImpl.FromFile(sdkConfigFile) - if err != nil { - t.Fatalf("Unexpected error from config: %v", err) - } - - _, err = New(c, WithLoggerPkg(lf)) + _, err := New(configImpl.FromFile(sdkConfigFile), + WithLoggerPkg(lf)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } diff --git a/test/integration/base_test_setup.go b/test/integration/base_test_setup.go index 52aadead81..6d889e5d31 100644 --- a/test/integration/base_test_setup.go +++ b/test/integration/base_test_setup.go @@ -86,12 +86,7 @@ func ExampleCCUpgradeArgs() [][]byte { // Initialize reads configuration from file and sets up client, channel and event hub func (setup *BaseSetupImpl) Initialize(t *testing.T) error { // Create SDK setup for the integration tests - c, err := config.FromFile(setup.ConfigFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - sdk, err := fabsdk.New(c) + sdk, err := fabsdk.New(config.FromFile(setup.ConfigFile)) if err != nil { return errors.WithMessage(err, "SDK init failed") } @@ -185,12 +180,8 @@ func (setup *BaseSetupImpl) setupEventHub(t *testing.T, client fab.FabricClient) } // InitConfig ... -func (setup *BaseSetupImpl) InitConfig() (apiconfig.Config, error) { - configImpl, err := config.FromFile(setup.ConfigFile) - if err != nil { - return nil, err - } - return configImpl, nil +func (setup *BaseSetupImpl) InitConfig() apiconfig.ConfigProvider { + return config.FromFile(setup.ConfigFile) } // InstallCC use low level client to install chaincode diff --git a/test/integration/e2e/end_to_end.go b/test/integration/e2e/end_to_end.go index d1798e8653..c7f58db605 100644 --- a/test/integration/e2e/end_to_end.go +++ b/test/integration/e2e/end_to_end.go @@ -35,18 +35,13 @@ const ( ) func runWithConfigFixture(t *testing.T) { - c, err := config.FromFile("../" + integration.ConfigTestFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - Run(t, c) + Run(t, config.FromFile("../"+integration.ConfigTestFile)) } // Run enables testing an end-to-end scenario against the supplied SDK options -func Run(t *testing.T, config apiconfig.Config, sdkOpts ...fabsdk.Option) { +func Run(t *testing.T, configOpt apiconfig.ConfigProvider, sdkOpts ...fabsdk.Option) { - sdk, err := fabsdk.New(config, sdkOpts...) + sdk, err := fabsdk.New(configOpt, sdkOpts...) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) } diff --git a/test/integration/fab/main_test.go b/test/integration/fab/main_test.go index aabd193ea2..cb4e2419c9 100644 --- a/test/integration/fab/main_test.go +++ b/test/integration/fab/main_test.go @@ -31,7 +31,7 @@ func setup() { ConfigFile: "../" + integration.ConfigTestFile, } - testFabricConfig, err = testSetup.InitConfig() + testFabricConfig, err = testSetup.InitConfig()() if err != nil { fmt.Printf("Failed InitConfig [%s]\n", err) os.Exit(1) diff --git a/test/integration/orgs/multiple_orgs_test.go b/test/integration/orgs/multiple_orgs_test.go index 402b9109be..1e58d8a9cb 100644 --- a/test/integration/orgs/multiple_orgs_test.go +++ b/test/integration/orgs/multiple_orgs_test.go @@ -50,12 +50,7 @@ var orgTestPeer1 fab.Peer func TestOrgsEndToEnd(t *testing.T) { // Create SDK setup for the integration tests - c, err := config.FromFile("../" + integration.ConfigTestFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - sdk, err := fabsdk.New(c) + sdk, err := fabsdk.New(config.FromFile("../" + integration.ConfigTestFile)) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) } @@ -222,7 +217,8 @@ func TestOrgsEndToEnd(t *testing.T) { mychannelUser := selection.ChannelUser{ChannelID: "orgchannel", UserName: "User1", OrgName: "Org1"} // Create SDK setup for channel client with dynamic selection - sdk, err = fabsdk.New(c, fabsdk.WithServicePkg(&DynamicSelectionProviderFactory{ChannelUsers: []selection.ChannelUser{mychannelUser}})) + sdk, err = fabsdk.New(config.FromFile("../"+integration.ConfigTestFile), + fabsdk.WithServicePkg(&DynamicSelectionProviderFactory{ChannelUsers: []selection.ChannelUser{mychannelUser}})) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) } diff --git a/test/integration/pkcs11/e2e_test.go b/test/integration/pkcs11/e2e_test.go index 1a2843cdaf..a970c08d0f 100644 --- a/test/integration/pkcs11/e2e_test.go +++ b/test/integration/pkcs11/e2e_test.go @@ -21,12 +21,9 @@ import ( func TestE2E(t *testing.T) { // Create SDK setup for the integration tests - c, err := config.FromFile("../" + ConfigTestFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - e2e.Run(t, c, fabsdk.WithCorePkg(&CustomCryptoSuiteProviderFactory{})) + e2e.Run(t, + config.FromFile("../"+ConfigTestFile), + fabsdk.WithCorePkg(&CustomCryptoSuiteProviderFactory{})) } // CustomCryptoSuiteProviderFactory is will provide custom cryptosuite (bccsp.BCCSP) diff --git a/test/integration/sdk/channel_client_test.go b/test/integration/sdk/channel_client_test.go index 756e26b8b2..6a7652629f 100644 --- a/test/integration/sdk/channel_client_test.go +++ b/test/integration/sdk/channel_client_test.go @@ -44,12 +44,7 @@ func TestChannelClient(t *testing.T) { } // Create SDK setup for the integration tests - c, err := config.FromFile(testSetup.ConfigFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - sdk, err := fabsdk.New(c) + sdk, err := fabsdk.New(config.FromFile(testSetup.ConfigFile)) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) } diff --git a/test/integration/sdk/custom_cryptosuite_test.go b/test/integration/sdk/custom_cryptosuite_test.go index e4bffcdfc6..7362917657 100644 --- a/test/integration/sdk/custom_cryptosuite_test.go +++ b/test/integration/sdk/custom_cryptosuite_test.go @@ -45,7 +45,7 @@ func TestEndToEndForCustomCryptoSuite(t *testing.T) { t.Fatalf("InstallAndInstantiateExampleCC return error: %v", err) } - defaultConfig, err := testSetup.InitConfig() + defaultConfig, err := testSetup.InitConfig()() if err != nil { panic(fmt.Sprintf("Failed to get default config [%s]", err)) @@ -56,12 +56,8 @@ func TestEndToEndForCustomCryptoSuite(t *testing.T) { customBccspProvider := getTestBCCSP(defaultConfig) // Create SDK setup with custom cryptosuite provider factory - c, err := config.FromFile(testSetup.ConfigFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - sdk, err := fabsdk.New(c, fabsdk.WithCorePkg(&CustomCryptoSuiteProviderFactory{bccspProvider: customBccspProvider})) + sdk, err := fabsdk.New(config.FromFile(testSetup.ConfigFile), + fabsdk.WithCorePkg(&CustomCryptoSuiteProviderFactory{bccspProvider: customBccspProvider})) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) @@ -135,7 +131,7 @@ func TestCustomCryptoSuite(t *testing.T) { ConfigFile: "../" + integration.ConfigTestFile, } - defaultConfig, err := testSetup.InitConfig() + defaultConfig, err := testSetup.InitConfig()() if err != nil { panic(fmt.Sprintf("Failed to get default config [%s]", err)) @@ -146,12 +142,8 @@ func TestCustomCryptoSuite(t *testing.T) { //Get BCCSP custom wrapper for Test BCCSP customBccspWrapper := getBCCSPWrapper(customBccspProvider) - c, err := config.FromFile(testSetup.ConfigFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - sdk, err := fabsdk.New(c, fabsdk.WithCorePkg(&CustomCryptoSuiteProviderFactory{bccspProvider: customBccspWrapper})) + sdk, err := fabsdk.New(config.FromFile(testSetup.ConfigFile), + fabsdk.WithCorePkg(&CustomCryptoSuiteProviderFactory{bccspProvider: customBccspWrapper})) if err != nil { t.Fatalf("Failed to create new SDK: %s", err) } diff --git a/test/integration/sdk/sdk_provider_test.go b/test/integration/sdk/sdk_provider_test.go index 7ea6117f25..be2ededc89 100644 --- a/test/integration/sdk/sdk_provider_test.go +++ b/test/integration/sdk/sdk_provider_test.go @@ -48,12 +48,8 @@ func TestDynamicSelection(t *testing.T) { mychannelUser := selection.ChannelUser{ChannelID: testSetup.ChannelID, UserName: "User1", OrgName: "Org1"} // Create SDK setup for channel client with dynamic selection - c, err := config.FromFile(testSetup.ConfigFile) - if err != nil { - t.Fatalf("Failed to load config: %s", err) - } - - sdk, err := fabsdk.New(c, fabsdk.WithServicePkg(&DynamicSelectionProviderFactory{ChannelUsers: []selection.ChannelUser{mychannelUser}})) + sdk, err := fabsdk.New(config.FromFile(testSetup.ConfigFile), + fabsdk.WithServicePkg(&DynamicSelectionProviderFactory{ChannelUsers: []selection.ChannelUser{mychannelUser}})) if err != nil { t.Fatalf("Failed to create new SDK: %s", err)