1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/libnetwork/ipamutils/utils_test.go

118 lines
3.5 KiB
Go
Raw Normal View History

package ipamutils
import (
"net"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func initBroadPredefinedNetworks() []*net.IPNet {
pl := make([]*net.IPNet, 0, 31)
mask := []byte{255, 255, 0, 0}
for i := 17; i < 32; i++ {
pl = append(pl, &net.IPNet{IP: []byte{172, byte(i), 0, 0}, Mask: mask})
}
mask20 := []byte{255, 255, 240, 0}
for i := 0; i < 16; i++ {
pl = append(pl, &net.IPNet{IP: []byte{192, 168, byte(i << 4), 0}, Mask: mask20})
}
return pl
}
func initGranularPredefinedNetworks() []*net.IPNet {
pl := make([]*net.IPNet, 0, 256*256)
mask := []byte{255, 255, 255, 0}
for i := 0; i < 256; i++ {
for j := 0; j < 256; j++ {
pl = append(pl, &net.IPNet{IP: []byte{10, byte(i), byte(j), 0}, Mask: mask})
}
}
return pl
Remove pre-defined networks from package init This moves the initialization of the pre-defined networks to where it's used instead of in package init. This reason for this change is having this be populated in `init()` causes it to always consume cpu, and memory (4.3MB of memory), to populate even if the package is unused (like for instnace, in a re-exec). Here is a memory profile of docker/docker just after starting the daemon of the top 10 largest memory consumers: Before: ``` flat flat% sum% cum cum% 0 0% 0% 11.89MB 95.96% runtime.goexit 0 0% 0% 6.79MB 54.82% runtime.main 0 0% 0% 5.79MB 46.74% main.init 0 0% 0% 4.79MB 38.67% github.com/docker/docker/api/server/router/network.init 0 0% 0% 4.79MB 38.67% github.com/docker/libnetwork.init 0 0% 0% 4.29MB 34.63% github.com/docker/libnetwork/ipam.init 0 0% 0% 4.29MB 34.63% github.com/docker/libnetwork/ipams/builtin.init 0 0% 0% 4.29MB 34.63% github.com/docker/libnetwork/ipamutils.init 0 0% 0% 4.29MB 34.63% github.com/docker/libnetwork/ipamutils.init.1 4.29MB 34.63% 34.63% 4.29MB 34.63% github.com/docker/libnetwork/ipamutils.initGranularPredefinedNetworks ``` After: ``` flat flat% sum% cum cum% 0 0% 0% 4439.37kB 89.66% runtime.goexit 0 0% 0% 4439.37kB 89.66% runtime.main 0 0% 0% 3882.11kB 78.40% github.com/docker/docker/cli.(*Cli).Run 0 0% 0% 3882.11kB 78.40% main.main 3882.11kB 78.40% 78.40% 3882.11kB 78.40% reflect.callMethod 0 0% 78.40% 3882.11kB 78.40% reflect.methodValueCall 0 0% 78.40% 557.26kB 11.25% github.com/docker/docker/api/server.init 557.26kB 11.25% 89.66% 557.26kB 11.25% html.init 0 0% 89.66% 557.26kB 11.25% html/template.init 0 0% 89.66% 557.26kB 11.25% main.init ``` Now, of course the docker daemon will still need to consume this memory, but at least now re-execs and such won't have to re-init these variables. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2016-04-04 09:50:26 -04:00
}
func initGlobalScopeNetworks() []*net.IPNet {
pl := make([]*net.IPNet, 0, 256*256)
mask := []byte{255, 255, 255, 0}
for i := 0; i < 256; i++ {
for j := 0; j < 256; j++ {
pl = append(pl, &net.IPNet{IP: []byte{30, byte(i), byte(j), 0}, Mask: mask})
}
}
return pl
}
func TestDefaultNetwork(t *testing.T) {
for _, nw := range PredefinedGlobalScopeDefaultNetworks {
if ones, bits := nw.Mask.Size(); bits != 32 || ones != 24 {
t.Fatalf("Unexpected size for network in granular list: %v", nw)
}
}
for _, nw := range PredefinedLocalScopeDefaultNetworks {
if ones, bits := nw.Mask.Size(); bits != 32 || (ones != 20 && ones != 16) {
t.Fatalf("Unexpected size for network in broad list: %v", nw)
}
}
originalBroadNets := initBroadPredefinedNetworks()
m := make(map[string]bool)
for _, v := range originalBroadNets {
m[v.String()] = true
}
for _, nw := range PredefinedLocalScopeDefaultNetworks {
_, ok := m[nw.String()]
assert.Check(t, ok)
delete(m, nw.String())
}
assert.Check(t, is.Len(m, 0))
originalGranularNets := initGranularPredefinedNetworks()
m = make(map[string]bool)
for _, v := range originalGranularNets {
m[v.String()] = true
}
for _, nw := range PredefinedGlobalScopeDefaultNetworks {
_, ok := m[nw.String()]
assert.Check(t, ok)
delete(m, nw.String())
}
assert.Check(t, is.Len(m, 0))
}
func TestConfigGlobalScopeDefaultNetworks(t *testing.T) {
err := ConfigGlobalScopeDefaultNetworks([]*NetworkToSplit{{"30.0.0.0/8", 24}})
assert.NilError(t, err)
originalGlobalScopeNetworks := initGlobalScopeNetworks()
m := make(map[string]bool)
for _, v := range originalGlobalScopeNetworks {
m[v.String()] = true
}
for _, nw := range PredefinedGlobalScopeDefaultNetworks {
_, ok := m[nw.String()]
assert.Check(t, ok)
delete(m, nw.String())
}
assert.Check(t, is.Len(m, 0))
}
func TestInitAddressPools(t *testing.T) {
err := ConfigLocalScopeDefaultNetworks([]*NetworkToSplit{{"172.80.0.0/16", 24}, {"172.90.0.0/16", 24}})
assert.NilError(t, err)
// Check for Random IPAddresses in PredefinedLocalScopeDefaultNetworks ex: first , last and middle
assert.Check(t, is.Len(PredefinedLocalScopeDefaultNetworks, 512), "Failed to find PredefinedLocalScopeDefaultNetworks")
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[0].String(), "172.80.0.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[127].String(), "172.80.127.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[255].String(), "172.80.255.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[256].String(), "172.90.0.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[383].String(), "172.90.127.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[511].String(), "172.90.255.0/24"))
}