2015-09-13 21:35:28 +00:00
|
|
|
package structs
|
2015-09-13 02:34:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
func TestNetworkIndex_Overcommitted(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
|
|
|
|
|
|
|
// Consume some network
|
|
|
|
reserved := &NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 505,
|
|
|
|
ReservedPorts: []int{8000, 9000},
|
|
|
|
}
|
|
|
|
collide := idx.AddReserved(reserved)
|
|
|
|
if collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !idx.Overcommitted() {
|
|
|
|
t.Fatalf("have no resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add resources
|
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.SetNode(n)
|
|
|
|
if idx.Overcommitted() {
|
|
|
|
t.Fatalf("have resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double up our ussage
|
|
|
|
idx.AddReserved(reserved)
|
|
|
|
if !idx.Overcommitted() {
|
|
|
|
t.Fatalf("should be overcommitted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 02:34:46 +00:00
|
|
|
func TestNetworkIndex_SetNode(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
2015-09-13 21:35:28 +00:00
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
ReservedPorts: []int{22},
|
|
|
|
MBits: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
collide := idx.SetNode(n)
|
|
|
|
if collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
|
|
|
|
if len(idx.AvailNetworks) != 1 {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if idx.AvailBandwidth["eth0"] != 1000 {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if idx.UsedBandwidth["eth0"] != 1 {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if _, ok := idx.UsedPorts["192.168.0.100"][22]; !ok {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkIndex_AddAllocs(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
2015-09-13 21:35:28 +00:00
|
|
|
allocs := []*Allocation{
|
|
|
|
&Allocation{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"web": &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 20,
|
|
|
|
ReservedPorts: []int{8000, 9000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-09-13 21:35:28 +00:00
|
|
|
&Allocation{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"api": &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 50,
|
|
|
|
ReservedPorts: []int{10000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
collide := idx.AddAllocs(allocs)
|
|
|
|
if collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
|
|
|
|
if idx.UsedBandwidth["eth0"] != 70 {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if _, ok := idx.UsedPorts["192.168.0.100"][8000]; !ok {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if _, ok := idx.UsedPorts["192.168.0.100"][9000]; !ok {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if _, ok := idx.UsedPorts["192.168.0.100"][10000]; !ok {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:31:01 +00:00
|
|
|
func TestNetworkIndex_AddReserved(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
|
|
|
|
2015-09-13 21:35:28 +00:00
|
|
|
reserved := &NetworkResource{
|
2015-09-13 21:31:01 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 20,
|
|
|
|
ReservedPorts: []int{8000, 9000},
|
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
collide := idx.AddReserved(reserved)
|
|
|
|
if collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-13 21:31:01 +00:00
|
|
|
|
|
|
|
if idx.UsedBandwidth["eth0"] != 20 {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if _, ok := idx.UsedPorts["192.168.0.100"][8000]; !ok {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if _, ok := idx.UsedPorts["192.168.0.100"][9000]; !ok {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
|
|
|
|
// Try to reserve the same network
|
|
|
|
collide = idx.AddReserved(reserved)
|
|
|
|
if !collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-13 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 02:34:46 +00:00
|
|
|
func TestNetworkIndex_yieldIP(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
2015-09-13 21:35:28 +00:00
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/30",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
ReservedPorts: []int{22},
|
|
|
|
MBits: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
idx.SetNode(n)
|
|
|
|
|
|
|
|
var out []string
|
2015-09-13 21:35:28 +00:00
|
|
|
idx.yieldIP(func(n *NetworkResource, ip net.IP) (stop bool) {
|
2015-09-13 02:34:46 +00:00
|
|
|
out = append(out, ip.String())
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
expect := []string{"192.168.0.100", "192.168.0.101",
|
|
|
|
"192.168.0.102", "192.168.0.103"}
|
|
|
|
if !reflect.DeepEqual(out, expect) {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkIndex_AssignNetwork(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
2015-09-13 21:35:28 +00:00
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/30",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
ReservedPorts: []int{22},
|
|
|
|
MBits: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
idx.SetNode(n)
|
|
|
|
|
2015-09-13 21:35:28 +00:00
|
|
|
allocs := []*Allocation{
|
|
|
|
&Allocation{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"web": &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 20,
|
|
|
|
ReservedPorts: []int{8000, 9000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-09-13 21:35:28 +00:00
|
|
|
&Allocation{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"api": &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
&NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 50,
|
|
|
|
ReservedPorts: []int{10000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.AddAllocs(allocs)
|
|
|
|
|
|
|
|
// Ask for a reserved port
|
2015-09-13 21:35:28 +00:00
|
|
|
ask := &NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
ReservedPorts: []int{8000},
|
|
|
|
}
|
2015-09-13 23:40:53 +00:00
|
|
|
offer, err := idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.101" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
if len(offer.ReservedPorts) != 1 || offer.ReservedPorts[0] != 8000 {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for dynamic ports
|
2015-09-13 21:35:28 +00:00
|
|
|
ask = &NetworkResource{
|
2015-09-22 20:33:16 +00:00
|
|
|
DynamicPorts: []string{"http", "https", "admin"},
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
2015-09-13 23:40:53 +00:00
|
|
|
offer, err = idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.100" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
if len(offer.ReservedPorts) != 3 {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for reserved + dynamic ports
|
2015-09-13 21:35:28 +00:00
|
|
|
ask = &NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
ReservedPorts: []int{12345},
|
2015-09-22 20:33:16 +00:00
|
|
|
DynamicPorts: []string{"http", "https", "admin"},
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
2015-09-13 23:40:53 +00:00
|
|
|
offer, err = idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.100" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
if len(offer.ReservedPorts) != 4 || offer.ReservedPorts[0] != 12345 {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for too much bandwidth
|
2015-09-13 21:35:28 +00:00
|
|
|
ask = &NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
MBits: 1000,
|
|
|
|
}
|
2015-09-13 23:40:53 +00:00
|
|
|
offer, err = idx.AssignNetwork(ask)
|
|
|
|
if err.Error() != "bandwidth exceeded" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
if offer != nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntContains(t *testing.T) {
|
|
|
|
l := []int{1, 2, 10, 20}
|
2015-09-13 21:36:33 +00:00
|
|
|
if IntContains(l, 50) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-13 21:36:33 +00:00
|
|
|
if !IntContains(l, 20) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-13 21:36:33 +00:00
|
|
|
if !IntContains(l, 1) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|