Skip to content

Commit 25a8094

Browse files
author
Adam Dyess
authored
Reintroduce 'DeadEntityException' allowance (#74)
* Reintroduce 'DeadEntityException' allowance when tearing down machines which may already be gone from libjuju model * bump to next predictable version * fixed issue with argument name
1 parent e31d0df commit 25a8094

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

pytest_operator/plugin.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
from _pytest.config import Config
4242
from _pytest.config.argparsing import Parser
4343
from juju.client.jujudata import FileJujuData
44+
from juju.exceptions import DeadEntityException
45+
from juju.errors import JujuError
4446
from juju.model import Model, Controller, websockets
4547

4648
log = logging.getLogger(__name__)
@@ -822,14 +824,22 @@ async def forget_model(
822824
@staticmethod
823825
async def _reset(model: Model, allow_failure, timeout: Optional[int] = None):
824826
# Forcibly destroy applications/machines in case any units are in error.
825-
log.info(f"Resetting model {model.info.name}...")
826-
for app in model.applications.values():
827-
log.info(f" Destroying application {app.name}")
828-
await app.destroy()
827+
async def _destroy(entity_name: str, **kwargs):
828+
for key, entity in getattr(model, entity_name).items():
829+
try:
830+
log.info(f" Destroying {entity_name} {key}")
831+
await entity.destroy(**kwargs)
832+
except DeadEntityException as e:
833+
log.warning(e)
834+
log.warning(f"{entity_name.title()} already dead, skipping")
835+
except JujuError as e:
836+
log.exception(e)
837+
if not allow_failure:
838+
raise
829839

830-
for machine in model.machines.values():
831-
log.info(f" Destroying machine {machine.id}")
832-
await machine.destroy(force=True)
840+
log.info(f"Resetting model {model.info.name}...")
841+
await _destroy("applications")
842+
await _destroy("machines", force=True)
833843

834844
if timeout is None:
835845
log.info("Not waiting on reset to complete.")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
name="pytest-operator",
2525
packages=find_packages(include=["pytest_operator"]),
2626
url="https://github.com/charmed-kubernetes/pytest-operator",
27-
version="0.18.0",
27+
version="0.19.0",
2828
zip_safe=True,
2929
install_requires=[
3030
"ipdb",

tox.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ commands = pytest \
4040

4141
[testenv:integration]
4242
passenv = HOME
43-
commands = pytest --tb=native --show-capture=no --log-cli-level=INFO -vs --ignore=tests/data --model-config tests/data/model-config.yaml {posargs:tests/integration}
43+
commands =
44+
pytest --tb=native --show-capture=no --log-cli-level=INFO\
45+
-vs --ignore=tests/data --ignore=tests/unit \
46+
--model-config tests/data/model-config.yaml {posargs:tests/integration}
4447
deps =
4548
juju
4649
-e {toxinidir}

0 commit comments

Comments
 (0)