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
1=== modified file 'charmhelpers/core/host.py'
2--- charmhelpers/core/host.py 2014-02-26 15:53:02 +0000
3+++ charmhelpers/core/host.py 2014-05-14 12:29:05 +0000
4@@ -12,6 +12,7 @@
5 import string
6 import subprocess
7 import hashlib
8+import apt_pkg
9
10 from collections import OrderedDict
11
12@@ -295,3 +296,16 @@
13 if 'link/ether' in words:
14 hwaddr = words[words.index('link/ether') + 1]
15 return hwaddr
16+
17+
18+def cmp_pkgrevno(package, revno, pkgcache=None):
19+ '''Compare supplied revno with the revno of the installed package
20+ 1 => Installed revno is greater than supplied arg
21+ 0 => Installed revno is the same as supplied arg
22+ -1 => Installed revno is less than supplied arg
23+ '''
24+ if not pkgcache:
25+ apt_pkg.init()
26+ pkgcache = apt_pkg.Cache()
27+ pkg = pkgcache[package]
28+ return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)
29
30=== modified file 'tests/core/test_host.py'
31--- tests/core/test_host.py 2013-11-29 11:24:48 +0000
32+++ tests/core/test_host.py 2014-05-14 12:29:05 +0000
33@@ -1,5 +1,6 @@
34 from collections import OrderedDict
35 import subprocess
36+import apt_pkg
37
38 from mock import patch, call
39 from testtools import TestCase
40@@ -690,3 +691,21 @@
41 nic = "eth0"
42 hwaddr = host.get_nic_hwaddr(nic)
43 self.assertEqual(hwaddr, 'e4:11:5b:ab:a7:3c')
44+
45+ @patch.object(apt_pkg, 'Cache')
46+ def test_cmp_pkgrevno_revnos(self, pkg_cache):
47+ class MockPackage:
48+ class MockPackageRevno:
49+ def __init__(self, ver_str):
50+ self.ver_str = ver_str
51+
52+ def __init__(self, current_ver):
53+ self.current_ver = self.MockPackageRevno(current_ver)
54+
55+ pkg_dict = {
56+ 'python': MockPackage('2.4')
57+ }
58+ pkg_cache.return_value = pkg_dict
59+ self.assertEqual(host.cmp_pkgrevno('python', '2.3'), 1)
60+ self.assertEqual(host.cmp_pkgrevno('python', '2.4'), 0)
61+ self.assertEqual(host.cmp_pkgrevno('python', '2.5'), -1)

Subscribers

People subscribed via source and target branches