2015-06-05 22:21:17 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2015-09-15 18:23:03 +00:00
|
|
|
"strings"
|
2015-06-05 22:21:17 +00:00
|
|
|
"testing"
|
2015-11-02 21:24:59 +00:00
|
|
|
"time"
|
2015-12-01 00:51:56 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2015-06-05 22:21:17 +00:00
|
|
|
)
|
|
|
|
|
2015-09-15 18:23:03 +00:00
|
|
|
func TestJob_Validate(t *testing.T) {
|
|
|
|
j := &Job{}
|
|
|
|
err := j.Validate()
|
|
|
|
mErr := err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "job region") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[1].Error(), "job ID") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[2].Error(), "job name") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[3].Error(), "job type") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[4].Error(), "priority") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[5].Error(), "datacenters") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[6].Error(), "task groups") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-12-01 00:51:56 +00:00
|
|
|
j = &Job{
|
|
|
|
Type: JobTypeService,
|
2015-12-01 16:40:32 +00:00
|
|
|
Periodic: &PeriodicConfig{
|
2015-12-01 00:51:56 +00:00
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = j.Validate()
|
|
|
|
mErr = err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Error(), "Periodic") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-15 18:23:03 +00:00
|
|
|
j = &Job{
|
|
|
|
Region: "global",
|
|
|
|
ID: GenerateUUID(),
|
|
|
|
Name: "my-job",
|
|
|
|
Type: JobTypeService,
|
|
|
|
Priority: 50,
|
|
|
|
Datacenters: []string{"dc1"},
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
&TaskGroup{
|
|
|
|
Name: "web",
|
2015-11-02 21:24:59 +00:00
|
|
|
RestartPolicy: &RestartPolicy{
|
|
|
|
Interval: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
Attempts: 10,
|
|
|
|
},
|
2015-09-15 18:23:03 +00:00
|
|
|
},
|
|
|
|
&TaskGroup{
|
|
|
|
Name: "web",
|
2015-11-02 21:24:59 +00:00
|
|
|
RestartPolicy: &RestartPolicy{
|
|
|
|
Interval: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
Attempts: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&TaskGroup{
|
|
|
|
RestartPolicy: &RestartPolicy{
|
|
|
|
Interval: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
Attempts: 10,
|
|
|
|
},
|
2015-09-15 18:23:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = j.Validate()
|
|
|
|
mErr = err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "2 redefines 'web' from group 1") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[1].Error(), "group 3 missing name") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[2].Error(), "Task group 1 validation failed") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-21 01:43:17 +00:00
|
|
|
func testJob() *Job {
|
|
|
|
return &Job{
|
2016-02-21 01:36:39 +00:00
|
|
|
Region: "global",
|
|
|
|
ID: GenerateUUID(),
|
|
|
|
Name: "my-job",
|
|
|
|
Type: JobTypeService,
|
|
|
|
Priority: 50,
|
|
|
|
AllAtOnce: false,
|
|
|
|
Datacenters: []string{"dc1"},
|
|
|
|
Constraints: []*Constraint{
|
|
|
|
&Constraint{
|
|
|
|
LTarget: "$attr.kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Periodic: &PeriodicConfig{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
&TaskGroup{
|
|
|
|
Name: "web",
|
|
|
|
Count: 10,
|
|
|
|
RestartPolicy: &RestartPolicy{
|
|
|
|
Attempts: 3,
|
|
|
|
Interval: 10 * time.Minute,
|
|
|
|
Delay: 1 * time.Minute,
|
|
|
|
},
|
|
|
|
Tasks: []*Task{
|
|
|
|
&Task{
|
|
|
|
Name: "web",
|
|
|
|
Driver: "exec",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"command": "/bin/date",
|
|
|
|
},
|
|
|
|
Env: map[string]string{
|
|
|
|
"FOO": "bar",
|
|
|
|
},
|
2016-03-16 03:21:52 +00:00
|
|
|
Artifacts: []*TaskArtifact{
|
|
|
|
{
|
|
|
|
GetterSource: "http://foo.com",
|
|
|
|
},
|
|
|
|
},
|
2016-02-21 01:36:39 +00:00
|
|
|
Services: []*Service{
|
|
|
|
{
|
|
|
|
Name: "${TASK}-frontend",
|
|
|
|
PortLabel: "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resources: &Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
MBits: 50,
|
|
|
|
DynamicPorts: []Port{{Label: "http"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"elb_check_type": "http",
|
|
|
|
"elb_check_interval": "30s",
|
|
|
|
"elb_check_min": "3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"owner": "armon",
|
|
|
|
},
|
|
|
|
}
|
2016-02-21 01:43:17 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
|
2016-02-21 01:43:17 +00:00
|
|
|
func TestJob_Copy(t *testing.T) {
|
|
|
|
j := testJob()
|
2015-12-18 20:26:28 +00:00
|
|
|
c := j.Copy()
|
|
|
|
if !reflect.DeepEqual(j, c) {
|
2016-02-11 17:08:20 +00:00
|
|
|
t.Fatalf("Copy() returned an unequal Job; got %#v; want %#v", c, j)
|
2015-12-18 20:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:40:32 +00:00
|
|
|
func TestJob_IsPeriodic(t *testing.T) {
|
|
|
|
j := &Job{
|
|
|
|
Type: JobTypeService,
|
|
|
|
Periodic: &PeriodicConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !j.IsPeriodic() {
|
|
|
|
t.Fatalf("IsPeriodic() returned false on periodic job")
|
|
|
|
}
|
|
|
|
|
|
|
|
j = &Job{
|
|
|
|
Type: JobTypeService,
|
|
|
|
}
|
|
|
|
if j.IsPeriodic() {
|
|
|
|
t.Fatalf("IsPeriodic() returned true on non-periodic job")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 18:23:03 +00:00
|
|
|
func TestTaskGroup_Validate(t *testing.T) {
|
2015-11-02 21:24:59 +00:00
|
|
|
tg := &TaskGroup{
|
|
|
|
RestartPolicy: &RestartPolicy{
|
2016-02-02 23:08:07 +00:00
|
|
|
Interval: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
Attempts: 10,
|
|
|
|
Mode: RestartPolicyModeDelay,
|
2015-11-02 21:24:59 +00:00
|
|
|
},
|
|
|
|
}
|
2015-09-15 18:23:03 +00:00
|
|
|
err := tg.Validate()
|
|
|
|
mErr := err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "group name") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[1].Error(), "count must be positive") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[2].Error(), "Missing tasks") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tg = &TaskGroup{
|
|
|
|
Name: "web",
|
|
|
|
Count: 1,
|
|
|
|
Tasks: []*Task{
|
|
|
|
&Task{Name: "web"},
|
|
|
|
&Task{Name: "web"},
|
|
|
|
&Task{},
|
|
|
|
},
|
2015-11-02 21:24:59 +00:00
|
|
|
RestartPolicy: &RestartPolicy{
|
2016-02-02 23:08:07 +00:00
|
|
|
Interval: 5 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
Attempts: 10,
|
|
|
|
Mode: RestartPolicyModeDelay,
|
2015-11-02 21:24:59 +00:00
|
|
|
},
|
2015-09-15 18:23:03 +00:00
|
|
|
}
|
|
|
|
err = tg.Validate()
|
|
|
|
mErr = err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "2 redefines 'web' from task 1") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[1].Error(), "Task 3 missing name") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[2].Error(), "Task 1 validation failed") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTask_Validate(t *testing.T) {
|
|
|
|
task := &Task{}
|
|
|
|
err := task.Validate()
|
|
|
|
mErr := err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "task name") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[1].Error(), "task driver") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(mErr.Errors[2].Error(), "task resources") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
task = &Task{
|
2016-02-02 21:50:30 +00:00
|
|
|
Name: "web",
|
|
|
|
Driver: "docker",
|
|
|
|
Resources: &Resources{
|
|
|
|
CPU: 100,
|
2016-02-12 06:50:34 +00:00
|
|
|
DiskMB: 200,
|
2016-02-02 21:50:30 +00:00
|
|
|
MemoryMB: 100,
|
|
|
|
IOPS: 10,
|
|
|
|
},
|
2016-02-11 18:42:56 +00:00
|
|
|
LogConfig: DefaultLogConfig(),
|
2015-09-15 18:23:03 +00:00
|
|
|
}
|
|
|
|
err = task.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:30:47 +00:00
|
|
|
func TestTask_Validate_LogConfig(t *testing.T) {
|
|
|
|
task := &Task{
|
|
|
|
LogConfig: DefaultLogConfig(),
|
|
|
|
Resources: &Resources{
|
|
|
|
DiskMB: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := task.Validate()
|
|
|
|
mErr := err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[3].Error(), "log storage") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:50:16 +00:00
|
|
|
func TestConstraint_Validate(t *testing.T) {
|
|
|
|
c := &Constraint{}
|
|
|
|
err := c.Validate()
|
|
|
|
mErr := err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "Missing constraint operand") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c = &Constraint{
|
|
|
|
LTarget: "$attr.kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
}
|
|
|
|
err = c.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform additional regexp validation
|
2015-10-26 20:47:56 +00:00
|
|
|
c.Operand = ConstraintRegex
|
2015-10-11 19:50:16 +00:00
|
|
|
c.RTarget = "(foo"
|
|
|
|
err = c.Validate()
|
|
|
|
mErr = err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "missing closing") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform version validation
|
2015-10-26 20:47:56 +00:00
|
|
|
c.Operand = ConstraintVersion
|
2015-10-11 19:50:16 +00:00
|
|
|
c.RTarget = "~> foo"
|
|
|
|
err = c.Validate()
|
|
|
|
mErr = err.(*multierror.Error)
|
|
|
|
if !strings.Contains(mErr.Errors[0].Error(), "Malformed constraint") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 22:04:36 +00:00
|
|
|
func TestResource_NetIndex(t *testing.T) {
|
2015-08-05 00:23:42 +00:00
|
|
|
r := &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
2015-09-13 22:04:36 +00:00
|
|
|
&NetworkResource{Device: "eth0"},
|
|
|
|
&NetworkResource{Device: "lo0"},
|
|
|
|
&NetworkResource{Device: ""},
|
2015-08-05 00:23:42 +00:00
|
|
|
},
|
|
|
|
}
|
2015-09-13 22:04:36 +00:00
|
|
|
if idx := r.NetIndex(&NetworkResource{Device: "eth0"}); idx != 0 {
|
2015-08-05 00:23:42 +00:00
|
|
|
t.Fatalf("Bad: %d", idx)
|
|
|
|
}
|
2015-09-13 22:04:36 +00:00
|
|
|
if idx := r.NetIndex(&NetworkResource{Device: "lo0"}); idx != 1 {
|
2015-08-05 00:23:42 +00:00
|
|
|
t.Fatalf("Bad: %d", idx)
|
|
|
|
}
|
2015-09-13 22:04:36 +00:00
|
|
|
if idx := r.NetIndex(&NetworkResource{Device: "eth1"}); idx != -1 {
|
2015-09-12 23:21:57 +00:00
|
|
|
t.Fatalf("Bad: %d", idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:32:57 +00:00
|
|
|
func TestResource_Superset(t *testing.T) {
|
|
|
|
r1 := &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 2000,
|
2015-08-05 00:32:57 +00:00
|
|
|
MemoryMB: 2048,
|
|
|
|
DiskMB: 10000,
|
|
|
|
IOPS: 100,
|
|
|
|
}
|
|
|
|
r2 := &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 2000,
|
2015-08-05 00:32:57 +00:00
|
|
|
MemoryMB: 1024,
|
|
|
|
DiskMB: 5000,
|
|
|
|
IOPS: 50,
|
|
|
|
}
|
|
|
|
|
2015-09-14 01:38:11 +00:00
|
|
|
if s, _ := r1.Superset(r1); !s {
|
2015-08-05 00:32:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-14 01:38:11 +00:00
|
|
|
if s, _ := r1.Superset(r2); !s {
|
2015-08-05 00:32:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-14 01:38:11 +00:00
|
|
|
if s, _ := r2.Superset(r1); s {
|
2015-08-05 00:32:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-14 01:38:11 +00:00
|
|
|
if s, _ := r2.Superset(r2); !s {
|
2015-08-05 00:32:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:41:02 +00:00
|
|
|
func TestResource_Add(t *testing.T) {
|
|
|
|
r1 := &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 2000,
|
2015-08-05 00:41:02 +00:00
|
|
|
MemoryMB: 2048,
|
|
|
|
DiskMB: 10000,
|
|
|
|
IOPS: 100,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
CIDR: "10.0.0.0/8",
|
|
|
|
MBits: 100,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"ssh", 22}},
|
2015-08-05 00:41:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r2 := &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 2000,
|
2015-08-05 00:41:02 +00:00
|
|
|
MemoryMB: 1024,
|
|
|
|
DiskMB: 5000,
|
|
|
|
IOPS: 50,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-12 23:33:41 +00:00
|
|
|
IP: "10.0.0.1",
|
2015-08-05 00:41:02 +00:00
|
|
|
MBits: 50,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"web", 80}},
|
2015-08-05 00:41:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r1.Add(r2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 3000,
|
2015-08-05 00:41:02 +00:00
|
|
|
MemoryMB: 3072,
|
|
|
|
DiskMB: 15000,
|
|
|
|
IOPS: 150,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
CIDR: "10.0.0.0/8",
|
|
|
|
MBits: 150,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"ssh", 22}, {"web", 80}},
|
2015-08-05 00:41:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect.Networks, r1.Networks) {
|
|
|
|
t.Fatalf("bad: %#v %#v", expect, r1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 00:04:09 +00:00
|
|
|
func TestResource_Add_Network(t *testing.T) {
|
|
|
|
r1 := &Resources{}
|
|
|
|
r2 := &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
MBits: 50,
|
2015-11-15 09:56:21 +00:00
|
|
|
DynamicPorts: []Port{{"http", 0}, {"https", 0}},
|
2015-09-13 00:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r3 := &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
MBits: 25,
|
2015-11-15 09:56:21 +00:00
|
|
|
DynamicPorts: []Port{{"admin", 0}},
|
2015-09-13 00:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r1.Add(r2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
err = r1.Add(r3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
MBits: 75,
|
2015-11-15 09:56:21 +00:00
|
|
|
DynamicPorts: []Port{{"http", 0}, {"https", 0}, {"admin", 0}},
|
2015-09-13 00:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expect.Networks, r1.Networks) {
|
2015-09-14 01:38:11 +00:00
|
|
|
t.Fatalf("bad: %#v %#v", expect.Networks[0], r1.Networks[0])
|
2015-09-13 00:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:21:17 +00:00
|
|
|
func TestEncodeDecode(t *testing.T) {
|
|
|
|
type FooRequest struct {
|
|
|
|
Foo string
|
|
|
|
Bar int
|
|
|
|
Baz bool
|
|
|
|
}
|
|
|
|
arg := &FooRequest{
|
|
|
|
Foo: "test",
|
|
|
|
Bar: 42,
|
|
|
|
Baz: true,
|
|
|
|
}
|
|
|
|
buf, err := Encode(1, arg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out FooRequest
|
|
|
|
err = Decode(buf[1:], &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(arg, &out) {
|
|
|
|
t.Fatalf("bad: %#v %#v", arg, out)
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 07:56:11 +00:00
|
|
|
|
2016-02-21 01:43:17 +00:00
|
|
|
func BenchmarkEncodeDecode(b *testing.B) {
|
|
|
|
job := testJob()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf, err := Encode(1, job)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out Job
|
|
|
|
err = Decode(buf[1:], &out)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:36:59 +00:00
|
|
|
func TestInvalidServiceCheck(t *testing.T) {
|
|
|
|
s := Service{
|
|
|
|
Name: "service-name",
|
|
|
|
PortLabel: "bar",
|
2015-11-26 20:40:42 +00:00
|
|
|
Checks: []*ServiceCheck{
|
2015-11-17 21:36:59 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
Name: "check-name",
|
|
|
|
Type: "lol",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := s.Validate(); err == nil {
|
2016-03-15 00:54:49 +00:00
|
|
|
t.Fatalf("Service should be invalid (invalid type)")
|
2015-11-17 07:56:11 +00:00
|
|
|
}
|
2016-02-05 22:42:35 +00:00
|
|
|
|
|
|
|
s = Service{
|
|
|
|
Name: "service.name",
|
|
|
|
PortLabel: "bar",
|
|
|
|
}
|
2016-02-05 23:15:56 +00:00
|
|
|
if err := s.Validate(); err == nil {
|
2016-03-15 00:54:49 +00:00
|
|
|
t.Fatalf("Service should be invalid (contains a dot): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s = Service{
|
|
|
|
Name: "-my-service",
|
|
|
|
PortLabel: "bar",
|
|
|
|
}
|
|
|
|
if err := s.Validate(); err == nil {
|
|
|
|
t.Fatalf("Service should be invalid (begins with a hyphen): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s = Service{
|
|
|
|
Name: "abcdef0123456789-abcdef0123456789-abcdef0123456789-abcdef0123456",
|
|
|
|
PortLabel: "bar",
|
|
|
|
}
|
|
|
|
if err := s.Validate(); err == nil {
|
|
|
|
t.Fatalf("Service should be invalid (too long): %v", err)
|
2016-02-05 22:42:35 +00:00
|
|
|
}
|
2015-11-17 07:56:11 +00:00
|
|
|
}
|
2015-11-21 20:34:01 +00:00
|
|
|
|
2015-12-14 23:57:56 +00:00
|
|
|
func TestDistinctCheckID(t *testing.T) {
|
2015-11-21 20:34:01 +00:00
|
|
|
c1 := ServiceCheck{
|
|
|
|
Name: "web-health",
|
|
|
|
Type: "http",
|
|
|
|
Path: "/health",
|
|
|
|
Interval: 2 * time.Second,
|
|
|
|
Timeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
c2 := ServiceCheck{
|
|
|
|
Name: "web-health",
|
|
|
|
Type: "http",
|
|
|
|
Path: "/health1",
|
|
|
|
Interval: 2 * time.Second,
|
|
|
|
Timeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
c3 := ServiceCheck{
|
|
|
|
Name: "web-health",
|
|
|
|
Type: "http",
|
|
|
|
Path: "/health",
|
|
|
|
Interval: 4 * time.Second,
|
|
|
|
Timeout: 3 * time.Second,
|
|
|
|
}
|
2015-12-14 23:57:56 +00:00
|
|
|
serviceID := "123"
|
|
|
|
c1Hash := c1.Hash(serviceID)
|
|
|
|
c2Hash := c2.Hash(serviceID)
|
|
|
|
c3Hash := c3.Hash(serviceID)
|
2015-11-21 20:34:01 +00:00
|
|
|
|
2015-11-26 21:47:02 +00:00
|
|
|
if c1Hash == c2Hash || c1Hash == c3Hash || c3Hash == c2Hash {
|
|
|
|
t.Fatalf("Checks need to be uniq c1: %s, c2: %s, c3: %s", c1Hash, c2Hash, c3Hash)
|
2015-11-21 20:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-11-27 03:26:00 +00:00
|
|
|
|
2015-12-14 23:47:01 +00:00
|
|
|
func TestService_InitFields(t *testing.T) {
|
2015-11-27 03:26:00 +00:00
|
|
|
job := "example"
|
|
|
|
taskGroup := "cache"
|
|
|
|
task := "redis"
|
|
|
|
|
|
|
|
s := Service{
|
|
|
|
Name: "${TASK}-db",
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:04:04 +00:00
|
|
|
s.InitFields(job, taskGroup, task)
|
2015-11-27 03:26:00 +00:00
|
|
|
if s.Name != "redis-db" {
|
|
|
|
t.Fatalf("Expected name: %v, Actual: %v", "redis-db", s.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Name = "db"
|
2015-12-11 00:04:04 +00:00
|
|
|
s.InitFields(job, taskGroup, task)
|
2015-11-27 03:26:00 +00:00
|
|
|
if s.Name != "db" {
|
|
|
|
t.Fatalf("Expected name: %v, Actual: %v", "redis-db", s.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Name = "${JOB}-${TASKGROUP}-${TASK}-db"
|
2015-12-11 00:04:04 +00:00
|
|
|
s.InitFields(job, taskGroup, task)
|
2015-11-27 03:26:00 +00:00
|
|
|
if s.Name != "example-cache-redis-db" {
|
|
|
|
t.Fatalf("Expected name: %v, Actual: %v", "expample-cache-redis-db", s.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Name = "${BASE}-db"
|
2015-12-11 00:04:04 +00:00
|
|
|
s.InitFields(job, taskGroup, task)
|
2015-11-27 03:26:00 +00:00
|
|
|
if s.Name != "example-cache-redis-db" {
|
|
|
|
t.Fatalf("Expected name: %v, Actual: %v", "expample-cache-redis-db", s.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJob_ExpandServiceNames(t *testing.T) {
|
|
|
|
j := &Job{
|
|
|
|
Name: "my-job",
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
&TaskGroup{
|
|
|
|
Name: "web",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Name: "frontend",
|
|
|
|
Services: []*Service{
|
|
|
|
{
|
|
|
|
Name: "${BASE}-default",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "jmx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&TaskGroup{
|
|
|
|
Name: "admin",
|
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Name: "admin-web",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
j.InitFields()
|
2015-11-27 03:26:00 +00:00
|
|
|
|
|
|
|
service1Name := j.TaskGroups[0].Tasks[0].Services[0].Name
|
|
|
|
if service1Name != "my-job-web-frontend-default" {
|
|
|
|
t.Fatalf("Expected Service Name: %s, Actual: %s", "my-job-web-frontend-default", service1Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
service2Name := j.TaskGroups[0].Tasks[0].Services[1].Name
|
|
|
|
if service2Name != "jmx" {
|
|
|
|
t.Fatalf("Expected Service Name: %s, Actual: %s", "jmx", service2Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-12-01 00:51:56 +00:00
|
|
|
|
|
|
|
func TestPeriodicConfig_EnabledInvalid(t *testing.T) {
|
|
|
|
// Create a config that is enabled but with no interval specified.
|
|
|
|
p := &PeriodicConfig{Enabled: true}
|
|
|
|
if err := p.Validate(); err == nil {
|
|
|
|
t.Fatal("Enabled PeriodicConfig with no spec or type shouldn't be valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a config that is enabled, with a spec but no type specified.
|
|
|
|
p = &PeriodicConfig{Enabled: true, Spec: "foo"}
|
|
|
|
if err := p.Validate(); err == nil {
|
|
|
|
t.Fatal("Enabled PeriodicConfig with no spec type shouldn't be valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a config that is enabled, with a spec type but no spec specified.
|
|
|
|
p = &PeriodicConfig{Enabled: true, SpecType: PeriodicSpecCron}
|
|
|
|
if err := p.Validate(); err == nil {
|
|
|
|
t.Fatal("Enabled PeriodicConfig with no spec shouldn't be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeriodicConfig_InvalidCron(t *testing.T) {
|
|
|
|
specs := []string{"foo", "* *", "@foo"}
|
|
|
|
for _, spec := range specs {
|
|
|
|
p := &PeriodicConfig{Enabled: true, SpecType: PeriodicSpecCron, Spec: spec}
|
|
|
|
if err := p.Validate(); err == nil {
|
|
|
|
t.Fatal("Invalid cron spec")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeriodicConfig_ValidCron(t *testing.T) {
|
|
|
|
specs := []string{"0 0 29 2 *", "@hourly", "0 0-15 * * *"}
|
|
|
|
for _, spec := range specs {
|
|
|
|
p := &PeriodicConfig{Enabled: true, SpecType: PeriodicSpecCron, Spec: spec}
|
|
|
|
if err := p.Validate(); err != nil {
|
|
|
|
t.Fatal("Passed valid cron")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeriodicConfig_NextCron(t *testing.T) {
|
|
|
|
from := time.Date(2009, time.November, 10, 23, 22, 30, 0, time.UTC)
|
|
|
|
specs := []string{"0 0 29 2 * 1980", "*/5 * * * *"}
|
|
|
|
expected := []time.Time{time.Time{}, time.Date(2009, time.November, 10, 23, 25, 0, 0, time.UTC)}
|
|
|
|
for i, spec := range specs {
|
|
|
|
p := &PeriodicConfig{Enabled: true, SpecType: PeriodicSpecCron, Spec: spec}
|
|
|
|
n := p.Next(from)
|
|
|
|
if expected[i] != n {
|
|
|
|
t.Fatalf("Next(%v) returned %v; want %v", from, n, expected[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 22:32:30 +00:00
|
|
|
|
|
|
|
func TestRestartPolicy_Validate(t *testing.T) {
|
|
|
|
// Policy with acceptable restart options passes
|
|
|
|
p := &RestartPolicy{
|
|
|
|
Mode: RestartPolicyModeFail,
|
|
|
|
Attempts: 0,
|
|
|
|
}
|
|
|
|
if err := p.Validate(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy with ambiguous restart options fails
|
|
|
|
p = &RestartPolicy{
|
|
|
|
Mode: RestartPolicyModeDelay,
|
|
|
|
Attempts: 0,
|
|
|
|
}
|
|
|
|
if err := p.Validate(); err == nil || !strings.Contains(err.Error(), "ambiguous") {
|
|
|
|
t.Fatalf("expect ambiguity error, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad policy mode fails
|
|
|
|
p = &RestartPolicy{
|
|
|
|
Mode: "nope",
|
|
|
|
Attempts: 1,
|
|
|
|
}
|
|
|
|
if err := p.Validate(); err == nil || !strings.Contains(err.Error(), "mode") {
|
|
|
|
t.Fatalf("expect mode error, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fails when attempts*delay does not fit inside interval
|
|
|
|
p = &RestartPolicy{
|
|
|
|
Mode: RestartPolicyModeDelay,
|
|
|
|
Attempts: 3,
|
|
|
|
Delay: 5 * time.Second,
|
|
|
|
Interval: time.Second,
|
|
|
|
}
|
|
|
|
if err := p.Validate(); err == nil || !strings.Contains(err.Error(), "can't restart") {
|
|
|
|
t.Fatalf("expect restart interval error, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 02:09:51 +00:00
|
|
|
|
|
|
|
func TestAllocation_Index(t *testing.T) {
|
|
|
|
a1 := Allocation{Name: "example.cache[0]"}
|
|
|
|
e1 := 0
|
|
|
|
a2 := Allocation{Name: "ex[123]am123ple.c311ac[123]he12[1][77]"}
|
|
|
|
e2 := 77
|
|
|
|
|
|
|
|
if a1.Index() != e1 || a2.Index() != e2 {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 22:46:06 +00:00
|
|
|
|
2016-03-15 02:55:30 +00:00
|
|
|
func TestTaskArtifact_Validate_Source(t *testing.T) {
|
|
|
|
valid := &TaskArtifact{GetterSource: "google.com"}
|
|
|
|
if err := valid.Validate(); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 22:46:06 +00:00
|
|
|
func TestTaskArtifact_Validate_Checksum(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input *TaskArtifact
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&TaskArtifact{
|
|
|
|
GetterSource: "foo.com",
|
|
|
|
GetterOptions: map[string]string{
|
|
|
|
"checksum": "no-type",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&TaskArtifact{
|
|
|
|
GetterSource: "foo.com",
|
|
|
|
GetterOptions: map[string]string{
|
|
|
|
"checksum": "md5:toosmall",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&TaskArtifact{
|
|
|
|
GetterSource: "foo.com",
|
|
|
|
GetterOptions: map[string]string{
|
|
|
|
"checksum": "invalid:type",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := tc.Input.Validate()
|
|
|
|
if (err != nil) != tc.Err {
|
|
|
|
t.Fatalf("case %d: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|