-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsubnet_test.go
132 lines (94 loc) · 2.96 KB
/
subnet_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"github.com/stretchr/testify/assert"
"github.com/willf/bitset"
"testing"
)
func TestNewSubnet(t *testing.T) {
subnet := NewSubnet()
assert.NotNil(t, subnet.Leases, "Leases must not be nil")
assert.NotNil(t, subnet.Bindings, "Bindings must not be nil")
assert.NotNil(t, subnet.Options, "Options must not be nil")
assert.NotNil(t, subnet.ActiveBits, "ActiveBits must not be nil")
}
func TestMarshalJsonSubnet(t *testing.T) {
_, s := simpleSetup()
as := convertSubnetToApiSubnet(s)
s2, _ := convertApiSubnetToSubnet(as, nil)
b, e := s2.MarshalJSON()
assert.Nil(t, e, "Error should be nil")
s3 := &Subnet{}
e3 := s3.UnmarshalJSON(b)
assert.Nil(t, e3, "Error should be nil")
assert.Equal(t, s3, s2, "Subnet should be equal")
}
func TestFreeLeaseNoMatch(t *testing.T) {
dt, s := simpleSetup()
s.Leases["one"] = &Lease{}
s.free_lease(dt, "two")
assert.NotNil(t, s.Leases["one"], "Lease one should not be nil")
assert.Nil(t, s.Leases["two"], "Lease two should be nil")
}
// Freeing a lease should clear a set ActiveBit if in range
func TestFreeLeaseMatch(t *testing.T) {
dt, s := simpleSetup()
s.Leases["one"] = &Lease{}
s.Leases["two"] = &Lease{
Ip: s.ActiveStart,
}
s.ActiveBits.Set(0)
s.free_lease(dt, "two")
assert.NotNil(t, s.Leases["one"], "Lease one should not be nil")
assert.Nil(t, s.Leases["two"], "Lease two should be nil")
assert.False(t, s.ActiveBits.Test(0), "First bit should be false")
}
func TestFindInfoNothing(t *testing.T) {
dt, s := simpleSetup()
l, b := s.find_info(dt, "fred")
assert.Nil(t, l, "Lease should be nil")
assert.Nil(t, b, "Binding should be nil")
}
func TestFindInfoLeaseButNotBinding(t *testing.T) {
dt, s := simpleSetup()
s.Leases["fred"] = &Lease{}
l, b := s.find_info(dt, "fred")
assert.NotNil(t, l, "Lease should not be nil")
assert.Nil(t, b, "Binding should be nil")
}
func TestFindInfoBindingButNoLease(t *testing.T) {
dt, s := simpleSetup()
s.Bindings["fred"] = &Binding{}
l, b := s.find_info(dt, "fred")
assert.Nil(t, l, "Lease should be nil")
assert.NotNil(t, b, "Binding should not be nil")
}
func TestFindInfoLeaseAndBinding(t *testing.T) {
dt, s := simpleSetup()
s.Leases["fred"] = &Lease{}
s.Bindings["fred"] = &Binding{}
l, b := s.find_info(dt, "fred")
assert.NotNil(t, l, "Lease should not be nil")
assert.NotNil(t, b, "Binding should not be nil")
}
func TestFirstClearBitNothing(t *testing.T) {
bs := bitset.New(2)
i, s := firstClearBit(bs)
assert.Equal(t, i, uint(0), "First bit should be 0")
assert.True(t, s, "Success should be true")
}
func TestFirstClearBitAllSet(t *testing.T) {
bs := bitset.New(2)
bs.Set(0)
bs.Set(1)
i, s := firstClearBit(bs)
assert.Equal(t, i, uint(0), "First bit should be 0")
assert.False(t, s, "Success should be false")
}
func TestFirstClearBitLastBit(t *testing.T) {
bs := bitset.New(3)
bs.Set(0)
bs.Set(1)
i, s := firstClearBit(bs)
assert.Equal(t, i, uint(2), "First bit should be 2")
assert.True(t, s, "Success should be true")
}