2015-09-13 21:35:28 +00:00
|
|
|
package structs
|
2015-09-13 02:34:46 +00:00
|
|
|
|
|
|
|
import (
|
2018-10-02 20:36:04 +00:00
|
|
|
"fmt"
|
2015-09-13 02:34:46 +00:00
|
|
|
"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,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"one", 8000}, {"two", 9000}},
|
2015-09-13 21:56:51 +00:00
|
|
|
}
|
|
|
|
collide := idx.AddReserved(reserved)
|
|
|
|
if collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !idx.Overcommitted() {
|
|
|
|
t.Fatalf("have no resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add resources
|
|
|
|
n := &Node{
|
2018-10-02 20:36:04 +00:00
|
|
|
NodeResources: &NodeResources{
|
2015-09-13 21:56:51 +00:00
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:56:51 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.SetNode(n)
|
|
|
|
if idx.Overcommitted() {
|
|
|
|
t.Fatalf("have resources")
|
|
|
|
}
|
|
|
|
|
2018-03-11 19:11:59 +00:00
|
|
|
// Double up our usage
|
2015-09-13 21:56:51 +00:00
|
|
|
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{
|
2018-10-02 20:36:04 +00:00
|
|
|
NodeResources: &NodeResources{
|
2015-09-13 21:35:28 +00:00
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:35:28 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
2018-10-02 20:36:04 +00:00
|
|
|
IP: "192.168.0.100",
|
2015-09-13 21:35:28 +00:00
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-02 20:36:04 +00:00
|
|
|
ReservedResources: &NodeReservedResources{
|
|
|
|
Networks: NodeReservedNetworkResources{
|
|
|
|
ReservedHostPorts: "22",
|
2015-09-13 21:35:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
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")
|
|
|
|
}
|
2016-02-20 20:08:27 +00:00
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(22) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkIndex_AddAllocs(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
2015-09-13 21:35:28 +00:00
|
|
|
allocs := []*Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2018-10-02 20:36:04 +00:00
|
|
|
AllocatedResources: &AllocatedResources{
|
|
|
|
Tasks: map[string]*AllocatedTaskResources{
|
|
|
|
"web": {
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 20,
|
|
|
|
ReservedPorts: []Port{{"one", 8000}, {"two", 9000}},
|
|
|
|
},
|
2015-09-13 02:34:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2018-10-02 20:36:04 +00:00
|
|
|
AllocatedResources: &AllocatedResources{
|
|
|
|
Tasks: map[string]*AllocatedTaskResources{
|
|
|
|
"api": {
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 50,
|
|
|
|
ReservedPorts: []Port{{"one", 10000}},
|
|
|
|
},
|
2015-09-13 02:34:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
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")
|
|
|
|
}
|
2016-02-20 20:08:27 +00:00
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(8000) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
2016-02-20 20:08:27 +00:00
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(9000) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
2016-02-20 20:08:27 +00:00
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(10000) {
|
2015-09-13 02:34:46 +00:00
|
|
|
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,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"one", 8000}, {"two", 9000}},
|
2015-09-13 21:31:01 +00:00
|
|
|
}
|
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")
|
|
|
|
}
|
2016-02-20 20:08:27 +00:00
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(8000) {
|
2015-09-13 21:31:01 +00:00
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
2016-02-20 20:08:27 +00:00
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(9000) {
|
2015-09-13 21:31:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-02 20:36:04 +00:00
|
|
|
// XXX Reserving ports doesn't work when yielding from a CIDR block. This is
|
|
|
|
// okay for now since we do not actually fingerprint CIDR blocks.
|
2015-09-13 02:34:46 +00:00
|
|
|
func TestNetworkIndex_yieldIP(t *testing.T) {
|
2018-10-02 20:36:04 +00:00
|
|
|
idx := NewNetworkIndex()
|
|
|
|
n := &Node{
|
|
|
|
NodeResources: &NodeResources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/30",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.SetNode(n)
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
idx.yieldIP(func(n *NetworkResource, ip net.IP) (stop bool) {
|
|
|
|
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()
|
|
|
|
n := &Node{
|
|
|
|
NodeResources: &NodeResources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/30",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.SetNode(n)
|
|
|
|
|
|
|
|
allocs := []*Allocation{
|
|
|
|
{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"web": {
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 20,
|
|
|
|
ReservedPorts: []Port{{"one", 8000}, {"two", 9000}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"api": {
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 50,
|
|
|
|
ReservedPorts: []Port{{"main", 10000}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.AddAllocs(allocs)
|
|
|
|
|
|
|
|
// Ask for a reserved port
|
|
|
|
ask := &NetworkResource{
|
|
|
|
ReservedPorts: []Port{{"main", 8000}},
|
|
|
|
}
|
|
|
|
offer, err := idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.101" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
rp := Port{"main", 8000}
|
|
|
|
if len(offer.ReservedPorts) != 1 || offer.ReservedPorts[0] != rp {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for dynamic ports
|
|
|
|
ask = &NetworkResource{
|
|
|
|
DynamicPorts: []Port{{"http", 0}, {"https", 0}, {"admin", 0}},
|
|
|
|
}
|
|
|
|
offer, err = idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.100" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
if len(offer.DynamicPorts) != 3 {
|
|
|
|
t.Fatalf("There should be three dynamic ports")
|
|
|
|
}
|
|
|
|
for _, port := range offer.DynamicPorts {
|
|
|
|
if port.Value == 0 {
|
|
|
|
t.Fatalf("Dynamic Port: %v should have been assigned a host port", port.Label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for reserved + dynamic ports
|
|
|
|
ask = &NetworkResource{
|
|
|
|
ReservedPorts: []Port{{"main", 2345}},
|
|
|
|
DynamicPorts: []Port{{"http", 0}, {"https", 0}, {"admin", 0}},
|
|
|
|
}
|
|
|
|
offer, err = idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.100" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
rp = Port{"main", 2345}
|
|
|
|
if len(offer.ReservedPorts) != 1 || offer.ReservedPorts[0] != rp {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for too much bandwidth
|
|
|
|
ask = &NetworkResource{
|
|
|
|
MBits: 1000,
|
|
|
|
}
|
|
|
|
offer, err = idx.AssignNetwork(ask)
|
|
|
|
if err.Error() != "bandwidth exceeded" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if offer != nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test ensures that even with a small domain of available ports we are
|
|
|
|
// able to make a dynamic port allocation.
|
|
|
|
func TestNetworkIndex_AssignNetwork_Dynamic_Contention(t *testing.T) {
|
|
|
|
|
|
|
|
// Create a node that only has one free port
|
|
|
|
idx := NewNetworkIndex()
|
|
|
|
n := &Node{
|
|
|
|
NodeResources: &NodeResources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReservedResources: &NodeReservedResources{
|
|
|
|
Networks: NodeReservedNetworkResources{
|
|
|
|
ReservedHostPorts: fmt.Sprintf("%d-%d", MinDynamicPort, MaxDynamicPort-1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.SetNode(n)
|
|
|
|
|
|
|
|
// Ask for dynamic ports
|
|
|
|
ask := &NetworkResource{
|
|
|
|
DynamicPorts: []Port{{"http", 0}},
|
|
|
|
}
|
|
|
|
offer, err := idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.100" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
if len(offer.DynamicPorts) != 1 {
|
|
|
|
t.Fatalf("There should be one dynamic ports")
|
|
|
|
}
|
|
|
|
if p := offer.DynamicPorts[0].Value; p != MaxDynamicPort {
|
|
|
|
t.Fatalf("Dynamic Port: should have been assigned %d; got %d", p, MaxDynamicPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
func TestNetworkIndex_Overcommitted_Old(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
|
|
|
|
|
|
|
// Consume some network
|
|
|
|
reserved := &NetworkResource{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 505,
|
|
|
|
ReservedPorts: []Port{{"one", 8000}, {"two", 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{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.SetNode(n)
|
|
|
|
if idx.Overcommitted() {
|
|
|
|
t.Fatalf("have resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double up our usage
|
|
|
|
idx.AddReserved(reserved)
|
|
|
|
if !idx.Overcommitted() {
|
|
|
|
t.Fatalf("should be overcommitted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
func TestNetworkIndex_SetNode_Old(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
ReservedPorts: []Port{{"ssh", 22}},
|
|
|
|
MBits: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
collide := idx.SetNode(n)
|
|
|
|
if collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !idx.UsedPorts["192.168.0.100"].Check(22) {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
func TestNetworkIndex_AddAllocs_Old(t *testing.T) {
|
|
|
|
idx := NewNetworkIndex()
|
|
|
|
allocs := []*Allocation{
|
|
|
|
{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"web": {
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 20,
|
|
|
|
ReservedPorts: []Port{{"one", 8000}, {"two", 9000}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TaskResources: map[string]*Resources{
|
|
|
|
"api": {
|
|
|
|
Networks: []*NetworkResource{
|
|
|
|
{
|
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 50,
|
|
|
|
ReservedPorts: []Port{{"one", 10000}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
collide := idx.AddAllocs(allocs)
|
|
|
|
if collide {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx.UsedBandwidth["eth0"] != 70 {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(8000) {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(9000) {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
if !idx.UsedPorts["192.168.0.100"].Check(10000) {
|
|
|
|
t.Fatalf("Bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
func TestNetworkIndex_yieldIP_Old(t *testing.T) {
|
2015-09-13 02:34:46 +00:00
|
|
|
idx := NewNetworkIndex()
|
2015-09-13 21:35:28 +00:00
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:35:28 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/30",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:35:28 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"ssh", 22}},
|
2015-09-13 21:35:28 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:36:04 +00:00
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
func TestNetworkIndex_AssignNetwork_Old(t *testing.T) {
|
2015-09-13 02:34:46 +00:00
|
|
|
idx := NewNetworkIndex()
|
2015-09-13 21:35:28 +00:00
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:35:28 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/30",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:35:28 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"ssh", 22}},
|
2015-09-13 21:35:28 +00:00
|
|
|
MBits: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
idx.SetNode(n)
|
|
|
|
|
2015-09-13 21:35:28 +00:00
|
|
|
allocs := []*Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:35:28 +00:00
|
|
|
TaskResources: map[string]*Resources{
|
2017-09-26 22:26:33 +00:00
|
|
|
"web": {
|
2015-09-13 21:35:28 +00:00
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 20,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"one", 8000}, {"two", 9000}},
|
2015-09-13 02:34:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 21:35:28 +00:00
|
|
|
TaskResources: map[string]*Resources{
|
2017-09-26 22:26:33 +00:00
|
|
|
"api": {
|
2015-09-13 21:35:28 +00:00
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 50,
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"main", 10000}},
|
2015-09-13 02:34:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
idx.AddAllocs(allocs)
|
|
|
|
|
|
|
|
// Ask for a reserved port
|
2015-09-13 21:35:28 +00:00
|
|
|
ask := &NetworkResource{
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"main", 8000}},
|
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.101" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
2015-11-15 09:56:21 +00:00
|
|
|
rp := Port{"main", 8000}
|
|
|
|
if len(offer.ReservedPorts) != 1 || offer.ReservedPorts[0] != rp {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for dynamic ports
|
2015-09-13 21:35:28 +00:00
|
|
|
ask = &NetworkResource{
|
2015-11-15 09:56:21 +00:00
|
|
|
DynamicPorts: []Port{{"http", 0}, {"https", 0}, {"admin", 0}},
|
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)
|
|
|
|
}
|
2015-11-15 09:56:21 +00:00
|
|
|
if len(offer.DynamicPorts) != 3 {
|
|
|
|
t.Fatalf("There should be three dynamic ports")
|
|
|
|
}
|
|
|
|
for _, port := range offer.DynamicPorts {
|
|
|
|
if port.Value == 0 {
|
|
|
|
t.Fatalf("Dynamic Port: %v should have been assigned a host port", port.Label)
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for reserved + dynamic ports
|
2015-09-13 21:35:28 +00:00
|
|
|
ask = &NetworkResource{
|
2015-11-15 09:56:21 +00:00
|
|
|
ReservedPorts: []Port{{"main", 2345}},
|
|
|
|
DynamicPorts: []Port{{"http", 0}, {"https", 0}, {"admin", 0}},
|
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)
|
|
|
|
}
|
2015-11-15 09:56:21 +00:00
|
|
|
|
|
|
|
rp = Port{"main", 2345}
|
|
|
|
if len(offer.ReservedPorts) != 1 || offer.ReservedPorts[0] != rp {
|
2015-09-13 02:34:46 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:36:04 +00:00
|
|
|
// COMPAT(0.11): Remove in 0.11
|
2016-08-05 23:23:41 +00:00
|
|
|
// This test ensures that even with a small domain of available ports we are
|
|
|
|
// able to make a dynamic port allocation.
|
2018-10-02 20:36:04 +00:00
|
|
|
func TestNetworkIndex_AssignNetwork_Dynamic_Contention_Old(t *testing.T) {
|
2016-08-05 23:23:41 +00:00
|
|
|
|
|
|
|
// Create a node that only has one free port
|
|
|
|
idx := NewNetworkIndex()
|
|
|
|
n := &Node{
|
|
|
|
Resources: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2016-08-05 23:23:41 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Reserved: &Resources{
|
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2016-08-05 23:23:41 +00:00
|
|
|
Device: "eth0",
|
|
|
|
IP: "192.168.0.100",
|
|
|
|
MBits: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-02-22 23:58:21 +00:00
|
|
|
for i := MinDynamicPort; i < MaxDynamicPort; i++ {
|
2016-08-05 23:23:41 +00:00
|
|
|
n.Reserved.Networks[0].ReservedPorts = append(n.Reserved.Networks[0].ReservedPorts, Port{Value: i})
|
|
|
|
}
|
|
|
|
|
|
|
|
idx.SetNode(n)
|
|
|
|
|
|
|
|
// Ask for dynamic ports
|
|
|
|
ask := &NetworkResource{
|
|
|
|
DynamicPorts: []Port{{"http", 0}},
|
|
|
|
}
|
|
|
|
offer, err := idx.AssignNetwork(ask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if offer == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if offer.IP != "192.168.0.100" {
|
|
|
|
t.Fatalf("bad: %#v", offer)
|
|
|
|
}
|
|
|
|
if len(offer.DynamicPorts) != 1 {
|
|
|
|
t.Fatalf("There should be three dynamic ports")
|
|
|
|
}
|
2016-08-17 18:08:21 +00:00
|
|
|
if p := offer.DynamicPorts[0].Value; p != MaxDynamicPort {
|
2016-08-05 23:23:41 +00:00
|
|
|
t.Fatalf("Dynamic Port: should have been assigned %d; got %d", p, MaxDynamicPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 02:34:46 +00:00
|
|
|
func TestIntContains(t *testing.T) {
|
2016-08-10 18:47:20 +00:00
|
|
|
l := []int{1, 2, 10, 20}
|
2015-11-15 09:56:21 +00:00
|
|
|
if isPortReserved(l, 50) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-15 09:56:21 +00:00
|
|
|
if !isPortReserved(l, 20) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-15 09:56:21 +00:00
|
|
|
if !isPortReserved(l, 1) {
|
2015-09-13 02:34:46 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|