jobspec: validate singletons

This commit is contained in:
Mitchell Hashimoto 2015-09-14 18:30:26 -07:00
parent 9c9e3ed083
commit 2e9e738036
4 changed files with 64 additions and 1 deletions

View file

@ -306,7 +306,7 @@ func parseTasks(result *[]*structs.Task, obj *hclobj.Object) error {
if o := o.Get("resources", false); o != nil {
var r structs.Resources
if err := parseResources(&r, o); err != nil {
return err
return fmt.Errorf("task '%s': %s", t.Name, err)
}
t.Resources = &r
@ -319,6 +319,10 @@ func parseTasks(result *[]*structs.Task, obj *hclobj.Object) error {
}
func parseResources(result *structs.Resources, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'resource' block allowed per task")
}
for _, o := range obj.Elem(false) {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
@ -332,6 +336,10 @@ func parseResources(result *structs.Resources, obj *hclobj.Object) error {
// Parse the network resources
if o := o.Get("network", false); o != nil {
if o.Len() > 1 {
return fmt.Errorf("only one 'network' resource allowed")
}
var r structs.NetworkResource
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {

View file

@ -112,6 +112,18 @@ func TestParse(t *testing.T) {
},
false,
},
{
"multi-network.hcl",
nil,
true,
},
{
"multi-resource.hcl",
nil,
true,
},
}
for _, tc := range cases {

View file

@ -0,0 +1,25 @@
job "binstore-storagelocker" {
group "binsl" {
count = 5
task "binstore" {
driver = "docker"
resources {
cpu = 500
memory = 128
network {
mbits = "100"
reserved_ports = [1,2,3]
dynamic_ports = 3
}
network {
mbits = "128"
reserved_ports = [1,2,3]
dynamic_ports = 3
}
}
}
}
}

View file

@ -0,0 +1,18 @@
job "binstore-storagelocker" {
group "binsl" {
count = 5
task "binstore" {
driver = "docker"
resources {
cpu = 500
memory = 128
}
resources {
cpu = 500
memory = 128
}
}
}
}