2015-09-15 00:43:42 +00:00
|
|
|
package jobspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-09-20 21:18:10 +00:00
|
|
|
"time"
|
2015-09-15 00:43:42 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
File string
|
|
|
|
Result *structs.Job
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"basic.hcl",
|
|
|
|
&structs.Job{
|
2015-09-17 05:06:55 +00:00
|
|
|
ID: "binstore-storagelocker",
|
2015-09-15 00:43:42 +00:00
|
|
|
Name: "binstore-storagelocker",
|
|
|
|
Type: "service",
|
|
|
|
Priority: 50,
|
|
|
|
AllAtOnce: true,
|
|
|
|
Datacenters: []string{"us2", "eu1"},
|
2015-09-15 01:18:49 +00:00
|
|
|
Region: "global",
|
2015-09-15 00:43:42 +00:00
|
|
|
|
2015-09-15 00:46:52 +00:00
|
|
|
Meta: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
|
2015-09-15 00:48:11 +00:00
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
2015-09-20 21:18:10 +00:00
|
|
|
Hard: true,
|
2015-09-15 00:48:11 +00:00
|
|
|
LTarget: "kernel.os",
|
|
|
|
RTarget: "windows",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-09-20 21:18:10 +00:00
|
|
|
Update: structs.UpdateStrategy{
|
|
|
|
Stagger: 60 * time.Second,
|
|
|
|
MaxParallel: 2,
|
|
|
|
},
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
TaskGroups: []*structs.TaskGroup{
|
|
|
|
&structs.TaskGroup{
|
|
|
|
Name: "outside",
|
|
|
|
Count: 1,
|
|
|
|
Tasks: []*structs.Task{
|
|
|
|
&structs.Task{
|
|
|
|
Name: "outside",
|
|
|
|
Driver: "java",
|
|
|
|
Config: map[string]string{
|
|
|
|
"jar": "s3://my-cool-store/foo.jar",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"my-cool-key": "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
&structs.TaskGroup{
|
|
|
|
Name: "binsl",
|
|
|
|
Count: 5,
|
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
2015-09-20 21:18:10 +00:00
|
|
|
Hard: true,
|
2015-09-15 00:43:42 +00:00
|
|
|
LTarget: "kernel.os",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"elb_mode": "tcp",
|
|
|
|
"elb_interval": "10",
|
|
|
|
"elb_checks": "3",
|
|
|
|
},
|
|
|
|
Tasks: []*structs.Task{
|
|
|
|
&structs.Task{
|
|
|
|
Name: "binstore",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]string{
|
|
|
|
"image": "hashicorp/binstore",
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 128,
|
2015-09-15 01:27:37 +00:00
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
MBits: 100,
|
|
|
|
ReservedPorts: []int{1, 2, 3},
|
|
|
|
DynamicPorts: 3,
|
|
|
|
},
|
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.Task{
|
|
|
|
Name: "storagelocker",
|
|
|
|
Driver: "java",
|
|
|
|
Config: map[string]string{
|
|
|
|
"image": "hashicorp/storagelocker",
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 128,
|
|
|
|
},
|
2015-09-15 00:50:34 +00:00
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
2015-09-20 21:18:10 +00:00
|
|
|
Hard: true,
|
2015-09-15 00:50:34 +00:00
|
|
|
LTarget: "kernel.arch",
|
|
|
|
RTarget: "amd64",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-09-15 01:30:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"multi-network.hcl",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"multi-resource.hcl",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2015-09-15 01:34:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"default-job.hcl",
|
|
|
|
&structs.Job{
|
2015-09-17 05:06:55 +00:00
|
|
|
ID: "foo",
|
2015-09-15 01:34:26 +00:00
|
|
|
Name: "foo",
|
|
|
|
Priority: 50,
|
|
|
|
Region: "global",
|
|
|
|
Type: "service",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-09-17 05:06:55 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"specify-job.hcl",
|
|
|
|
&structs.Job{
|
|
|
|
ID: "job1",
|
|
|
|
Name: "My Job",
|
|
|
|
Priority: 50,
|
|
|
|
Region: "global",
|
|
|
|
Type: "service",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
path, err := filepath.Abs(filepath.Join("./test-fixtures", tc.File))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("file: %s\n\n%s", tc.File, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := ParseFile(path)
|
|
|
|
if (err != nil) != tc.Err {
|
|
|
|
t.Fatalf("file: %s\n\n%s", tc.File, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, tc.Result) {
|
|
|
|
t.Fatalf("file: %s\n\n%#v\n\n%#v", tc.File, actual, tc.Result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|