Skip to content

Commit 3422c09

Browse files
committed
Add unit tests for different scenarios
1 parent f2ad443 commit 3422c09

1 file changed

Lines changed: 81 additions & 8 deletions

File tree

tests/unit/test_model.py

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,25 +293,23 @@ async def test_timeout(self):
293293
self.assertEqual(str(cm.exception), "Timed out waiting for model:\nnonexisting_app (missing)")
294294

295295
@pytest.mark.asyncio
296+
@pytest.mark.wait_for_idle
296297
async def test_wait_for_active_status(self):
298+
app_status = 'active'
297299
# create a custom apps mock
298300
from types import SimpleNamespace
299301
app = SimpleNamespace(
300-
status="active",
302+
status=app_status,
301303
units=[SimpleNamespace(
302304
name="mockunit/0",
303-
workload_status="active",
305+
workload_status='active',
304306
workload_status_message="workload_status_message",
305307
machine=None,
306308
agent_status="idle",
307309
)],
308310
)
309-
# This is a small hack to act like we're getting 'unknown'
310-
# from the api (the get_status() call), which shouldn't
311-
# change the semantics of this test, as the 'unknown'
312-
# has the lowest severity (so the app's 'active' status
313-
# will overrule it)
314-
app.get_status = base.AsyncMock(return_value='unknown')
311+
312+
app.get_status = base.AsyncMock(return_value=app_status)
315313
apps = {"dummy_app": app}
316314

317315
with patch.object(Model, 'applications', new_callable=PropertyMock) as mock_apps:
@@ -328,3 +326,78 @@ async def test_wait_for_active_status(self):
328326
await m.wait_for_idle(apps=["dummy_app"], wait_for_active=True, status="doesn't matter")
329327

330328
mock_apps.assert_called_with()
329+
330+
@pytest.mark.asyncio
331+
@pytest.mark.wait_for_idle
332+
async def test_wait_for_active_units_waiting_application(self):
333+
# If the app is in waiting state, then wait more even if the units are ready
334+
app_status = 'waiting'
335+
# create a custom apps mock
336+
from types import SimpleNamespace
337+
app = SimpleNamespace(
338+
status=app_status,
339+
units=[SimpleNamespace(
340+
name="mockunit/0",
341+
workload_status='active',
342+
workload_status_message="workload_status_message",
343+
machine=None,
344+
agent_status="idle",
345+
),
346+
SimpleNamespace(
347+
name="mockunit/1",
348+
workload_status='active',
349+
workload_status_message="workload_status_message",
350+
machine=None,
351+
agent_status="idle",
352+
)],
353+
)
354+
355+
app.get_status = base.AsyncMock(return_value=app_status)
356+
apps = {"dummy_app": app}
357+
358+
with patch.object(Model, 'applications', new_callable=PropertyMock) as mock_apps:
359+
mock_apps.return_value = apps
360+
m = Model()
361+
362+
with self.assertRaises(jasyncio.TimeoutError):
363+
await m.wait_for_idle(apps=["dummy_app"], status="active")
364+
365+
mock_apps.assert_called_with()
366+
367+
@pytest.mark.asyncio
368+
@pytest.mark.wait_for_idle
369+
async def test_wait_for_active_units_waiting_for_units(self):
370+
# If user wants to see a particular number of units, then application may be in a waiting
371+
# state, return when there's at least that number of units in the desired state
372+
app_status = 'waiting'
373+
# create a custom apps mock
374+
from types import SimpleNamespace
375+
app = SimpleNamespace(
376+
status=app_status,
377+
units=[SimpleNamespace(
378+
name="mockunit/0",
379+
workload_status='active',
380+
workload_status_message="workload_status_message",
381+
machine=None,
382+
agent_status="idle",
383+
),
384+
SimpleNamespace(
385+
name="mockunit/1",
386+
workload_status='waiting',
387+
workload_status_message="workload_status_message",
388+
machine=None,
389+
agent_status="idle",
390+
)],
391+
)
392+
393+
app.get_status = base.AsyncMock(return_value=app_status)
394+
apps = {"dummy_app": app}
395+
396+
with patch.object(Model, 'applications', new_callable=PropertyMock) as mock_apps:
397+
mock_apps.return_value = apps
398+
m = Model()
399+
400+
await m.wait_for_idle(apps=["dummy_app"], status="active", wait_for_units=1,
401+
timeout=None)
402+
403+
mock_apps.assert_called_with()

0 commit comments

Comments
 (0)