2015-09-15 00:43:42 +00:00
|
|
|
package jobspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2015-09-23 20:44:08 +00:00
|
|
|
"strings"
|
2015-09-15 00:43:42 +00:00
|
|
|
"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{
|
|
|
|
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",
|
2015-11-15 08:10:48 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-15 00:43:42 +00:00
|
|
|
"jar": "s3://my-cool-store/foo.jar",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"my-cool-key": "foobar",
|
|
|
|
},
|
2016-02-12 06:33:41 +00:00
|
|
|
LogConfig: structs.DefaultLogConfig(),
|
2015-09-15 00:43:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
&structs.TaskGroup{
|
|
|
|
Name: "binsl",
|
|
|
|
Count: 5,
|
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
|
|
|
LTarget: "kernel.os",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"elb_mode": "tcp",
|
|
|
|
"elb_interval": "10",
|
|
|
|
"elb_checks": "3",
|
|
|
|
},
|
2015-10-30 23:32:05 +00:00
|
|
|
RestartPolicy: &structs.RestartPolicy{
|
2016-02-02 23:08:07 +00:00
|
|
|
Interval: 10 * time.Minute,
|
|
|
|
Attempts: 5,
|
|
|
|
Delay: 15 * time.Second,
|
|
|
|
Mode: "delay",
|
2015-10-30 23:32:05 +00:00
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
Tasks: []*structs.Task{
|
|
|
|
&structs.Task{
|
|
|
|
Name: "binstore",
|
|
|
|
Driver: "docker",
|
2015-11-15 08:10:48 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-15 00:43:42 +00:00
|
|
|
"image": "hashicorp/binstore",
|
|
|
|
},
|
2015-11-18 10:07:07 +00:00
|
|
|
Services: []*structs.Service{
|
2015-11-17 06:51:08 +00:00
|
|
|
{
|
2015-11-18 00:05:03 +00:00
|
|
|
Name: "binstore-storagelocker-binsl-binstore",
|
2015-11-17 06:51:08 +00:00
|
|
|
Tags: []string{"foo", "bar"},
|
|
|
|
PortLabel: "http",
|
2015-11-26 08:21:25 +00:00
|
|
|
Checks: []*structs.ServiceCheck{
|
2015-11-17 06:51:08 +00:00
|
|
|
{
|
|
|
|
Name: "check-name",
|
|
|
|
Type: "tcp",
|
|
|
|
Interval: 10 * time.Second,
|
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-09-30 16:18:43 +00:00
|
|
|
Env: map[string]string{
|
|
|
|
"HELLO": "world",
|
|
|
|
"LOREM": "ipsum",
|
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 128,
|
2016-02-12 06:33:41 +00:00
|
|
|
DiskMB: 300,
|
2016-02-03 01:39:01 +00:00
|
|
|
IOPS: 0,
|
2015-09-15 01:27:37 +00:00
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
MBits: 100,
|
2015-11-15 08:10:48 +00:00
|
|
|
ReservedPorts: []structs.Port{{"one", 1}, {"two", 2}, {"three", 3}},
|
|
|
|
DynamicPorts: []structs.Port{{"http", 0}, {"https", 0}, {"admin", 0}},
|
2015-09-15 01:27:37 +00:00
|
|
|
},
|
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
},
|
2015-12-23 00:10:30 +00:00
|
|
|
KillTimeout: 22 * time.Second,
|
2016-02-05 07:28:01 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 100,
|
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
},
|
|
|
|
&structs.Task{
|
|
|
|
Name: "storagelocker",
|
|
|
|
Driver: "java",
|
2015-11-15 08:10:48 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-15 00:43:42 +00:00
|
|
|
"image": "hashicorp/storagelocker",
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 128,
|
2016-02-12 06:33:41 +00:00
|
|
|
DiskMB: 300,
|
2016-02-02 20:00:26 +00:00
|
|
|
IOPS: 30,
|
2015-09-15 00:43:42 +00:00
|
|
|
},
|
2015-09-15 00:50:34 +00:00
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
|
|
|
LTarget: "kernel.arch",
|
|
|
|
RTarget: "amd64",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
2016-02-12 06:33:41 +00:00
|
|
|
LogConfig: structs.DefaultLogConfig(),
|
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
|
|
|
|
2015-10-11 19:20:58 +00:00
|
|
|
{
|
|
|
|
"version-constraint.hcl",
|
|
|
|
&structs.Job{
|
|
|
|
ID: "foo",
|
|
|
|
Name: "foo",
|
|
|
|
Priority: 50,
|
|
|
|
Region: "global",
|
|
|
|
Type: "service",
|
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
|
|
|
LTarget: "$attr.kernel.version",
|
|
|
|
RTarget: "~> 3.2",
|
2015-10-26 20:47:56 +00:00
|
|
|
Operand: structs.ConstraintVersion,
|
2015-10-11 19:20:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-10-11 19:37:50 +00:00
|
|
|
{
|
|
|
|
"regexp-constraint.hcl",
|
|
|
|
&structs.Job{
|
|
|
|
ID: "foo",
|
|
|
|
Name: "foo",
|
|
|
|
Priority: 50,
|
|
|
|
Region: "global",
|
|
|
|
Type: "service",
|
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
|
|
|
LTarget: "$attr.kernel.version",
|
|
|
|
RTarget: "[0-9.]+",
|
2015-10-26 20:47:56 +00:00
|
|
|
Operand: structs.ConstraintRegex,
|
2015-10-11 19:37:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-10-22 23:37:20 +00:00
|
|
|
{
|
2015-10-23 00:40:41 +00:00
|
|
|
"distinctHosts-constraint.hcl",
|
2015-10-22 23:37:20 +00:00
|
|
|
&structs.Job{
|
|
|
|
ID: "foo",
|
|
|
|
Name: "foo",
|
|
|
|
Priority: 50,
|
|
|
|
Region: "global",
|
|
|
|
Type: "service",
|
|
|
|
Constraints: []*structs.Constraint{
|
|
|
|
&structs.Constraint{
|
2015-10-26 20:47:56 +00:00
|
|
|
Operand: structs.ConstraintDistinctHosts,
|
2015-10-22 23:37:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-12-01 00:51:56 +00:00
|
|
|
{
|
|
|
|
"periodic-cron.hcl",
|
|
|
|
&structs.Job{
|
|
|
|
ID: "foo",
|
|
|
|
Name: "foo",
|
|
|
|
Priority: 50,
|
|
|
|
Region: "global",
|
|
|
|
Type: "service",
|
2015-12-01 16:58:36 +00:00
|
|
|
Periodic: &structs.PeriodicConfig{
|
2016-01-07 19:19:46 +00:00
|
|
|
Enabled: true,
|
|
|
|
SpecType: structs.PeriodicSpecCron,
|
|
|
|
Spec: "*/5 * * *",
|
|
|
|
ProhibitOverlap: true,
|
2015-12-01 00:51:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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-11-16 18:00:06 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"task-nested-config.hcl",
|
|
|
|
&structs.Job{
|
|
|
|
Region: "global",
|
|
|
|
ID: "foo",
|
|
|
|
Name: "foo",
|
|
|
|
Type: "service",
|
|
|
|
Priority: 50,
|
|
|
|
|
|
|
|
TaskGroups: []*structs.TaskGroup{
|
|
|
|
&structs.TaskGroup{
|
|
|
|
Name: "bar",
|
|
|
|
Count: 1,
|
|
|
|
Tasks: []*structs.Task{
|
|
|
|
&structs.Task{
|
|
|
|
Name: "bar",
|
|
|
|
Driver: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"port_map": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"db": 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-02-05 07:28:01 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-11-16 18:00:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-09-15 00:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2015-11-09 06:57:39 +00:00
|
|
|
t.Logf("Testing parse: %s", tc.File)
|
|
|
|
|
2015-09-15 00:43:42 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 20:44:08 +00:00
|
|
|
|
|
|
|
func TestBadPorts(t *testing.T) {
|
|
|
|
path, err := filepath.Abs(filepath.Join("./test-fixtures", "bad-ports.hcl"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't get absoluate path for file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ParseFile(path)
|
|
|
|
|
2015-11-15 08:10:48 +00:00
|
|
|
if !strings.Contains(err.Error(), errPortLabel.Error()) {
|
|
|
|
t.Fatalf("\nExpected error\n %s\ngot\n %v", errPortLabel, err)
|
2015-09-23 20:44:08 +00:00
|
|
|
}
|
2015-09-26 01:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverlappingPorts(t *testing.T) {
|
|
|
|
path, err := filepath.Abs(filepath.Join("./test-fixtures", "overlapping-ports.hcl"))
|
|
|
|
if err != nil {
|
2015-11-18 01:12:21 +00:00
|
|
|
t.Fatalf("Can't get absolute path for file: %s", err)
|
2015-09-26 01:59:17 +00:00
|
|
|
}
|
2015-09-23 20:44:08 +00:00
|
|
|
|
2015-09-26 01:59:17 +00:00
|
|
|
_, err = ParseFile(path)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected an error")
|
|
|
|
}
|
|
|
|
|
2015-12-18 20:33:38 +00:00
|
|
|
if !strings.Contains(err.Error(), "found a port label collision") {
|
2015-09-26 01:59:17 +00:00
|
|
|
t.Fatalf("Expected collision error; got %v", err)
|
|
|
|
}
|
2015-09-23 20:44:08 +00:00
|
|
|
}
|
2015-11-18 01:06:29 +00:00
|
|
|
|
|
|
|
func TestIncompleteServiceDefn(t *testing.T) {
|
|
|
|
path, err := filepath.Abs(filepath.Join("./test-fixtures", "incorrect-service-def.hcl"))
|
|
|
|
if err != nil {
|
2015-11-18 01:12:21 +00:00
|
|
|
t.Fatalf("Can't get absolute path for file: %s", err)
|
2015-11-18 01:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ParseFile(path)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "Only one service block may omit the Name field") {
|
|
|
|
t.Fatalf("Expected collision error; got %v", err)
|
|
|
|
}
|
|
|
|
}
|