netconfig: implement spoof_hardware_addr for easier testing
This commit is contained in:
parent
2ef8bf6ea0
commit
1b004597c1
@ -22,6 +22,7 @@ const goldenInterfaces = `
|
||||
},
|
||||
{
|
||||
"hardware_addr": "02:73:53:00:b0:0c",
|
||||
"spoof_hardware_addr": "02:73:53:00:b0:aa",
|
||||
"name": "lan0",
|
||||
"addr": "192.168.42.1/24"
|
||||
}
|
||||
@ -124,6 +125,14 @@ func TestNetconfig(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
link, err := exec.Command("ip", "netns", "exec", ns, "ip", "link", "show", "dev", "lan0").Output()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(link), "link/ether 02:73:53:00:b0:aa") {
|
||||
t.Errorf("lan0 MAC address is not 02:73:53:00:b0:aa")
|
||||
}
|
||||
|
||||
addrs, err := exec.Command("ip", "netns", "exec", ns, "ip", "address", "show", "dev", "uplink0").Output()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -148,6 +148,7 @@ func applyDhcp6(dir string) error {
|
||||
|
||||
type InterfaceDetails struct {
|
||||
HardwareAddr string `json:"hardware_addr"` // e.g. dc:9b:9c:ee:72:fd
|
||||
SpoofHardwareAddr string `json:"spoof_hardware_addr"` // e.g. dc:9b:9c:ee:72:fd
|
||||
Name string `json:"name"` // e.g. uplink0, or lan0
|
||||
Addr string `json:"addr"` // e.g. 192.168.42.1/24
|
||||
}
|
||||
@ -190,6 +191,9 @@ func applyInterfaces(dir, root string) error {
|
||||
byHardwareAddr := make(map[string]InterfaceDetails)
|
||||
for _, details := range cfg.Interfaces {
|
||||
byHardwareAddr[details.HardwareAddr] = details
|
||||
if spoof := details.SpoofHardwareAddr; spoof != "" {
|
||||
byHardwareAddr[spoof] = details
|
||||
}
|
||||
}
|
||||
links, err := netlink.LinkList()
|
||||
for _, l := range links {
|
||||
@ -214,6 +218,16 @@ func applyInterfaces(dir, root string) error {
|
||||
attr.Name = details.Name
|
||||
}
|
||||
|
||||
if spoof := details.SpoofHardwareAddr; spoof != "" {
|
||||
hwaddr, err := net.ParseMAC(spoof)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ParseMAC(%q): %v", spoof, err)
|
||||
}
|
||||
if err := netlink.LinkSetHardwareAddr(l, hwaddr); err != nil {
|
||||
return fmt.Errorf("LinkSetHardwareAddr(%v): %v", hwaddr, err)
|
||||
}
|
||||
}
|
||||
|
||||
if attr.OperState != netlink.OperUp {
|
||||
// Set the interface to up, which is required by all other configuration.
|
||||
if err := netlink.LinkSetUp(l); err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user