Skip to content

Commit 9e0e39d

Browse files
authored
Merge pull request #978 from jack-w-shaw/JUJU-4779_switch_to_local_properly-master
#978 Ensure that we provide the server with a valid charm origin when switching to local charms. Previously, we simply sent back the same charm origin when switching to a local charm. However, this is not valid, since we don't know where we switched from. If it's a charmhub charm, it's origin will not be valid. Also, parse the revision from the new charm url the server sends us This is equivalent to: juju/juju#16434 #### QA Steps Edit line 26 of examples/local_refresh.py to point to a local ubuntu charm ``` $ juju deploy ubuntu $ juju mongo juju:PRIMARY> db.applications.find().pretty() ... { "_id" : "277dae9c-abe3-4aba-83e4-0a63622e2981:ubuntu", "name" : "ubuntu", "model-uuid" : "277dae9c-abe3-4aba-83e4-0a63622e2981", "subordinate" : false, "charmurl" : "ch:amd64/focal/ubuntu-24", "charm-origin" : { "source" : "charm-hub", "type" : "charm", "id" : "DksXQKAQTZfsUmBAGanZAhpoS4dpmXel", "hash" : "9b2877f7ebf04b348cf40ace32fdfc6d1cc0157dea7fd6740c4ba74e0e201e5b", "revision" : 24, "channel" : { "risk" : "stable" }, "platform" : { "architecture" : "amd64", "os" : "ubuntu", "channel" : "22.04" } }, "charmmodifiedversion" : 0, "forcecharm" : false, "life" : 0, "unitcount" : 1, "relationcount" : 0, "minunits" : 0, "txn-revno" : NumberLong(3), "metric-credentials" : BinData(0,""), "exposed" : false, "scale" : 0, "passwordhash" : "", "provisioning-state" : null } $ python examples/local_refresh.py $ juju mongo juju:PRIMARY> db.applications.find().pretty() ... { "_id" : "277dae9c-abe3-4aba-83e4-0a63622e2981:ubuntu", "name" : "ubuntu", "model-uuid" : "277dae9c-abe3-4aba-83e4-0a63622e2981", "subordinate" : false, "charmurl" : "local:jammy/ubuntu-13", "charm-origin" : { "source" : "local", "type" : "charm", "id" : "DksXQKAQTZfsUmBAGanZAhpoS4dpmXel", "hash" : "9b2877f7ebf04b348cf40ace32fdfc6d1cc0157dea7fd6740c4ba74e0e201e5b", "revision" : 13, "channel" : { "risk" : "stable" }, "platform" : { "architecture" : "amd64", "os" : "ubuntu", "channel" : "22.04" } }, "charmmodifiedversion" : 1, "forcecharm" : false, "life" : 0, "unitcount" : 1, "relationcount" : 0, "minunits" : 0, "txn-revno" : NumberLong(4), "metric-credentials" : BinData(0,""), "exposed" : false, "scale" : 0, "passwordhash" : "", "provisioning-state" : null } ``` NOTE: This doesn't quite work as expected, but this is not a python-libjuju bug, but in juju/juju here: https://github.com/juju/juju/blob/3.3/state/application.go#L1792-L1840 However, @hmlanigan is working on a fix (juju/juju#16315) that will resolve this, so proposing this change to python-libjuju as a stepping stone towards to complete solution ``` tox -e integration -- tests/integration/test_application.py::test_local_refresh ``` All CI tests need to pass.
2 parents 078f060 + d78c5e0 commit 9e0e39d

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

examples/local_refresh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def main():
2020

2121
try:
2222
print('Get deployed application')
23-
app = model.appplications["ubuntu"]
23+
app = model.applications["ubuntu"]
2424

2525
print('Refresh/Upgrade Ubuntu charm with local charm')
2626
await app.refresh(path="path/to/local/ubuntu.charm")

juju/application.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,17 @@ async def local_refresh(
848848
metadata,
849849
resources=resources)
850850

851+
# We know this charm is a local charm, but this charm origin could be
852+
# the charm origin of a charmhub charm. Ensure that we update/remove
853+
# the appropriate fields.
854+
charm_origin.source = "local"
855+
charm_origin.track = None
856+
charm_origin.risk = None
857+
charm_origin.branch = None
858+
charm_origin.hash_ = None
859+
charm_origin.id_ = None
860+
charm_origin.revision = URL.parse(charm_url).revision
861+
851862
set_charm_args = {
852863
'application': self.entity_id,
853864
'charm_origin': charm_origin,

tests/integration/test_application.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .. import base
1010
from juju import jasyncio, errors
1111
from juju.url import URL, Schema
12+
from juju.client import client
1213

1314
MB = 1
1415

@@ -232,6 +233,22 @@ async def test_refresh_charmhub_to_local(event_loop):
232233
assert app.data['charm-url'].startswith('local:')
233234

234235

236+
@base.bootstrapped
237+
@pytest.mark.asyncio
238+
async def test_local_refresh(event_loop):
239+
charm_path = INTEGRATION_TEST_DIR / 'charm'
240+
async with base.CleanModel() as model:
241+
app = await model.deploy('ubuntu')
242+
origin = client.CharmOrigin(source="charm-hub", track="20.04", risk="stable",
243+
branch="deadbeef", hash_="hash", id_="id", revision=12,
244+
base=client.Base("20.04", "ubuntu"))
245+
246+
await app.local_refresh(charm_origin=origin, path=charm_path)
247+
248+
assert origin == client.CharmOrigin(source="local", revision=0,
249+
base=client.Base("20.04", "ubuntu"))
250+
251+
235252
@base.bootstrapped
236253
@pytest.mark.asyncio
237254
async def test_trusted(event_loop):

0 commit comments

Comments
 (0)