Skip to content

Commit

Permalink
marshal ExternalBuilders into yaml and use as strings in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vladyslav Kopaihorodskyi <vlad.kopaygorodsky@gmail.com>
  • Loading branch information
kopaygorodsky committed Jul 19, 2022
1 parent 4271229 commit f715daf
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/peer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
94 changes: 81 additions & 13 deletions core/peer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package peer

import (
"crypto/tls"
"gopkg.in/yaml.v2"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand All @@ -416,6 +452,7 @@ func TestPropagateEnvironment(t *testing.T) {
{
Name: "testName",
PropagateEnvironment: []string{"KEY=VALUE"},
Environment: []string{},
Path: "/testPath",
},
{
Expand All @@ -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)

Expand All @@ -441,26 +492,43 @@ 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)
}

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")
}

0 comments on commit f715daf

Please sign in to comment.