Skip to content

Commit

Permalink
Fix IPMask marshalling
Browse files Browse the repository at this point in the history
Fix marshalling and add test

Signed-off-by: Flavio Crisciani <[email protected]>
  • Loading branch information
Flavio Crisciani committed Oct 4, 2017
1 parent 2154459 commit c32647e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
21 changes: 12 additions & 9 deletions drivers/overlay/peerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,32 @@ type peerEntry struct {
}

func (p *peerEntry) MarshalDB() peerEntryDB {
ones, bits := p.peerIPMask.Size()
return peerEntryDB{
eid: p.eid,
vtep: p.vtep.String(),
peerIPMask: p.peerIPMask.String(),
isLocal: p.isLocal,
eid: p.eid,
vtep: p.vtep.String(),
peerIPMaskOnes: ones,
peerIPMaskBits: bits,
isLocal: p.isLocal,
}
}

// This the structure saved into the set (SetMatrix), due to the implementation of it
// the value inserted in the set has to be Hashable so the []byte had to be converted into
// strings
type peerEntryDB struct {
eid string
vtep string
peerIPMask string
isLocal bool
eid string
vtep string
peerIPMaskOnes int
peerIPMaskBits int
isLocal bool
}

func (p *peerEntryDB) UnMarshalDB() peerEntry {
return peerEntry{
eid: p.eid,
vtep: net.ParseIP(p.vtep),
peerIPMask: net.IPMask(net.ParseIP(p.peerIPMask)),
peerIPMask: net.CIDRMask(p.peerIPMaskOnes, p.peerIPMaskBits),
isLocal: p.isLocal,
}
}
Expand Down
30 changes: 30 additions & 0 deletions drivers/overlay/peerdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package overlay

import (
"net"
"testing"

_ "github.com/docker/libnetwork/testutils"
)

func TestPeerMarshal(t *testing.T) {
_, ipNet, _ := net.ParseCIDR("192.168.0.1/24")
p := &peerEntry{eid: "eid",
isLocal: true,
peerIPMask: ipNet.Mask,
vtep: ipNet.IP}
entryDB := p.MarshalDB()
x := entryDB.UnMarshalDB()
if x.eid != p.eid {
t.Fatalf("Incorrect Unmarshalling for eid: %v != %v", x.eid, p.eid)
}
if x.isLocal != p.isLocal {
t.Fatalf("Incorrect Unmarshalling for isLocal: %v != %v", x.isLocal, p.isLocal)
}
if x.peerIPMask.String() != p.peerIPMask.String() {
t.Fatalf("Incorrect Unmarshalling for eid: %v != %v", x.peerIPMask, p.peerIPMask)
}
if x.vtep.String() != p.vtep.String() {
t.Fatalf("Incorrect Unmarshalling for eid: %v != %v", x.vtep, p.vtep)
}
}

0 comments on commit c32647e

Please sign in to comment.