Fix single app deploy not using config_yaml
[osm/N2VC.git] / tests / base.py
1 import mock
2 import subprocess
3 import uuid
4
5 import pytest
6
7 from juju.controller import Controller
8 from juju.client.connection import JujuData
9
10
11 def is_bootstrapped():
12 result = subprocess.run(['juju', 'switch'], stdout=subprocess.PIPE)
13 return (
14 result.returncode == 0 and
15 len(result.stdout.decode().strip()) > 0)
16
17 bootstrapped = pytest.mark.skipif(
18 not is_bootstrapped(),
19 reason='bootstrapped Juju environment required')
20
21
22 class CleanModel():
23 def __init__(self):
24 self.controller = None
25 self.model = None
26
27 async def __aenter__(self):
28 self.controller = Controller()
29 await self.controller.connect_current()
30
31 model_name = 'model-{}'.format(uuid.uuid4())
32 self.model = await self.controller.add_model(model_name)
33
34 # Ensure that we connect to the new model by default. This also
35 # prevents failures if test was started with no current model.
36 self._patch_cm = mock.patch.object(JujuData, 'current_model',
37 return_value=model_name)
38 self._patch_cm.start()
39
40 return self.model
41
42 async def __aexit__(self, exc_type, exc, tb):
43 self._patch_cm.stop()
44 await self.model.disconnect()
45 await self.controller.destroy_model(self.model.info.uuid)
46 await self.controller.disconnect()