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
=== modified file 'charmhelpers/core/host.py'
--- charmhelpers/core/host.py 2013-09-26 14:36:33 +0000
+++ charmhelpers/core/host.py 2013-11-14 10:38:45 +0000
@@ -245,3 +245,37 @@
245 random_chars = [245 random_chars = [
246 random.choice(alphanumeric_chars) for _ in range(length)]246 random.choice(alphanumeric_chars) for _ in range(length)]
247 return(''.join(random_chars))247 return(''.join(random_chars))
248
249
250def list_nics(nic_type):
251 '''Return a list of nics of given type(s)'''
252 if isinstance(nic_type, basestring):
253 int_types = [nic_type]
254 else:
255 int_types = nic_type
256 interfaces = []
257 for int_type in int_types:
258 cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
259 ip_output = subprocess.check_output(cmd).split('\n')
260 ip_output = (line for line in ip_output if line)
261 for line in ip_output:
262 if line.split()[1].startswith(int_type):
263 interfaces.append(line.split()[1].replace(":", ""))
264 return interfaces
265
266
267def set_nic_mtu(nic, mtu):
268 '''Set MTU on a network interface'''
269 cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
270 subprocess.check_call(cmd)
271
272
273def get_nic_mtu(nic):
274 cmd = ['ip', 'addr', 'show', nic]
275 ip_output = subprocess.check_output(cmd).split('\n')
276 mtu = ""
277 for line in ip_output:
278 words = line.split()
279 if 'mtu' in words:
280 mtu = words[words.index("mtu") + 1]
281 return mtu
248282
=== modified file 'tests/core/test_host.py'
--- tests/core/test_host.py 2013-10-20 16:03:15 +0000
+++ tests/core/test_host.py 2013-11-14 10:38:45 +0000
@@ -24,6 +24,18 @@
24DISTRIB_DESCRIPTION="Ubuntu Saucy Salamander (development branch)"24DISTRIB_DESCRIPTION="Ubuntu Saucy Salamander (development branch)"
25'''25'''
2626
27IP_LINE_ETH0 = ("""
282: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
29 link/ether e4:11:5b:ab:a7:3c brd ff:ff:ff:ff:ff:ff
30""")
31
32IP_LINE_ETH1 = ("""
333: eth1: <BROADCAST,MULTICAST> mtu 1546 qdisc noop state DOWN qlen 1000
34 link/ether e4:11:5b:ab:a7:3c brd ff:ff:ff:ff:ff:ff
35""")
36
37IP_LINES = IP_LINE_ETH0 + IP_LINE_ETH1
38
2739
28class HelpersTest(TestCase):40class HelpersTest(TestCase):
29 @patch('subprocess.call')41 @patch('subprocess.call')
@@ -645,3 +657,27 @@
645657
646 pw2 = host.pwgen(10)658 pw2 = host.pwgen(10)
647 self.assertNotEqual(pw, pw2, 'Duplicated password')659 self.assertNotEqual(pw, pw2, 'Duplicated password')
660
661 @patch('subprocess.check_output')
662 def test_list_nics(self, check_output):
663 check_output.return_value = IP_LINES
664 nics = host.list_nics('eth')
665 self.assertEqual(nics, ['eth0', 'eth1'])
666 nics = host.list_nics(['eth'])
667 self.assertEqual(nics, ['eth0', 'eth1'])
668
669 @patch('subprocess.check_call')
670 def test_set_nic_mtu(self, mock_call):
671 mock_call.return_value = 0
672 nic = 'eth7'
673 mtu = '1546'
674 #result = host.set_nic_mtu(nic, mtu)
675 host.set_nic_mtu(nic, mtu)
676 mock_call.assert_called_with(['ip', 'link', 'set', nic, 'mtu', mtu])
677
678 @patch('subprocess.check_output')
679 def test_get_nic_mtu(self, check_output):
680 check_output.return_value = IP_LINE_ETH0
681 nic = "eth0"
682 mtu = host.get_nic_mtu(nic)
683 self.assertEqual(mtu, '1500')

Subscribers

People subscribed via source and target branches