Merge lp:~marcoceppi/charm-helpers/resource-get into lp:charm-helpers

Proposed by Marco Ceppi
Status: Merged
Merged at revision: 543
Proposed branch: lp:~marcoceppi/charm-helpers/resource-get
Merge into: lp:charm-helpers
Diff against target: 63 lines (+34/-1)
2 files modified
charmhelpers/core/hookenv.py (+18/-0)
tests/core/test_hookenv.py (+16/-1)
To merge this branch: bzr merge lp:~marcoceppi/charm-helpers/resource-get
Reviewer Review Type Date Requested Status
Adam Israel (community) Approve
Review via email: mp+288328@code.launchpad.net

Commit message

Adds support for resource-get

Description of the change

Support resource-get command

* Returns the path of the resource as provided by resource-get <resource>
* Returns False when resource is not available
* Raises NotImplementedError for all versions of Juju which resource-get is not available

To post a comment you must log in.
Revision history for this message
Adam Israel (aisrael) wrote :

Hey Marco. This looks good to me. +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2016-03-07 12:55:39 +0000
3+++ charmhelpers/core/hookenv.py 2016-03-07 18:04:29 +0000
4@@ -912,6 +912,24 @@
5 subprocess.check_call(cmd)
6
7
8+@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
9+def resource_get(name):
10+ """used to fetch the resource path of the given name.
11+
12+ <name> must match a name of defined resource in metadata.yaml
13+
14+ returns either a path or False if resource not available
15+ """
16+ if not name:
17+ return False
18+
19+ cmd = ['resource-get', name]
20+ try:
21+ return subprocess.check_output(cmd).decode('UTF-8')
22+ except subprocess.CalledProcessError:
23+ return False
24+
25+
26 @cached
27 def juju_version():
28 """Full version string (eg. '1.23.3.1-trusty-amd64')"""
29
30=== modified file 'tests/core/test_hookenv.py'
31--- tests/core/test_hookenv.py 2016-03-07 12:54:54 +0000
32+++ tests/core/test_hookenv.py 2016-03-07 18:04:29 +0000
33@@ -1160,6 +1160,21 @@
34 self.assertEqual(hookenv.charm_dir(), '/var/empty')
35
36 @patch('subprocess.check_output')
37+ def test_resource_get_unsupported(self, check_output_):
38+ check_output_.side_effect = OSError(2, 'resource-get')
39+ self.assertRaises(NotImplementedError, hookenv.resource_get, 'foo')
40+
41+ @patch('subprocess.check_output')
42+ def test_resource_get(self, check_output_):
43+ check_output_.return_value = b'/tmp/file'
44+ self.assertEqual(hookenv.resource_get('file'), '/tmp/file')
45+ check_output_.side_effect = CalledProcessError(
46+ 1, '/foo/bin/resource-get',
47+ 'error: could not download resource: resource#foo/file not found')
48+ self.assertFalse(hookenv.resource_get('no-file'))
49+ self.assertFalse(hookenv.resource_get(None))
50+
51+ @patch('subprocess.check_output')
52 def test_is_leader_unsupported(self, check_output_):
53 check_output_.side_effect = OSError(2, 'is-leader')
54 self.assertRaises(NotImplementedError, hookenv.is_leader)
55@@ -1613,7 +1628,7 @@
56
57 @patch('subprocess.check_output')
58 def test_network_get_primary_unsupported(self, check_output):
59- '''Ensure that NotImplementedError is thrown when run on Juju < 2.0'''
60+ '''Ensure that NotImplementedError is thrown when run on Juju < 2.0'''
61 check_output.side_effect = OSError(2, 'network-get')
62 self.assertRaises(NotImplementedError, hookenv.network_get_primary_address,
63 'mybinding')

Subscribers

People subscribed via source and target branches