Skip to content

Commit 798a386

Browse files
committed
Add unit tests for utils.should_upgrade_resource()
1 parent 076e735 commit 798a386

1 file changed

Lines changed: 77 additions & 7 deletions

File tree

tests/unit/test_utils.py

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
import pytest
33

44
from juju.utils import series_selector, get_base_from_origin_or_channel, \
5-
parse_base_arg, juju_config_dir, juju_ssh_key_paths, \
6-
DEFAULT_SUPPORTED_LTS, get_series_version, get_version_series, \
7-
base_channel_to_series, base_channel_from_series, \
8-
get_os_from_series
9-
from juju.client import client
5+
parse_base_arg, DEFAULT_SUPPORTED_LTS, get_series_version, \
6+
get_version_series, base_channel_to_series, \
7+
base_channel_from_series, get_os_from_series
108
from juju.errors import JujuError
119
from juju.url import URL
10+
from juju import utils
11+
from juju.client import client
1212

1313

1414
class TestDirResolve(unittest.TestCase):
1515
def test_config_dir(self):
16-
config_dir = juju_config_dir()
16+
config_dir = utils.juju_config_dir()
1717
assert 'local/share/juju' in config_dir
1818

1919
def test_juju_ssh_key_paths(self):
20-
public, private = juju_ssh_key_paths()
20+
public, private = utils.juju_ssh_key_paths()
2121
assert public.endswith('ssh/juju_id_rsa.pub')
2222
assert private.endswith('ssh/juju_id_rsa')
2323

@@ -73,3 +73,73 @@ def test_get_os_from_series(self):
7373
def test_get_base_from_series(self):
7474
orgn = client.CharmOrigin(track='latest', risk='edge')
7575
assert get_base_from_origin_or_channel(orgn, series='jammy') == client.Base('22.04/edge', 'ubuntu')
76+
77+
78+
class TestShouldUpgradeResource(unittest.TestCase):
79+
def test_should_upgrade_resource_no_same_rev(self):
80+
# fields are trimmed for readability
81+
res = {'created-at': '2019-10-24T20:45:19.201000',
82+
'description': 'The policy.d overrides file',
83+
'download': {'hash-sha-256': 'e3b0c4', 'hash-sha-384': '38b060a751ac914898b95b',
84+
'hash-sha-512': 'cf83e1357eef1a538327af927da3e',
85+
'hash-sha3-384': '0c63a75b1bbed1e058d5f004',
86+
'size': 0,
87+
'url': 'https://api.charmhub.io/api/v1/resMGU0L516cGTTwNam.policyd-override_0'},
88+
'filename': 'policyd-override.zip', 'name': 'policyd-override', 'revision': 0, 'type': 'file'}
89+
90+
existing = {
91+
'policyd-override':
92+
client.Resource(charmresource=None,
93+
application='keystone', id_='keystone/policyd-override', pending_id='',
94+
timestamp='0001-01-01T00:00:00Z', username='', name='policyd-override',
95+
origin='store', type='file', path='policyd-override.zip',
96+
description='The policy.doverrides file',
97+
revision=0, fingerprint='OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb',
98+
size=0)}
99+
assert not utils.should_upgrade_resource(res, existing)
100+
101+
def test_should_upgrade_resource_no_local_upload(self):
102+
# fields are trimmed for readability
103+
res = {'created-at': '2019-10-24T20:45:19.201000',
104+
'description': 'The policy.d overrides file',
105+
'download': {'hash-sha-256': 'e3b0c4', 'hash-sha-384': '38b060a751ac914898b95b',
106+
'hash-sha-512': 'cf83e1357eef1a538327af927da3e',
107+
'hash-sha3-384': '0c63a75b1bbed1e058d5f004',
108+
'size': 0,
109+
'url': 'https://api.charmhub.io/api/v1/resMGU0L516cGTTwNam.policyd-override_0'},
110+
'filename': 'policyd-override.zip', 'name': 'local_res', 'revision': 0,
111+
'type': 'file'}
112+
113+
existing = {
114+
'local_res':
115+
client.Resource(charmresource=None,
116+
application='keystone', id_='keystone/policyd-override', pending_id='',
117+
timestamp='0001-01-01T00:00:00Z', username='', name='policyd-override',
118+
origin='upload', type='file', path='policyd-override.zip',
119+
description='The policy.doverrides file',
120+
revision=0, fingerprint='OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb',
121+
size=0)}
122+
assert not utils.should_upgrade_resource(res, existing)
123+
124+
def test_should_upgrade_resource_yes_new_revision(self):
125+
# fields are trimmed for readability
126+
res = {'created-at': '2019-10-24T20:45:19.201000',
127+
'description': 'The policy.d overrides file',
128+
'download': {'hash-sha-256': 'e3b0c4', 'hash-sha-384': '38b060a751ac914898b95b',
129+
'hash-sha-512': 'cf83e1357eef1a538327af927da3e',
130+
'hash-sha3-384': '0c63a75b1bbed1e058d5f004',
131+
'size': 0,
132+
'url': 'https://api.charmhub.io/api/v1/resMGU0L516cGTTwNam.policyd-override_0'},
133+
'filename': 'policyd-override.zip', 'name': 'policyd-override', 'revision': 1,
134+
'type': 'file'}
135+
136+
existing = {
137+
'policyd-override':
138+
client.Resource(charmresource=None,
139+
application='keystone', id_='keystone/policyd-override', pending_id='',
140+
timestamp='0001-01-01T00:00:00Z', username='', name='policyd-override',
141+
origin='store', type='file', path='policyd-override.zip',
142+
description='The policy.doverrides file',
143+
revision=0, fingerprint='OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb',
144+
size=0)}
145+
assert utils.should_upgrade_resource(res, existing)

0 commit comments

Comments
 (0)