Skip to content

Commit

Permalink
Don't test if there's no k8s around, only wait for model to disappear…
Browse files Browse the repository at this point in the history
… when we're not keeping it
  • Loading branch information
addyess committed Feb 29, 2024
1 parent fb8b155 commit c4c2d17
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
16 changes: 8 additions & 8 deletions pytest_operator/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,9 @@ async def forget_model(
if timeout is None and model_state.timeout:
timeout = model_state.timeout

async def is_model_alive():
return model_name in await self._controller.list_models()

with self.model_context(alias) as model:
await self.log_model()
model_name = model.info.name
Expand All @@ -931,15 +934,12 @@ async def forget_model(
destroy_storage=destroy_storage,
max_wait=timeout,
)
await model.disconnect()
if timeout and await is_model_alive():
log.warning("Waiting for model %s to die...", model_name)
while await is_model_alive():
asyncio.sleep(5)

async def model_alive():
return model_name in await self._controller.list_models()

if timeout and await model_alive():
log.warning("Waiting for model %s to leave...", model_name)
while await model_alive():
asyncio.sleep(5)
await model.disconnect()

# stop managing this model now
log.info(f"Forgetting model {alias}...")
Expand Down
12 changes: 9 additions & 3 deletions tests/integration/test_opstest_add_k8s.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# test that pytest operator supports adding a k8s to an existing controller
# This is a new k8s cloud created/managed by pytest-operator

from pytest_operator.plugin import OpsTest
from kubernetes import config as k8s_config
from kubernetes.client import Configuration
from kubernetes.config.config_exception import ConfigException
import pytest

from pytest_operator.plugin import OpsTest


async def test_add_k8s_cloud(ops_test: OpsTest):
async def test_add_k8s(ops_test: OpsTest):
config = type.__call__(Configuration)
k8s_config.load_config(client_configuration=config)
try:
k8s_config.load_config(client_configuration=config)
except ConfigException:
pytest.skip("No Kubernetes config found to add-k8s")
k8s_cloud = await ops_test.add_k8s(config, skip_storage=True, storage_class=None)
k8s_model = await ops_test.track_model(
"secondary", cloud_name=k8s_cloud, keep=ops_test.ModelKeep.NEVER
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/test_pytest_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ async def test_2_create_delete_new_model(self, ops_test):
}

# track the main model with a second alias, don't do this other than testing
# this will forget the duplicate without resetting/deleting the main model
# because "duplicate" will be in "keep_model" mode since ops_tests believes
# it's an existing model.
model_name = prior_model.info.name
duplicate = await ops_test.track_model("duplicate", model_name=model_name)
assert duplicate.info.uuid == prior_model.info.uuid
Expand Down

0 comments on commit c4c2d17

Please sign in to comment.