nomad: share and test superset

This commit is contained in:
Armon Dadgar 2015-08-04 17:32:57 -07:00
parent b140ba5f51
commit f23e0db780
3 changed files with 73 additions and 25 deletions

View file

@ -180,28 +180,3 @@ func addResources(base, delta *structs.Resources) {
}
}
}
// resourceSubset checks if a resource utilization is a subset of another set
func resourceSubset(super, sub *structs.Resources) bool {
if super.CPU < sub.CPU {
return false
}
if super.MemoryMB < sub.MemoryMB {
return false
}
if super.DiskMB < sub.DiskMB {
return false
}
if super.IOPS < sub.IOPS {
return false
}
for _, net := range super.Networks {
idx := sub.NetIndexByCIDR(net.CIDR)
if idx >= 0 {
if net.MBits < sub.Networks[idx].MBits {
return false
}
}
}
return true
}

View file

@ -319,6 +319,39 @@ func (r *Resources) NetIndexByCIDR(cidr string) int {
return -1
}
// Superset checks if one set of resources is a superset
// of another.
func (r *Resources) Superset(other *Resources) bool {
if r.CPU < other.CPU {
return false
}
if r.MemoryMB < other.MemoryMB {
return false
}
if r.DiskMB < other.DiskMB {
return false
}
if r.IOPS < other.IOPS {
return false
}
for _, net := range r.Networks {
idx := other.NetIndexByCIDR(net.CIDR)
if idx >= 0 {
if net.MBits < other.Networks[idx].MBits {
return false
}
}
}
// Check that other does not have a network we are missing
for _, net := range other.Networks {
idx := r.NetIndexByCIDR(net.CIDR)
if idx == -1 {
return false
}
}
return true
}
// NetworkResource is used to represesent available network
// resources
type NetworkResource struct {

View file

@ -23,6 +23,46 @@ func TestResource_NetIndexByCIDR(t *testing.T) {
}
}
func TestResource_Superset(t *testing.T) {
r1 := &Resources{
CPU: 2.0,
MemoryMB: 2048,
DiskMB: 10000,
IOPS: 100,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "10.0.0.0/8",
MBits: 100,
},
},
}
r2 := &Resources{
CPU: 1.0,
MemoryMB: 1024,
DiskMB: 5000,
IOPS: 50,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "10.0.0.0/8",
MBits: 50,
},
},
}
if !r1.Superset(r1) {
t.Fatalf("bad")
}
if !r1.Superset(r2) {
t.Fatalf("bad")
}
if r2.Superset(r1) {
t.Fatalf("bad")
}
if !r2.Superset(r2) {
t.Fatalf("bad")
}
}
func TestEncodeDecode(t *testing.T) {
type FooRequest struct {
Foo string