-
Notifications
You must be signed in to change notification settings - Fork 3
/
subnet_test.go
248 lines (211 loc) · 6.67 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package lochness_test
import (
"encoding/json"
"errors"
"net"
"testing"
"github.com/mistifyio/lochness"
"github.com/mistifyio/lochness/internal/tests/common"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func TestSubnet(t *testing.T) {
suite.Run(t, new(SubnetSuite))
}
type SubnetSuite struct {
common.Suite
}
func (s *SubnetSuite) TestNewSubnet() {
subnet := s.Context.NewSubnet()
s.NotNil(uuid.Parse(subnet.ID))
}
func (s *SubnetSuite) TestSubnet() {
subnet := s.NewSubnet()
tests := []struct {
description string
ID string
expectedErr bool
}{
{"missing id", "", true},
{"invalid ID", "adf", true},
{"nonexistant ID", uuid.New(), true},
{"real ID", subnet.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
n, err := s.Context.Subnet(test.ID)
if test.expectedErr {
s.Error(err, msg("lookup should fail"))
s.Nil(n, msg("failure shouldn't return a subnet"))
} else {
s.NoError(err, msg("lookup should succeed"))
s.True(assert.ObjectsAreEqual(subnet, n), msg("success should return correct data"))
}
}
}
func (s *SubnetSuite) TestRefresh() {
subnet := s.NewSubnet()
subnetCopy := &lochness.Subnet{}
*subnetCopy = *subnet
network := s.NewNetwork()
_ = network.AddSubnet(subnet)
_, _ = subnet.ReserveAddress("foobar")
_ = subnet.Save()
s.NoError(subnetCopy.Refresh(), "refresh existing should succeed")
s.True(assert.ObjectsAreEqual(subnet, subnetCopy), "refresh should pull new data")
NewSubnet := s.Context.NewSubnet()
s.Error(NewSubnet.Refresh(), "unsaved subnet refresh should fail")
}
func (s *SubnetSuite) TestJSON() {
subnet := s.NewSubnet()
subnetBytes, err := json.Marshal(subnet)
s.NoError(err)
subnetFromJSON := &lochness.Subnet{}
s.NoError(json.Unmarshal(subnetBytes, subnetFromJSON))
s.Equal(subnet.ID, subnetFromJSON.ID)
s.Equal(subnet.CIDR, subnetFromJSON.CIDR)
s.Equal(subnet.StartRange, subnetFromJSON.StartRange)
}
func (s *SubnetSuite) TestValidate() {
tests := []struct {
description string
id string
cidr string
start string
end string
expectedErr bool
}{
{"missing ID", "", "192.168.100.1/24", "192.168.100.2", "192.168.100.3", true},
{"invalid ID", "asdf", "192.168.100.1/24", "192.168.100.2", "192.168.100.3", true},
{"missing cidr ", uuid.New(), "", "192.168.100.2", "192.168.100.3", true},
{"missing start", uuid.New(), "192.168.100.1/24", "", "192.168.100.3", true},
{"outside range start", uuid.New(), "192.168.100.1/24", "192.168.200.2", "192.168.100.3", true},
{"missing end", uuid.New(), "192.168.100.1/24", "192.168.100.2", "", true},
{"outside range end", uuid.New(), "192.168.100.1/24", "192.168.100.2", "192.168.200.3", true},
{"end before start", uuid.New(), "192.168.100.1/24", "192.168.100.3", "192.168.100.2", true},
{"all fields", uuid.New(), "192.168.100.1/24", "192.168.100.2", "192.168.100.3", false},
}
for _, test := range tests {
msg := s.Messager(test.description)
sub := &lochness.Subnet{
ID: test.id,
StartRange: net.ParseIP(test.start),
EndRange: net.ParseIP(test.end),
}
_, sub.CIDR, _ = net.ParseCIDR(test.cidr)
err := sub.Validate()
if test.expectedErr {
s.Error(err, msg("should be invalid"))
} else {
s.NoError(err, msg("should be valid"))
}
}
}
func (s *SubnetSuite) TestSave() {
subnet := s.NewSubnet()
subnetCopy := &lochness.Subnet{}
*subnetCopy = *subnet
network := s.NewNetwork()
_ = network.AddSubnet(subnet)
_ = subnet.Save()
s.NoError(subnetCopy.Refresh(), "refresh existing should succeed")
s.True(assert.ObjectsAreEqual(subnet, subnetCopy), "refresh should pull new data")
NewSubnet := s.Context.NewSubnet()
s.Error(NewSubnet.Refresh(), "unsaved subnet refresh should fail")
}
func (s *SubnetSuite) TestDelete() {
subnet := s.NewSubnet()
network := s.NewNetwork()
_ = network.AddSubnet(subnet)
invalidSub := s.Context.NewSubnet()
invalidSub.ID = "asdf"
tests := []struct {
description string
sub *lochness.Subnet
expectChange bool
}{
{"invalid subnet", invalidSub, false},
{"nonexistant subnet", s.Context.NewSubnet(), false},
{"existing subnet", subnet, true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.sub.Delete()
s.NoError(err)
if !test.expectChange {
continue
}
_ = network.Refresh()
s.Len(network.Subnets(), 0, msg("should remove subnet link"))
}
}
func (s *SubnetSuite) TestAvailableAddresses() {
subnet := s.NewSubnet()
addresses := subnet.AvailableAddresses()
s.Len(addresses, 9, "all addresses should be available")
s.Equal(subnet.StartRange, addresses[0], "should start at the beginning of the range")
s.Equal(subnet.EndRange, addresses[len(addresses)-1], "should end at the end of the array")
}
func (s *SubnetSuite) TestReserveAddress() {
subnet := s.NewSubnet()
n := len(subnet.AvailableAddresses())
for i := 0; i <= n; i++ {
msg := s.Messager("attempt " + string(i))
ip, err := subnet.ReserveAddress("foo")
if i < n {
s.NoError(err, msg("should succeed when addresses available"))
s.NotNil(ip, msg("should return ip when addresses available"))
s.Len(subnet.AvailableAddresses(), n-i-1, msg("should update available addresses"))
} else {
s.Error(err, msg("should fail when no addresses available"))
s.Nil(ip, msg("should not return ip when no addresses available"))
s.Len(subnet.AvailableAddresses(), 0, msg("should have no available addresses"))
}
}
}
func (s *SubnetSuite) TestReleaseAddress() {
subnet := s.NewSubnet()
ip, _ := subnet.ReserveAddress("foobar")
n := len(subnet.AvailableAddresses())
s.NoError(subnet.ReleaseAddress(net.ParseIP("192.168.0.1")))
s.Len(subnet.AvailableAddresses(), n)
s.NoError(subnet.ReleaseAddress(ip))
s.Len(subnet.AvailableAddresses(), n+1)
}
func (s *SubnetSuite) TestAddresses() {
subnet := s.NewSubnet()
addresses := subnet.AvailableAddresses()
ip, _ := subnet.ReserveAddress("foobar")
addressMap := subnet.Addresses()
for _, address := range addresses {
val, ok := addressMap[address.String()]
if ip.Equal(address) {
s.True(ok)
s.Equal("foobar", val)
} else {
s.False(ok)
}
}
}
func (s *SubnetSuite) TestForEachSubnet() {
subnet := s.NewSubnet()
subnet2 := s.NewSubnet()
expectedFound := map[string]bool{
subnet.ID: true,
subnet2.ID: true,
}
resultFound := make(map[string]bool)
err := s.Context.ForEachSubnet(func(sub *lochness.Subnet) error {
resultFound[sub.ID] = true
return nil
})
s.NoError(err)
s.True(assert.ObjectsAreEqual(expectedFound, resultFound))
returnErr := errors.New("an error")
err = s.Context.ForEachSubnet(func(sub *lochness.Subnet) error {
return returnErr
})
s.Error(err)
s.Equal(returnErr, err)
}