Merge lp:~gnuoy/charm-helpers/add-packacerevno-method into lp:charm-helpers

Proposed by Liam Young
Status: Merged
Merged at revision: 151
Proposed branch: lp:~gnuoy/charm-helpers/add-packacerevno-method
Merge into: lp:charm-helpers
Diff against target: 61 lines (+33/-0)
2 files modified
charmhelpers/core/host.py (+14/-0)
tests/core/test_host.py (+19/-0)
To merge this branch: bzr merge lp:~gnuoy/charm-helpers/add-packacerevno-method
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+219509@code.launchpad.net
To post a comment you must log in.

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 2014-02-26 15:53:02 +0000
+++ charmhelpers/core/host.py 2014-05-14 12:29:05 +0000
@@ -12,6 +12,7 @@
12import string12import string
13import subprocess13import subprocess
14import hashlib14import hashlib
15import apt_pkg
1516
16from collections import OrderedDict17from collections import OrderedDict
1718
@@ -295,3 +296,16 @@
295 if 'link/ether' in words:296 if 'link/ether' in words:
296 hwaddr = words[words.index('link/ether') + 1]297 hwaddr = words[words.index('link/ether') + 1]
297 return hwaddr298 return hwaddr
299
300
301def cmp_pkgrevno(package, revno, pkgcache=None):
302 '''Compare supplied revno with the revno of the installed package
303 1 => Installed revno is greater than supplied arg
304 0 => Installed revno is the same as supplied arg
305 -1 => Installed revno is less than supplied arg
306 '''
307 if not pkgcache:
308 apt_pkg.init()
309 pkgcache = apt_pkg.Cache()
310 pkg = pkgcache[package]
311 return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)
298312
=== modified file 'tests/core/test_host.py'
--- tests/core/test_host.py 2013-11-29 11:24:48 +0000
+++ tests/core/test_host.py 2014-05-14 12:29:05 +0000
@@ -1,5 +1,6 @@
1from collections import OrderedDict1from collections import OrderedDict
2import subprocess2import subprocess
3import apt_pkg
34
4from mock import patch, call5from mock import patch, call
5from testtools import TestCase6from testtools import TestCase
@@ -690,3 +691,21 @@
690 nic = "eth0"691 nic = "eth0"
691 hwaddr = host.get_nic_hwaddr(nic)692 hwaddr = host.get_nic_hwaddr(nic)
692 self.assertEqual(hwaddr, 'e4:11:5b:ab:a7:3c')693 self.assertEqual(hwaddr, 'e4:11:5b:ab:a7:3c')
694
695 @patch.object(apt_pkg, 'Cache')
696 def test_cmp_pkgrevno_revnos(self, pkg_cache):
697 class MockPackage:
698 class MockPackageRevno:
699 def __init__(self, ver_str):
700 self.ver_str = ver_str
701
702 def __init__(self, current_ver):
703 self.current_ver = self.MockPackageRevno(current_ver)
704
705 pkg_dict = {
706 'python': MockPackage('2.4')
707 }
708 pkg_cache.return_value = pkg_dict
709 self.assertEqual(host.cmp_pkgrevno('python', '2.3'), 1)
710 self.assertEqual(host.cmp_pkgrevno('python', '2.4'), 0)
711 self.assertEqual(host.cmp_pkgrevno('python', '2.5'), -1)

Subscribers

People subscribed via source and target branches