2015-06-13 19:04:06 -04:00
|
|
|
package idm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-06-18 22:14:03 -04:00
|
|
|
|
2015-09-07 13:33:28 -04:00
|
|
|
_ "github.com/docker/libnetwork/testutils"
|
2015-06-13 19:04:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
2015-06-15 14:43:02 -04:00
|
|
|
_, err := New(nil, "", 0, 1)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected failure, but succeeded")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:43:02 -04:00
|
|
|
_, err = New(nil, "myset", 1<<10, 0)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected failure, but succeeded")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:43:02 -04:00
|
|
|
i, err := New(nil, "myset", 0, 10)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected failure: %v", err)
|
|
|
|
}
|
|
|
|
if i.handle == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("set is not initialized")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
if i.start != 0 {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("unexpected start")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
if i.end != 10 {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("unexpected end")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocate(t *testing.T) {
|
2015-06-15 14:43:02 -04:00
|
|
|
i, err := New(nil, "myids", 50, 52)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = i.GetSpecificID(49); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected failure but succeeded")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = i.GetSpecificID(53); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected failure but succeeded")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err := i.GetID(false)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 50 {
|
|
|
|
t.Fatalf("Unexpected first id returned: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.GetSpecificID(50)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetID(false)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 51 {
|
|
|
|
t.Fatalf("Unexpected id returned: %d", o)
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetID(false)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 52 {
|
|
|
|
t.Fatalf("Unexpected id returned: %d", o)
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetID(false)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected failure but succeeded: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Release(50)
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetID(false)
|
2015-06-13 19:04:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 50 {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Unexpected id returned")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
i.Release(52)
|
|
|
|
err = i.GetSpecificID(52)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUninitialized(t *testing.T) {
|
|
|
|
i := &Idm{}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
if _, err := i.GetID(false); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected failure but succeeded")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.GetSpecificID(44); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected failure but succeeded")
|
2015-06-13 19:04:06 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-28 14:27:21 -05:00
|
|
|
|
|
|
|
func TestAllocateInRange(t *testing.T) {
|
|
|
|
i, err := New(nil, "myset", 5, 10)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err := i.GetIDInRange(6, 6, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 6 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 6. Got: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = i.GetSpecificID(6); err == nil {
|
|
|
|
t.Fatalf("Expected failure but succeeded")
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetID(false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 5 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 5. Got: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Release(6)
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetID(false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 6 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 6. Got: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 7; n <= 10; n++ {
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err := i.GetIDInRange(7, 10, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != uint64(n) {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: %d. Got: %d", n, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = i.GetSpecificID(7); err == nil {
|
|
|
|
t.Fatalf("Expected failure but succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = i.GetSpecificID(10); err == nil {
|
|
|
|
t.Fatalf("Expected failure but succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Release(10)
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetIDInRange(5, 10, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 10 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 10. Got: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Release(5)
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetIDInRange(5, 10, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 5 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 5. Got: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 5; n <= 10; n++ {
|
|
|
|
i.Release(uint64(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 5; n <= 10; n++ {
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err := i.GetIDInRange(5, 10, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != uint64(n) {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: %d. Got: %d", n, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 5; n <= 10; n++ {
|
|
|
|
if err = i.GetSpecificID(uint64(n)); err == nil {
|
|
|
|
t.Fatalf("Expected failure but succeeded for id: %d", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New larger set
|
|
|
|
ul := uint64((1 << 24) - 1)
|
|
|
|
i, err = New(nil, "newset", 0, ul)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetIDInRange(4096, ul, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 4096 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 4096. Got: %d", o)
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetIDInRange(4096, ul, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 4097 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 4097. Got: %d", o)
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:41:21 -04:00
|
|
|
o, err = i.GetIDInRange(4096, ul, false)
|
2016-11-28 14:27:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 4098 {
|
|
|
|
t.Fatalf("Unexpected id returned. Expected: 4098. Got: %d", o)
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 22:41:21 -04:00
|
|
|
|
|
|
|
func TestAllocateSerial(t *testing.T) {
|
|
|
|
i, err := New(nil, "myids", 50, 55)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = i.GetSpecificID(49); err == nil {
|
|
|
|
t.Fatal("Expected failure but succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = i.GetSpecificID(56); err == nil {
|
|
|
|
t.Fatal("Expected failure but succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
o, err := i.GetID(true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 50 {
|
|
|
|
t.Fatalf("Unexpected first id returned: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.GetSpecificID(50)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
o, err = i.GetID(true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 51 {
|
|
|
|
t.Fatalf("Unexpected id returned: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
o, err = i.GetID(true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 52 {
|
|
|
|
t.Fatalf("Unexpected id returned: %d", o)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Release(50)
|
|
|
|
|
|
|
|
o, err = i.GetID(true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if o != 53 {
|
|
|
|
t.Fatal("Unexpected id returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Release(52)
|
|
|
|
err = i.GetSpecificID(52)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|