本文整理汇总了Golang中github.com/docker/libnetwork/types.CompareIPNet函数的典型用法代码示例。如果您正苦于以下问题:Golang CompareIPNet函数的具体用法?Golang CompareIPNet怎么用?Golang CompareIPNet使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CompareIPNet函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestIPDataMarshalling
func TestIPDataMarshalling(t *testing.T) {
i := &IPAMData{
AddressSpace: "giallo",
Pool: &net.IPNet{IP: net.IP{10, 10, 10, 8}, Mask: net.IPMask{255, 255, 255, 0}},
Gateway: &net.IPNet{IP: net.IP{10, 10, 10, 254}, Mask: net.IPMask{255, 255, 255, 0}},
AuxAddresses: map[string]*net.IPNet{
"ip1": &net.IPNet{IP: net.IP{10, 10, 10, 1}, Mask: net.IPMask{255, 255, 255, 0}},
"ip2": &net.IPNet{IP: net.IP{10, 10, 10, 2}, Mask: net.IPMask{255, 255, 255, 0}},
},
}
b, err := json.Marshal(i)
if err != nil {
t.Fatal(err)
}
ii := &IPAMData{}
err = json.Unmarshal(b, &ii)
if err != nil {
t.Fatal(err)
}
if i.AddressSpace != ii.AddressSpace || !types.CompareIPNet(i.Pool, ii.Pool) ||
!types.CompareIPNet(i.Gateway, ii.Gateway) ||
!compareAddresses(i.AuxAddresses, ii.AuxAddresses) {
t.Fatalf("JSON marsh/unmarsh failed.\nOriginal:\n%s\nDecoded:\n%s", i, ii)
}
}
示例2: isolateNetwork
// Install/Removes the iptables rules needed to isolate this network
// from each of the other networks
func (n *bridgeNetwork) isolateNetwork(others []*bridgeNetwork, enable bool) error {
n.Lock()
thisV4 := n.bridge.bridgeIPv4
thisV6 := getV6Network(n.config, n.bridge)
n.Unlock()
// Install the rules to isolate this networks against each of the other networks
for _, o := range others {
o.Lock()
otherV4 := o.bridge.bridgeIPv4
otherV6 := getV6Network(o.config, o.bridge)
o.Unlock()
if !types.CompareIPNet(thisV4, otherV4) {
// It's ok to pass a.b.c.d/x, iptables will ignore the host subnet bits
if err := setINC(thisV4.String(), otherV4.String(), enable); err != nil {
return err
}
}
if thisV6 != nil && otherV6 != nil && !types.CompareIPNet(thisV6, otherV6) {
if err := setINC(thisV6.String(), otherV6.String(), enable); err != nil {
return err
}
}
}
return nil
}
示例3: compareEndpointInterface
func compareEndpointInterface(a, b *endpointInterface) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
return a.srcName == b.srcName && a.dstPrefix == b.dstPrefix && a.v4PoolID == b.v4PoolID && a.v6PoolID == b.v6PoolID &&
types.CompareIPNet(a.addr, b.addr) && types.CompareIPNet(a.addrv6, b.addrv6)
}
示例4: TestNetworkRequest
func TestNetworkRequest(t *testing.T) {
defer testutils.SetupTestOSContext(t)()
ipamutils.InitNetworks()
nw, err := FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
if err != nil {
t.Fatal(err)
}
var found bool
for _, exp := range ipamutils.PredefinedBroadNetworks {
if types.CompareIPNet(exp, nw) {
found = true
break
}
}
if !found {
t.Fatalf("Found unexpected broad network %s", nw)
}
nw, err = FindAvailableNetwork(ipamutils.PredefinedGranularNetworks)
if err != nil {
t.Fatal(err)
}
found = false
for _, exp := range ipamutils.PredefinedGranularNetworks {
if types.CompareIPNet(exp, nw) {
found = true
break
}
}
if !found {
t.Fatalf("Found unexpected granular network %s", nw)
}
// Add iface and ssert returned address on request
createInterface(t, "test", "172.17.42.1/16")
_, exp, err := net.ParseCIDR("172.18.0.0/16")
if err != nil {
t.Fatal(err)
}
nw, err = FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(exp, nw) {
t.Fatalf("exected %s. got %s", exp, nw)
}
}
示例5: TestPredefinedPool
func TestPredefinedPool(t *testing.T) {
a, err := getAllocator()
if err != nil {
t.Fatal(err)
}
if _, err := a.getPredefinedPool("blue", false); err == nil {
t.Fatalf("Expected failure for non default addr space")
}
exp, err := ipamutils.FindAvailableNetwork(a.predefined[localAddressSpace])
if err != nil {
t.Fatal(err)
}
nw, err := a.getPredefinedPool(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(nw, exp) {
t.Fatalf("Unexpected default network returned: %s. Expected: %s", nw, exp)
}
pid, nw, _, err := a.RequestPool(localAddressSpace, exp.String(), "", nil, false)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(nw, exp) {
t.Fatalf("Unexpected default network returned: %s. Expected: %s", nw, exp)
}
nw2, err := a.getPredefinedPool(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if types.CompareIPNet(nw, nw2) {
t.Fatalf("Unexpected default network returned: %s = %s", nw2, nw)
}
if err := a.ReleasePool(pid); err != nil {
t.Fatal(err)
}
nw, err = a.getPredefinedPool(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(nw, exp) {
t.Fatalf("Unexpected default network returned: %s. Expected %s", nw, exp)
}
}
示例6: TestPoolDataMarshal
func TestPoolDataMarshal(t *testing.T) {
_, nw, err := net.ParseCIDR("172.28.30.1/24")
if err != nil {
t.Fatal(err)
}
p := &PoolData{
ParentKey: SubnetKey{AddressSpace: "Blue", Subnet: "172.28.0.0/16"},
Pool: nw,
Range: &AddressRange{Sub: &net.IPNet{IP: net.IP{172, 28, 20, 0}, Mask: net.IPMask{255, 255, 255, 0}}, Start: 0, End: 255},
RefCount: 4,
}
ba, err := json.Marshal(p)
if err != nil {
t.Fatal(err)
}
var q PoolData
err = json.Unmarshal(ba, &q)
if err != nil {
t.Fatal(err)
}
if p.ParentKey != q.ParentKey || !types.CompareIPNet(p.Range.Sub, q.Range.Sub) ||
p.Range.Start != q.Range.Start || p.Range.End != q.Range.End || p.RefCount != q.RefCount ||
!types.CompareIPNet(p.Pool, q.Pool) {
t.Fatalf("\n%#v\n%#v", p, &q)
}
p = &PoolData{
ParentKey: SubnetKey{AddressSpace: "Blue", Subnet: "172.28.0.0/16"},
Pool: nw,
RefCount: 4,
}
ba, err = json.Marshal(p)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(ba, &q)
if err != nil {
t.Fatal(err)
}
if q.Range != nil {
t.Fatalf("Unexpected Range")
}
}
示例7: setupVerifyAndReconcile
func setupVerifyAndReconcile(config *networkConfiguration, i *bridgeInterface) error {
// Fetch a single IPv4 and a slice of IPv6 addresses from the bridge.
addrv4, addrsv6, err := i.addresses()
if err != nil {
return fmt.Errorf("Failed to verify ip addresses: %v", err)
}
// Verify that the bridge does have an IPv4 address.
if addrv4.IPNet == nil {
return &ErrNoIPAddr{}
}
// Verify that the bridge IPv4 address matches the requested configuration.
if config.AddressIPv4 != nil && !addrv4.IP.Equal(config.AddressIPv4.IP) {
return &IPv4AddrNoMatchError{IP: addrv4.IP, CfgIP: config.AddressIPv4.IP}
}
// Verify that one of the bridge IPv6 addresses matches the requested
// configuration.
if config.EnableIPv6 && !findIPv6Address(netlink.Addr{IPNet: bridgeIPv6}, addrsv6) {
return (*IPv6AddrNoMatchError)(bridgeIPv6)
}
// Release any residual IPv6 address that might be there because of older daemon instances
for _, addrv6 := range addrsv6 {
if addrv6.IP.IsGlobalUnicast() && !types.CompareIPNet(addrv6.IPNet, i.bridgeIPv6) {
if err := i.nlh.AddrDel(i.Link, &addrv6); err != nil {
log.Warnf("Failed to remove residual IPv6 address %s from bridge: %v", addrv6.IPNet, err)
}
}
}
return nil
}
示例8: TestPoolRequest
func TestPoolRequest(t *testing.T) {
a := allocator{}
pid, pool, _, err := a.RequestPool(defaultAS, "", "", nil, false)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(defaultPool, pool) {
t.Fatalf("Unexpected pool returned. Expected %v. Got: %v", defaultPool, pool)
}
if pid != defaultPoolID {
t.Fatalf("Unexpected pool id returned. Expected: %s. Got: %s", defaultPoolID, pid)
}
_, _, _, err = a.RequestPool("default", "", "", nil, false)
if err == nil {
t.Fatalf("Unexpected success")
}
_, _, _, err = a.RequestPool(defaultAS, "192.168.0.0/16", "", nil, false)
if err == nil {
t.Fatalf("Unexpected success")
}
_, _, _, err = a.RequestPool(defaultAS, "", "192.168.0.0/24", nil, false)
if err == nil {
t.Fatalf("Unexpected success")
}
_, _, _, err = a.RequestPool(defaultAS, "", "", nil, true)
if err == nil {
t.Fatalf("Unexpected success")
}
}
示例9: setupBridgeIPv4
func setupBridgeIPv4(config *networkConfiguration, i *bridgeInterface) error {
addrv4List, _, err := i.addresses()
if err != nil {
return fmt.Errorf("failed to retrieve bridge interface addresses: %v", err)
}
addrv4, _ := selectIPv4Address(addrv4List, config.AddressIPv4)
if !types.CompareIPNet(addrv4.IPNet, config.AddressIPv4) {
if addrv4.IPNet != nil {
if err := i.nlh.AddrDel(i.Link, &addrv4); err != nil {
return fmt.Errorf("failed to remove current ip address from bridge: %v", err)
}
}
log.Debugf("Assigning address to bridge interface %s: %s", config.BridgeName, config.AddressIPv4)
if err := i.nlh.AddrAdd(i.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil {
return &IPv4AddrAddError{IP: config.AddressIPv4, Err: err}
}
}
// Store bridge network and default gateway
i.bridgeIPv4 = config.AddressIPv4
i.gatewayIPv4 = config.AddressIPv4.IP
return nil
}
示例10: TestPredefinedPool
func TestPredefinedPool(t *testing.T) {
a, err := getAllocator()
if err != nil {
t.Fatal(err)
}
if _, err := a.getPredefinedPool("blue", false); err == nil {
t.Fatalf("Expected failure for non default addr space")
}
pid, nw, _, err := a.RequestPool(localAddressSpace, "", "", nil, false)
if err != nil {
t.Fatal(err)
}
nw2, err := a.getPredefinedPool(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if types.CompareIPNet(nw, nw2) {
t.Fatalf("Unexpected default network returned: %s = %s", nw2, nw)
}
if err := a.ReleasePool(pid); err != nil {
t.Fatal(err)
}
}
示例11: TestIpamReleaseOnNetDriverFailures
func TestIpamReleaseOnNetDriverFailures(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
cfgOptions, err := OptionBoltdbWithRandomDBFile()
c, err := New(cfgOptions...)
if err != nil {
t.Fatal(err)
}
defer c.Stop()
cc := c.(*controller)
bd := badDriver{failNetworkCreation: true}
cc.drivers[badDriverName] = &driverData{driver: &bd, capability: driverapi.Capability{DataScope: datastore.LocalScope}}
// Test whether ipam state release is invoked on network create failure from net driver
// by checking whether subsequent network creation requesting same gateway IP succeeds
ipamOpt := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{&IpamConf{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.254"}}, nil)
if _, err := c.NewNetwork(badDriverName, "badnet1", ipamOpt); err == nil {
t.Fatalf("bad network driver should have failed network creation")
}
gnw, err := c.NewNetwork("bridge", "goodnet1", ipamOpt)
if err != nil {
t.Fatal(err)
}
gnw.Delete()
// Now check whether ipam release works on endpoint creation failure
bd.failNetworkCreation = false
bnw, err := c.NewNetwork(badDriverName, "badnet2", ipamOpt)
if err != nil {
t.Fatal(err)
}
defer bnw.Delete()
if _, err := bnw.CreateEndpoint("ep0"); err == nil {
t.Fatalf("bad network driver should have failed endpoint creation")
}
// Now create good bridge network with different gateway
ipamOpt2 := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{&IpamConf{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.253"}}, nil)
gnw, err = c.NewNetwork("bridge", "goodnet2", ipamOpt2)
if err != nil {
t.Fatal(err)
}
defer gnw.Delete()
ep, err := gnw.CreateEndpoint("ep1")
if err != nil {
t.Fatal(err)
}
defer ep.Delete()
expectedIP, _ := types.ParseCIDR("10.34.0.1/16")
if !types.CompareIPNet(ep.Info().Iface().Address(), expectedIP) {
t.Fatalf("Ipam release must have failed, endpoint has unexpected address: %v", ep.Info().Iface().Address())
}
}
示例12: TestSubnetsMarshal
func TestSubnetsMarshal(t *testing.T) {
a, err := getAllocator()
if err != nil {
t.Fatal(err)
}
pid0, _, _, err := a.RequestPool(localAddressSpace, "192.168.0.0/16", "", nil, false)
if err != nil {
t.Fatal(err)
}
pid1, _, _, err := a.RequestPool(localAddressSpace, "192.169.0.0/16", "", nil, false)
if err != nil {
t.Fatal(err)
}
_, _, err = a.RequestAddress(pid0, nil, nil)
if err != nil {
t.Fatal(err)
}
cfg, err := a.getAddrSpace(localAddressSpace)
if err != nil {
t.Fatal(err)
}
ba := cfg.Value()
if err := cfg.SetValue(ba); err != nil {
t.Fatal(err)
}
expIP := &net.IPNet{IP: net.IP{192, 168, 0, 2}, Mask: net.IPMask{255, 255, 0, 0}}
ip, _, err := a.RequestAddress(pid0, nil, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(expIP, ip) {
t.Fatalf("Got unexpected ip after pool config restore: %s", ip)
}
expIP = &net.IPNet{IP: net.IP{192, 169, 0, 1}, Mask: net.IPMask{255, 255, 0, 0}}
ip, _, err = a.RequestAddress(pid1, nil, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(expIP, ip) {
t.Fatalf("Got unexpected ip after pool config restore: %s", ip)
}
}
示例13: compareIPNets
func compareIPNets(t *testing.T, kind string, shouldBe string, supplied net.IPNet) {
_, net, _ := net.ParseCIDR(shouldBe)
if net == nil {
t.Fatalf(`Invalid IP network to test against: "%s"`, shouldBe)
}
if !types.CompareIPNet(net, &supplied) {
t.Fatalf(`%s IP networks are not equal: expected "%s", got %v`, kind, shouldBe, supplied)
}
}
示例14: compareIpamInfoList
func compareIpamInfoList(listA, listB []*IpamInfo) bool {
var a, b *IpamInfo
if len(listA) != len(listB) {
return false
}
for i := 0; i < len(listA); i++ {
a = listA[i]
b = listB[i]
if a.PoolID != b.PoolID || !compareStringMaps(a.Meta, b.Meta) ||
!types.CompareIPNet(a.Gateway, b.Gateway) ||
a.AddressSpace != b.AddressSpace ||
!types.CompareIPNet(a.Pool, b.Pool) ||
!compareAddresses(a.AuxAddresses, b.AuxAddresses) {
return false
}
}
return true
}
示例15: TestNetworkRequest
func TestNetworkRequest(t *testing.T) {
defer testutils.SetupTestOSContext(t)()
ipamutils.InitNetworks()
_, exp, err := net.ParseCIDR("172.17.0.0/16")
if err != nil {
t.Fatal(err)
}
nw, err := FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(exp, nw) {
t.Fatalf("exected %s. got %s", exp, nw)
}
_, exp, err = net.ParseCIDR("10.0.0.0/24")
if err != nil {
t.Fatal(err)
}
nw, err = FindAvailableNetwork(ipamutils.PredefinedGranularNetworks)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(exp, nw) {
t.Fatalf("exected %s. got %s", exp, nw)
}
// Add iface and ssert returned address on request
createInterface(t, "test", "172.17.42.1/16")
_, exp, err = net.ParseCIDR("172.18.0.0/16")
if err != nil {
t.Fatal(err)
}
nw, err = FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(exp, nw) {
t.Fatalf("exected %s. got %s", exp, nw)
}
}