X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=tests%2Fbase.py;h=8ea51092d5129c85bb2375f87a56c8f92148cd31;hb=7c2a530853c95b8a3518f6db0870f94858f87c27;hp=35003cbafb7074181b74c5e46ee8b56240c5c49e;hpb=f3cc1159b22f570b9b82c62cf7f4cfef9278bac4;p=osm%2FN2VC.git diff --git a/tests/base.py b/tests/base.py index 35003cb..8ea5109 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,7 +1,12 @@ +import mock import subprocess +import uuid import pytest +from juju.controller import Controller +from juju.client.connection import JujuData + def is_bootstrapped(): result = subprocess.run(['juju', 'switch'], stdout=subprocess.PIPE) @@ -12,3 +17,48 @@ def is_bootstrapped(): bootstrapped = pytest.mark.skipif( not is_bootstrapped(), reason='bootstrapped Juju environment required') + + +class CleanController(): + def __init__(self): + self.controller = None + + async def __aenter__(self): + self.controller = Controller() + await self.controller.connect_current() + return self.controller + + async def __aexit__(self, exc_type, exc, tb): + await self.controller.disconnect() + + +class CleanModel(): + def __init__(self): + self.controller = None + self.model = None + + async def __aenter__(self): + self.controller = Controller() + await self.controller.connect_current() + + model_name = 'model-{}'.format(uuid.uuid4()) + self.model = await self.controller.add_model(model_name) + + # Ensure that we connect to the new model by default. This also + # prevents failures if test was started with no current model. + self._patch_cm = mock.patch.object(JujuData, 'current_model', + return_value=model_name) + self._patch_cm.start() + + return self.model + + async def __aexit__(self, exc_type, exc, tb): + self._patch_cm.stop() + await self.model.disconnect() + await self.controller.destroy_model(self.model.info.uuid) + await self.controller.disconnect() + + +class AsyncMock(mock.MagicMock): + async def __call__(self, *args, **kwargs): + return super().__call__(*args, **kwargs)