store: add IPStore tests

This commit is contained in:
Leo Balduf 2016-06-18 15:51:10 -04:00
parent c191e04ee7
commit a4d808dea9
3 changed files with 266 additions and 227 deletions

View file

@ -17,6 +17,8 @@ var (
v6 = net.ParseIP("0c22:384e:0:0c22:384e::68")
v4 = net.ParseIP("12.13.14.15")
v4s = net.ParseIP("12.13.14.15").To4()
ipStoreTester = store.PrepareIPStoreTester(&ipStoreDriver{})
)
func TestKey(t *testing.T) {
@ -36,240 +38,17 @@ func TestKey(t *testing.T) {
}
func TestIPStore(t *testing.T) {
var d = &ipStoreDriver{}
s, err := d.New(&store.DriverConfig{})
require.Nil(t, err)
require.NotNil(t, s)
// check default state
found, err := s.HasIP(v4)
require.Nil(t, err)
require.False(t, found)
// check IPv4
err = s.AddIP(v4)
require.Nil(t, err)
found, err = s.HasIP(v4)
require.Nil(t, err)
require.True(t, found)
found, err = s.HasIP(v4s)
require.Nil(t, err)
require.True(t, found)
found, err = s.HasIP(v6)
require.Nil(t, err)
require.False(t, found)
// check removes
err = s.RemoveIP(v6)
require.NotNil(t, err)
err = s.RemoveIP(v4s)
require.Nil(t, err)
found, err = s.HasIP(v4)
require.Nil(t, err)
require.False(t, found)
// check IPv6
err = s.AddIP(v6)
require.Nil(t, err)
found, err = s.HasIP(v6)
require.Nil(t, err)
require.True(t, found)
err = s.RemoveIP(v6)
require.Nil(t, err)
found, err = s.HasIP(v6)
require.Nil(t, err)
require.False(t, found)
errChan := s.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
ipStoreTester.TestIPStore(t, &store.DriverConfig{})
}
func TestHasAllHasAny(t *testing.T) {
var d = &ipStoreDriver{}
s, err := d.New(&store.DriverConfig{})
require.Nil(t, err)
require.NotNil(t, s)
found, err := s.HasAnyIP(nil)
require.Nil(t, err)
require.False(t, found)
found, err = s.HasAllIPs(nil)
require.Nil(t, err)
require.True(t, found)
found, err = s.HasAllIPs([]net.IP{v4})
require.Nil(t, err)
require.False(t, found)
err = s.AddIP(v4)
require.Nil(t, err)
found, err = s.HasAnyIP([]net.IP{v4, v6})
require.Nil(t, err)
require.True(t, found)
found, err = s.HasAllIPs([]net.IP{v4, v6})
require.Nil(t, err)
require.False(t, found)
found, err = s.HasAllIPs([]net.IP{v4})
require.Nil(t, err)
require.True(t, found)
err = s.AddIP(v6)
require.Nil(t, err)
found, err = s.HasAnyIP([]net.IP{v4, v6})
require.Nil(t, err)
require.True(t, found)
found, err = s.HasAllIPs([]net.IP{v4, v6})
require.Nil(t, err)
require.True(t, found)
errChan := s.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
ipStoreTester.TestHasAllHasAny(t, &store.DriverConfig{})
}
func TestNetworks(t *testing.T) {
var (
d = &ipStoreDriver{}
net1 = "192.168.22.255/24"
net2 = "192.168.23.255/24"
includedIP = net.ParseIP("192.168.22.23")
excludedIP = net.ParseIP("192.168.23.22")
)
s, err := d.New(&store.DriverConfig{})
require.Nil(t, err)
require.NotNil(t, s)
match, err := s.HasIP(includedIP)
require.Nil(t, err)
require.False(t, match)
match, err = s.HasIP(excludedIP)
require.Nil(t, err)
require.False(t, match)
err = s.AddNetwork("")
require.NotNil(t, err)
err = s.RemoveNetwork("")
require.NotNil(t, err)
err = s.AddNetwork(net1)
require.Nil(t, err)
match, err = s.HasIP(includedIP)
require.Nil(t, err)
require.True(t, match)
match, err = s.HasIP(excludedIP)
require.Nil(t, err)
require.False(t, match)
err = s.RemoveNetwork(net2)
require.NotNil(t, err)
err = s.RemoveNetwork(net1)
require.Nil(t, err)
match, err = s.HasIP(includedIP)
require.Nil(t, err)
require.False(t, match)
match, err = s.HasIP(excludedIP)
require.Nil(t, err)
require.False(t, match)
errChan := s.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
ipStoreTester.TestNetworks(t, &store.DriverConfig{})
}
func TestHasAllHasAnyNetworks(t *testing.T) {
var (
d = &ipStoreDriver{}
net1 = "192.168.22.255/24"
net2 = "192.168.23.255/24"
inNet1 = net.ParseIP("192.168.22.234")
inNet2 = net.ParseIP("192.168.23.123")
excluded = net.ParseIP("10.154.243.22")
)
s, err := d.New(&store.DriverConfig{})
require.Nil(t, err)
require.NotNil(t, s)
match, err := s.HasAnyIP([]net.IP{inNet1, inNet2, excluded})
require.Nil(t, err)
require.False(t, match)
match, err = s.HasAllIPs([]net.IP{inNet1, inNet2, excluded})
require.Nil(t, err)
require.False(t, match)
err = s.AddNetwork(net1)
require.Nil(t, err)
match, err = s.HasAnyIP([]net.IP{inNet1, inNet2})
require.Nil(t, err)
require.True(t, match)
match, err = s.HasAllIPs([]net.IP{inNet1, inNet2})
require.Nil(t, err)
require.False(t, match)
err = s.AddNetwork(net2)
require.Nil(t, err)
match, err = s.HasAnyIP([]net.IP{inNet1, inNet2, excluded})
require.Nil(t, err)
require.True(t, match)
match, err = s.HasAllIPs([]net.IP{inNet1, inNet2})
require.Nil(t, err)
require.True(t, match)
match, err = s.HasAllIPs([]net.IP{inNet1, inNet2, excluded})
require.Nil(t, err)
require.False(t, match)
err = s.RemoveNetwork(net1)
require.Nil(t, err)
match, err = s.HasAnyIP([]net.IP{inNet1, inNet2})
require.Nil(t, err)
require.True(t, match)
match, err = s.HasAllIPs([]net.IP{inNet1, inNet2})
require.Nil(t, err)
require.False(t, match)
err = s.RemoveNetwork(net2)
require.Nil(t, err)
match, err = s.HasAnyIP([]net.IP{inNet1, inNet2})
require.Nil(t, err)
require.False(t, match)
match, err = s.HasAllIPs([]net.IP{inNet1, inNet2})
require.Nil(t, err)
require.False(t, match)
errChan := s.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
ipStoreTester.TestHasAllHasAnyNetworks(t, &store.DriverConfig{})
}

View file

@ -9,6 +9,8 @@ import (
"strings"
"testing"
"net"
"github.com/stretchr/testify/require"
)

View file

@ -7,6 +7,8 @@ package store
import (
"testing"
"net"
"github.com/stretchr/testify/require"
)
@ -93,3 +95,259 @@ func (s *stringStoreTester) TestStringStore(t *testing.T, cfg *DriverConfig) {
err = <-errChan
require.Nil(t, err, "StringStore shutdown must not fail")
}
// IPStoreTester is a collection of tests for an IPStore driver.
// Every benchmark expects a new, clean storage. Every benchmark should be
// called with a DriverConfig that ensures this.
type IPStoreTester interface {
TestIPStore(*testing.T, *DriverConfig)
TestHasAllHasAny(*testing.T, *DriverConfig)
TestNetworks(*testing.T, *DriverConfig)
TestHasAllHasAnyNetworks(*testing.T, *DriverConfig)
}
var _ IPStoreTester = &ipStoreTester{}
type ipStoreTester struct {
v6, v4, v4s net.IP
net1, net2 string
inNet1, inNet2 net.IP
excluded net.IP
driver IPStoreDriver
}
// PrepareIPStoreTester prepares a reusable suite for IPStore driver
// tests.
func PrepareIPStoreTester(driver IPStoreDriver) IPStoreTester {
return &ipStoreTester{
v6: net.ParseIP("0c22:384e:0:0c22:384e::68"),
v4: net.ParseIP("12.13.14.15"),
v4s: net.ParseIP("12.13.14.15").To4(),
net1: "192.168.22.255/24",
net2: "192.168.23.255/24",
inNet1: net.ParseIP("192.168.22.22"),
inNet2: net.ParseIP("192.168.23.23"),
excluded: net.ParseIP("10.154.243.22"),
driver: driver,
}
}
func (s *ipStoreTester) TestIPStore(t *testing.T, cfg *DriverConfig) {
is, err := s.driver.New(cfg)
require.Nil(t, err)
require.NotNil(t, is)
// check default state
found, err := is.HasIP(s.v4)
require.Nil(t, err)
require.False(t, found)
// check IPv4
err = is.AddIP(s.v4)
require.Nil(t, err)
found, err = is.HasIP(s.v4)
require.Nil(t, err)
require.True(t, found)
found, err = is.HasIP(s.v4s)
require.Nil(t, err)
require.True(t, found)
found, err = is.HasIP(s.v6)
require.Nil(t, err)
require.False(t, found)
// check removes
err = is.RemoveIP(s.v6)
require.NotNil(t, err)
err = is.RemoveIP(s.v4s)
require.Nil(t, err)
found, err = is.HasIP(s.v4)
require.Nil(t, err)
require.False(t, found)
// check IPv6
err = is.AddIP(s.v6)
require.Nil(t, err)
found, err = is.HasIP(s.v6)
require.Nil(t, err)
require.True(t, found)
err = is.RemoveIP(s.v6)
require.Nil(t, err)
found, err = is.HasIP(s.v6)
require.Nil(t, err)
require.False(t, found)
errChan := is.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
}
func (s *ipStoreTester) TestHasAllHasAny(t *testing.T, cfg *DriverConfig) {
is, err := s.driver.New(cfg)
require.Nil(t, err)
require.NotNil(t, is)
found, err := is.HasAnyIP(nil)
require.Nil(t, err)
require.False(t, found)
found, err = is.HasAllIPs(nil)
require.Nil(t, err)
require.True(t, found)
found, err = is.HasAllIPs([]net.IP{s.v6})
require.Nil(t, err)
require.False(t, found)
err = is.AddIP(s.v4)
require.Nil(t, err)
found, err = is.HasAnyIP([]net.IP{s.v6, s.v4})
require.Nil(t, err)
require.True(t, found)
found, err = is.HasAllIPs([]net.IP{s.v6, s.v4})
require.Nil(t, err)
require.False(t, found)
found, err = is.HasAllIPs([]net.IP{s.v4})
require.Nil(t, err)
require.True(t, found)
err = is.AddIP(s.v6)
require.Nil(t, err)
found, err = is.HasAnyIP([]net.IP{s.v6, s.v6})
require.Nil(t, err)
require.True(t, found)
found, err = is.HasAllIPs([]net.IP{s.v6, s.v6})
require.Nil(t, err)
require.True(t, found)
errChan := is.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
}
func (s *ipStoreTester) TestNetworks(t *testing.T, cfg *DriverConfig) {
is, err := s.driver.New(cfg)
require.Nil(t, err)
require.NotNil(t, is)
match, err := is.HasIP(s.inNet1)
require.Nil(t, err)
require.False(t, match)
match, err = is.HasIP(s.inNet2)
require.Nil(t, err)
require.False(t, match)
err = is.AddNetwork("")
require.NotNil(t, err)
err = is.RemoveNetwork("")
require.NotNil(t, err)
err = is.AddNetwork(s.net1)
require.Nil(t, err)
match, err = is.HasIP(s.inNet1)
require.Nil(t, err)
require.True(t, match)
match, err = is.HasIP(s.inNet2)
require.Nil(t, err)
require.False(t, match)
err = is.RemoveNetwork(s.net2)
require.NotNil(t, err)
err = is.RemoveNetwork(s.net1)
require.Nil(t, err)
match, err = is.HasIP(s.inNet1)
require.Nil(t, err)
require.False(t, match)
match, err = is.HasIP(s.inNet2)
require.Nil(t, err)
require.False(t, match)
errChan := is.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
}
func (s *ipStoreTester) TestHasAllHasAnyNetworks(t *testing.T, cfg *DriverConfig) {
is, err := s.driver.New(cfg)
require.Nil(t, err)
require.NotNil(t, s)
match, err := is.HasAnyIP([]net.IP{s.inNet1, s.inNet2, s.excluded})
require.Nil(t, err)
require.False(t, match)
match, err = is.HasAllIPs([]net.IP{s.inNet1, s.inNet2, s.excluded})
require.Nil(t, err)
require.False(t, match)
err = is.AddNetwork(s.net1)
require.Nil(t, err)
match, err = is.HasAnyIP([]net.IP{s.inNet1, s.inNet2})
require.Nil(t, err)
require.True(t, match)
match, err = is.HasAllIPs([]net.IP{s.inNet1, s.inNet2})
require.Nil(t, err)
require.False(t, match)
err = is.AddNetwork(s.net2)
require.Nil(t, err)
match, err = is.HasAnyIP([]net.IP{s.inNet1, s.inNet2, s.excluded})
require.Nil(t, err)
require.True(t, match)
match, err = is.HasAllIPs([]net.IP{s.inNet1, s.inNet2})
require.Nil(t, err)
require.True(t, match)
match, err = is.HasAllIPs([]net.IP{s.inNet1, s.inNet2, s.excluded})
require.Nil(t, err)
require.False(t, match)
err = is.RemoveNetwork(s.net1)
require.Nil(t, err)
match, err = is.HasAnyIP([]net.IP{s.inNet1, s.inNet2})
require.Nil(t, err)
require.True(t, match)
match, err = is.HasAllIPs([]net.IP{s.inNet1, s.inNet2})
require.Nil(t, err)
require.False(t, match)
err = is.RemoveNetwork(s.net2)
require.Nil(t, err)
match, err = is.HasAnyIP([]net.IP{s.inNet1, s.inNet2})
require.Nil(t, err)
require.False(t, match)
match, err = is.HasAllIPs([]net.IP{s.inNet1, s.inNet2})
require.Nil(t, err)
require.False(t, match)
errChan := is.Stop()
err = <-errChan
require.Nil(t, err, "IPStore shutdown must not fail")
}