Skip to content

Commit

Permalink
Add (async_)block_until_charm_channel support
Browse files Browse the repository at this point in the history
This adds async_block_until_charm_channel and
block_until_charm_channel which will initially be
used for charm upgrade testing.
  • Loading branch information
Corey Bryant committed Oct 18, 2023
1 parent 44333de commit f585eaf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions unit_tests/test_zaza_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
AsyncTimeoutError = asyncio.futures.TimeoutError

import copy
import collections
import concurrent
import datetime
import mock
Expand All @@ -38,6 +39,7 @@
import unit_tests.utils as ut_utils

import zaza.model as model
import zaza.utilities.ro_types as ro_types
import zaza


Expand Down Expand Up @@ -1771,6 +1773,28 @@ async def _get_status(*args):
with self.assertRaises(AsyncTimeoutError):
model.block_until_charm_url('app', 'something wrong', timeout=0.1)

def test_block_until_charm_channel(self):

async def _block_until(f, timeout=None):
rc = await f()
if not rc:
raise AsyncTimeoutError

async def _get_status(*args):
return self.juju_status
self.patch_object(model, 'Model')
self.Model.return_value = self.Model_mock
self.patch_object(model, 'async_block_until')
self.async_block_until.side_effect = _block_until
self.patch_object(model, 'async_get_status')
self.async_get_status.side_effect = _get_status
target_channel = '2023.2/stable'
charm_channel = collections.OrderedDict({'charm_channel': target_channel})
self.juju_status.applications[self.application] = ro_types.resolve_immutable(charm_channel)
model.block_until_charm_channel('app', target_channel)
with self.assertRaises(AsyncTimeoutError):
model.block_until_charm_channel('app', 'something wrong', timeout=0.1)

def block_until_service_status_base(self, rou_return):

async def _block_until(f, timeout=None):
Expand Down
28 changes: 28 additions & 0 deletions zaza/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,34 @@ async def _check_charm_url():
block_until_charm_url = sync_wrapper(async_block_until_charm_url)


async def async_block_until_charm_channel(application, target_channel,
model_name=None, timeout=2700):
"""Block until the charm channel matches target_channel.
An example accessing this function via its sync wrapper::
block_until_charm_channel('cinder', '2023.2/stable')
:param application_name: Name of application
:type application_name: str
:param target_channel: Target charm channel
:type target_channel: str
:param model_name: Name of model to interact with.
:type model_name: str
:param timeout: Time to wait for status to be achieved
:type timeout: float
"""
async def _check_charm_channel():
model_status = await async_get_status(model_name)
charm_channel = model_status.applications[application].charm_channel
return charm_channel == target_channel

await async_block_until(_check_charm_channel, timeout=timeout)


block_until_charm_channel = sync_wrapper(async_block_until_charm_channel)


async def async_block_until_service_status(unit_name, services, target_status,
model_name=None, timeout=2700,
pgrep_full=False):
Expand Down

0 comments on commit f585eaf

Please sign in to comment.