2015-08-05 00:19:05 +00:00
|
|
|
package structs
|
|
|
|
|
2015-09-07 22:08:50 +00:00
|
|
|
import (
|
2016-08-31 20:40:43 +00:00
|
|
|
"fmt"
|
2016-10-20 20:55:35 +00:00
|
|
|
"reflect"
|
2015-09-07 22:08:50 +00:00
|
|
|
"regexp"
|
2016-10-27 18:48:00 +00:00
|
|
|
"sort"
|
2015-09-07 22:08:50 +00:00
|
|
|
"testing"
|
|
|
|
)
|
2015-08-05 00:19:05 +00:00
|
|
|
|
|
|
|
func TestRemoveAllocs(t *testing.T) {
|
|
|
|
l := []*Allocation{
|
|
|
|
&Allocation{ID: "foo"},
|
|
|
|
&Allocation{ID: "bar"},
|
|
|
|
&Allocation{ID: "baz"},
|
|
|
|
&Allocation{ID: "zip"},
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:52:56 +00:00
|
|
|
out := RemoveAllocs(l, []*Allocation{l[1], l[3]})
|
2015-08-05 00:19:05 +00:00
|
|
|
if len(out) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
if out[0].ID != "foo" && out[1].ID != "baz" {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 00:28:19 +00:00
|
|
|
|
2015-12-15 03:20:57 +00:00
|
|
|
func TestFilterTerminalAllocs(t *testing.T) {
|
2015-08-23 01:27:51 +00:00
|
|
|
l := []*Allocation{
|
2016-08-31 20:40:43 +00:00
|
|
|
&Allocation{
|
|
|
|
ID: "bar",
|
|
|
|
Name: "myname1",
|
|
|
|
DesiredStatus: AllocDesiredStatusEvict,
|
|
|
|
},
|
2015-08-25 23:18:37 +00:00
|
|
|
&Allocation{ID: "baz", DesiredStatus: AllocDesiredStatusStop},
|
2016-04-08 21:22:06 +00:00
|
|
|
&Allocation{
|
|
|
|
ID: "foo",
|
|
|
|
DesiredStatus: AllocDesiredStatusRun,
|
|
|
|
ClientStatus: AllocClientStatusPending,
|
|
|
|
},
|
|
|
|
&Allocation{
|
|
|
|
ID: "bam",
|
2016-08-31 20:40:43 +00:00
|
|
|
Name: "myname",
|
2016-04-08 21:22:06 +00:00
|
|
|
DesiredStatus: AllocDesiredStatusRun,
|
|
|
|
ClientStatus: AllocClientStatusComplete,
|
2016-08-31 20:40:43 +00:00
|
|
|
CreateIndex: 5,
|
|
|
|
},
|
|
|
|
&Allocation{
|
|
|
|
ID: "lol",
|
|
|
|
Name: "myname",
|
|
|
|
DesiredStatus: AllocDesiredStatusRun,
|
|
|
|
ClientStatus: AllocClientStatusComplete,
|
|
|
|
CreateIndex: 2,
|
2016-04-08 21:22:06 +00:00
|
|
|
},
|
2015-08-23 01:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:36:30 +00:00
|
|
|
out, terminalAllocs := FilterTerminalAllocs(l)
|
2016-04-08 21:22:06 +00:00
|
|
|
if len(out) != 1 {
|
2015-08-23 01:27:51 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
2016-04-08 21:22:06 +00:00
|
|
|
if out[0].ID != "foo" {
|
2015-08-23 01:27:51 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
2016-08-30 22:36:30 +00:00
|
|
|
|
2016-08-31 20:40:43 +00:00
|
|
|
if len(terminalAllocs) != 3 {
|
|
|
|
for _, o := range terminalAllocs {
|
|
|
|
fmt.Printf("%#v \n", o)
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:36:30 +00:00
|
|
|
t.Fatalf("bad: %#v", terminalAllocs)
|
|
|
|
}
|
2016-08-31 20:40:43 +00:00
|
|
|
|
|
|
|
if terminalAllocs["myname"].ID != "bam" {
|
|
|
|
t.Fatalf("bad: %#v", terminalAllocs["myname"])
|
|
|
|
}
|
2015-08-23 01:27:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
func TestAllocsFit_PortsOvercommitted(t *testing.T) {
|
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-15 18:12:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "10.0.0.0/8",
|
|
|
|
MBits: 100,
|
2015-09-13 21:56:51 +00:00
|
|
|
},
|
2015-08-05 00:28:19 +00:00
|
|
|
},
|
2015-09-13 21:56:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
a1 := &Allocation{
|
2016-08-27 03:38:50 +00:00
|
|
|
Job: &Job{
|
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
2016-09-14 22:43:42 +00:00
|
|
|
Name: "web",
|
|
|
|
EphemeralDisk: DefaultEphemeralDisk(),
|
2016-08-27 03:38:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-09-13 21:56:51 +00:00
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"web": &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-15 18:12:46 +00:00
|
|
|
Device: "eth0",
|
2015-09-13 21:56:51 +00:00
|
|
|
IP: "10.0.0.1",
|
|
|
|
MBits: 50,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"main", 8000}},
|
2015-09-13 21:56:51 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-05 00:28:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
|
|
|
|
// Should fit one allocation
|
2015-09-15 18:12:46 +00:00
|
|
|
fit, dim, _, err := AllocsFit(n, []*Allocation{a1}, nil)
|
2015-09-13 21:56:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !fit {
|
2015-09-15 18:12:46 +00:00
|
|
|
t.Fatalf("Bad: %s", dim)
|
2015-08-05 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
// Should not fit second allocation
|
2015-09-14 01:38:11 +00:00
|
|
|
fit, _, _, err = AllocsFit(n, []*Allocation{a1, a1}, nil)
|
2015-09-13 21:56:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if fit {
|
|
|
|
t.Fatalf("Bad")
|
2015-08-05 00:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-05 00:48:24 +00:00
|
|
|
|
|
|
|
func TestAllocsFit(t *testing.T) {
|
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 2000,
|
2015-08-05 00:48:24 +00:00
|
|
|
MemoryMB: 2048,
|
|
|
|
DiskMB: 10000,
|
|
|
|
IOPS: 100,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-15 18:12:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "10.0.0.0/8",
|
|
|
|
MBits: 100,
|
2015-08-05 00:48:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 1000,
|
2015-08-05 00:48:24 +00:00
|
|
|
MemoryMB: 1024,
|
|
|
|
DiskMB: 5000,
|
|
|
|
IOPS: 50,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-15 18:12:46 +00:00
|
|
|
Device: "eth0",
|
2015-09-13 21:56:51 +00:00
|
|
|
IP: "10.0.0.1",
|
2015-08-05 00:48:24 +00:00
|
|
|
MBits: 50,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"main", 80}},
|
2015-08-05 00:48:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
a1 := &Allocation{
|
|
|
|
Resources: &Resources{
|
2015-09-23 18:14:32 +00:00
|
|
|
CPU: 1000,
|
2015-08-05 00:48:24 +00:00
|
|
|
MemoryMB: 1024,
|
|
|
|
DiskMB: 5000,
|
|
|
|
IOPS: 50,
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-15 18:12:46 +00:00
|
|
|
Device: "eth0",
|
2015-09-13 21:56:51 +00:00
|
|
|
IP: "10.0.0.1",
|
2015-08-05 00:48:24 +00:00
|
|
|
MBits: 50,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"main", 8000}},
|
2015-08-05 00:48:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fit one allocation
|
2015-09-14 01:38:11 +00:00
|
|
|
fit, _, used, err := AllocsFit(n, []*Allocation{a1}, nil)
|
2015-08-05 00:48:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !fit {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
|
2015-08-13 18:54:59 +00:00
|
|
|
// Sanity check the used resources
|
2015-09-23 18:14:32 +00:00
|
|
|
if used.CPU != 2000 {
|
2015-08-13 18:54:59 +00:00
|
|
|
t.Fatalf("bad: %#v", used)
|
|
|
|
}
|
|
|
|
if used.MemoryMB != 2048 {
|
|
|
|
t.Fatalf("bad: %#v", used)
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:48:24 +00:00
|
|
|
// Should not fit second allocation
|
2015-09-14 01:38:11 +00:00
|
|
|
fit, _, used, err = AllocsFit(n, []*Allocation{a1, a1}, nil)
|
2015-08-05 00:48:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if fit {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
2015-08-13 18:54:59 +00:00
|
|
|
|
|
|
|
// Sanity check the used resources
|
2015-09-23 18:14:32 +00:00
|
|
|
if used.CPU != 3000 {
|
2015-08-13 18:54:59 +00:00
|
|
|
t.Fatalf("bad: %#v", used)
|
|
|
|
}
|
|
|
|
if used.MemoryMB != 3072 {
|
|
|
|
t.Fatalf("bad: %#v", used)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScoreFit(t *testing.T) {
|
|
|
|
node := &Node{}
|
|
|
|
node.Resources = &Resources{
|
|
|
|
CPU: 4096,
|
|
|
|
MemoryMB: 8192,
|
|
|
|
}
|
|
|
|
node.Reserved = &Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 4096,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a perfect fit
|
|
|
|
util := &Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 4096,
|
|
|
|
}
|
|
|
|
score := ScoreFit(node, util)
|
|
|
|
if score != 18.0 {
|
|
|
|
t.Fatalf("bad: %v", score)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the worst fit
|
|
|
|
util = &Resources{
|
|
|
|
CPU: 0,
|
|
|
|
MemoryMB: 0,
|
|
|
|
}
|
|
|
|
score = ScoreFit(node, util)
|
|
|
|
if score != 0.0 {
|
|
|
|
t.Fatalf("bad: %v", score)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a mid-case scenario
|
|
|
|
util = &Resources{
|
|
|
|
CPU: 1024,
|
|
|
|
MemoryMB: 2048,
|
|
|
|
}
|
|
|
|
score = ScoreFit(node, util)
|
|
|
|
if score < 10.0 || score > 16.0 {
|
|
|
|
t.Fatalf("bad: %v", score)
|
|
|
|
}
|
2015-08-05 00:48:24 +00:00
|
|
|
}
|
2015-09-07 22:08:50 +00:00
|
|
|
|
|
|
|
func TestGenerateUUID(t *testing.T) {
|
|
|
|
prev := GenerateUUID()
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
id := GenerateUUID()
|
|
|
|
if prev == id {
|
|
|
|
t.Fatalf("Should get a new ID!")
|
|
|
|
}
|
|
|
|
|
|
|
|
matched, err := regexp.MatchString(
|
|
|
|
"[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
|
|
|
|
if !matched || err != nil {
|
|
|
|
t.Fatalf("expected match %s %v %s", id, matched, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 00:50:14 +00:00
|
|
|
|
|
|
|
func TestSliceStringIsSubset(t *testing.T) {
|
|
|
|
l := []string{"a", "b", "c"}
|
|
|
|
s := []string{"d"}
|
|
|
|
|
|
|
|
sub, offending := SliceStringIsSubset(l, l[:1])
|
|
|
|
if !sub || len(offending) != 0 {
|
|
|
|
t.Fatalf("bad %v %v", sub, offending)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, offending = SliceStringIsSubset(l, s)
|
|
|
|
if sub || len(offending) == 0 || offending[0] != "d" {
|
|
|
|
t.Fatalf("bad %v %v", sub, offending)
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 20:55:35 +00:00
|
|
|
|
|
|
|
func TestMapStringStringSliceValueSet(t *testing.T) {
|
|
|
|
m := map[string][]string{
|
|
|
|
"foo": []string{"1", "2"},
|
|
|
|
"bar": []string{"3"},
|
|
|
|
"baz": nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
act := MapStringStringSliceValueSet(m)
|
|
|
|
exp := []string{"1", "2", "3"}
|
2016-10-27 18:48:00 +00:00
|
|
|
sort.Strings(act)
|
2016-10-20 20:55:35 +00:00
|
|
|
if !reflect.DeepEqual(act, exp) {
|
2016-10-27 18:48:00 +00:00
|
|
|
t.Fatalf("Bad; got %v; want %v", act, exp)
|
2016-10-20 20:55:35 +00:00
|
|
|
}
|
|
|
|
}
|