forked from metal-stack/go-ipam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_test.go
238 lines (201 loc) · 5.38 KB
/
sql_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
package ipam
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_sql_prefixExists(t *testing.T) {
testWithSQLBackends(t, func(t *testing.T, db *sql) {
require.NotNil(t, db)
// Existing Prefix
prefix := Prefix{Cidr: "10.0.0.0/16"}
p, err := db.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
require.Equal(t, prefix.Cidr, p.Cidr)
got, exists := db.prefixExists(prefix)
require.True(t, exists)
require.Equal(t, got.Cidr, prefix.Cidr)
// NonExisting Prefix
notExistingPrefix := Prefix{Cidr: "10.0.0.0/8"}
got, exists = db.prefixExists(notExistingPrefix)
require.False(t, exists)
require.Nil(t, got)
// Delete Existing Prefix
_, err = db.DeletePrefix(prefix)
require.Nil(t, err)
got, exists = db.prefixExists(prefix)
require.False(t, exists)
require.Nil(t, got)
})
}
func Test_sql_CreatePrefix(t *testing.T) {
testWithSQLBackends(t, func(t *testing.T, db *sql) {
require.NotNil(t, db)
// Existing Prefix
prefix := Prefix{Cidr: "11.0.0.0/16"}
got, exists := db.prefixExists(prefix)
require.False(t, exists)
require.Nil(t, got)
p, err := db.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
require.Equal(t, prefix.Cidr, p.Cidr)
got, exists = db.prefixExists(prefix)
require.True(t, exists)
require.Equal(t, got.Cidr, prefix.Cidr)
// Duplicate Prefix
p, err = db.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
require.Equal(t, prefix.Cidr, p.Cidr)
ps, err := db.ReadAllPrefixCidrs()
require.Nil(t, err)
require.NotNil(t, ps)
require.Equal(t, 1, len(ps))
})
}
func Test_sql_ReadPrefix(t *testing.T) {
testWithSQLBackends(t, func(t *testing.T, db *sql) {
require.NotNil(t, db)
// Prefix
p, err := db.ReadPrefix("12.0.0.0/8")
require.NotNil(t, err)
require.Equal(t, "unable to read prefix:sql: no rows in result set", err.Error())
require.Empty(t, p)
prefix := Prefix{Cidr: "12.0.0.0/16"}
p, err = db.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
p, err = db.ReadPrefix("12.0.0.0/16")
require.Nil(t, err)
require.NotNil(t, p)
require.Equal(t, "12.0.0.0/16", p.Cidr)
})
}
func Test_sql_ReadAllPrefix(t *testing.T) {
testWithSQLBackends(t, func(t *testing.T, db *sql) {
require.NotNil(t, db)
// no Prefixes
ps, err := db.ReadAllPrefixCidrs()
require.Nil(t, err)
require.NotNil(t, ps)
require.Equal(t, 0, len(ps))
// One Prefix
prefix := Prefix{Cidr: "12.0.0.0/16"}
p, err := db.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
ps, err = db.ReadAllPrefixCidrs()
require.Nil(t, err)
require.NotNil(t, ps)
require.Equal(t, 1, len(ps))
// no Prefixes again
_, err = db.DeletePrefix(prefix)
require.Nil(t, err)
ps, err = db.ReadAllPrefixCidrs()
require.Nil(t, err)
require.NotNil(t, ps)
require.Equal(t, 0, len(ps))
})
}
func Test_sql_UpdatePrefix(t *testing.T) {
testWithSQLBackends(t, func(t *testing.T, db *sql) {
require.NotNil(t, db)
// Prefix
prefix := Prefix{Cidr: "13.0.0.0/16", ParentCidr: "13.0.0.0/8"}
p, err := db.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
// Check if present
p, err = db.ReadPrefix("13.0.0.0/16")
require.Nil(t, err)
require.NotNil(t, p)
require.Equal(t, "13.0.0.0/16", p.Cidr)
require.Equal(t, "13.0.0.0/8", p.ParentCidr)
// Modify
prefix.ParentCidr = "13.0.0.0/12"
p, err = db.UpdatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
p, err = db.ReadPrefix("13.0.0.0/16")
require.Nil(t, err)
require.NotNil(t, p)
require.Equal(t, "13.0.0.0/16", p.Cidr)
require.Equal(t, "13.0.0.0/12", p.ParentCidr)
})
}
func Test_ConcurrentAcquirePrefix(t *testing.T) {
testWithSQLBackends(t, func(t *testing.T, db *sql) {
require.NotNil(t, db)
ipamer := NewWithStorage(db)
const parentCidr = "1.0.0.0/16"
_, err := ipamer.NewPrefix(parentCidr)
require.Nil(t, err)
count := 20
prefixes := make(chan string)
for i := 0; i < count; i++ {
go acquirePrefix(t, db, parentCidr, prefixes)
}
prefixMap := make(map[string]bool)
for i := 0; i < count; i++ {
p := <-prefixes
_, duplicate := prefixMap[p]
if duplicate {
t.Errorf("prefix:%s already acquired", p)
}
prefixMap[p] = true
}
})
}
func acquirePrefix(t *testing.T, db *sql, cidr string, prefixes chan string) {
require.NotNil(t, db)
ipamer := NewWithStorage(db)
var cp *Prefix
var err error
for cp == nil {
cp, err = ipamer.AcquireChildPrefix(cidr, 26)
if err != nil {
t.Error(err)
}
time.Sleep(100 * time.Millisecond)
}
prefixes <- cp.String()
}
func Test_ConcurrentAcquireIP(t *testing.T) {
testWithSQLBackends(t, func(t *testing.T, db *sql) {
require.NotNil(t, db)
ipamer := NewWithStorage(db)
const parentCidr = "2.7.0.0/16"
_, err := ipamer.NewPrefix(parentCidr)
require.Nil(t, err)
count := 30
ips := make(chan string)
for i := 0; i < count; i++ {
go acquireIP(t, db, parentCidr, ips)
}
ipMap := make(map[string]bool)
for i := 0; i < count; i++ {
p := <-ips
_, duplicate := ipMap[p]
if duplicate {
t.Errorf("prefix:%s already acquired", p)
}
ipMap[p] = true
}
})
}
func acquireIP(t *testing.T, db *sql, prefix string, ips chan string) {
require.NotNil(t, db)
ipamer := NewWithStorage(db)
var ip *IP
var err error
for ip == nil {
ip, err = ipamer.AcquireIP(prefix)
if err != nil {
t.Error(err)
}
time.Sleep(100 * time.Millisecond)
}
ips <- ip.IP.String()
}