From f715daf8a7b76d2a30556af5520d6ca1eed322f9 Mon Sep 17 00:00:00 2001 From: Vladyslav Kopaihorodskyi Date: Wed, 20 Jul 2022 01:06:36 +0300 Subject: [PATCH] marshal ExternalBuilders into yaml and use as strings in tests Signed-off-by: Vladyslav Kopaihorodskyi --- core/peer/config.go | 2 +- core/peer/config_test.go | 94 ++++++++++++++++++++++++++++++++++------ 2 files changed, 82 insertions(+), 14 deletions(-) diff --git a/core/peer/config.go b/core/peer/config.go index 3797f1911db..6dcd24d4fd2 100644 --- a/core/peer/config.go +++ b/core/peer/config.go @@ -290,7 +290,7 @@ func (c *Config) load() error { if builder.Name == "" { return fmt.Errorf("external builder at path %s has no name attribute", builder.Path) } - if builder.Environment != nil && builder.PropagateEnvironment == nil { + if builder.Environment != nil && len(builder.PropagateEnvironment) == 0 { c.ExternalBuilders[builderIndex].PropagateEnvironment = builder.Environment } } diff --git a/core/peer/config_test.go b/core/peer/config_test.go index 80f0981dc6b..021823fafb1 100644 --- a/core/peer/config_test.go +++ b/core/peer/config_test.go @@ -7,6 +7,7 @@ package peer import ( "crypto/tls" + "gopkg.in/yaml.v2" "io/ioutil" "net" "os" @@ -308,7 +309,19 @@ func TestGlobalConfig(t *testing.T) { viper.Set("metrics.statsd.prefix", "testPrefix") viper.Set("chaincode.pull", false) - viper.Set("chaincode.externalBuilders", "[{name: relative, path: relative/plugin_dir}, {name: absolute, path: /absolute/plugin_dir}]") + + extBuildersConfig, err := yaml.Marshal([]ExternalBuilder{ + { + Path: "relative/plugin_dir", + Name: "relative", + }, + { + Path: "/absolute/plugin_dir", + Name: "absolute", + }, + }) + require.NoError(t, err) + viper.Set("chaincode.externalBuilders", extBuildersConfig) coreConfig, err := GlobalConfig() assert.NoError(t, err) @@ -343,12 +356,16 @@ func TestGlobalConfig(t *testing.T) { ChaincodePull: false, ExternalBuilders: []ExternalBuilder{ { - Path: "relative/plugin_dir", - Name: "relative", + Path: "relative/plugin_dir", + Name: "relative", + PropagateEnvironment: []string{}, + Environment: []string{}, }, { - Path: "/absolute/plugin_dir", - Name: "absolute", + Path: "/absolute/plugin_dir", + Name: "absolute", + PropagateEnvironment: []string{}, + Environment: []string{}, }, }, OperationsListenAddress: "127.0.0.1:9443", @@ -396,7 +413,26 @@ func TestGlobalConfigDefault(t *testing.T) { func TestPropagateEnvironment(t *testing.T) { defer viper.Reset() viper.Set("peer.address", "localhost:8080") - viper.Set("chaincode.externalBuilders", "[{name: testName, environmentWhitelist: [KEY=VALUE], path: /testPath}, {name: testName, propagateEnvironment: [KEY=VALUE], path: /testPath}, {name: testName, environmentWhitelist: [KEY=VALUE], propagateEnvironment: [KEY=VALUE2], path: /testPath}]") + extBuildersConfig, err := yaml.Marshal([]ExternalBuilder{ + { + Name: "testName", + Environment: []string{"KEY=VALUE"}, + Path: "/testPath", + }, + { + Name: "testName", + PropagateEnvironment: []string{"KEY=VALUE"}, + Path: "/testPath", + }, + { + Name: "testName", + Environment: []string{"KEY=VALUE"}, + PropagateEnvironment: []string{"KEY=VALUE2"}, + Path: "/testPath", + }, + }) + require.NoError(t, err) + viper.Set("chaincode.externalBuilders", extBuildersConfig) coreConfig, err := GlobalConfig() assert.NoError(t, err) @@ -416,6 +452,7 @@ func TestPropagateEnvironment(t *testing.T) { { Name: "testName", PropagateEnvironment: []string{"KEY=VALUE"}, + Environment: []string{}, Path: "/testPath", }, { @@ -432,7 +469,21 @@ func TestPropagateEnvironment(t *testing.T) { func TestExternalBuilderConfigAsEnvVar(t *testing.T) { defer viper.Reset() viper.Set("peer.address", "localhost:8080") - viper.Set("chaincode.externalBuilders", "[{name: relative, path: relative/plugin_dir, propagateEnvironment: [ENVVAR_NAME_TO_PROPAGATE_FROM_PEER, GOPROXY]}, {name: absolute, path: /absolute/plugin_dir}]") + extBuildersConfig, err := yaml.Marshal([]ExternalBuilder{ + { + Path: "relative/plugin_dir", + Name: "relative", + PropagateEnvironment: []string{"ENVVAR_NAME_TO_PROPAGATE_FROM_PEER", "GOPROXY"}, + }, + { + Path: "/absolute/plugin_dir", + Name: "absolute", + }, + }) + require.NoError(t, err) + + viper.Set("chaincode.externalBuilders", extBuildersConfig) + coreConfig, err := GlobalConfig() require.NoError(t, err) @@ -441,10 +492,13 @@ func TestExternalBuilderConfigAsEnvVar(t *testing.T) { Path: "relative/plugin_dir", Name: "relative", PropagateEnvironment: []string{"ENVVAR_NAME_TO_PROPAGATE_FROM_PEER", "GOPROXY"}, + Environment: []string{}, }, { - Path: "/absolute/plugin_dir", - Name: "absolute", + Path: "/absolute/plugin_dir", + Name: "absolute", + PropagateEnvironment: []string{}, + Environment: []string{}, }, }, coreConfig.ExternalBuilders) } @@ -452,15 +506,29 @@ func TestExternalBuilderConfigAsEnvVar(t *testing.T) { func TestMissingExternalBuilderPath(t *testing.T) { defer viper.Reset() viper.Set("peer.address", "localhost:8080") - viper.Set("chaincode.externalBuilders", "[{name: testName}]") - _, err := GlobalConfig() + extBuildersConfig, err := yaml.Marshal([]ExternalBuilder{ + { + Name: "testName", + }, + }) + require.NoError(t, err) + viper.Set("chaincode.externalBuilders", extBuildersConfig) + _, err = GlobalConfig() assert.EqualError(t, err, "invalid external builder configuration, path attribute missing in one or more builders") } func TestMissingExternalBuilderName(t *testing.T) { defer viper.Reset() viper.Set("peer.address", "localhost:8080") - viper.Set("chaincode.externalBuilders", "[{path: relative/plugin_dir}]") - _, err := GlobalConfig() + + extBuildersConfig, err := yaml.Marshal([]ExternalBuilder{ + { + Path: "relative/plugin_dir", + }, + }) + require.NoError(t, err) + viper.Set("chaincode.externalBuilders", extBuildersConfig) + + _, err = GlobalConfig() assert.EqualError(t, err, "external builder at path relative/plugin_dir has no name attribute") }