Skip to content

Commit 9c713c3

Browse files
committed
Split out the resource refresh decider logic into a separate function
So that we can unit test it
1 parent cb1a678 commit 9c713c3

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

juju/application.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -742,21 +742,8 @@ async def refresh(
742742
# Compute the difference btw resources needed and the existing resources
743743
resources_to_update = []
744744
for resource in charm_resources:
745-
# should upgrade resource?
746-
res_name = resource.get('Name', resource.get('name'))
747-
# no, if it's upload
748-
if existing_resources[res_name].origin == 'upload':
749-
continue
750-
751-
# no, if we already have it (and upstream doesn't have a newer res available)
752-
if res_name in existing_resources:
753-
available_rev = resource.get('Revision', resource.get('revision', -1))
754-
u_fields = existing_resources[res_name].unknown_fields
755-
existing_rev = u_fields.get('Revision', resource.get('revision', -1))
756-
if existing_rev >= available_rev:
757-
continue
758-
759-
resources_to_update.append(resource)
745+
if utils.should_upgrade_resource(resource, existing_resources):
746+
resources_to_update.append(resource)
760747

761748
# Update the resources
762749
if resources_to_update:

juju/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,30 @@ def base_channel_to_series(channel):
389389
:return: str series (e.g. focal)
390390
"""
391391
return get_version_series(origin.Channel.parse(channel).track)
392+
393+
394+
def should_upgrade_resource(available_resource, existing_resources):
395+
"""Called in the context of upgrade_charm. Given a resource R, takes a look at the resources we
396+
already have and decides if we need to refresh R.
397+
398+
:param dict[str] available_resource: The dict representing the client.Resource coming from the
399+
charmhub api. We're considering if we need to refresh this during upgrade_charm.
400+
:param dict[str] existing_resources: The dict coming from resources_facade.ListResources
401+
representing the resources of the currently deployed charm.
402+
403+
:result bool: The decision to refresh the given resource
404+
"""
405+
# should upgrade resource?
406+
res_name = available_resource.get('Name', available_resource.get('name'))
407+
# no, if it's upload
408+
if existing_resources[res_name].origin == 'upload':
409+
return False
410+
411+
# no, if we already have it (and upstream doesn't have a newer res available)
412+
if res_name in existing_resources:
413+
available_rev = available_resource.get('Revision', available_resource.get('revision', -1))
414+
u_fields = existing_resources[res_name].unknown_fields
415+
existing_rev = u_fields.get('Revision', u_fields.get('revision', -1))
416+
if existing_rev >= available_rev:
417+
return False
418+
return True

0 commit comments

Comments
 (0)