open-nomad/api/compose_test.go
Alex Dadgar 1e3c3cb287 Deprecate IOPS
IOPS have been modelled as a resource since Nomad 0.1 but has never
actually been detected and there is no plan in the short term to add
detection. This is because IOPS is a bit simplistic of a unit to define
the performance requirements from the underlying storage system. In its
current state it adds unnecessary confusion and can be removed without
impacting any users. This PR leaves IOPS defined at the jobspec parsing
level and in the api/ resources since these are the two public uses of
the field. These should be considered deprecated and only exist to allow
users to stop using them during the Nomad 0.9.x release. In the future,
there should be no expectation that the field will exist.
2018-12-06 15:09:26 -08:00

147 lines
3.1 KiB
Go

package api
import (
"reflect"
"testing"
"github.com/hashicorp/nomad/helper"
)
func TestCompose(t *testing.T) {
t.Parallel()
// Compose a task
task := NewTask("task1", "exec").
SetConfig("foo", "bar").
SetMeta("foo", "bar").
Constrain(NewConstraint("kernel.name", "=", "linux")).
Require(&Resources{
CPU: helper.IntToPtr(1250),
MemoryMB: helper.IntToPtr(1024),
DiskMB: helper.IntToPtr(2048),
Networks: []*NetworkResource{
{
CIDR: "0.0.0.0/0",
MBits: helper.IntToPtr(100),
ReservedPorts: []Port{{"", 80}, {"", 443}},
},
},
})
// 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", "region1", 2).
SetMeta("foo", "bar").
AddDatacenter("dc1").
Constrain(NewConstraint("kernel.name", "=", "linux")).
AddTaskGroup(grp)
// Check that the composed result looks correct
expect := &Job{
Region: helper.StringToPtr("region1"),
ID: helper.StringToPtr("job1"),
Name: helper.StringToPtr("myjob"),
Type: helper.StringToPtr(JobTypeService),
Priority: helper.IntToPtr(2),
Datacenters: []string{
"dc1",
},
Meta: map[string]string{
"foo": "bar",
},
Constraints: []*Constraint{
{
LTarget: "kernel.name",
RTarget: "linux",
Operand: "=",
},
},
TaskGroups: []*TaskGroup{
{
Name: helper.StringToPtr("grp1"),
Count: helper.IntToPtr(2),
Constraints: []*Constraint{
{
LTarget: "kernel.name",
RTarget: "linux",
Operand: "=",
},
},
Affinities: []*Affinity{
{
LTarget: "${node.class}",
RTarget: "large",
Operand: "=",
Weight: 50,
},
},
Spreads: []*Spread{
{
Attribute: "${node.datacenter}",
Weight: 30,
SpreadTarget: []*SpreadTarget{
{
Value: "dc1",
Percent: 80,
},
{
Value: "dc2",
Percent: 20,
},
},
},
},
Tasks: []*Task{
{
Name: "task1",
Driver: "exec",
Resources: &Resources{
CPU: helper.IntToPtr(1250),
MemoryMB: helper.IntToPtr(1024),
DiskMB: helper.IntToPtr(2048),
Networks: []*NetworkResource{
{
CIDR: "0.0.0.0/0",
MBits: helper.IntToPtr(100),
ReservedPorts: []Port{
{"", 80},
{"", 443},
},
},
},
},
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)
}
}