Skip to content

Commit

Permalink
Parse user-provided ConfigMap YAML
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Yunt <andrew.yunt@gmail.com>

Add . to yaml suffix in GenConfig validate yaml parser

Signed-off-by: Andrew Yunt <andrew.yunt@gmail.com>

Add edge test cases for ConfigMap YAML validation

Signed-off-by: Andrew Yunt <andrew.yunt@gmail.com>
  • Loading branch information
andrewyunt authored and johnSchnake committed Jun 2, 2022
1 parent c4593e0 commit 35589fa
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"regexp"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -107,6 +108,15 @@ func (gc *GenConfig) Validate() error {
if !nameRegexp.MatchString(m.SonobuoyConfig.PluginName) {
return fmt.Errorf("invalid plugin name %q; name must only include lowercase alphanumeric values '.' or '-'. This is due to Kubernetes requiring various values to follow RFC 1123 for valid subdomain names", m.SonobuoyConfig.PluginName)
}

for key, value := range m.ConfigMap {
if strings.HasSuffix(key, ".yml") || strings.HasSuffix(key, ".yaml") {
var i interface{}
if err := yaml.Unmarshal([]byte(value), &i); err != nil {
return fmt.Errorf("failed to parse value of key %v in ConfigMap for plugin %v: %v", key, m.SonobuoyConfig.PluginName, err)
}
}
}
}
return nil
}
Expand Down
102 changes: 102 additions & 0 deletions pkg/client/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package client

import (
"testing"

"github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest"
)

// ConfigValidator allows the command configurations to be validated.
Expand All @@ -32,6 +34,106 @@ func TestConfigValidation(t *testing.T) {
valid bool
expectedError string
}{
{
desc: "gen config with valid yaml value is valid",
config: &GenConfig{
StaticPlugins: []*manifest.Manifest{
{
SonobuoyConfig: manifest.SonobuoyConfig{
PluginName: "test",
},
ConfigMap: map[string]string{
"test.yaml": "test: \"valid\"",
},
},
},
},
valid: true,
},
{
desc: "gen config with valid yml value is valid",
config: &GenConfig{
StaticPlugins: []*manifest.Manifest{
{
SonobuoyConfig: manifest.SonobuoyConfig{
PluginName: "test",
},
ConfigMap: map[string]string{
"test.yml": "test: \"valid\"",
},
},
},
},
valid: true,
},
{
desc: "gen config with invalid yaml value is not valid",
config: &GenConfig{
StaticPlugins: []*manifest.Manifest{
{
SonobuoyConfig: manifest.SonobuoyConfig{
PluginName: "test",
},
ConfigMap: map[string]string{
"test.yaml": "test: \"in\"valid\"",
},
},
},
},
valid: false,
expectedError: "failed to parse value of key test.yaml in ConfigMap for plugin test: error converting YAML to JSON: yaml: did not find expected key",
},
{
desc: "gen config with invalid yml value is not valid",
config: &GenConfig{
StaticPlugins: []*manifest.Manifest{
{
SonobuoyConfig: manifest.SonobuoyConfig{
PluginName: "test",
},
ConfigMap: map[string]string{
"test.yml": "test: \"in\"valid\"",
},
},
},
},
valid: false,
expectedError: "failed to parse value of key test.yml in ConfigMap for plugin test: error converting YAML to JSON: yaml: did not find expected key",
},
{
desc: "gen config with valid yaml value and invalid yaml value is not valid",
config: &GenConfig{
StaticPlugins: []*manifest.Manifest{
{
SonobuoyConfig: manifest.SonobuoyConfig{
PluginName: "test",
},
ConfigMap: map[string]string{
"test.yaml": "test: \"valid\"",
"test2.yaml": "test: \"in\"valid\"",
},
},
},
},
valid: false,
expectedError: "failed to parse value of key test2.yaml in ConfigMap for plugin test: error converting YAML to JSON: yaml: did not find expected key",
},
{
desc: "gen config with invalid yaml value for non-yaml key is valid",
config: &GenConfig{
StaticPlugins: []*manifest.Manifest{
{
SonobuoyConfig: manifest.SonobuoyConfig{
PluginName: "test",
},
ConfigMap: map[string]string{
"test.txt": "test: \"in\"valid\"",
},
},
},
},
valid: true,
},
{
desc: "log config with no namespace is not valid",
config: &LogConfig{},
Expand Down

0 comments on commit 35589fa

Please sign in to comment.