Skip to content

Commit

Permalink
Merge pull request #25 from YuviGold/withoptions-set-macaddress
Browse files Browse the repository at this point in the history
Set MacAddress on returned objects from WithOptions functions
  • Loading branch information
milosgajdos authored Jul 30, 2020
2 parents f075bef + 814340f commit 24ae257
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 2 deletions.
7 changes: 7 additions & 0 deletions macvlan_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ func NewMacVlanLinkWithOptions(masterDev string, opts MacVlanOptions) (MacVlaner
return nil, fmt.Errorf("Incorrect options specified. Attempt to delete the link failed: %s", errDel)
}
}

hwaddr, err := net.ParseMAC(opts.MacAddr)
if err != nil {
return nil, err
}

macVlanIfc.HardwareAddr = hwaddr
}

masterIfc, err := net.InterfaceByName(masterDev)
Expand Down
70 changes: 70 additions & 0 deletions macvlan_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,73 @@ func Test_NewMacVlanLink(t *testing.T) {
}
}
}

type macvlnWithOptionsTest struct {
masterDev string
opts *MacVlanOptions
}

var macvlnWithOptionsTests = []macvlnWithOptionsTest{
{"master01", &MacVlanOptions{Dev: "test", MacAddr: "aa:aa:aa:aa:aa:aa", Mode: "bridge"}},
}

func Test_NewMacVlanLinkWithOptions(t *testing.T) {
for _, tt := range macvlnWithOptionsTests {
var iface *net.Interface

tl := &testLink{}

if err := tl.prepTestLink(tt.masterDev, "dummy"); err != nil {
t.Skipf("NewMacVlanLinkWithOptions test requries external command: %v", err)
}

if err := tl.create(); err != nil {
t.Fatalf("testLink.create failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}

mvln, err := NewMacVlanLinkWithOptions(tt.masterDev, *tt.opts)
if err != nil {
t.Fatalf("NewMacVlanLinkWithOptions(%s, %s) failed to run: %s", tt.masterDev, *tt.opts, err)
}

iface = mvln.NetInterface()

if iface.HardwareAddr.String() != tt.opts.MacAddr {
tl.teardown()
t.Fatalf("NewMacVlanLinkWithOptions(%s, %s) failed: expected %s, returned %s",
tt.masterDev, *tt.opts, tt.opts.MacAddr, iface.HardwareAddr.String())
}

mvlnName := iface.Name
if _, err := net.InterfaceByName(mvlnName); err != nil {
tl.teardown()
t.Fatalf("Could not find %s on the host: %s", mvlnName, err)
}

testRes, err := linkInfo(mvlnName, "macvlan")
if err != nil {
tl.teardown()
t.Fatalf("Failed to list %s operation mode: %s", mvlnName, err)
}

if testRes.linkType != "macvlan" {
tl.teardown()
t.Fatalf("NewMacVlanLinkWithOptions(%s, %s) failed: expected macvlan, returned %s",
tt.masterDev, *tt.opts, testRes.linkType)
}

if testRes.linkData != "bridge" {
tl.teardown()
t.Fatalf("NewMacVlanLinkWithOptions(%s, %s) failed: expected bridge, returned %s",
tt.masterDev, *tt.opts, testRes.linkData)
}

if err := tl.teardown(); err != nil {
t.Fatalf("testLink.teardown failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
}
}
7 changes: 7 additions & 0 deletions macvtap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func NewMacVtapLinkWithOptions(masterDev string, opts MacVlanOptions) (MacVtaper
errDel)
}
}

hwaddr, err := net.ParseMAC(opts.MacAddr)
if err != nil {
return nil, err
}

macVtapIfc.HardwareAddr = hwaddr
}

masterIfc, err := net.InterfaceByName(masterDev)
Expand Down
70 changes: 70 additions & 0 deletions macvtap_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,73 @@ func Test_NewMacVtapLink(t *testing.T) {
}
}
}

type macvtpWithOptionsTest struct {
masterDev string
opts *MacVlanOptions
}

var macvtpWithOptionsTests = []macvtpWithOptionsTest{
{"master01", &MacVlanOptions{Dev: "test", MacAddr: "aa:aa:aa:aa:aa:aa", Mode: "bridge"}},
}

func Test_NewMacVtapLinkWithOptions(t *testing.T) {
for _, tt := range macvtpWithOptionsTests {
var iface *net.Interface

tl := &testLink{}

if err := tl.prepTestLink(tt.masterDev, "dummy"); err != nil {
t.Skipf("NewMacVtapLinkWithOptions test requries external command: %v", err)
}

if err := tl.create(); err != nil {
t.Fatalf("testLink.create failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}

macvtp, err := NewMacVtapLinkWithOptions(tt.masterDev, *tt.opts)
if err != nil {
t.Fatalf("NewMacVtapLinkWithOptions(%s, %s) failed to run: %s", tt.masterDev, *tt.opts, err)
}

iface = macvtp.NetInterface()

if iface.HardwareAddr.String() != tt.opts.MacAddr {
tl.teardown()
t.Fatalf("NewMacVtapLinkWithOptions(%s, %s) failed: expected %s, returned %s",
tt.masterDev, *tt.opts, tt.opts.MacAddr, iface.HardwareAddr.String())
}

mvtpName := iface.Name
if _, err := net.InterfaceByName(mvtpName); err != nil {
tl.teardown()
t.Fatalf("Could not find %s on the host: %s", mvtpName, err)
}

testRes, err := linkInfo(mvtpName, "macvtap")
if err != nil {
tl.teardown()
t.Fatalf("Failed to list %s operation mode: %s", mvtpName, err)
}

if testRes.linkType != "macvtap" {
tl.teardown()
t.Fatalf("NewMacVtapLinkWithOptions(%s, %s) failed: expected macvtap, returned %s",
tt.masterDev, *tt.opts, testRes.linkType)
}

if testRes.linkData != "bridge" {
tl.teardown()
t.Fatalf("NewMacVtapLinkWithOptions(%s, %s) failed: expected bridge, returned %s",
tt.masterDev, *tt.opts, testRes.linkData)
}

if err := tl.teardown(); err != nil {
t.Fatalf("testLink.teardown failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
}
}
7 changes: 7 additions & 0 deletions vlan_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func NewVlanLinkWithOptions(masterDev string, opts VlanOptions) (Vlaner, error)
errDel)
}
}

hwaddr, err := net.ParseMAC(opts.MacAddr)
if err != nil {
return nil, err
}

vlanIfc.HardwareAddr = hwaddr
}

masterIfc, err := net.InterfaceByName(masterDev)
Expand Down
81 changes: 79 additions & 2 deletions vlan_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_NewVlanLink(t *testing.T) {

if testRes.linkType != "vlan" {
tl.teardown()
t.Fatalf("NewMacVlanLink(%s, %d) failed: expected vlan, returned %s",
t.Fatalf("NewVlanLink(%s, %d) failed: expected vlan, returned %s",
tt.masterDev, tt.id, testRes.linkType)
}

Expand All @@ -62,7 +62,7 @@ func Test_NewVlanLink(t *testing.T) {

if uint16(id) != tt.id {
tl.teardown()
t.Fatalf("NewMacVlanLink(%s, %d) failed: expected %d, returned %d",
t.Fatalf("NewVlanLink(%s, %d) failed: expected %d, returned %d",
tt.masterDev, tt.id, tt.id, id)
}

Expand All @@ -73,3 +73,80 @@ func Test_NewVlanLink(t *testing.T) {
}
}
}

type vlnWithOptionsTest struct {
masterDev string
opts *VlanOptions
}

var vlnWithOptionsTests = []vlnWithOptionsTest{
{"master01", &VlanOptions{Dev: "test", MacAddr: "aa:aa:aa:aa:aa:aa", Id: 10}},
{"master02", &VlanOptions{Dev: "test", MacAddr: "aa:aa:aa:aa:aa:aa", Id: 20}},
}

func Test_NewVlanLinkWithOptions(t *testing.T) {
for _, tt := range vlnWithOptionsTests {
var iface *net.Interface

tl := &testLink{}

if err := tl.prepTestLink(tt.masterDev, "dummy"); err != nil {
t.Skipf("NewVlanLinkWithOptions test requries external command: %v", err)
}

if err := tl.create(); err != nil {
t.Fatalf("testLink.create failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}

vln, err := NewVlanLinkWithOptions(tt.masterDev, *tt.opts)
if err != nil {
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed to run: %s", tt.masterDev, *tt.opts, err)
}

iface = vln.NetInterface()

if iface.HardwareAddr.String() != tt.opts.MacAddr {
tl.teardown()
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed: expected %s, returned %s",
tt.masterDev, *tt.opts, tt.opts.MacAddr, iface.HardwareAddr.String())
}

vlnName := vln.NetInterface().Name
if _, err := net.InterfaceByName(vlnName); err != nil {
tl.teardown()
t.Fatalf("Could not find %s on the host: %s", vlnName, err)
}

testRes, err := linkInfo(vlnName, "vlan")
if err != nil {
tl.teardown()
t.Fatalf("Failed to list %s operation mode: %s", vlnName, err)
}

if testRes.linkType != "vlan" {
tl.teardown()
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed: expected vlan, returned %s",
tt.masterDev, *tt.opts, testRes.linkType)
}

id, err := strconv.Atoi(testRes.linkData)
if err != nil {
tl.teardown()
t.Fatalf("Failed to convert link data %s : %s", testRes.linkData, err)
}

if uint16(id) != tt.opts.Id {
tl.teardown()
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed: expected %d, returned %d",
tt.masterDev, *tt.opts, tt.opts.Id, id)
}

if err := tl.teardown(); err != nil {
t.Fatalf("testLink.teardown failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
}
}

0 comments on commit 24ae257

Please sign in to comment.