Skip to content

Commit 7d2a423

Browse files
authored
Merge pull request #665 from cderici/wait-for-app-remove
#665 #### Description This is to make life easier by allowing the following pattern: ```python await ops_test.model.applications[related_app.name].remove() # Block until it is really gone. Added after an itest failed when tried to redeploy: # juju.errors.JujuError: ['cannot add application "related-app": application already exists'] await ops_test.model.block_until(lambda: related_app.name not in ops_test.model.applications) ``` to be written as: ```python await ops_test.model.remove_application(related_app.name, block_until_done=True) ``` Fixes #656 #### QA Steps ```sh tox -e integration -- tests/integration/test_application.py::test_app_remove_wait_flag ``` #### Notes & Discussion Another option would be to do `await ops_test.model.applications[related_app.name].remove(block_until_done=True)`, however, then it gets weird to ask the `app.model` to `block_until` the `app.name` is no longer in `model.applications` while the `app` object is being torn down.
2 parents b5c3185 + 6ebc6a6 commit 7d2a423

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

juju/model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,21 @@ async def reset(self, force=False):
875875
lambda: len(self.machines) == 0
876876
)
877877

878+
async def remove_application(self, app_name, block_until_done=False):
879+
"""Removes the given application from the model.
880+
881+
:param str app_name: Name of the application
882+
:param bool block_until_done: Ensure the app is removed from the
883+
model when returned
884+
"""
885+
if app_name not in self.applications:
886+
raise JujuError("Given application doesn't seem to appear in the\
887+
model: %s\nCurrent applications are: %s" %
888+
(app_name, self.applications))
889+
await self.applications[app_name].remove()
890+
if block_until_done:
891+
await self.block_until(lambda: app_name not in self.applications)
892+
878893
async def block_until(self, *conditions, timeout=None, wait_period=0.5):
879894
"""Return only after all conditions are true.
880895

tests/integration/test_application.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,15 @@ async def test_trusted(event_loop):
235235
await ubuntu_app.set_trusted(False)
236236
trusted = await ubuntu_app.get_trusted()
237237
assert trusted is False
238+
239+
240+
@base.bootstrapped
241+
@pytest.mark.asyncio
242+
async def test_app_remove_wait_flag(event_loop):
243+
async with base.CleanModel() as model:
244+
app = await model.deploy('cs:ubuntu')
245+
a_name = app.name
246+
await model.wait_for_idle(status="active")
247+
248+
await model.remove_application(app.name, block_until_done=True)
249+
assert a_name not in model.applications

0 commit comments

Comments
 (0)