2015-10-03 19:11:50 -04:00
|
|
|
package driverapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2015-10-03 19:11:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2016-02-12 08:07:00 -05:00
|
|
|
"ip1": {IP: net.IP{10, 10, 10, 1}, Mask: net.IPMask{255, 255, 255, 0}},
|
|
|
|
"ip2": {IP: net.IP{10, 10, 10, 2}, Mask: net.IPMask{255, 255, 255, 0}},
|
2015-10-03 19:11:50 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateAndIsV6(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
i := &IPAMData{
|
|
|
|
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{
|
2016-02-12 08:07:00 -05:00
|
|
|
"ip1": {IP: net.IP{10, 10, 10, 1}, Mask: net.IPMask{255, 255, 255, 0}},
|
|
|
|
"ip2": {IP: net.IP{10, 10, 10, 2}, Mask: net.IPMask{255, 255, 255, 0}},
|
2015-10-03 19:11:50 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check ip version
|
|
|
|
if i.IsV6() {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("incorrect ip version returned")
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
orig := i.Pool
|
2015-11-15 12:28:53 -05:00
|
|
|
if i.Pool, err = types.ParseCIDR("2001:db8::33/64"); err != nil {
|
2015-10-03 19:11:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !i.IsV6() {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("incorrect ip version returned")
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
i.Pool = orig
|
|
|
|
|
|
|
|
// valid ip data
|
|
|
|
if err = i.Validate(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incongruent gw ver
|
2015-11-15 12:28:53 -05:00
|
|
|
if i.Gateway, err = types.ParseCIDR("2001:db8::45/65"); err != nil {
|
2015-10-03 19:11:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = i.Validate(); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("expected error but succeeded")
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
i.Gateway = nil
|
|
|
|
|
|
|
|
// incongruent secondary ip ver
|
2015-11-15 12:28:53 -05:00
|
|
|
if i.AuxAddresses["ip2"], err = types.ParseCIDR("2001:db8::44/80"); err != nil {
|
2015-10-03 19:11:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = i.Validate(); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("expected error but succeeded")
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
delete(i.AuxAddresses, "ip2")
|
|
|
|
|
|
|
|
// gw outside pool
|
|
|
|
if i.Gateway, err = types.ParseCIDR("10.10.15.254/24"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = i.Validate(); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("expected error but succeeded")
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
i.Gateway = nil
|
|
|
|
|
|
|
|
// sec ip outside of pool
|
|
|
|
if i.AuxAddresses["ip1"], err = types.ParseCIDR("10.10.2.1/24"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = i.Validate(); err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("expected error but succeeded")
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
}
|