Merge lp:~gnuoy/charm-helpers/mtu-functions into lp:charm-helpers

Proposed by Liam Young
Status: Merged
Approved by: Tom Haddon
Approved revision: 97
Merged at revision: 97
Proposed branch: lp:~gnuoy/charm-helpers/mtu-functions
Merge into: lp:charm-helpers
Diff against target: 92 lines (+70/-0)
2 files modified
charmhelpers/core/host.py (+34/-0)
tests/core/test_host.py (+36/-0)
To merge this branch: bzr merge lp:~gnuoy/charm-helpers/mtu-functions
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Review via email: mp+195197@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Tom Haddon (mthaddon) wrote :

Looks good. Approved.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/host.py'
2--- charmhelpers/core/host.py 2013-09-26 14:36:33 +0000
3+++ charmhelpers/core/host.py 2013-11-14 10:38:45 +0000
4@@ -245,3 +245,37 @@
5 random_chars = [
6 random.choice(alphanumeric_chars) for _ in range(length)]
7 return(''.join(random_chars))
8+
9+
10+def list_nics(nic_type):
11+ '''Return a list of nics of given type(s)'''
12+ if isinstance(nic_type, basestring):
13+ int_types = [nic_type]
14+ else:
15+ int_types = nic_type
16+ interfaces = []
17+ for int_type in int_types:
18+ cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
19+ ip_output = subprocess.check_output(cmd).split('\n')
20+ ip_output = (line for line in ip_output if line)
21+ for line in ip_output:
22+ if line.split()[1].startswith(int_type):
23+ interfaces.append(line.split()[1].replace(":", ""))
24+ return interfaces
25+
26+
27+def set_nic_mtu(nic, mtu):
28+ '''Set MTU on a network interface'''
29+ cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
30+ subprocess.check_call(cmd)
31+
32+
33+def get_nic_mtu(nic):
34+ cmd = ['ip', 'addr', 'show', nic]
35+ ip_output = subprocess.check_output(cmd).split('\n')
36+ mtu = ""
37+ for line in ip_output:
38+ words = line.split()
39+ if 'mtu' in words:
40+ mtu = words[words.index("mtu") + 1]
41+ return mtu
42
43=== modified file 'tests/core/test_host.py'
44--- tests/core/test_host.py 2013-10-20 16:03:15 +0000
45+++ tests/core/test_host.py 2013-11-14 10:38:45 +0000
46@@ -24,6 +24,18 @@
47 DISTRIB_DESCRIPTION="Ubuntu Saucy Salamander (development branch)"
48 '''
49
50+IP_LINE_ETH0 = ("""
51+2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
52+ link/ether e4:11:5b:ab:a7:3c brd ff:ff:ff:ff:ff:ff
53+""")
54+
55+IP_LINE_ETH1 = ("""
56+3: eth1: <BROADCAST,MULTICAST> mtu 1546 qdisc noop state DOWN qlen 1000
57+ link/ether e4:11:5b:ab:a7:3c brd ff:ff:ff:ff:ff:ff
58+""")
59+
60+IP_LINES = IP_LINE_ETH0 + IP_LINE_ETH1
61+
62
63 class HelpersTest(TestCase):
64 @patch('subprocess.call')
65@@ -645,3 +657,27 @@
66
67 pw2 = host.pwgen(10)
68 self.assertNotEqual(pw, pw2, 'Duplicated password')
69+
70+ @patch('subprocess.check_output')
71+ def test_list_nics(self, check_output):
72+ check_output.return_value = IP_LINES
73+ nics = host.list_nics('eth')
74+ self.assertEqual(nics, ['eth0', 'eth1'])
75+ nics = host.list_nics(['eth'])
76+ self.assertEqual(nics, ['eth0', 'eth1'])
77+
78+ @patch('subprocess.check_call')
79+ def test_set_nic_mtu(self, mock_call):
80+ mock_call.return_value = 0
81+ nic = 'eth7'
82+ mtu = '1546'
83+ #result = host.set_nic_mtu(nic, mtu)
84+ host.set_nic_mtu(nic, mtu)
85+ mock_call.assert_called_with(['ip', 'link', 'set', nic, 'mtu', mtu])
86+
87+ @patch('subprocess.check_output')
88+ def test_get_nic_mtu(self, check_output):
89+ check_output.return_value = IP_LINE_ETH0
90+ nic = "eth0"
91+ mtu = host.get_nic_mtu(nic)
92+ self.assertEqual(mtu, '1500')

Subscribers

People subscribed via source and target branches