open-nomad/nomad/structs/structs_test.go

144 lines
2.4 KiB
Go
Raw Normal View History

2015-06-05 22:21:17 +00:00
package structs
import (
"reflect"
"testing"
)
2015-08-05 00:23:42 +00:00
func TestResource_NetIndexByCIDR(t *testing.T) {
r := &Resources{
Networks: []*NetworkResource{
&NetworkResource{CIDR: "10.0.0.0/8"},
&NetworkResource{CIDR: "127.0.0.0/24"},
},
}
if idx := r.NetIndexByCIDR("10.0.0.0/8"); idx != 0 {
t.Fatalf("Bad: %d", idx)
}
if idx := r.NetIndexByCIDR("127.0.0.0/24"); idx != 1 {
t.Fatalf("Bad: %d", idx)
}
if idx := r.NetIndexByCIDR("10.0.0.0/16"); idx != -1 {
t.Fatalf("Bad: %d", idx)
}
}
2015-08-05 00:32:57 +00:00
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")
}
}
2015-08-05 00:41:02 +00:00
func TestResource_Add(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,
ReservedPorts: []int{22},
},
},
}
r2 := &Resources{
CPU: 1.0,
MemoryMB: 1024,
DiskMB: 5000,
IOPS: 50,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "10.0.0.0/8",
MBits: 50,
ReservedPorts: []int{80},
},
},
}
err := r1.Add(r2)
if err != nil {
t.Fatalf("Err: %v", err)
}
expect := &Resources{
CPU: 3.0,
MemoryMB: 3072,
DiskMB: 15000,
IOPS: 150,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "10.0.0.0/8",
MBits: 150,
ReservedPorts: []int{22, 80},
},
},
}
if !reflect.DeepEqual(expect.Networks, r1.Networks) {
t.Fatalf("bad: %#v %#v", expect, r1)
}
}
2015-06-05 22:21:17 +00:00
func TestEncodeDecode(t *testing.T) {
type FooRequest struct {
Foo string
Bar int
Baz bool
}
arg := &FooRequest{
Foo: "test",
Bar: 42,
Baz: true,
}
buf, err := Encode(1, arg)
if err != nil {
t.Fatalf("err: %v", err)
}
var out FooRequest
err = Decode(buf[1:], &out)
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(arg, &out) {
t.Fatalf("bad: %#v %#v", arg, out)
}
}