Add functional tests for Model.add_machine()
authorTim Van Steenburgh <tvansteenburgh@gmail.com>
Thu, 23 Feb 2017 22:28:36 +0000 (17:28 -0500)
committerTim Van Steenburgh <tvansteenburgh@gmail.com>
Thu, 23 Feb 2017 22:28:36 +0000 (17:28 -0500)
Clean up other tests, ensuring that connections are closed
and a new event loop is used for each test.

Update py.test args in tox.ini to properly on nocapture so
logging output can be seen when needed.

juju/client/connection.py
tests/client/test_client.py
tests/client/test_connection.py
tests/functional/__init__.py [new file with mode: 0644]
tests/functional/test_model.py [new file with mode: 0644]
tests/test_loop.py [deleted file]
tests/unit/test_loop.py [new file with mode: 0644]
tox.ini

index 9e8cb8f..b17ac45 100644 (file)
@@ -1,3 +1,4 @@
+import asyncio
 import base64
 import io
 import json
index 8598878..653da5b 100644 (file)
@@ -1,5 +1,4 @@
-import asyncio
-import unittest
+import pytest
 
 from juju.client.connection import Connection
 from juju.client import client
@@ -8,19 +7,18 @@ from ..base import bootstrapped
 
 
 @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)
index 18b5863..0a7402b 100644 (file)
@@ -1,15 +1,13 @@
-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()
diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/functional/test_model.py b/tests/functional/test_model.py
new file mode 100644 (file)
index 0000000..52d8a03
--- /dev/null
@@ -0,0 +1,62 @@
+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
diff --git a/tests/test_loop.py b/tests/test_loop.py
deleted file mode 100644 (file)
index ad043fc..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-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())
diff --git a/tests/unit/test_loop.py b/tests/unit/test_loop.py
new file mode 100644 (file)
index 0000000..f12368e
--- /dev/null
@@ -0,0 +1,30 @@
+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())
diff --git a/tox.ini b/tox.ini
index 5babec0..235813e 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -11,7 +11,8 @@ skipsdist=True
 usedevelop=True
 passenv =
     HOME
-commands = py.test -rsx
+commands = py.test -ra -s -x
 deps =
     pytest
+    pytest-asyncio
     mock