2015-09-09 20:02:39 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-03-17 13:34:57 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2023-01-01 18:57:26 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2015-09-09 20:02:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompose(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2015-09-09 20:02:39 +00:00
|
|
|
// 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-10-27 21:31:14 +00:00
|
|
|
Constrain(NewConstraint("kernel.name", "=", "linux")).
|
2015-09-10 00:29:43 +00:00
|
|
|
Require(&Resources{
|
2022-08-17 16:26:34 +00:00
|
|
|
CPU: pointerOf(1250),
|
|
|
|
MemoryMB: pointerOf(1024),
|
|
|
|
DiskMB: pointerOf(2048),
|
2016-02-19 23:49:32 +00:00
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2016-02-19 23:49:32 +00:00
|
|
|
CIDR: "0.0.0.0/0",
|
2022-08-17 16:26:34 +00:00
|
|
|
MBits: pointerOf(100),
|
2020-06-19 17:53:31 +00:00
|
|
|
ReservedPorts: []Port{{"", 80, 0, ""}, {"", 443, 0, ""}},
|
2016-02-19 23:49:32 +00:00
|
|
|
},
|
2015-09-09 20:02:39 +00:00
|
|
|
},
|
2016-02-19 23:49:32 +00:00
|
|
|
})
|
2015-09-09 20:02:39 +00:00
|
|
|
|
|
|
|
// Compose a task group
|
2018-07-18 15:53:03 +00:00
|
|
|
|
|
|
|
st1 := NewSpreadTarget("dc1", 80)
|
|
|
|
st2 := NewSpreadTarget("dc2", 20)
|
2015-09-10 00:59:18 +00:00
|
|
|
grp := NewTaskGroup("grp1", 2).
|
2015-10-27 21:31:14 +00:00
|
|
|
Constrain(NewConstraint("kernel.name", "=", "linux")).
|
2018-07-16 13:30:58 +00:00
|
|
|
AddAffinity(NewAffinity("${node.class}", "=", "large", 50)).
|
2018-07-18 15:53:03 +00:00
|
|
|
AddSpread(NewSpread("${node.datacenter}", 30, []*SpreadTarget{st1, st2})).
|
2015-09-09 20:02:39 +00:00
|
|
|
SetMeta("foo", "bar").
|
|
|
|
AddTask(task)
|
|
|
|
|
2015-09-10 00:29:43 +00:00
|
|
|
// Compose a job
|
2019-05-02 20:00:21 +00:00
|
|
|
job := NewServiceJob("job1", "myjob", "global", 2).
|
2015-09-10 00:29:43 +00:00
|
|
|
SetMeta("foo", "bar").
|
|
|
|
AddDatacenter("dc1").
|
2015-10-27 21:31:14 +00:00
|
|
|
Constrain(NewConstraint("kernel.name", "=", "linux")).
|
2015-09-10 00:29:43 +00:00
|
|
|
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{
|
2022-08-17 16:26:34 +00:00
|
|
|
Region: pointerOf("global"),
|
|
|
|
ID: pointerOf("job1"),
|
|
|
|
Name: pointerOf("myjob"),
|
|
|
|
Type: pointerOf(JobTypeService),
|
|
|
|
Priority: pointerOf(2),
|
2015-09-10 00:59:18 +00:00
|
|
|
Datacenters: []string{
|
|
|
|
"dc1",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2015-09-09 20:02:39 +00:00
|
|
|
Constraints: []*Constraint{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-09 20:02:39 +00:00
|
|
|
LTarget: "kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 00:29:43 +00:00
|
|
|
TaskGroups: []*TaskGroup{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2022-08-17 16:26:34 +00:00
|
|
|
Name: pointerOf("grp1"),
|
|
|
|
Count: pointerOf(2),
|
2015-09-10 00:29:43 +00:00
|
|
|
Constraints: []*Constraint{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-10 00:29:43 +00:00
|
|
|
LTarget: "kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
2018-07-16 13:30:58 +00:00
|
|
|
Affinities: []*Affinity{
|
|
|
|
{
|
|
|
|
LTarget: "${node.class}",
|
|
|
|
RTarget: "large",
|
|
|
|
Operand: "=",
|
2022-08-17 16:26:34 +00:00
|
|
|
Weight: pointerOf(int8(50)),
|
2018-07-16 13:30:58 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-18 15:53:03 +00:00
|
|
|
Spreads: []*Spread{
|
|
|
|
{
|
|
|
|
Attribute: "${node.datacenter}",
|
2022-08-17 16:26:34 +00:00
|
|
|
Weight: pointerOf(int8(30)),
|
2018-07-18 15:53:03 +00:00
|
|
|
SpreadTarget: []*SpreadTarget{
|
|
|
|
{
|
|
|
|
Value: "dc1",
|
|
|
|
Percent: 80,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "dc2",
|
|
|
|
Percent: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 00:29:43 +00:00
|
|
|
Tasks: []*Task{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-10 00:59:18 +00:00
|
|
|
Name: "task1",
|
|
|
|
Driver: "exec",
|
2015-09-10 00:29:43 +00:00
|
|
|
Resources: &Resources{
|
2022-08-17 16:26:34 +00:00
|
|
|
CPU: pointerOf(1250),
|
|
|
|
MemoryMB: pointerOf(1024),
|
|
|
|
DiskMB: pointerOf(2048),
|
2015-09-10 00:29:43 +00:00
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-10 00:59:18 +00:00
|
|
|
CIDR: "0.0.0.0/0",
|
2022-08-17 16:26:34 +00:00
|
|
|
MBits: pointerOf(100),
|
2015-11-15 01:30:36 +00:00
|
|
|
ReservedPorts: []Port{
|
2020-06-19 17:53:31 +00:00
|
|
|
{"", 80, 0, ""},
|
|
|
|
{"", 443, 0, ""},
|
2015-09-10 00:59:18 +00:00
|
|
|
},
|
2015-09-10 00:29:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Constraints: []*Constraint{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-10 00:29:43 +00:00
|
|
|
LTarget: "kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
},
|
|
|
|
},
|
2015-11-15 01:30:36 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-10 00:29:43 +00:00
|
|
|
"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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, expect, job)
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|