From: Tim Van Steenburgh Date: Thu, 23 Feb 2017 22:28:36 +0000 (-0500) Subject: Add functional tests for Model.add_machine() X-Git-Tag: 0.3.0~2^2~2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=21ba8065355c663597df7bd2b481ade519f89a79;p=osm%2FN2VC.git Add functional tests for Model.add_machine() 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. --- diff --git a/juju/client/connection.py b/juju/client/connection.py index 9e8cb8f..b17ac45 100644 --- a/juju/client/connection.py +++ b/juju/client/connection.py @@ -1,3 +1,4 @@ +import asyncio import base64 import io import json diff --git a/tests/client/test_client.py b/tests/client/test_client.py index 8598878..653da5b 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -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) diff --git a/tests/client/test_connection.py b/tests/client/test_connection.py index 18b5863..0a7402b 100644 --- a/tests/client/test_connection.py +++ b/tests/client/test_connection.py @@ -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 index 0000000..e69de29 diff --git a/tests/functional/test_model.py b/tests/functional/test_model.py new file mode 100644 index 0000000..52d8a03 --- /dev/null +++ b/tests/functional/test_model.py @@ -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 index ad043fc..0000000 --- a/tests/test_loop.py +++ /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 index 0000000..f12368e --- /dev/null +++ b/tests/unit/test_loop.py @@ -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 --- 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