+import asyncio
import base64
import io
import json
-import asyncio
-import unittest
+import pytest
from juju.client.connection import Connection
from juju.client import client
@bootstrapped
-class UserManagerTest(unittest.TestCase):
- def test_user_info(self):
- loop = asyncio.get_event_loop()
- conn = loop.run_until_complete(
- Connection.connect_current())
- conn = loop.run_until_complete(
- conn.controller())
+@pytest.mark.asyncio
+async def test_user_info(event_loop):
+ conn = await Connection.connect_current()
+ controller_conn = await conn.controller()
- um = client.UserManagerFacade()
- um.connect(conn)
- result = loop.run_until_complete(
- um.UserInfo([client.Entity('user-admin')], True))
+ um = client.UserManagerFacade()
+ um.connect(controller_conn)
+ result = await um.UserInfo(
+ [client.Entity('user-admin')], True)
+ await conn.close()
+ await controller_conn.close()
- self.assertIsInstance(result, client.UserInfoResults)
- for r in result.results:
- self.assertIsInstance(r, client.UserInfoResult)
+ assert isinstance(result, client.UserInfoResults)
+ for r in result.results:
+ assert isinstance(r, client.UserInfoResult)
-import asyncio
-import unittest
+import pytest
from juju.client.connection import Connection
from ..base import bootstrapped
@bootstrapped
-class FunctionalConnectionTest(unittest.TestCase):
- def test_connect_current(self):
- loop = asyncio.get_event_loop()
- conn = loop.run_until_complete(
- Connection.connect_current())
+@pytest.mark.asyncio
+async def test_connect_current(event_loop):
+ conn = await Connection.connect_current()
- self.assertIsInstance(conn, Connection)
+ assert isinstance(conn, Connection)
+ await conn.close()
--- /dev/null
+import uuid
+
+import pytest
+
+from juju.controller import Controller
+
+from ..base import bootstrapped
+
+MB = 1
+GB = 1024
+
+
+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)
+
+ return self.model
+
+ async def __aexit__(self, exc_type, exc, tb):
+ await self.model.disconnect()
+ await self.controller.destroy_model(self.model.info.uuid)
+ await self.controller.disconnect()
+
+
+@bootstrapped
+@pytest.mark.asyncio
+async def test_add_machine(event_loop):
+ from juju.machine import Machine
+
+ async with CleanModel() as model:
+ # add a new default machine
+ machine1 = await model.add_machine()
+
+ # add a machine with constraints, disks, and series
+ machine2 = await model.add_machine(
+ constraints={
+ 'mem': 256 * MB,
+ },
+ disks=[{
+ 'pool': 'rootfs',
+ 'size': 10 * GB,
+ 'count': 1,
+ }],
+ series='xenial',
+ )
+
+ # add a lxd container to machine2
+ machine3 = await model.add_machine(
+ 'lxd:{}'.format(machine2.id))
+
+ for m in (machine1, machine2, machine3):
+ assert isinstance(m, Machine)
+
+ assert len(model.machines) == 3
+++ /dev/null
-import unittest
-import juju.loop
-
-
-class TestLoop(unittest.TestCase):
- def test_run(self):
- async def _test():
- return 'success'
- self.assertEqual(juju.loop.run(_test()), 'success')
-
- def test_run_interrupt(self):
- async def _test():
- juju.loop.run._sigint = True
- self.assertRaises(KeyboardInterrupt, juju.loop.run, _test())
-
- def test_run_exception(self):
- async def _test():
- raise ValueError()
- self.assertRaises(ValueError, juju.loop.run, _test())
--- /dev/null
+import asyncio
+import unittest
+import juju.loop
+
+
+class TestLoop(unittest.TestCase):
+ def setUp(self):
+ # new event loop for each test
+ policy = asyncio.get_event_loop_policy()
+ self.loop = policy.new_event_loop()
+ policy.set_event_loop(self.loop)
+
+ def tearDown(self):
+ self.loop.close()
+
+ def test_run(self):
+ assert asyncio.get_event_loop() == self.loop
+ async def _test():
+ return 'success'
+ self.assertEqual(juju.loop.run(_test()), 'success')
+
+ def test_run_interrupt(self):
+ async def _test():
+ juju.loop.run._sigint = True
+ self.assertRaises(KeyboardInterrupt, juju.loop.run, _test())
+
+ def test_run_exception(self):
+ async def _test():
+ raise ValueError()
+ self.assertRaises(ValueError, juju.loop.run, _test())
usedevelop=True
passenv =
HOME
-commands = py.test -rsx
+commands = py.test -ra -s -x
deps =
pytest
+ pytest-asyncio
mock