Skip to content

Commit

Permalink
Parser: check required fields (#344)
Browse files Browse the repository at this point in the history
* Parser: check required fields

* TestAdditionalInstances fixture: include additional_container's port

* Include additional container's port in TestAdditionalContainerInstances
  • Loading branch information
edigaryev authored Mar 18, 2021
1 parent e0adb15 commit b0974aa
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ aliases: &container_body
image: mysql:latest
cpu: 1
memory: 1024
port: 3306
environment:
MYSQL_ROOT_PASSWORD: ""
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/nameable/nameable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package nameable
type Nameable interface {
Matches(s string) bool
String() string
MapKey() string
}
4 changes: 4 additions & 0 deletions pkg/parser/nameable/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (rn *RegexNameable) String() string {
return rn.re.String()
}

func (rn *RegexNameable) MapKey() string {
return "RegexNameable(" + rn.re.String() + ")"
}

func (rn *RegexNameable) FirstGroupOrDefault(s string, defaultValue string) string {
submatch := rn.re.FindStringSubmatch(s)
if submatch == nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/parser/nameable/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ func (sn *SimpleNameable) Name() string {
func (sn *SimpleNameable) String() string {
return sn.name
}

func (sn *SimpleNameable) MapKey() string {
return "SimpleNameable(" + sn.name + ")"
}
15 changes: 13 additions & 2 deletions pkg/parser/parseable/parseable.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func (parser *DefaultParser) Parse(node *node.Node) error {
}
}

// Check required fields

for _, child := range node.Children {
// double check collectible fields
for _, collectibleField := range parser.collectibleFields {
Expand All @@ -51,17 +49,30 @@ func (parser *DefaultParser) Parse(node *node.Node) error {
}
}

seenFields := map[string]struct{}{}

for _, child := range node.Children {
for _, field := range parser.fields {
if field.name.Matches(child.Name) {
seenFields[field.name.MapKey()] = struct{}{}

if err := field.onFound(child); err != nil {
return err
}

break
}
}
}

for _, field := range parser.fields {
_, seen := seenFields[field.name.MapKey()]

if field.required && !seen {
return node.ParserError("required field %q was not set", field.name.String())
}
}

return nil
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ func TestValidConfigs(t *testing.T) {
}
}

func TestInvalidConfigs(t *testing.T) {
var invalidCases = []struct {
Name string
Error string
}{
{"invalid-missing-required-field", "parsing error: 6:3: required field \"steps\" was not set"},
}

for _, invalidCase := range invalidCases {
invalidCase := invalidCase

t.Run(invalidCase.Name, func(t *testing.T) {
p := parser.New()
_, err := p.ParseFromFile(context.Background(), absolutize(invalidCase.Name+".yml"))

require.Error(t, err)
assert.Equal(t, invalidCase.Error, err.Error())
})
}
}

func TestAdditionalInstances(t *testing.T) {
containerInstanceReflect := (&api.ContainerInstance{}).ProtoReflect()
p := parser.New(parser.WithAdditionalInstances(map[string]protoreflect.MessageDescriptor{
Expand Down
6 changes: 6 additions & 0 deletions pkg/parser/testdata/invalid-missing-required-field.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
container:
image: debian:latest

# Pipe is missing "steps" required field
pipe:
script: true
6 changes: 4 additions & 2 deletions pkg/parser/testdata/proto-instance.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"image": "mysql:latest",
"memory": 1024,
"name": "mysql"
"name": "mysql",
"containerPort": 3306
}
],
"cpu": 2.5,
Expand Down Expand Up @@ -57,7 +58,8 @@
},
"image": "mysql:latest",
"memory": 1024,
"name": "mysql"
"name": "mysql",
"containerPort": 3306
}
],
"cpu": 2.5,
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/testdata/proto-instance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ aliases: &container_body
image: mysql:latest
cpu: 1
memory: 1024
port: 3306
environment:
MYSQL_ROOT_PASSWORD: ""

Expand Down

0 comments on commit b0974aa

Please sign in to comment.