2015-09-09 20:02:39 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompose(t *testing.T) {
|
|
|
|
// Compose a task
|
2015-09-10 00:59:18 +00:00
|
|
|
task := NewTask("task1", "exec").
|
2015-09-09 20:02:39 +00:00
|
|
|
SetConfig("foo", "bar").
|
2015-09-10 00:59:18 +00:00
|
|
|
SetMeta("foo", "bar").
|
2015-09-10 00:29:43 +00:00
|
|
|
Constrain(HardConstraint("kernel.name", "=", "linux")).
|
|
|
|
Require(&Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 1250,
|
2015-09-09 20:02:39 +00:00
|
|
|
MemoryMB: 1024,
|
|
|
|
DiskMB: 2048,
|
|
|
|
IOPS: 1024,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
CIDR: "0.0.0.0/0",
|
|
|
|
MBits: 100,
|
|
|
|
ReservedPorts: []int{80, 443},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Compose a task group
|
2015-09-10 00:59:18 +00:00
|
|
|
grp := NewTaskGroup("grp1", 2).
|
2015-09-09 20:02:39 +00:00
|
|
|
Constrain(HardConstraint("kernel.name", "=", "linux")).
|
|
|
|
SetMeta("foo", "bar").
|
|
|
|
AddTask(task)
|
|
|
|
|
2015-09-10 00:29:43 +00:00
|
|
|
// Compose a job
|
2015-09-16 18:42:08 +00:00
|
|
|
job := NewServiceJob("job1", "myjob", "region1", 2).
|
2015-09-10 00:29:43 +00:00
|
|
|
SetMeta("foo", "bar").
|
|
|
|
AddDatacenter("dc1").
|
|
|
|
Constrain(HardConstraint("kernel.name", "=", "linux")).
|
|
|
|
AddTaskGroup(grp)
|
|
|
|
|
2015-09-09 20:02:39 +00:00
|
|
|
// Check that the composed result looks correct
|
2015-09-10 00:29:43 +00:00
|
|
|
expect := &Job{
|
2015-09-16 18:42:08 +00:00
|
|
|
Region: "region1",
|
2015-09-10 00:59:18 +00:00
|
|
|
ID: "job1",
|
|
|
|
Name: "myjob",
|
|
|
|
Type: JobTypeService,
|
|
|
|
Priority: 2,
|
|
|
|
Datacenters: []string{
|
|
|
|
"dc1",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2015-09-09 20:02:39 +00:00
|
|
|
Constraints: []*Constraint{
|
|
|
|
&Constraint{
|
|
|
|
Hard: true,
|
|
|
|
LTarget: "kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 00:29:43 +00:00
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
&TaskGroup{
|
2015-09-10 00:59:18 +00:00
|
|
|
Name: "grp1",
|
2015-09-10 00:29:43 +00:00
|
|
|
Count: 2,
|
|
|
|
Constraints: []*Constraint{
|
|
|
|
&Constraint{
|
|
|
|
Hard: true,
|
|
|
|
LTarget: "kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Tasks: []*Task{
|
|
|
|
&Task{
|
2015-09-10 00:59:18 +00:00
|
|
|
Name: "task1",
|
|
|
|
Driver: "exec",
|
2015-09-10 00:29:43 +00:00
|
|
|
Resources: &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 1250,
|
2015-09-10 00:29:43 +00:00
|
|
|
MemoryMB: 1024,
|
|
|
|
DiskMB: 2048,
|
|
|
|
IOPS: 1024,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-10 00:59:18 +00:00
|
|
|
CIDR: "0.0.0.0/0",
|
|
|
|
MBits: 100,
|
|
|
|
ReservedPorts: []int{
|
|
|
|
80,
|
|
|
|
443,
|
|
|
|
},
|
2015-09-10 00:29:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Constraints: []*Constraint{
|
|
|
|
&Constraint{
|
|
|
|
Hard: true,
|
|
|
|
LTarget: "kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
Weight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Config: map[string]string{
|
|
|
|
"foo": "bar",
|
2015-09-10 00:59:18 +00:00
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"foo": "bar",
|
2015-09-09 20:02:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 00:29:43 +00:00
|
|
|
Meta: map[string]string{
|
2015-09-09 20:02:39 +00:00
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-10 00:29:43 +00:00
|
|
|
if !reflect.DeepEqual(job, expect) {
|
|
|
|
t.Fatalf("expect: %#v, got: %#v", expect, job)
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
}
|