Skip to content

Commit

Permalink
Merge pull request #31 from calyptia/validate-single-plugin
Browse files Browse the repository at this point in the history
validate small config section
  • Loading branch information
niedbalski authored Sep 15, 2022
2 parents 240dc63 + af3e6b3 commit 6f381e8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 32 deletions.
81 changes: 49 additions & 32 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,38 +180,8 @@ func (cfg *Config) ValidateWithSchema(schema Schema) error {
continue
}

schemaSections, ok := findSchemaSections(schema, section.Type)
if !ok {
return fmt.Errorf("unknown section %q", ConfigSectionTypes[section.Type])
}

pluginName := getPluginNameParameter(section)
schemaSection, ok := findSchemaSection(schemaSections, pluginName)
if !ok {
return fmt.Errorf("unknown plugin %q", pluginName)
}

for _, field := range section.Fields {
if isCloudVariable(field.Values) {
continue
}

if isCommonProperty(field.Key) {
if len(field.Values) == 0 {
return fmt.Errorf("%s: expected %q to be a valid string; got %q", schemaSection.Name, field.Key, field.Values.ToString())
}

continue
}

opts, ok := findSchemaOptions(schemaSection.Properties, field.Key)
if !ok {
return fmt.Errorf("%s: unknown property %q", schemaSection.Name, field.Key)
}

if !validProp(opts, field) {
return fmt.Errorf("%s: expected %q to be a valid %s; got %q", schemaSection.Name, opts.Name, opts.Type, field.Values.ToString())
}
if err := section.ValidateWithSchema(schema); err != nil {
return err
}
}

Expand Down Expand Up @@ -256,6 +226,53 @@ func findSchemaSection(ss []SchemaSection, pluginName string) (SchemaSection, bo
return SchemaSection{}, false
}

func (section ConfigSection) Validate() error {
return section.ValidateWithSchema(DefaultSchema)
}

func (section ConfigSection) ValidateWithSchema(schema Schema) error {
if !supportedSchemaSection(section.Type) {
// TODO: support all sections on schema.
return fmt.Errorf("unsupported plugin kind %q", ConfigSectionTypes[section.Type])
}

schemaSections, ok := findSchemaSections(schema, section.Type)
if !ok {
return fmt.Errorf("unknown section %q", ConfigSectionTypes[section.Type])
}

pluginName := getPluginNameParameter(section)
schemaSection, ok := findSchemaSection(schemaSections, pluginName)
if !ok {
return fmt.Errorf("unknown plugin %q", pluginName)
}

for _, field := range section.Fields {
if isCloudVariable(field.Values) {
continue
}

if isCommonProperty(field.Key) {
if len(field.Values) == 0 {
return fmt.Errorf("%s: expected %q to be a valid string; got %q", schemaSection.Name, field.Key, field.Values.ToString())
}

continue
}

opts, ok := findSchemaOptions(schemaSection.Properties, field.Key)
if !ok {
return fmt.Errorf("%s: unknown property %q", schemaSection.Name, field.Key)
}

if !validProp(opts, field) {
return fmt.Errorf("%s: expected %q to be a valid %s; got %q", schemaSection.Name, opts.Name, opts.Type, field.Values.ToString())
}
}

return nil
}

func isCommonProperty(name string) bool {
for _, got := range [...]string{"Name", "Alias", "Tag", "Match", "Match_Regex"} {
if strings.EqualFold(name, got) {
Expand Down
68 changes: 68 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,71 @@ func TestConfig_Validate(t *testing.T) {
})
}
}

func TestConfigSection_Validate(t *testing.T) {
tt := []struct {
name string
kind ConfigSectionType
props map[string]string
want string
}{
{
name: "input_cpu_pid_float",
kind: InputSection,
props: map[string]string{
"name": "cpu",
"pid": "3.4",
},
want: `cpu: expected "pid" to be a valid integer; got "3.4"`,
},
{
name: "input_cpu_pid_integer",
kind: InputSection,
props: map[string]string{
"name": "cpu",
"pid": "5",
},
},
{
name: "unsupported_kind",
kind: NoneSection,
want: `unsupported plugin kind "NONE"`,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
fields := Fields{}
for k, v := range tc.props {
v := v
fields = append(fields, Field{
Key: k,
Values: Values{{String: &v}},
})
}
section := ConfigSection{
Type: tc.kind,
Fields: fields,
}

err := section.Validate()
if tc.want == "" && err == nil {
return
}

if tc.want == "" && err != nil {
t.Error(err)
return
}

if tc.want != "" && err == nil {
t.Errorf("expected error:\n%s\ngot: nil\n", tc.want)
return
}

if tc.want != err.Error() {
t.Errorf("expected error:\n%s\ngot:\n%s\n", tc.want, err.Error())
return
}
})
}
}

0 comments on commit 6f381e8

Please sign in to comment.