open-nomad/api/compose_test.go
Seth Hoenig 3943dd1e16 ci: use serial testing for api in CI
This is a followup to running tests in serial in CI.
Since the API package cannot import anything outside of api/,
copy the ci.Parallel function into api/internal/testutil, and
have api tests use that.
2022-03-17 08:35:01 -05:00

147 lines
3.1 KiB
Go

package api
import (
"reflect"
"testing"
"github.com/hashicorp/nomad/api/internal/testutil"
)
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: intToPtr(1250),
MemoryMB: intToPtr(1024),
DiskMB: intToPtr(2048),
Networks: []*NetworkResource{
{
CIDR: "0.0.0.0/0",
MBits: intToPtr(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: stringToPtr("global"),
ID: stringToPtr("job1"),
Name: stringToPtr("myjob"),
Type: stringToPtr(JobTypeService),
Priority: intToPtr(2),
Datacenters: []string{
"dc1",
},
Meta: map[string]string{
"foo": "bar",
},
Constraints: []*Constraint{
{
LTarget: "kernel.name",
RTarget: "linux",
Operand: "=",
},
},
TaskGroups: []*TaskGroup{
{
Name: stringToPtr("grp1"),
Count: intToPtr(2),
Constraints: []*Constraint{
{
LTarget: "kernel.name",
RTarget: "linux",
Operand: "=",
},
},
Affinities: []*Affinity{
{
LTarget: "${node.class}",
RTarget: "large",
Operand: "=",
Weight: int8ToPtr(50),
},
},
Spreads: []*Spread{
{
Attribute: "${node.datacenter}",
Weight: int8ToPtr(30),
SpreadTarget: []*SpreadTarget{
{
Value: "dc1",
Percent: 80,
},
{
Value: "dc2",
Percent: 20,
},
},
},
},
Tasks: []*Task{
{
Name: "task1",
Driver: "exec",
Resources: &Resources{
CPU: intToPtr(1250),
MemoryMB: intToPtr(1024),
DiskMB: intToPtr(2048),
Networks: []*NetworkResource{
{
CIDR: "0.0.0.0/0",
MBits: intToPtr(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",
},
},
},
}
if !reflect.DeepEqual(job, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, job)
}
}