api: more tests

This commit is contained in:
Ryan Uber
2015-09-09 17:59:18 -07:00
parent 9fea4bf6cb
commit dc8a672acd
3 changed files with 254 additions and 23 deletions

View File

@@ -7,8 +7,9 @@ import (
func TestCompose(t *testing.T) {
// Compose a task
task := NewTask("mytask", "docker").
task := NewTask("task1", "exec").
SetConfig("foo", "bar").
SetMeta("foo", "bar").
Constrain(HardConstraint("kernel.name", "=", "linux")).
Require(&Resources{
CPU: 1.25,
@@ -25,8 +26,7 @@ func TestCompose(t *testing.T) {
})
// Compose a task group
grp := NewTaskGroup("mygroup").
SetCount(2).
grp := NewTaskGroup("grp1", 2).
Constrain(HardConstraint("kernel.name", "=", "linux")).
SetMeta("foo", "bar").
AddTask(task)
@@ -40,11 +40,16 @@ func TestCompose(t *testing.T) {
// Check that the composed result looks correct
expect := &Job{
ID: "job1",
Name: "myjob",
Type: JobTypeService,
Priority: 2,
Datacenters: []string{"dc1"},
ID: "job1",
Name: "myjob",
Type: JobTypeService,
Priority: 2,
Datacenters: []string{
"dc1",
},
Meta: map[string]string{
"foo": "bar",
},
Constraints: []*Constraint{
&Constraint{
Hard: true,
@@ -56,7 +61,7 @@ func TestCompose(t *testing.T) {
},
TaskGroups: []*TaskGroup{
&TaskGroup{
Name: "mygroup",
Name: "grp1",
Count: 2,
Constraints: []*Constraint{
&Constraint{
@@ -69,8 +74,8 @@ func TestCompose(t *testing.T) {
},
Tasks: []*Task{
&Task{
Name: "mytask",
Driver: "docker",
Name: "task1",
Driver: "exec",
Resources: &Resources{
CPU: 1.25,
MemoryMB: 1024,
@@ -78,9 +83,12 @@ func TestCompose(t *testing.T) {
IOPS: 1024,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "0.0.0.0/0",
MBits: 100,
ReservedPorts: []int{80, 443},
CIDR: "0.0.0.0/0",
MBits: 100,
ReservedPorts: []int{
80,
443,
},
},
},
},
@@ -95,13 +103,14 @@ func TestCompose(t *testing.T) {
},
Config: map[string]string{
"foo": "bar",
"baz": "zip",
},
Meta: map[string]string{
"foo": "bar",
},
},
},
Meta: map[string]string{
"foo": "bar",
"baz": "zip",
},
},
},

View File

@@ -10,18 +10,13 @@ type TaskGroup struct {
}
// NewTaskGroup creates a new TaskGroup.
func NewTaskGroup(name string) *TaskGroup {
func NewTaskGroup(name string, count int) *TaskGroup {
return &TaskGroup{
Name: name,
Count: 1,
Count: count,
}
}
func (g *TaskGroup) SetCount(count int) *TaskGroup {
g.Count = count
return g
}
// Constrain is used to add a constraint to a task group.
func (g *TaskGroup) Constrain(c *Constraint) *TaskGroup {
g.Constraints = append(g.Constraints, c)

227
api/tasks_test.go Normal file
View File

@@ -0,0 +1,227 @@
package api
import (
"reflect"
"testing"
)
func TestTaskGroup_NewTaskGroup(t *testing.T) {
grp := NewTaskGroup("grp1", 2)
expect := &TaskGroup{
Name: "grp1",
Count: 2,
}
if !reflect.DeepEqual(grp, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, grp)
}
}
func TestTaskGroup_Constrain(t *testing.T) {
grp := NewTaskGroup("grp1", 1)
// Add a constraint to the group
out := grp.Constrain(HardConstraint("kernel.name", "=", "darwin"))
if n := len(grp.Constraints); n != 1 {
t.Fatalf("expected 1 constraint, got: %d", n)
}
// Check that the group was returned
if out != grp {
t.Fatalf("expected: %#v, got: %#v", grp, out)
}
// Add a second constraint
grp.Constrain(SoftConstraint("memory.totalbytes", ">=", "128000000", 2))
expect := []*Constraint{
&Constraint{
Hard: true,
LTarget: "kernel.name",
RTarget: "darwin",
Operand: "=",
Weight: 0,
},
&Constraint{
Hard: false,
LTarget: "memory.totalbytes",
RTarget: "128000000",
Operand: ">=",
Weight: 2,
},
}
if !reflect.DeepEqual(grp.Constraints, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, grp.Constraints)
}
}
func TestTaskGroup_SetMeta(t *testing.T) {
grp := NewTaskGroup("grp1", 1)
// Initializes an empty map
out := grp.SetMeta("foo", "bar")
if grp.Meta == nil {
t.Fatalf("should be initialized")
}
// Check that we returned the group
if out != grp {
t.Fatalf("expect: %#v, got: %#v", grp, out)
}
// Add a second meta k/v
grp.SetMeta("baz", "zip")
expect := map[string]string{"foo": "bar", "baz": "zip"}
if !reflect.DeepEqual(grp.Meta, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, grp.Meta)
}
}
func TestTaskGroup_AddTask(t *testing.T) {
grp := NewTaskGroup("grp1", 1)
// Add the task to the task group
out := grp.AddTask(NewTask("task1", "java"))
if n := len(grp.Tasks); n != 1 {
t.Fatalf("expected 1 task, got: %d", n)
}
// Check that we returned the group
if out != grp {
t.Fatalf("expect: %#v, got: %#v", grp, out)
}
// Add a second task
grp.AddTask(NewTask("task2", "exec"))
expect := []*Task{
&Task{
Name: "task1",
Driver: "java",
},
&Task{
Name: "task2",
Driver: "exec",
},
}
if !reflect.DeepEqual(grp.Tasks, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, grp.Tasks)
}
}
func TestTask_NewTask(t *testing.T) {
task := NewTask("task1", "exec")
expect := &Task{
Name: "task1",
Driver: "exec",
}
if !reflect.DeepEqual(task, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, task)
}
}
func TestTask_SetConfig(t *testing.T) {
task := NewTask("task1", "exec")
// Initializes an empty map
out := task.SetConfig("foo", "bar")
if task.Config == nil {
t.Fatalf("should be initialized")
}
// Check that we returned the task
if out != task {
t.Fatalf("expect: %#v, got: %#v", task, out)
}
// Set another config value
task.SetConfig("baz", "zip")
expect := map[string]string{"foo": "bar", "baz": "zip"}
if !reflect.DeepEqual(task.Config, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, task.Config)
}
}
func TestTask_SetMeta(t *testing.T) {
task := NewTask("task1", "exec")
// Initializes an empty map
out := task.SetMeta("foo", "bar")
if task.Meta == nil {
t.Fatalf("should be initialized")
}
// Check that we returned the task
if out != task {
t.Fatalf("expect: %#v, got: %#v", task, out)
}
// Set another meta k/v
task.SetMeta("baz", "zip")
expect := map[string]string{"foo": "bar", "baz": "zip"}
if !reflect.DeepEqual(task.Meta, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, task.Meta)
}
}
func TestTask_Require(t *testing.T) {
task := NewTask("task1", "exec")
// Create some require resources
resources := &Resources{
CPU: 1.25,
MemoryMB: 128,
DiskMB: 2048,
IOPS: 1024,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "0.0.0.0/0",
MBits: 100,
ReservedPorts: []int{80, 443},
},
},
}
out := task.Require(resources)
if !reflect.DeepEqual(task.Resources, resources) {
t.Fatalf("expect: %#v, got: %#v", resources, task.Resources)
}
// Check that we returned the task
if out != task {
t.Fatalf("expect: %#v, got: %#v", task, out)
}
}
func TestTask_Constrain(t *testing.T) {
task := NewTask("task1", "exec")
// Add a constraint to the task
out := task.Constrain(HardConstraint("kernel.name", "=", "darwin"))
if n := len(task.Constraints); n != 1 {
t.Fatalf("expected 1 constraint, got: %d", n)
}
// Check that the task was returned
if out != task {
t.Fatalf("expected: %#v, got: %#v", task, out)
}
// Add a second constraint
task.Constrain(SoftConstraint("memory.totalbytes", ">=", "128000000", 2))
expect := []*Constraint{
&Constraint{
Hard: true,
LTarget: "kernel.name",
RTarget: "darwin",
Operand: "=",
Weight: 0,
},
&Constraint{
Hard: false,
LTarget: "memory.totalbytes",
RTarget: "128000000",
Operand: ">=",
Weight: 2,
},
}
if !reflect.DeepEqual(task.Constraints, expect) {
t.Fatalf("expect: %#v, got: %#v", expect, task.Constraints)
}
}