Skip to content

Commit dbe0f66

Browse files
committed
Add unit tests for different scenarios
1 parent 4c7c60f commit dbe0f66

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
@@ -296,25 +296,23 @@ async def test_timeout(self):
296296
self.assertEqual(str(cm.exception), "Timed out waiting for model:\nnonexisting_app (missing)")
297297

298298
@pytest.mark.asyncio
299+
@pytest.mark.wait_for_idle
299300
async def test_wait_for_active_status(self):
301+
app_status = 'active'
300302
# create a custom apps mock
301303
from types import SimpleNamespace
302304
app = SimpleNamespace(
303-
status="active",
305+
status=app_status,
304306
units=[SimpleNamespace(
305307
name="mockunit/0",
306-
workload_status="active",
308+
workload_status='active',
307309
workload_status_message="workload_status_message",
308310
machine=None,
309311
agent_status="idle",
310312
)],
311313
)
312-
# This is a small hack to act like we're getting 'unknown'
313-
# from the api (the get_status() call), which shouldn't
314-
# change the semantics of this test, as the 'unknown'
315-
# has the lowest severity (so the app's 'active' status
316-
# will overrule it)
317-
app.get_status = base.AsyncMock(return_value='unknown')
314+
315+
app.get_status = base.AsyncMock(return_value=app_status)
318316
apps = {"dummy_app": app}
319317

320318
with patch.object(Model, 'applications', new_callable=PropertyMock) as mock_apps:
@@ -331,3 +329,78 @@ async def test_wait_for_active_status(self):
331329
await m.wait_for_idle(apps=["dummy_app"], wait_for_active=True, status="doesn't matter")
332330

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

0 commit comments

Comments
 (0)