aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRonan Amicel <ronan.amicel@gmail.com>2012-09-14 00:40:27 +0200
committerRonan Amicel <ronan.amicel@gmail.com>2012-09-14 00:59:35 +0200
commit356f5ad09f936045892bce5552d82fca096daf2f (patch)
tree804a27cee7bc0da57844476f53591d0c45643ff6
parentFix vagrant test cases (diff)
downloadfabtools-356f5ad09f936045892bce5552d82fca096daf2f.tar.xz
Improvements to the network module
-rw-r--r--fabtools/network.py26
-rw-r--r--fabtools/tests/fabfiles/network.py52
2 files changed, 76 insertions, 2 deletions
diff --git a/fabtools/network.py b/fabtools/network.py
index 7e048ec..0ade487 100644
--- a/fabtools/network.py
+++ b/fabtools/network.py
@@ -6,17 +6,39 @@ from __future__ import with_statement
from fabric.api import *
+def interfaces():
+ """
+ Get the list of network interfaces
+
+ >>> fabtools.network.interfaces()
+ ['eth0', 'eth1', 'lo']
+
+ """
+ with settings(hide('running', 'stdout')):
+ res = run('/sbin/ifconfig -s')
+ return map(lambda line: line.split(' ')[0], res.splitlines()[1:])
+
+
def address(interface):
"""
- Get IPV4 address assigned to an interface
+ Get the IPv4 address assigned to an interface
+
+ >>> fabtools.network.address('lo')
+ '127.0.0.1'
+
"""
with settings(hide('running', 'stdout')):
- return run("/sbin/ifconfig %s | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }'" % interface)
+ res = run("/sbin/ifconfig %(interface)s | grep 'inet addr'" % locals())
+ return res.split()[1].split(':')[1]
def nameservers():
"""
Get the list of nameserver addresses
+
+ >>> fabtools.network.nameservers()
+ ['208.67.222.222', '208.67.220.220']
+
"""
with settings(hide('running', 'stdout')):
res = run("cat /etc/resolv.conf | grep 'nameserver' | cut -d\ -f2")
diff --git a/fabtools/tests/fabfiles/network.py b/fabtools/tests/fabfiles/network.py
new file mode 100644
index 0000000..b131ae3
--- /dev/null
+++ b/fabtools/tests/fabfiles/network.py
@@ -0,0 +1,52 @@
+import socket
+
+from fabric.api import task
+import fabtools
+
+
+def is_valid_ipv4_address(address):
+ try:
+ _ = socket.inet_pton(socket.AF_INET, address)
+ except AttributeError: # no inet_pton here, sorry
+ try:
+ _ = socket.inet_aton(address)
+ except socket.error:
+ return False
+ return address.count('.') == 3
+ except socket.error: # not a valid address
+ return False
+ return True
+
+
+def is_valid_ipv6_address(address):
+ try:
+ _ = socket.inet_pton(socket.AF_INET6, address)
+ except socket.error: # not a valid address
+ return False
+ return True
+
+
+def is_valid_address(address):
+ return is_valid_ipv4_address(address) or is_valid_ipv6_address()
+
+
+@task
+def network():
+ """
+ Test network interfaces
+ """
+
+ interfaces = fabtools.network.interfaces()
+
+ # Check IPv4 addresses
+ for interface in interfaces:
+ ipv4_address = fabtools.network.address(interface)
+ assert is_valid_ipv4_address(ipv4_address), ipv4_address
+
+ # Check loopback interface
+ assert 'lo' in interfaces, interfaces
+ assert fabtools.network.address('lo') == '127.0.0.1'
+
+ # Check name servers
+ for address in fabtools.network.nameservers():
+ assert is_valid_address(address), address