Skip to content

Commit b04014a

Browse files
committed
Add unit tests for utils.should_upgrade_resource()
1 parent 9c713c3 commit b04014a

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

tests/unit/test_utils.py

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,85 @@
11
import unittest
22

3-
from juju.utils import juju_config_dir, juju_ssh_key_paths
3+
from juju import utils
4+
from juju.client import client
45

56

67
class TestDirResolve(unittest.TestCase):
78
def test_config_dir(self):
8-
config_dir = juju_config_dir()
9+
config_dir = utils.juju_config_dir()
910
assert 'local/share/juju' in config_dir
1011

1112
def test_juju_ssh_key_paths(self):
12-
public, private = juju_ssh_key_paths()
13+
public, private = utils.juju_ssh_key_paths()
1314
assert public.endswith('ssh/juju_id_rsa.pub')
1415
assert private.endswith('ssh/juju_id_rsa')
16+
17+
18+
class TestShouldUpgradeResource(unittest.TestCase):
19+
def test_should_upgrade_resource_no_same_rev(self):
20+
# fields are trimmed for readability
21+
res = {'created-at': '2019-10-24T20:45:19.201000',
22+
'description': 'The policy.d overrides file',
23+
'download': {'hash-sha-256': 'e3b0c4', 'hash-sha-384': '38b060a751ac914898b95b',
24+
'hash-sha-512': 'cf83e1357eef1a538327af927da3e',
25+
'hash-sha3-384': '0c63a75b1bbed1e058d5f004',
26+
'size': 0,
27+
'url': 'https://api.charmhub.io/api/v1/resMGU0L516cGTTwNam.policyd-override_0'},
28+
'filename': 'policyd-override.zip', 'name': 'policyd-override', 'revision': 0, 'type': 'file'}
29+
30+
existing = {
31+
'policyd-override':
32+
client.Resource(charmresource=None,
33+
application='keystone', id_='keystone/policyd-override', pending_id='',
34+
timestamp='0001-01-01T00:00:00Z', username='', name='policyd-override',
35+
origin='store', type='file', path='policyd-override.zip',
36+
description='The policy.doverrides file',
37+
revision=0, fingerprint='OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb',
38+
size=0)}
39+
assert not utils.should_upgrade_resource(res, existing)
40+
41+
def test_should_upgrade_resource_no_local_upload(self):
42+
# fields are trimmed for readability
43+
res = {'created-at': '2019-10-24T20:45:19.201000',
44+
'description': 'The policy.d overrides file',
45+
'download': {'hash-sha-256': 'e3b0c4', 'hash-sha-384': '38b060a751ac914898b95b',
46+
'hash-sha-512': 'cf83e1357eef1a538327af927da3e',
47+
'hash-sha3-384': '0c63a75b1bbed1e058d5f004',
48+
'size': 0,
49+
'url': 'https://api.charmhub.io/api/v1/resMGU0L516cGTTwNam.policyd-override_0'},
50+
'filename': 'policyd-override.zip', 'name': 'local_res', 'revision': 0,
51+
'type': 'file'}
52+
53+
existing = {
54+
'local_res':
55+
client.Resource(charmresource=None,
56+
application='keystone', id_='keystone/policyd-override', pending_id='',
57+
timestamp='0001-01-01T00:00:00Z', username='', name='policyd-override',
58+
origin='upload', type='file', path='policyd-override.zip',
59+
description='The policy.doverrides file',
60+
revision=0, fingerprint='OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb',
61+
size=0)}
62+
assert not utils.should_upgrade_resource(res, existing)
63+
64+
def test_should_upgrade_resource_yes_new_revision(self):
65+
# fields are trimmed for readability
66+
res = {'created-at': '2019-10-24T20:45:19.201000',
67+
'description': 'The policy.d overrides file',
68+
'download': {'hash-sha-256': 'e3b0c4', 'hash-sha-384': '38b060a751ac914898b95b',
69+
'hash-sha-512': 'cf83e1357eef1a538327af927da3e',
70+
'hash-sha3-384': '0c63a75b1bbed1e058d5f004',
71+
'size': 0,
72+
'url': 'https://api.charmhub.io/api/v1/resMGU0L516cGTTwNam.policyd-override_0'},
73+
'filename': 'policyd-override.zip', 'name': 'policyd-override', 'revision': 1,
74+
'type': 'file'}
75+
76+
existing = {
77+
'policyd-override':
78+
client.Resource(charmresource=None,
79+
application='keystone', id_='keystone/policyd-override', pending_id='',
80+
timestamp='0001-01-01T00:00:00Z', username='', name='policyd-override',
81+
origin='store', type='file', path='policyd-override.zip',
82+
description='The policy.doverrides file',
83+
revision=0, fingerprint='OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb',
84+
size=0)}
85+
assert utils.should_upgrade_resource(res, existing)

0 commit comments

Comments
 (0)