2015-05-06 19:57:38 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-10-03 19:11:50 -04:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-05-06 19:57:38 -04:00
|
|
|
"testing"
|
2016-09-20 11:59:32 -04:00
|
|
|
"time"
|
2015-05-06 19:57:38 -04:00
|
|
|
|
2017-06-06 19:04:50 -04:00
|
|
|
"github.com/docker/libnetwork/common"
|
2015-10-15 17:41:07 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2016-01-28 14:54:03 -05:00
|
|
|
"github.com/docker/libnetwork/discoverapi"
|
2015-05-06 19:57:38 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-10-06 23:29:30 -04:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-10-03 19:11:50 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2017-06-18 08:25:58 -04:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-10-15 17:41:07 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-10-03 19:11:50 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-06 19:57:38 -04:00
|
|
|
)
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func TestNetworkMarshalling(t *testing.T) {
|
|
|
|
n := &network{
|
|
|
|
name: "Miao",
|
|
|
|
id: "abccba",
|
|
|
|
ipamType: "default",
|
|
|
|
addrSpace: "viola",
|
|
|
|
networkType: "bridge",
|
|
|
|
enableIPv6: true,
|
|
|
|
persist: true,
|
2017-04-07 13:51:39 -04:00
|
|
|
configOnly: true,
|
|
|
|
configFrom: "configOnlyX",
|
2016-01-07 15:57:22 -05:00
|
|
|
ipamOptions: map[string]string{
|
|
|
|
netlabel.MacAddress: "a:b:c:d:e:f",
|
2016-04-07 12:36:53 -04:00
|
|
|
"primary": "",
|
2016-01-07 15:57:22 -05:00
|
|
|
},
|
2015-10-03 19:11:50 -04:00
|
|
|
ipamV4Config: []*IpamConf{
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-03 19:11:50 -04:00
|
|
|
PreferredPool: "10.2.0.0/16",
|
|
|
|
SubPool: "10.2.0.0/24",
|
2016-01-07 15:57:22 -05:00
|
|
|
Gateway: "",
|
|
|
|
AuxAddresses: nil,
|
2015-10-03 19:11:50 -04:00
|
|
|
},
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-03 19:11:50 -04:00
|
|
|
PreferredPool: "10.2.0.0/16",
|
|
|
|
SubPool: "10.2.1.0/24",
|
|
|
|
Gateway: "10.2.1.254",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ipamV6Config: []*IpamConf{
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-03 19:11:50 -04:00
|
|
|
PreferredPool: "abcd::/64",
|
|
|
|
SubPool: "abcd:abcd:abcd:abcd:abcd::/80",
|
|
|
|
Gateway: "abcd::29/64",
|
|
|
|
AuxAddresses: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ipamV4Info: []*IpamInfo{
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-03 19:11:50 -04:00
|
|
|
PoolID: "ipoolverde123",
|
|
|
|
Meta: map[string]string{
|
|
|
|
netlabel.Gateway: "10.2.1.255/16",
|
|
|
|
},
|
|
|
|
IPAMData: driverapi.IPAMData{
|
|
|
|
AddressSpace: "viola",
|
|
|
|
Pool: &net.IPNet{
|
|
|
|
IP: net.IP{10, 2, 0, 0},
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
},
|
|
|
|
Gateway: nil,
|
|
|
|
AuxAddresses: nil,
|
|
|
|
},
|
|
|
|
},
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-03 19:11:50 -04:00
|
|
|
PoolID: "ipoolblue345",
|
|
|
|
Meta: map[string]string{
|
|
|
|
netlabel.Gateway: "10.2.1.255/16",
|
|
|
|
},
|
|
|
|
IPAMData: driverapi.IPAMData{
|
|
|
|
AddressSpace: "viola",
|
|
|
|
Pool: &net.IPNet{
|
|
|
|
IP: net.IP{10, 2, 1, 0},
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
},
|
|
|
|
Gateway: &net.IPNet{IP: net.IP{10, 2, 1, 254}, Mask: net.IPMask{255, 255, 255, 0}},
|
|
|
|
AuxAddresses: map[string]*net.IPNet{
|
2016-02-12 08:07:00 -05:00
|
|
|
"ip3": {IP: net.IP{10, 2, 1, 3}, Mask: net.IPMask{255, 255, 255, 0}},
|
|
|
|
"ip5": {IP: net.IP{10, 2, 1, 55}, Mask: net.IPMask{255, 255, 255, 0}},
|
2015-10-03 19:11:50 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-03 19:11:50 -04:00
|
|
|
PoolID: "weirdinfo",
|
|
|
|
IPAMData: driverapi.IPAMData{
|
|
|
|
Gateway: &net.IPNet{
|
|
|
|
IP: net.IP{11, 2, 1, 255},
|
|
|
|
Mask: net.IPMask{255, 0, 0, 0},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ipamV6Info: []*IpamInfo{
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-03 19:11:50 -04:00
|
|
|
PoolID: "ipoolv6",
|
|
|
|
IPAMData: driverapi.IPAMData{
|
|
|
|
AddressSpace: "viola",
|
|
|
|
Pool: &net.IPNet{
|
|
|
|
IP: net.IP{0xab, 0xcd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
Mask: net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
Gateway: &net.IPNet{
|
|
|
|
IP: net.IP{0xab, 0xcd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29},
|
|
|
|
Mask: net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
AuxAddresses: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-04-07 12:36:53 -04:00
|
|
|
labels: map[string]string{
|
|
|
|
"color": "blue",
|
|
|
|
"superimposed": "",
|
|
|
|
},
|
2016-09-20 11:59:32 -04:00
|
|
|
created: time.Now(),
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nn := &network{}
|
|
|
|
err = json.Unmarshal(b, nn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.name != nn.name || n.id != nn.id || n.networkType != nn.networkType || n.ipamType != nn.ipamType ||
|
2015-10-12 01:28:26 -04:00
|
|
|
n.addrSpace != nn.addrSpace || n.enableIPv6 != nn.enableIPv6 ||
|
2015-10-03 19:11:50 -04:00
|
|
|
n.persist != nn.persist || !compareIpamConfList(n.ipamV4Config, nn.ipamV4Config) ||
|
|
|
|
!compareIpamInfoList(n.ipamV4Info, nn.ipamV4Info) || !compareIpamConfList(n.ipamV6Config, nn.ipamV6Config) ||
|
2016-04-07 12:36:53 -04:00
|
|
|
!compareIpamInfoList(n.ipamV6Info, nn.ipamV6Info) ||
|
|
|
|
!compareStringMaps(n.ipamOptions, nn.ipamOptions) ||
|
2016-09-20 11:59:32 -04:00
|
|
|
!compareStringMaps(n.labels, nn.labels) ||
|
2017-04-07 13:51:39 -04:00
|
|
|
!n.created.Equal(nn.created) ||
|
|
|
|
n.configOnly != nn.configOnly || n.configFrom != nn.configFrom {
|
2015-10-03 19:11:50 -04:00
|
|
|
t.Fatalf("JSON marsh/unmarsh failed."+
|
|
|
|
"\nOriginal:\n%#v\nDecoded:\n%#v"+
|
|
|
|
"\nOriginal ipamV4Conf: %#v\n\nDecoded ipamV4Conf: %#v"+
|
|
|
|
"\nOriginal ipamV4Info: %s\n\nDecoded ipamV4Info: %s"+
|
|
|
|
"\nOriginal ipamV6Conf: %#v\n\nDecoded ipamV6Conf: %#v"+
|
|
|
|
"\nOriginal ipamV6Info: %s\n\nDecoded ipamV6Info: %s",
|
|
|
|
n, nn, printIpamConf(n.ipamV4Config), printIpamConf(nn.ipamV4Config),
|
|
|
|
printIpamInfo(n.ipamV4Info), printIpamInfo(nn.ipamV4Info),
|
|
|
|
printIpamConf(n.ipamV6Config), printIpamConf(nn.ipamV6Config),
|
|
|
|
printIpamInfo(n.ipamV6Info), printIpamInfo(nn.ipamV6Info))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printIpamConf(list []*IpamConf) string {
|
|
|
|
s := fmt.Sprintf("\n[]*IpamConfig{")
|
|
|
|
for _, i := range list {
|
|
|
|
s = fmt.Sprintf("%s %v,", s, i)
|
|
|
|
}
|
|
|
|
s = fmt.Sprintf("%s}", s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func printIpamInfo(list []*IpamInfo) string {
|
|
|
|
s := fmt.Sprintf("\n[]*IpamInfo{")
|
|
|
|
for _, i := range list {
|
|
|
|
s = fmt.Sprintf("%s\n{\n%s\n}", s, i)
|
|
|
|
}
|
|
|
|
s = fmt.Sprintf("%s\n}", s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpointMarshalling(t *testing.T) {
|
2015-11-15 12:28:53 -05:00
|
|
|
ip, nw6, err := net.ParseCIDR("2001:db8:4003::122/64")
|
2015-10-03 19:11:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
nw6.IP = ip
|
|
|
|
|
2016-11-21 00:25:01 -05:00
|
|
|
var lla []*net.IPNet
|
|
|
|
for _, nw := range []string{"169.254.0.1/16", "169.254.1.1/16", "169.254.2.2/16"} {
|
|
|
|
ll, _ := types.ParseCIDR(nw)
|
|
|
|
lla = append(lla, ll)
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
e := &endpoint{
|
|
|
|
name: "Bau",
|
|
|
|
id: "efghijklmno",
|
|
|
|
sandboxID: "ambarabaciccicocco",
|
2015-10-20 15:12:16 -04:00
|
|
|
anonymous: true,
|
2015-10-03 19:11:50 -04:00
|
|
|
iface: &endpointInterface{
|
|
|
|
mac: []byte{11, 12, 13, 14, 15, 16},
|
|
|
|
addr: &net.IPNet{
|
|
|
|
IP: net.IP{10, 0, 1, 23},
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
},
|
|
|
|
addrv6: nw6,
|
|
|
|
srcName: "veth12ab1314",
|
|
|
|
dstPrefix: "eth",
|
2015-10-04 00:25:57 -04:00
|
|
|
v4PoolID: "poolpool",
|
|
|
|
v6PoolID: "poolv6",
|
2016-11-21 00:25:01 -05:00
|
|
|
llAddrs: lla,
|
2015-10-03 19:11:50 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ee := &endpoint{}
|
|
|
|
err = json.Unmarshal(b, ee)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-20 15:12:16 -04:00
|
|
|
if e.name != ee.name || e.id != ee.id || e.sandboxID != ee.sandboxID || !compareEndpointInterface(e.iface, ee.iface) || e.anonymous != ee.anonymous {
|
2015-10-03 19:11:50 -04:00
|
|
|
t.Fatalf("JSON marsh/unmarsh failed.\nOriginal:\n%#v\nDecoded:\n%#v\nOriginal iface: %#v\nDecodediface:\n%#v", e, ee, e.iface, ee.iface)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareEndpointInterface(a, b *endpointInterface) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-10-04 00:25:57 -04:00
|
|
|
return a.srcName == b.srcName && a.dstPrefix == b.dstPrefix && a.v4PoolID == b.v4PoolID && a.v6PoolID == b.v6PoolID &&
|
2016-11-21 00:25:01 -05:00
|
|
|
types.CompareIPNet(a.addr, b.addr) && types.CompareIPNet(a.addrv6, b.addrv6) && compareNwLists(a.llAddrs, b.llAddrs)
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func compareIpamConfList(listA, listB []*IpamConf) bool {
|
|
|
|
var a, b *IpamConf
|
|
|
|
if len(listA) != len(listB) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(listA); i++ {
|
|
|
|
a = listA[i]
|
|
|
|
b = listB[i]
|
|
|
|
if a.PreferredPool != b.PreferredPool ||
|
2015-10-09 20:06:52 -04:00
|
|
|
a.SubPool != b.SubPool ||
|
2015-10-03 19:11:50 -04:00
|
|
|
a.Gateway != b.Gateway || !compareStringMaps(a.AuxAddresses, b.AuxAddresses) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareStringMaps(a, b map[string]string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(a) > 0 {
|
|
|
|
for k := range a {
|
|
|
|
if a[k] != b[k] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareAddresses(a, b map[string]*net.IPNet) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(a) > 0 {
|
|
|
|
for k := range a {
|
|
|
|
if !types.CompareIPNet(a[k], b[k]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 00:25:01 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareNwLists(a, b []*net.IPNet) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k := range a {
|
|
|
|
if !types.CompareIPNet(a[k], b[k]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
return true
|
|
|
|
}
|
2015-10-06 23:29:30 -04:00
|
|
|
|
|
|
|
func TestAuxAddresses(t *testing.T) {
|
|
|
|
c, err := New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Stop()
|
|
|
|
|
2015-10-20 15:12:16 -04:00
|
|
|
n := &network{ipamType: ipamapi.DefaultIPAM, networkType: "bridge", ctrlr: c.(*controller)}
|
2015-10-06 23:29:30 -04:00
|
|
|
|
|
|
|
input := []struct {
|
|
|
|
masterPool string
|
|
|
|
subPool string
|
|
|
|
auxAddresses map[string]string
|
|
|
|
good bool
|
|
|
|
}{
|
|
|
|
{"192.168.0.0/16", "", map[string]string{"goodOne": "192.168.2.2"}, true},
|
|
|
|
{"192.168.0.0/16", "", map[string]string{"badOne": "192.169.2.3"}, false},
|
|
|
|
{"192.168.0.0/16", "192.168.1.0/24", map[string]string{"goodOne": "192.168.1.2"}, true},
|
|
|
|
{"192.168.0.0/16", "192.168.1.0/24", map[string]string{"stillGood": "192.168.2.4"}, true},
|
|
|
|
{"192.168.0.0/16", "192.168.1.0/24", map[string]string{"badOne": "192.169.2.4"}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range input {
|
|
|
|
|
2016-02-12 08:07:00 -05:00
|
|
|
n.ipamV4Config = []*IpamConf{{PreferredPool: i.masterPool, SubPool: i.subPool, AuxAddresses: i.auxAddresses}}
|
2015-10-06 23:29:30 -04:00
|
|
|
|
2015-10-04 00:25:57 -04:00
|
|
|
err = n.ipamAllocate()
|
2015-10-06 23:29:30 -04:00
|
|
|
|
|
|
|
if i.good != (err == nil) {
|
|
|
|
t.Fatalf("Unexpected result for %v: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.ipamRelease()
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 17:41:07 -04:00
|
|
|
|
2016-05-08 03:48:04 -04:00
|
|
|
func TestSRVServiceQuery(t *testing.T) {
|
|
|
|
c, err := New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Stop()
|
|
|
|
|
|
|
|
n, err := c.NewNetwork("bridge", "net1", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep, err := n.CreateEndpoint("testep")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := c.NewSandbox("c1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep.Join(sb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sr := svcInfo{
|
2017-06-18 08:25:58 -04:00
|
|
|
svcMap: common.NewSetMatrix(),
|
|
|
|
svcIPv6Map: common.NewSetMatrix(),
|
2017-06-06 19:04:50 -04:00
|
|
|
ipMap: common.NewSetMatrix(),
|
2016-05-08 03:48:04 -04:00
|
|
|
service: make(map[string][]servicePorts),
|
|
|
|
}
|
|
|
|
// backing container for the service
|
|
|
|
cTarget := serviceTarget{
|
|
|
|
name: "task1.web.swarm",
|
|
|
|
ip: net.ParseIP("192.168.10.2"),
|
|
|
|
port: 80,
|
|
|
|
}
|
|
|
|
// backing host for the service
|
|
|
|
hTarget := serviceTarget{
|
|
|
|
name: "node1.docker-cluster",
|
|
|
|
ip: net.ParseIP("10.10.10.2"),
|
|
|
|
port: 45321,
|
|
|
|
}
|
|
|
|
httpPort := servicePorts{
|
|
|
|
portName: "_http",
|
|
|
|
proto: "_tcp",
|
|
|
|
target: []serviceTarget{cTarget},
|
|
|
|
}
|
|
|
|
|
|
|
|
extHTTPPort := servicePorts{
|
|
|
|
portName: "_host_http",
|
|
|
|
proto: "_tcp",
|
|
|
|
target: []serviceTarget{hTarget},
|
|
|
|
}
|
|
|
|
sr.service["web.swarm"] = append(sr.service["web.swarm"], httpPort)
|
|
|
|
sr.service["web.swarm"] = append(sr.service["web.swarm"], extHTTPPort)
|
|
|
|
|
|
|
|
c.(*controller).svcRecords[n.ID()] = sr
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
_, ip := ep.Info().Sandbox().ResolveService("_http._tcp.web.swarm")
|
|
|
|
|
2016-05-08 03:48:04 -04:00
|
|
|
if len(ip) == 0 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if ip[0].String() != "192.168.10.2" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
_, ip = ep.Info().Sandbox().ResolveService("_host_http._tcp.web.swarm")
|
|
|
|
|
2016-05-08 03:48:04 -04:00
|
|
|
if len(ip) == 0 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if ip[0].String() != "10.10.10.2" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-12 18:40:39 -04:00
|
|
|
// Service name with invalid protocol name. Should fail without error
|
2016-09-19 18:48:06 -04:00
|
|
|
_, ip = ep.Info().Sandbox().ResolveService("_http._icmp.web.swarm")
|
2016-08-12 18:40:39 -04:00
|
|
|
if len(ip) != 0 {
|
|
|
|
t.Fatal("Valid response for invalid service name")
|
2016-05-08 03:48:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-18 08:25:58 -04:00
|
|
|
func TestServiceVIPReuse(t *testing.T) {
|
|
|
|
c, err := New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Stop()
|
|
|
|
|
|
|
|
n, err := c.NewNetwork("bridge", "net1", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep, err := n.CreateEndpoint("testep")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := c.NewSandbox("c1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep.Join(sb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add 2 services with same name but different service ID to share the same VIP
|
|
|
|
n.(*network).addSvcRecords("ep1", "service_test", "serviceID1", net.ParseIP("192.168.0.1"), net.IP{}, true, "test")
|
|
|
|
n.(*network).addSvcRecords("ep2", "service_test", "serviceID2", net.ParseIP("192.168.0.1"), net.IP{}, true, "test")
|
|
|
|
|
|
|
|
ipToResolve := netutils.ReverseIP("192.168.0.1")
|
|
|
|
|
|
|
|
ipList, _ := n.(*network).ResolveName("service_test", types.IPv4)
|
|
|
|
if len(ipList) == 0 {
|
|
|
|
t.Fatal("There must be the VIP")
|
|
|
|
}
|
|
|
|
if len(ipList) != 1 {
|
|
|
|
t.Fatal("It must return only 1 VIP")
|
|
|
|
}
|
|
|
|
if ipList[0].String() != "192.168.0.1" {
|
|
|
|
t.Fatal("The service VIP is 192.168.0.1")
|
|
|
|
}
|
|
|
|
name := n.(*network).ResolveIP(ipToResolve)
|
|
|
|
if name == "" {
|
|
|
|
t.Fatal("It must return a name")
|
|
|
|
}
|
|
|
|
if name != "service_test.net1" {
|
|
|
|
t.Fatalf("It must return the service_test.net1 != %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete service record for one of the services, the IP should remain because one service is still associated with it
|
|
|
|
n.(*network).deleteSvcRecords("ep1", "service_test", "serviceID1", net.ParseIP("192.168.0.1"), net.IP{}, true, "test")
|
|
|
|
ipList, _ = n.(*network).ResolveName("service_test", types.IPv4)
|
|
|
|
if len(ipList) == 0 {
|
|
|
|
t.Fatal("There must be the VIP")
|
|
|
|
}
|
|
|
|
if len(ipList) != 1 {
|
|
|
|
t.Fatal("It must return only 1 VIP")
|
|
|
|
}
|
|
|
|
if ipList[0].String() != "192.168.0.1" {
|
|
|
|
t.Fatal("The service VIP is 192.168.0.1")
|
|
|
|
}
|
|
|
|
name = n.(*network).ResolveIP(ipToResolve)
|
|
|
|
if name == "" {
|
|
|
|
t.Fatal("It must return a name")
|
|
|
|
}
|
|
|
|
if name != "service_test.net1" {
|
|
|
|
t.Fatalf("It must return the service_test.net1 != %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete again the service using the previous service ID, nothing should happen
|
|
|
|
n.(*network).deleteSvcRecords("ep2", "service_test", "serviceID1", net.ParseIP("192.168.0.1"), net.IP{}, true, "test")
|
|
|
|
ipList, _ = n.(*network).ResolveName("service_test", types.IPv4)
|
|
|
|
if len(ipList) == 0 {
|
|
|
|
t.Fatal("There must be the VIP")
|
|
|
|
}
|
|
|
|
if len(ipList) != 1 {
|
|
|
|
t.Fatal("It must return only 1 VIP")
|
|
|
|
}
|
|
|
|
if ipList[0].String() != "192.168.0.1" {
|
|
|
|
t.Fatal("The service VIP is 192.168.0.1")
|
|
|
|
}
|
|
|
|
name = n.(*network).ResolveIP(ipToResolve)
|
|
|
|
if name == "" {
|
|
|
|
t.Fatal("It must return a name")
|
|
|
|
}
|
|
|
|
if name != "service_test.net1" {
|
|
|
|
t.Fatalf("It must return the service_test.net1 != %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete now using the second service ID, now all the entries should be gone
|
|
|
|
n.(*network).deleteSvcRecords("ep2", "service_test", "serviceID2", net.ParseIP("192.168.0.1"), net.IP{}, true, "test")
|
|
|
|
ipList, _ = n.(*network).ResolveName("service_test", types.IPv4)
|
|
|
|
if len(ipList) != 0 {
|
|
|
|
t.Fatal("All the VIPs should be gone now")
|
|
|
|
}
|
|
|
|
name = n.(*network).ResolveIP(ipToResolve)
|
|
|
|
if name != "" {
|
|
|
|
t.Fatalf("It must return empty no more services associated, instead:%s", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 17:41:07 -04:00
|
|
|
func TestIpamReleaseOnNetDriverFailures(t *testing.T) {
|
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgOptions, err := OptionBoltdbWithRandomDBFile()
|
2017-06-12 14:30:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-15 17:41:07 -04:00
|
|
|
c, err := New(cfgOptions...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Stop()
|
|
|
|
|
|
|
|
cc := c.(*controller)
|
2016-02-29 21:17:04 -05:00
|
|
|
|
|
|
|
if err := cc.drvRegistry.AddDriver(badDriverName, badDriverInit, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-15 17:41:07 -04:00
|
|
|
|
|
|
|
// 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
|
2016-02-12 08:07:00 -05:00
|
|
|
ipamOpt := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.254"}}, nil, nil)
|
2016-02-29 14:49:04 -05:00
|
|
|
if _, err := c.NewNetwork(badDriverName, "badnet1", "", ipamOpt); err == nil {
|
2015-10-15 17:41:07 -04:00
|
|
|
t.Fatalf("bad network driver should have failed network creation")
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:49:04 -05:00
|
|
|
gnw, err := c.NewNetwork("bridge", "goodnet1", "", ipamOpt)
|
2015-10-15 17:41:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
gnw.Delete()
|
|
|
|
|
|
|
|
// Now check whether ipam release works on endpoint creation failure
|
|
|
|
bd.failNetworkCreation = false
|
2016-02-29 14:49:04 -05:00
|
|
|
bnw, err := c.NewNetwork(badDriverName, "badnet2", "", ipamOpt)
|
2015-10-15 17:41:07 -04:00
|
|
|
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
|
2018-05-10 17:47:52 -04:00
|
|
|
ipamOpt2 := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{{PreferredPool: "10.35.0.0/16", Gateway: "10.35.255.253"}}, nil, nil)
|
2016-02-29 14:49:04 -05:00
|
|
|
gnw, err = c.NewNetwork("bridge", "goodnet2", "", ipamOpt2)
|
2015-10-15 17:41:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer gnw.Delete()
|
|
|
|
|
|
|
|
ep, err := gnw.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-08 14:24:14 -05:00
|
|
|
defer ep.Delete(false)
|
2015-10-15 17:41:07 -04:00
|
|
|
|
2018-05-10 17:47:52 -04:00
|
|
|
expectedIP, _ := types.ParseCIDR("10.35.0.1/16")
|
2015-10-15 17:41:07 -04:00
|
|
|
if !types.CompareIPNet(ep.Info().Iface().Address(), expectedIP) {
|
|
|
|
t.Fatalf("Ipam release must have failed, endpoint has unexpected address: %v", ep.Info().Iface().Address())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var badDriverName = "bad network driver"
|
|
|
|
|
|
|
|
type badDriver struct {
|
|
|
|
failNetworkCreation bool
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
var bd = badDriver{failNetworkCreation: true}
|
|
|
|
|
|
|
|
func badDriverInit(reg driverapi.DriverCallback, opt map[string]interface{}) error {
|
|
|
|
return reg.RegisterDriver(badDriverName, &bd, driverapi.Capability{DataScope: datastore.LocalScope})
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
func (b *badDriver) CreateNetwork(nid string, options map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
|
2015-10-15 17:41:07 -04:00
|
|
|
if b.failNetworkCreation {
|
|
|
|
return fmt.Errorf("I will not create any network")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b *badDriver) DeleteNetwork(nid string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b *badDriver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, options map[string]interface{}) error {
|
|
|
|
return fmt.Errorf("I will not create any endpoint")
|
|
|
|
}
|
|
|
|
func (b *badDriver) DeleteEndpoint(nid, eid string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b *badDriver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (b *badDriver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
|
|
|
|
return fmt.Errorf("I will not allow any join")
|
|
|
|
}
|
|
|
|
func (b *badDriver) Leave(nid, eid string) error {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-28 14:54:03 -05:00
|
|
|
func (b *badDriver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
|
2015-10-15 17:41:07 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-01-28 14:54:03 -05:00
|
|
|
func (b *badDriver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
|
2015-10-15 17:41:07 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b *badDriver) Type() string {
|
|
|
|
return badDriverName
|
|
|
|
}
|
2016-12-18 22:56:34 -05:00
|
|
|
func (b *badDriver) IsBuiltIn() bool {
|
|
|
|
return false
|
|
|
|
}
|
2015-12-07 17:45:51 -05:00
|
|
|
func (b *badDriver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b *badDriver) RevokeExternalConnectivity(nid, eid string) error {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-26 14:53:13 -05:00
|
|
|
|
2016-02-26 14:54:35 -05:00
|
|
|
func (b *badDriver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
|
2016-02-26 14:53:13 -05:00
|
|
|
return nil, types.NotImplementedErrorf("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *badDriver) NetworkFree(id string) error {
|
|
|
|
return types.NotImplementedErrorf("not implemented")
|
|
|
|
}
|
2016-04-18 22:55:39 -04:00
|
|
|
|
|
|
|
func (b *badDriver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
|
|
|
|
}
|
2017-03-02 02:57:37 -05:00
|
|
|
|
|
|
|
func (b *badDriver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
|
|
|
|
return "", nil
|
|
|
|
}
|