Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge #39 and #41 and add tests - bump to 0.19 #42

Merged
merged 10 commits into from
Oct 9, 2019
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ Before commiting and opening PRs, ensure that you have pre-commit hooks running:
Release notes
-------------

0.19
____

* Adds support for interfaces with VLAN notation, e.g. `eth2.2` #40
* Fetch MTU values from `ip` command results #39

0.18
____

Expand Down
2 changes: 1 addition & 1 deletion src/ifcfg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from . import parser, tools

__version__ = "0.18"
__version__ = "0.19"

Log = tools.minimal_logger(__name__)

Expand Down
10 changes: 7 additions & 3 deletions src/ifcfg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def get_command(cls):
@classmethod
def get_patterns(cls):
return [
r'(?P<device>^[-a-zA-Z0-9:]+): flags=(?P<flags>.*) mtu (?P<mtu>.*)',
r'(?P<device>^[-a-zA-Z0-9:\.]+): flags=(?P<flags>.*) mtu (?P<mtu>\d+)',
r'.*inet\s+(?P<inet4>[\d\.]+).*',
r'.*inet6\s+(?P<inet6>[\d\:abcdef]+).*',
r'.*broadcast (?P<broadcast>[^\s]*).*',
Expand Down Expand Up @@ -264,14 +264,18 @@ def default_interface(self):


class LinuxParser(UnixParser):
"""
A parser for certain ifconfig versions.
"""

@classmethod
def get_patterns(cls):
return super(LinuxParser, cls).get_patterns() + [
r'(?P<device>^[a-zA-Z0-9:_-]+)(.*)Link encap:(.*).*',
r'(?P<device>^[a-zA-Z0-9:_\-\.]+)(.*)Link encap:(.*).*',
r'(.*)Link encap:(.*)(HWaddr )(?P<ether>[^\s]*).*',
r'.*(inet addr:\s*)(?P<inet4>[^\s]+).*',
r'.*(inet6 addr:\s*)(?P<inet6>[^\s\/]+)',
r'.*(MTU:\s*)(?P<mtu>\d+)',
r'.*(P-t-P:)(?P<ptp>[^\s]*).*',
r'.*(Bcast:)(?P<broadcast>[^\s]*).*',
r'.*(Mask:)(?P<netmask>[^\s]*).*',
Expand Down Expand Up @@ -301,7 +305,7 @@ def get_command(cls):
@classmethod
def get_patterns(cls):
return [
r'\s*[0-9]+:\s+(?P<device>[^@:]+)[^:]*:.*mtu (?P<mtu>.*)',
r'\s*[0-9]+:\s+(?P<device>[^@:]+)[^:]*:.*mtu (?P<mtu>\d+)',
r'.*(inet\s)(?P<inet4>[\d\.]+)',
r'.*(inet6 )(?P<inet6>[^/]*).*',
r'.*(ether )(?P<ether>[^\s]*).*',
Expand Down
79 changes: 79 additions & 0 deletions tests/ifconfig_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,85 @@
status: active
"""

# See: https://github.com/ftao/python-ifcfg/issues/40
LINUX_VLAN = """
br2 Link encap:Ethernet HWaddr 08:00:27:7c:6d:9d
inet addr:10.0.0.1 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe7c:6d9d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:1528 (1.4 KiB)

br4 Link encap:Ethernet HWaddr 08:00:27:7c:6d:9d
inet addr:10.0.2.1 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe7c:6d9d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:727 errors:0 dropped:0 overruns:0 frame:0
TX packets:643 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:88547 (86.4 KiB) TX bytes:890348 (869.4 KiB)

brlan Link encap:Ethernet HWaddr 08:00:27:d8:78:41
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fed8:7841/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6905 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:351150 (342.9 KiB) TX bytes:1568 (1.5 KiB)

eth0 Link encap:Ethernet HWaddr 08:00:27:d8:78:41
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7353 errors:0 dropped:433 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:482610 (471.2 KiB) TX bytes:1568 (1.5 KiB)

eth1 Link encap:Ethernet HWaddr 08:00:27:f2:d0:cb
inet addr:172.16.25.205 Bcast:172.16.25.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fef2:d0cb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:65901 errors:0 dropped:0 overruns:0 frame:0
TX packets:36638 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:80272939 (76.5 MiB) TX bytes:5598473 (5.3 MiB)

eth2 Link encap:Ethernet HWaddr 08:00:27:7c:6d:9d
inet6 addr: fe80::a00:27ff:fe7c:6d9d/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:7200 errors:0 dropped:431 overruns:0 frame:0
TX packets:111 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:471382 (460.3 KiB) TX bytes:8368 (8.1 KiB)

eth2.2 Link encap:Ethernet HWaddr 08:00:27:7c:6d:9d
inet6 addr: fe80::a00:27ff:fe7c:6d9d/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:31 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:2278 (2.2 KiB)

eth2.4 Link encap:Ethernet HWaddr 08:00:27:7c:6d:9d
inet6 addr: fe80::a00:27ff:fe7c:6d9d/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:52 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:3704 (3.6 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:12786 errors:0 dropped:0 overruns:0 frame:0
TX packets:12786 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:1853178 (1.7 MiB) TX bytes:1853178 (1.7 MiB)
"""


ROUTE_OUTPUT = """
Kernel IP routing table
Expand Down
11 changes: 11 additions & 0 deletions tests/ifconfig_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def test_linuxdocker(self):
eq_(interfaces['br-736aa253dd57']['broadcast'], '0.0.0.0')
eq_(interfaces['br-736aa253dd57']['netmask'], '255.255.0.0')

def test_vlan(self):
"""
Regression test for: https://github.com/ftao/python-ifcfg/issues/40
"""
ifcfg.distro = 'Linux'
ifcfg.Parser = LinuxParser
parser = ifcfg.get_parser(ifconfig=ifconfig_out.LINUX_VLAN)
interfaces = parser.interfaces
self.assertEqual(len(interfaces.keys()), 9)
eq_(interfaces['eth2.2']['ether'], '08:00:27:7c:6d:9d')

def test_macosx(self):
ifcfg.distro = 'MacOSX'
ifcfg.Parser = ifcfg.get_parser_class()
Expand Down
4 changes: 4 additions & 0 deletions tests/ip_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ def test_linux(self):
# Connected interface
eq_(interfaces['wlp3s0']['ether'], 'a0:00:00:00:00:00')
eq_(interfaces['wlp3s0']['inet'], '192.168.12.34')
eq_(interfaces['wlp3s0']['inet4'], ['192.168.12.34'])
eq_(interfaces['wlp3s0']['inet6'], ['fd37:a521:ada9::869', 'fd37:a521:ada9:0:b9f7:44f8:bb19:c78c', 'fd37:a521:ada9:0:9073:a91:d14f:8087', 'fe80::205f:5d09:d0da:7aed'])
eq_(interfaces['wlp3s0']['broadcast'], '192.168.12.255')
eq_(interfaces['wlp3s0']['netmask'], '/24')
eq_(interfaces['wlp3s0']['mtu'], '1500')
# Connected interface
eq_(interfaces['enp6s0.2']['ether'], '00:73:00:5c:09:9a')
eq_(interfaces['enp6s0.2']['inet'], '10.2.2.253')
eq_(interfaces['enp6s0.2']['mtu'], '1500')
eq_(interfaces['enp6s0.2']['inet4'], ['10.2.2.253', '10.1.1.253'])
eq_(interfaces['enp6s0.2']['inet6'], [])

Expand All @@ -47,6 +50,7 @@ def test_linux_multi_inet4(self):
eq_(interfaces['eth0']['inet4'], ['192.168.13.1', '192.168.10.3'])
eq_(interfaces['eth0']['broadcast'], '192.168.13.255')
eq_(interfaces['eth0']['netmask'], '/24')
eq_(interfaces['eth0']['mtu'], '1500')

def test_default_interface(self):
ifcfg.distro = 'Linux'
Expand Down