open-nomad/jobspec/parse_test.go

596 lines
14 KiB
Go
Raw Normal View History

2015-09-15 00:43:42 +00:00
package jobspec
import (
"path/filepath"
"reflect"
"strings"
2015-09-15 00:43:42 +00:00
"testing"
"time"
2015-09-15 00:43:42 +00:00
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper"
2015-09-15 00:43:42 +00:00
"github.com/hashicorp/nomad/nomad/structs"
"github.com/kr/pretty"
2016-08-16 22:22:26 +00:00
capi "github.com/hashicorp/consul/api"
2015-09-15 00:43:42 +00:00
)
func TestParse(t *testing.T) {
cases := []struct {
File string
Result *api.Job
2015-09-15 00:43:42 +00:00
Err bool
}{
{
"basic.hcl",
&api.Job{
ID: helper.StringToPtr("binstore-storagelocker"),
Name: helper.StringToPtr("binstore-storagelocker"),
Type: helper.StringToPtr("batch"),
Priority: helper.IntToPtr(52),
AllAtOnce: helper.BoolToPtr(true),
2015-09-15 00:43:42 +00:00
Datacenters: []string{"us2", "eu1"},
Region: helper.StringToPtr("fooregion"),
VaultToken: helper.StringToPtr("foo"),
2015-09-15 00:43:42 +00:00
2015-09-15 00:46:52 +00:00
Meta: map[string]string{
"foo": "bar",
},
Constraints: []*api.Constraint{
&api.Constraint{
2015-09-15 00:48:11 +00:00
LTarget: "kernel.os",
RTarget: "windows",
Operand: "=",
},
},
Update: &api.UpdateStrategy{
Stagger: 60 * time.Second,
MaxParallel: 2,
},
TaskGroups: []*api.TaskGroup{
&api.TaskGroup{
Name: helper.StringToPtr("outside"),
Tasks: []*api.Task{
&api.Task{
2015-09-15 00:43:42 +00:00
Name: "outside",
Driver: "java",
2015-11-15 08:10:48 +00:00
Config: map[string]interface{}{
"jar_path": "s3://my-cool-store/foo.jar",
2015-09-15 00:43:42 +00:00
},
Meta: map[string]string{
"my-cool-key": "foobar",
},
},
},
},
&api.TaskGroup{
Name: helper.StringToPtr("binsl"),
Count: helper.IntToPtr(5),
Constraints: []*api.Constraint{
&api.Constraint{
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",
},
RestartPolicy: &api.RestartPolicy{
Interval: helper.TimeToPtr(10 * time.Minute),
Attempts: helper.IntToPtr(5),
Delay: helper.TimeToPtr(15 * time.Second),
Mode: helper.StringToPtr("delay"),
},
EphemeralDisk: &api.EphemeralDisk{
Sticky: helper.BoolToPtr(true),
SizeMB: helper.IntToPtr(150),
},
Tasks: []*api.Task{
&api.Task{
2015-09-15 00:43:42 +00:00
Name: "binstore",
Driver: "docker",
User: "bob",
2015-11-15 08:10:48 +00:00
Config: map[string]interface{}{
2015-09-15 00:43:42 +00:00
"image": "hashicorp/binstore",
"labels": []map[string]interface{}{
map[string]interface{}{
"FOO": "bar",
},
},
2015-09-15 00:43:42 +00:00
},
2017-03-01 23:30:01 +00:00
Services: []*api.Service{
2015-11-17 06:51:08 +00:00
{
Tags: []string{"foo", "bar"},
PortLabel: "http",
Checks: []api.ServiceCheck{
2015-11-17 06:51:08 +00:00
{
Name: "check-name",
Type: "tcp",
PortLabel: "admin",
Interval: 10 * time.Second,
Timeout: 2 * time.Second,
2015-11-17 06:51:08 +00:00
},
},
},
},
Env: map[string]string{
"HELLO": "world",
"LOREM": "ipsum",
},
Resources: &api.Resources{
CPU: helper.IntToPtr(500),
MemoryMB: helper.IntToPtr(128),
Networks: []*api.NetworkResource{
&api.NetworkResource{
MBits: helper.IntToPtr(100),
ReservedPorts: []api.Port{{Label: "one", Value: 1}, {Label: "two", Value: 2}, {Label: "three", Value: 3}},
DynamicPorts: []api.Port{{Label: "http", Value: 0}, {Label: "https", Value: 0}, {Label: "admin", Value: 0}},
2015-09-15 01:27:37 +00:00
},
},
2015-09-15 00:43:42 +00:00
},
KillTimeout: helper.TimeToPtr(22 * time.Second),
LogConfig: &api.LogConfig{
MaxFiles: helper.IntToPtr(14),
MaxFileSizeMB: helper.IntToPtr(101),
2016-02-05 07:28:01 +00:00
},
Artifacts: []*api.TaskArtifact{
2016-03-14 18:13:43 +00:00
{
GetterSource: helper.StringToPtr("http://foo.com/artifact"),
GetterOptions: map[string]string{
"checksum": "md5:b8a4f3f72ecab0510a6a31e997461c5f",
2016-03-14 18:13:43 +00:00
},
},
{
GetterSource: helper.StringToPtr("http://bar.com/artifact"),
RelativeDest: helper.StringToPtr("test/foo/"),
GetterOptions: map[string]string{
"checksum": "md5:ff1cc0d3432dad54d607c1505fb7245c",
2016-03-14 18:13:43 +00:00
},
},
},
Vault: &api.Vault{
2016-10-11 22:25:49 +00:00
Policies: []string{"foo", "bar"},
Env: helper.BoolToPtr(true),
ChangeMode: helper.StringToPtr(structs.VaultChangeModeRestart),
2016-08-09 23:07:45 +00:00
},
Templates: []*api.Template{
2016-09-26 22:23:26 +00:00
{
SourcePath: helper.StringToPtr("foo"),
DestPath: helper.StringToPtr("foo"),
ChangeMode: helper.StringToPtr("foo"),
ChangeSignal: helper.StringToPtr("foo"),
Splay: helper.TimeToPtr(10 * time.Second),
Perms: helper.StringToPtr("0644"),
2016-09-26 22:23:26 +00:00
},
{
SourcePath: helper.StringToPtr("bar"),
DestPath: helper.StringToPtr("bar"),
ChangeMode: helper.StringToPtr(structs.TemplateChangeModeRestart),
Splay: helper.TimeToPtr(5 * time.Second),
Perms: helper.StringToPtr("777"),
LeftDelim: helper.StringToPtr("--"),
RightDelim: helper.StringToPtr("__"),
2016-09-26 22:23:26 +00:00
},
},
2017-02-11 00:57:47 +00:00
Leader: true,
2015-09-15 00:43:42 +00:00
},
&api.Task{
2015-09-15 00:43:42 +00:00
Name: "storagelocker",
Driver: "docker",
User: "",
2015-11-15 08:10:48 +00:00
Config: map[string]interface{}{
2015-09-15 00:43:42 +00:00
"image": "hashicorp/storagelocker",
},
Resources: &api.Resources{
CPU: helper.IntToPtr(500),
MemoryMB: helper.IntToPtr(128),
IOPS: helper.IntToPtr(30),
2015-09-15 00:43:42 +00:00
},
Constraints: []*api.Constraint{
&api.Constraint{
2015-09-15 00:50:34 +00:00
LTarget: "kernel.arch",
RTarget: "amd64",
Operand: "=",
},
},
Vault: &api.Vault{
2016-10-11 22:25:49 +00:00
Policies: []string{"foo", "bar"},
Env: helper.BoolToPtr(false),
ChangeMode: helper.StringToPtr(structs.VaultChangeModeSignal),
ChangeSignal: helper.StringToPtr("SIGUSR1"),
2016-10-11 22:25:49 +00:00
},
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
2016-08-09 23:07:45 +00:00
{
"multi-vault.hcl",
nil,
true,
},
2015-09-15 01:34:26 +00:00
{
"default-job.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
2015-09-15 01:34:26 +00:00
},
false,
},
{
"version-constraint.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
Constraints: []*api.Constraint{
&api.Constraint{
LTarget: "$attr.kernel.version",
RTarget: "~> 3.2",
Operand: structs.ConstraintVersion,
},
},
},
false,
},
{
"regexp-constraint.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
Constraints: []*api.Constraint{
&api.Constraint{
LTarget: "$attr.kernel.version",
RTarget: "[0-9.]+",
Operand: structs.ConstraintRegex,
},
},
},
false,
},
2016-10-19 20:06:28 +00:00
{
"set-contains-constraint.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
Constraints: []*api.Constraint{
&api.Constraint{
2016-10-19 20:06:28 +00:00
LTarget: "$meta.data",
RTarget: "foo,bar,baz",
Operand: structs.ConstraintSetContains,
},
},
},
false,
},
{
2015-10-23 00:40:41 +00:00
"distinctHosts-constraint.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
Constraints: []*api.Constraint{
&api.Constraint{
Operand: structs.ConstraintDistinctHosts,
},
},
},
false,
},
2017-03-07 22:20:02 +00:00
{
"distinctProperty-constraint.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
Constraints: []*api.Constraint{
&api.Constraint{
Operand: structs.ConstraintDistinctProperty,
LTarget: "${meta.rack}",
},
},
},
false,
},
2015-12-01 00:51:56 +00:00
{
"periodic-cron.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
Periodic: &api.PeriodicConfig{
SpecType: helper.StringToPtr(api.PeriodicSpecCron),
Spec: helper.StringToPtr("*/5 * * *"),
ProhibitOverlap: helper.BoolToPtr(true),
TimeZone: helper.StringToPtr("Europe/Minsk"),
2015-12-01 00:51:56 +00:00
},
},
false,
},
{
"specify-job.hcl",
&api.Job{
ID: helper.StringToPtr("job1"),
Name: helper.StringToPtr("My Job"),
},
false,
},
{
"task-nested-config.hcl",
&api.Job{
ID: helper.StringToPtr("foo"),
Name: helper.StringToPtr("foo"),
TaskGroups: []*api.TaskGroup{
&api.TaskGroup{
Name: helper.StringToPtr("bar"),
Tasks: []*api.Task{
&api.Task{
Name: "bar",
Driver: "docker",
Config: map[string]interface{}{
"image": "hashicorp/image",
"port_map": []map[string]interface{}{
map[string]interface{}{
"db": 1234,
},
},
},
},
},
},
},
},
false,
},
2016-03-16 03:21:52 +00:00
{
"bad-artifact.hcl",
nil,
true,
},
{
"artifacts.hcl",
&api.Job{
ID: helper.StringToPtr("binstore-storagelocker"),
Name: helper.StringToPtr("binstore-storagelocker"),
TaskGroups: []*api.TaskGroup{
&api.TaskGroup{
Name: helper.StringToPtr("binsl"),
Tasks: []*api.Task{
&api.Task{
Name: "binstore",
Driver: "docker",
Artifacts: []*api.TaskArtifact{
{
GetterSource: helper.StringToPtr("http://foo.com/bar"),
2016-06-10 19:28:27 +00:00
GetterOptions: map[string]string{"foo": "bar"},
RelativeDest: helper.StringToPtr(""),
},
{
GetterSource: helper.StringToPtr("http://foo.com/baz"),
2016-06-10 19:28:27 +00:00
GetterOptions: nil,
RelativeDest: nil,
},
{
GetterSource: helper.StringToPtr("http://foo.com/bam"),
2016-06-10 19:28:27 +00:00
GetterOptions: nil,
RelativeDest: helper.StringToPtr("var/foo"),
},
},
},
},
},
},
},
false,
},
2016-08-16 21:34:36 +00:00
{
"service-check-initial-status.hcl",
&api.Job{
ID: helper.StringToPtr("check_initial_status"),
Name: helper.StringToPtr("check_initial_status"),
Type: helper.StringToPtr("service"),
TaskGroups: []*api.TaskGroup{
&api.TaskGroup{
Name: helper.StringToPtr("group"),
Count: helper.IntToPtr(1),
Tasks: []*api.Task{
&api.Task{
2016-08-16 21:34:36 +00:00
Name: "task",
2017-03-01 23:30:01 +00:00
Services: []*api.Service{
2016-08-16 21:34:36 +00:00
{
Tags: []string{"foo", "bar"},
PortLabel: "http",
Checks: []api.ServiceCheck{
2016-08-16 21:34:36 +00:00
{
Name: "check-name",
Type: "http",
Interval: 10 * time.Second,
Timeout: 2 * time.Second,
InitialStatus: capi.HealthPassing,
2016-08-16 21:34:36 +00:00
},
},
},
},
},
},
},
},
},
false,
},
2016-09-21 18:18:44 +00:00
{
// TODO This should be pushed into the API
2016-09-21 18:18:44 +00:00
"vault_inheritance.hcl",
&api.Job{
ID: helper.StringToPtr("example"),
Name: helper.StringToPtr("example"),
TaskGroups: []*api.TaskGroup{
&api.TaskGroup{
Name: helper.StringToPtr("cache"),
Tasks: []*api.Task{
&api.Task{
Name: "redis",
Vault: &api.Vault{
2016-10-11 22:25:49 +00:00
Policies: []string{"group"},
Env: helper.BoolToPtr(true),
ChangeMode: helper.StringToPtr(structs.VaultChangeModeRestart),
2016-09-21 18:18:44 +00:00
},
},
&api.Task{
Name: "redis2",
Vault: &api.Vault{
2016-10-11 22:25:49 +00:00
Policies: []string{"task"},
Env: helper.BoolToPtr(false),
ChangeMode: helper.StringToPtr(structs.VaultChangeModeRestart),
2016-09-21 18:18:44 +00:00
},
},
},
},
&api.TaskGroup{
Name: helper.StringToPtr("cache2"),
Tasks: []*api.Task{
&api.Task{
Name: "redis",
Vault: &api.Vault{
2016-10-11 22:25:49 +00:00
Policies: []string{"job"},
Env: helper.BoolToPtr(true),
ChangeMode: helper.StringToPtr(structs.VaultChangeModeRestart),
2016-09-21 18:18:44 +00:00
},
},
},
},
},
},
false,
},
2016-11-23 23:48:36 +00:00
{
"parameterized_job.hcl",
&api.Job{
ID: helper.StringToPtr("parameterized_job"),
Name: helper.StringToPtr("parameterized_job"),
ParameterizedJob: &api.ParameterizedJobConfig{
2016-12-14 20:50:08 +00:00
Payload: "required",
2016-11-23 23:48:36 +00:00
MetaRequired: []string{"foo", "bar"},
MetaOptional: []string{"baz", "bam"},
},
TaskGroups: []*api.TaskGroup{
{
Name: helper.StringToPtr("foo"),
Tasks: []*api.Task{
{
2016-11-23 23:48:36 +00:00
Name: "bar",
Driver: "docker",
DispatchPayload: &api.DispatchPayloadConfig{
2016-12-14 20:50:08 +00:00
File: "foo/bar",
2016-11-23 23:48:36 +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)
2015-09-15 00:43:42 +00:00
if (err != nil) != tc.Err {
t.Fatalf("file: %s\n\n%s", tc.File, err)
continue
}
if !reflect.DeepEqual(actual, tc.Result) {
for _, d := range pretty.Diff(actual, tc.Result) {
t.Logf(d)
}
t.Fatalf("file: %s", tc.File)
}
2015-09-15 00:43:42 +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 absolute 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)
}
}
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)
}
_, 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") {
t.Fatalf("Expected collision error; got %v", err)
}
}
2016-03-10 18:16:35 +00:00
func TestIncorrectKey(t *testing.T) {
path, err := filepath.Abs(filepath.Join("./test-fixtures", "basic_wrong_key.hcl"))
if err != nil {
t.Fatalf("Can't get absolute path for file: %s", err)
}
_, err = ParseFile(path)
if err == nil {
t.Fatalf("Expected an error")
}
if !strings.Contains(err.Error(), "* group: 'binsl', task: 'binstore', service: 'foo', check -> invalid key: nterval") {
t.Fatalf("Expected key error; got %v", err)
2016-03-10 18:16:35 +00:00
}
}