Skip to content

Commit 076e735

Browse files
committed
Split out the resource refresh decider logic into a separate function
So that we can unit test it
1 parent 02c1e65 commit 076e735

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
@@ -751,21 +751,8 @@ async def refresh(
751751
# Compute the difference btw resources needed and the existing resources
752752
resources_to_update = []
753753
for resource in charm_resources:
754-
# should upgrade resource?
755-
res_name = resource.get('Name', resource.get('name'))
756-
# no, if it's upload
757-
if existing_resources[res_name].origin == 'upload':
758-
continue
759-
760-
# no, if we already have it (and upstream doesn't have a newer res available)
761-
if res_name in existing_resources:
762-
available_rev = resource.get('Revision', resource.get('revision', -1))
763-
u_fields = existing_resources[res_name].unknown_fields
764-
existing_rev = u_fields.get('Revision', resource.get('revision', -1))
765-
if existing_rev >= available_rev:
766-
continue
767-
768-
resources_to_update.append(resource)
754+
if utils.should_upgrade_resource(resource, existing_resources):
755+
resources_to_update.append(resource)
769756

770757
# Update the resources
771758
if resources_to_update:

juju/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,30 @@ def series_selector(series_arg='', charm_url=None, model_config=None, supported_
523523
# Charm hasn't specified a default (likely due to being a local charm
524524
# deployed by path). Last chance, best we can do is default to LTS.
525525
return DEFAULT_SUPPORTED_LTS
526+
527+
528+
def should_upgrade_resource(available_resource, existing_resources):
529+
"""Called in the context of upgrade_charm. Given a resource R, takes a look at the resources we
530+
already have and decides if we need to refresh R.
531+
532+
:param dict[str] available_resource: The dict representing the client.Resource coming from the
533+
charmhub api. We're considering if we need to refresh this during upgrade_charm.
534+
:param dict[str] existing_resources: The dict coming from resources_facade.ListResources
535+
representing the resources of the currently deployed charm.
536+
537+
:result bool: The decision to refresh the given resource
538+
"""
539+
# should upgrade resource?
540+
res_name = available_resource.get('Name', available_resource.get('name'))
541+
# no, if it's upload
542+
if existing_resources[res_name].origin == 'upload':
543+
return False
544+
545+
# no, if we already have it (and upstream doesn't have a newer res available)
546+
if res_name in existing_resources:
547+
available_rev = available_resource.get('Revision', available_resource.get('revision', -1))
548+
u_fields = existing_resources[res_name].unknown_fields
549+
existing_rev = u_fields.get('Revision', u_fields.get('revision', -1))
550+
if existing_rev >= available_rev:
551+
return False
552+
return True

0 commit comments

Comments
 (0)