cd75858f4a
* api: swap testify for test (acl) * api: swap testify for test (agent) Please enter the commit message for your changes. Lines starting * api: swap testify for test (allocations) * api: swap testify for test (api) * api: swap testify for test (compose) * api: swap testify for test (constraint) * api: swap testify for test (consul) * api: swap testify for test (csi) * api: swap testify for test (evaluations) * api: swap testify for test (event stream) * api: swap testify for test (fs) * api: swap testify for test (ioutil) * api: swap testify for test (jobs) * api: swap testify for test (keyring) * api: swap testify for test (operator_ent) * api: swap testify for test (operator_metrics) * api: swap testify for test (operator) * api: swap testify for test (quota) * api: swap testify for test (resources) * api: swap testify for test (fix operator_metrics) * api: swap testify for test (scaling) * api: swap testify for test (search) * api: swap testify for test (sentinel) * api: swap testify for test (services) * api: swap testify for test (status) * api: swap testify for test (system) * api: swap testify for test (tasks) * api: swap testify for test (utils) * api: swap testify for test (variables) * api: remove dependencies on testify and pretty
145 lines
3.1 KiB
Go
145 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
|
"github.com/shoenig/test/must"
|
|
)
|
|
|
|
func TestCompose(t *testing.T) {
|
|
testutil.Parallel(t)
|
|
// Compose a task
|
|
task := NewTask("task1", "exec").
|
|
SetConfig("foo", "bar").
|
|
SetMeta("foo", "bar").
|
|
Constrain(NewConstraint("kernel.name", "=", "linux")).
|
|
Require(&Resources{
|
|
CPU: pointerOf(1250),
|
|
MemoryMB: pointerOf(1024),
|
|
DiskMB: pointerOf(2048),
|
|
Networks: []*NetworkResource{
|
|
{
|
|
CIDR: "0.0.0.0/0",
|
|
MBits: pointerOf(100),
|
|
ReservedPorts: []Port{{"", 80, 0, ""}, {"", 443, 0, ""}},
|
|
},
|
|
},
|
|
})
|
|
|
|
// Compose a task group
|
|
|
|
st1 := NewSpreadTarget("dc1", 80)
|
|
st2 := NewSpreadTarget("dc2", 20)
|
|
grp := NewTaskGroup("grp1", 2).
|
|
Constrain(NewConstraint("kernel.name", "=", "linux")).
|
|
AddAffinity(NewAffinity("${node.class}", "=", "large", 50)).
|
|
AddSpread(NewSpread("${node.datacenter}", 30, []*SpreadTarget{st1, st2})).
|
|
SetMeta("foo", "bar").
|
|
AddTask(task)
|
|
|
|
// Compose a job
|
|
job := NewServiceJob("job1", "myjob", "global", 2).
|
|
SetMeta("foo", "bar").
|
|
AddDatacenter("dc1").
|
|
Constrain(NewConstraint("kernel.name", "=", "linux")).
|
|
AddTaskGroup(grp)
|
|
|
|
// Check that the composed result looks correct
|
|
expect := &Job{
|
|
Region: pointerOf("global"),
|
|
ID: pointerOf("job1"),
|
|
Name: pointerOf("myjob"),
|
|
Type: pointerOf(JobTypeService),
|
|
Priority: pointerOf(2),
|
|
Datacenters: []string{
|
|
"dc1",
|
|
},
|
|
Meta: map[string]string{
|
|
"foo": "bar",
|
|
},
|
|
Constraints: []*Constraint{
|
|
{
|
|
LTarget: "kernel.name",
|
|
RTarget: "linux",
|
|
Operand: "=",
|
|
},
|
|
},
|
|
TaskGroups: []*TaskGroup{
|
|
{
|
|
Name: pointerOf("grp1"),
|
|
Count: pointerOf(2),
|
|
Constraints: []*Constraint{
|
|
{
|
|
LTarget: "kernel.name",
|
|
RTarget: "linux",
|
|
Operand: "=",
|
|
},
|
|
},
|
|
Affinities: []*Affinity{
|
|
{
|
|
LTarget: "${node.class}",
|
|
RTarget: "large",
|
|
Operand: "=",
|
|
Weight: pointerOf(int8(50)),
|
|
},
|
|
},
|
|
Spreads: []*Spread{
|
|
{
|
|
Attribute: "${node.datacenter}",
|
|
Weight: pointerOf(int8(30)),
|
|
SpreadTarget: []*SpreadTarget{
|
|
{
|
|
Value: "dc1",
|
|
Percent: 80,
|
|
},
|
|
{
|
|
Value: "dc2",
|
|
Percent: 20,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Tasks: []*Task{
|
|
{
|
|
Name: "task1",
|
|
Driver: "exec",
|
|
Resources: &Resources{
|
|
CPU: pointerOf(1250),
|
|
MemoryMB: pointerOf(1024),
|
|
DiskMB: pointerOf(2048),
|
|
Networks: []*NetworkResource{
|
|
{
|
|
CIDR: "0.0.0.0/0",
|
|
MBits: pointerOf(100),
|
|
ReservedPorts: []Port{
|
|
{"", 80, 0, ""},
|
|
{"", 443, 0, ""},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Constraints: []*Constraint{
|
|
{
|
|
LTarget: "kernel.name",
|
|
RTarget: "linux",
|
|
Operand: "=",
|
|
},
|
|
},
|
|
Config: map[string]interface{}{
|
|
"foo": "bar",
|
|
},
|
|
Meta: map[string]string{
|
|
"foo": "bar",
|
|
},
|
|
},
|
|
},
|
|
Meta: map[string]string{
|
|
"foo": "bar",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
must.Eq(t, expect, job)
|
|
}
|