Procházet zdrojové kódy

libnet/d/bridge: fix bridgeInterface.addresses()

addresses() would incorrectly return all IP addresses assigned to any
interface in the network namespace if exists() is false. This went
unnoticed as the unit test covering this case tested the method inside a
clean new network namespace, which had no interfaces brought up and
therefore no IP addresses assigned. Modifying
testutils.SetupTestOSContext() to bring up the loopback interface 'lo'
resulted in the loopback addresses 127.0.0.1 and [::1] being assigned to
the loopback interface, causing addresses() to return the loopback
addresses and TestAddressesEmptyInterface to start failing. Fix the
implementation of addresses() so that it only ever returns addresses for
the bridge interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider před 2 roky
rodič
revize
7b2308980c
1 změnil soubory, kde provedl 4 přidání a 0 odebrání
  1. 4 0
      libnetwork/drivers/bridge/interface.go

+ 4 - 0
libnetwork/drivers/bridge/interface.go

@@ -57,6 +57,10 @@ func (i *bridgeInterface) exists() bool {
 
 
 // addresses returns all IPv4 addresses and all IPv6 addresses for the bridge interface.
 // addresses returns all IPv4 addresses and all IPv6 addresses for the bridge interface.
 func (i *bridgeInterface) addresses() ([]netlink.Addr, []netlink.Addr, error) {
 func (i *bridgeInterface) addresses() ([]netlink.Addr, []netlink.Addr, error) {
+	if !i.exists() {
+		// A nonexistent interface, by definition, cannot have any addresses.
+		return nil, nil, nil
+	}
 	v4addr, err := i.nlh.AddrList(i.Link, netlink.FAMILY_V4)
 	v4addr, err := i.nlh.AddrList(i.Link, netlink.FAMILY_V4)
 	if err != nil {
 	if err != nil {
 		return nil, nil, fmt.Errorf("Failed to retrieve V4 addresses: %v", err)
 		return nil, nil, fmt.Errorf("Failed to retrieve V4 addresses: %v", err)