This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
forked from metal-stack/go-ipam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_test.go
87 lines (68 loc) · 1.76 KB
/
memory_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
package ipam
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func Test_ReadPrefix(t *testing.T) {
m := NewMemory()
// Prefix
p, err := m.ReadPrefix("12.0.0.0/8")
require.NotNil(t, err)
require.Equal(t, "Prefix 12.0.0.0/8 not found", err.Error())
require.Empty(t, p)
prefix := Prefix{Cidr: "12.0.0.0/16"}
p, err = m.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
p, err = m.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_UpdatePrefix(t *testing.T) {
m := NewMemory()
prefix := Prefix{}
p, err := m.UpdatePrefix(prefix)
require.NotNil(t, err)
require.Empty(t, p)
require.Equal(t, "prefix not present:{ false map[] 0 map[] 0}", err.Error())
prefix.Cidr = "1.2.3.4/24"
p, err = m.UpdatePrefix(prefix)
require.NotNil(t, err)
require.Empty(t, p)
require.Equal(t, "prefix not found:1.2.3.4/24", err.Error())
}
// ensure that locks on memory storage work
func Test_UpdatePrefix_Concurrent(t *testing.T) {
m := NewMemory()
for i := 0; i < 50000; i++ {
go func(run int) {
prefix := Prefix{}
cidr := calcPrefix24(run) + "/24"
prefix.Cidr = cidr
p, err := m.CreatePrefix(prefix)
require.Nil(t, err)
require.NotNil(t, p)
p, err = m.ReadPrefix(cidr)
require.Nil(t, err)
require.NotNil(t, p)
p, err = m.UpdatePrefix(p)
require.Nil(t, err)
require.NotNil(t, p)
p, err = m.ReadPrefix(cidr)
require.Nil(t, err)
require.NotNil(t, p)
p, err = m.DeletePrefix(p)
require.Nil(t, err)
require.NotNil(t, p)
}(i)
}
}
// calcs distinct /24 prefix for given test run
func calcPrefix24(run int) string {
i3 := run % 256
i2 := (run / 256) % 256
i1 := (run / 65536) % 256
return fmt.Sprintf("%d.%d.%d.0", i1, i2, i3)
}