382da4362413291a218f83d6885724b7ff2ad9fd
[osm/N2VC.git] / tests / base.py
1 import uuid
2 import subprocess
3
4 import pytest
5
6 from juju.controller import Controller
7
8
9 def is_bootstrapped():
10 result = subprocess.run(['juju', 'switch'], stdout=subprocess.PIPE)
11 return (
12 result.returncode == 0 and
13 len(result.stdout.decode().strip()) > 0)
14
15 bootstrapped = pytest.mark.skipif(
16 not is_bootstrapped(),
17 reason='bootstrapped Juju environment required')
18
19
20 class CleanModel():
21 def __init__(self):
22 self.controller = None
23 self.model = None
24
25 async def __aenter__(self):
26 self.controller = Controller()
27 await self.controller.connect_current()
28
29 model_name = 'model-{}'.format(uuid.uuid4())
30 self.model = await self.controller.add_model(model_name)
31
32 return self.model
33
34 async def __aexit__(self, exc_type, exc, tb):
35 await self.model.disconnect()
36 await self.controller.destroy_model(self.model.info.uuid)
37 await self.controller.disconnect()