Skip to content
Snippets Groups Projects
Commit 1bb5d61f authored by Hannes Feldt's avatar Hannes Feldt
Browse files

Resolve L2SS-1408: "Fix async tests"

parent b9a56a61
Branches
Tags
1 merge request!780Resolve L2SS-1408: "Fix async tests"
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# #
# Url to the jumppad download location # Url to the jumppad download location
jumppad_download="https://git.astron.nl/lofar2.0/tango/-/package_files/37347/download" jumppad_download="https://git.astron.nl/lofar2.0/tango/-/package_files/40604/download"
if [ -z "$LOFAR20_DIR" ]; then if [ -z "$LOFAR20_DIR" ]; then
# We assume we aren't in the PATH, so we can derive our path. # We assume we aren't in the PATH, so we can derive our path.
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
import unittest import unittest
import asynctest
import testscenarios import testscenarios
from tangostationcontrol.common.lofar_logging import configure_logger from tangostationcontrol.common.lofar_logging import configure_logger
...@@ -25,7 +24,9 @@ class IntegrationTestCase(BaseIntegrationTestCase): ...@@ -25,7 +24,9 @@ class IntegrationTestCase(BaseIntegrationTestCase):
super().setUp() super().setUp()
class IntegrationAsyncTestCase(testscenarios.WithScenarios, asynctest.TestCase): class IntegrationAsyncTestCase(
testscenarios.WithScenarios, unittest.IsolatedAsyncioTestCase
):
"""Integration test case base class for all asyncio unit tests.""" """Integration test case base class for all asyncio unit tests."""
def setUp(self): def setUp(self):
......
...@@ -90,7 +90,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -90,7 +90,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace, self.namespace,
5, 5,
self.fault_func, self.fault_func,
self.loop,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
) )
try: try:
...@@ -106,7 +105,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -106,7 +105,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace, self.namespace,
5, 5,
self.fault_func, self.fault_func,
self.loop,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
) )
try: try:
...@@ -135,7 +133,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -135,7 +133,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace, self.namespace,
5, 5,
self.fault_func, self.fault_func,
self.loop,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
) )
try: try:
...@@ -166,7 +163,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -166,7 +163,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace, self.namespace,
5, 5,
self.fault_func, self.fault_func,
self.loop,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
) )
...@@ -185,7 +181,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -185,7 +181,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace, self.namespace,
5, 5,
self.fault_func, self.fault_func,
self.loop,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
) )
try: try:
...@@ -208,7 +203,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -208,7 +203,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace, self.namespace,
5, 5,
self.fault_func, self.fault_func,
self.loop,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
) )
try: try:
...@@ -230,7 +224,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -230,7 +224,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace, self.namespace,
5, 5,
self.fault_func, self.fault_func,
self.loop,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
) )
...@@ -251,7 +244,6 @@ class TestClientServer(base.IntegrationAsyncTestCase): ...@@ -251,7 +244,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
5, 5,
self.fault_func, self.fault_func,
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
self.loop,
) )
try: try:
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
# integration process, which may cause wedges in the gate later. # integration process, which may cause wedges in the gate later.
importlib-metadata<2.0.0,>=0.12;python_version<"3.8" importlib-metadata<2.0.0,>=0.12;python_version<"3.8"
asynctest>=0.13.0 # Apache-2.0
bandit>=1.6.0 # Apache-2.0 bandit>=1.6.0 # Apache-2.0
virtualenv>=20.16.0 # MIT virtualenv>=20.16.0 # MIT
build>=0.8.0 # MIT build>=0.8.0 # MIT
......
...@@ -7,7 +7,6 @@ from unittest import mock ...@@ -7,7 +7,6 @@ from unittest import mock
from typing import Dict from typing import Dict
from typing import Union from typing import Union
import asynctest
import testscenarios import testscenarios
from tangostationcontrol.common.lofar_logging import configure_logger from tangostationcontrol.common.lofar_logging import configure_logger
...@@ -41,7 +40,7 @@ class TestCase(BaseTestCase): ...@@ -41,7 +40,7 @@ class TestCase(BaseTestCase):
super().setUp() super().setUp()
class AsyncTestCase(testscenarios.WithScenarios, asynctest.TestCase): class AsyncTestCase(testscenarios.WithScenarios, unittest.IsolatedAsyncioTestCase):
"""Test case base class for all asyncio unit tests.""" """Test case base class for all asyncio unit tests."""
def setUp(self): def setUp(self):
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
import asyncio import asyncio
import io import io
import unittest
from unittest import mock from unittest import mock
from unittest.mock import patch
import asynctest
import asyncua import asyncua
import numpy import numpy
...@@ -15,8 +16,6 @@ from tangostationcontrol.clients.opcua_client import ( ...@@ -15,8 +16,6 @@ from tangostationcontrol.clients.opcua_client import (
OPCUAConnectionStatus, OPCUAConnectionStatus,
) )
from test import base
class AttrProps: class AttrProps:
def __init__(self, numpy_type): def __init__(self, numpy_type):
...@@ -44,27 +43,39 @@ IMAGE_SHAPE = (2, 3) ...@@ -44,27 +43,39 @@ IMAGE_SHAPE = (2, 3)
DIMENSION_TESTS = [SCALAR_SHAPE, SPECTRUM_SHAPE, IMAGE_SHAPE] DIMENSION_TESTS = [SCALAR_SHAPE, SPECTRUM_SHAPE, IMAGE_SHAPE]
class TestOPCua(base.AsyncTestCase): def create_mocked_coroutine(return_value=None):
@asynctest.patch.object(OPCUAConnection, "ping") if return_value is None:
@asynctest.patch.object(OPCUAConnection, "_servername") return_value = mock.Mock()
@asynctest.patch.object(opcua_client, "Client")
async def mock_coro(*args, **kwargs):
return return_value
return mock.Mock(wraps=mock_coro)
class TestOPCua(unittest.IsolatedAsyncioTestCase):
@patch.object(OPCUAConnection, "ping")
@patch.object(OPCUAConnection, "_servername")
@patch.object(opcua_client, "Client")
async def test_opcua_connection(self, m_opc_client, m_servername, m_ping): async def test_opcua_connection(self, m_opc_client, m_servername, m_ping):
""" """
This tests verifies whether the correct connection steps happen. It checks whether we can init an OPCUAConnection object This tests verifies whether the correct connection steps happen. It checks whether we can init an OPCUAConnection object
Whether we can set the namespace, and the OPCua client. Whether we can set the namespace, and the OPCua client.
""" """
m_opc_client_members = asynctest.asynctest.CoroutineMock() m_opc_client_members = create_mocked_coroutine()
m_opc_client_members.get_namespace_index = asynctest.asynctest.CoroutineMock( m_opc_client_members.get_namespace_index = create_mocked_coroutine(
return_value=42 return_value=42
) )
m_opc_client_members.connect = asynctest.asynctest.CoroutineMock() m_opc_client_members.connect = create_mocked_coroutine()
m_opc_client_members.disconnect = asynctest.asynctest.CoroutineMock() m_opc_client_members.disconnect = create_mocked_coroutine()
m_opc_client_members.send_hello = asynctest.asynctest.CoroutineMock() m_opc_client_members.send_hello = create_mocked_coroutine()
m_opc_client_members.update_protocol_attributes = ( m_objects_node = mock.Mock()
asynctest.asynctest.CoroutineMock() m_objects_node.get_child = create_mocked_coroutine()
) m_objects_node.get_children_descriptions = create_mocked_coroutine([])
m_opc_client_members.get_objects_node = mock.Mock(return_value=m_objects_node)
m_opc_client_members.update_protocol_attributes = create_mocked_coroutine()
m_opc_client.return_value = m_opc_client_members m_opc_client.return_value = m_opc_client_members
test_client = OPCUAConnection( test_client = OPCUAConnection(
...@@ -73,7 +84,6 @@ class TestOPCua(base.AsyncTestCase): ...@@ -73,7 +84,6 @@ class TestOPCua(base.AsyncTestCase):
5, 5,
mock.Mock(), mock.Mock(),
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
self.loop,
) )
try: try:
...@@ -89,10 +99,10 @@ class TestOPCua(base.AsyncTestCase): ...@@ -89,10 +99,10 @@ class TestOPCua(base.AsyncTestCase):
finally: finally:
await test_client.stop() await test_client.stop()
@asynctest.patch.object(OPCUAConnection, "ping") @patch.object(OPCUAConnection, "ping")
@asynctest.patch.object(OPCUAConnection, "_servername") @patch.object(OPCUAConnection, "_servername")
@asynctest.patch.object(opcua_client, "Client") @patch.object(opcua_client, "Client")
@asynctest.patch.object(opcua_client, "ProtocolAttribute") @patch.object(opcua_client, "ProtocolAttribute")
async def test_opcua_attr_setup( async def test_opcua_attr_setup(
self, m_protocol_attr, m_opc_client, m_servername, m_ping self, m_protocol_attr, m_opc_client, m_servername, m_ping
): ):
...@@ -104,22 +114,18 @@ class TestOPCua(base.AsyncTestCase): ...@@ -104,22 +114,18 @@ class TestOPCua(base.AsyncTestCase):
Test succeeds if there are no errors. Test succeeds if there are no errors.
""" """
m_opc_client_members = asynctest.asynctest.CoroutineMock() m_opc_client_members = create_mocked_coroutine()
m_opc_client_members.get_namespace_index = asynctest.asynctest.CoroutineMock( m_opc_client_members.get_namespace_index = create_mocked_coroutine(
return_value=2 return_value=2
) )
m_opc_client_members.connect = asynctest.asynctest.CoroutineMock() m_opc_client_members.connect = create_mocked_coroutine()
m_opc_client_members.disconnect = asynctest.asynctest.CoroutineMock() m_opc_client_members.disconnect = create_mocked_coroutine()
m_opc_client_members.send_hello = asynctest.asynctest.CoroutineMock() m_opc_client_members.send_hello = create_mocked_coroutine()
m_objects_node = asynctest.Mock() m_objects_node = mock.Mock()
m_objects_node.get_child = asynctest.asynctest.CoroutineMock() m_objects_node.get_child = create_mocked_coroutine()
m_objects_node.get_children_descriptions = asynctest.asynctest.CoroutineMock() m_objects_node.get_children_descriptions = create_mocked_coroutine([])
m_opc_client_members.get_objects_node = asynctest.Mock( m_opc_client_members.get_objects_node = mock.Mock(return_value=m_objects_node)
return_value=m_objects_node m_opc_client_members.update_protocol_attributes = create_mocked_coroutine()
)
m_opc_client_members.update_protocol_attributes = (
asynctest.asynctest.CoroutineMock()
)
m_opc_client.return_value = m_opc_client_members m_opc_client.return_value = m_opc_client_members
...@@ -153,7 +159,6 @@ class TestOPCua(base.AsyncTestCase): ...@@ -153,7 +159,6 @@ class TestOPCua(base.AsyncTestCase):
5, 5,
mock.Mock(), mock.Mock(),
OPCUAConnectionStatus(), OPCUAConnectionStatus(),
self.loop,
) )
try: try:
...@@ -240,7 +245,7 @@ class TestOPCua(base.AsyncTestCase): ...@@ -240,7 +245,7 @@ class TestOPCua(base.AsyncTestCase):
for i in ATTR_TEST_TYPES: for i in ATTR_TEST_TYPES:
self._get_test_value(j, i.numpy_type) self._get_test_value(j, i.numpy_type)
m_node = asynctest.asynctest.CoroutineMock() m_node = mock.Mock()
if len(j) == 1: if len(j) == 1:
test = opcua_client.ProtocolAttribute( test = opcua_client.ProtocolAttribute(
...@@ -274,7 +279,7 @@ class TestOPCua(base.AsyncTestCase): ...@@ -274,7 +279,7 @@ class TestOPCua(base.AsyncTestCase):
# test 0-2 dimensions of strings # test 0-2 dimensions of strings
for dims in range(0, 2): for dims in range(0, 2):
m_node = asynctest.asynctest.CoroutineMock() m_node = mock.Mock()
m_node.get_value.return_value = get_unicode_value(dims) m_node.get_value.return_value = get_unicode_value(dims)
# create the ProtocolAttribute to test # create the ProtocolAttribute to test
...@@ -372,7 +377,7 @@ class TestOPCua(base.AsyncTestCase): ...@@ -372,7 +377,7 @@ class TestOPCua(base.AsyncTestCase):
), ),
) )
m_node = asynctest.asynctest.CoroutineMock() m_node = mock.Mock()
m_node.set_data_value.return_value = asyncio.Future() m_node.set_data_value.return_value = asyncio.Future()
m_node.set_data_value.return_value.set_result(None) m_node.set_data_value.return_value.set_result(None)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
min_version = 4.3.3 min_version = 4.3.3
requires = requires =
tox-ignore-env-name-mismatch ~= 0.2.0 tox-ignore-env-name-mismatch ~= 0.2.0
envlist = black,pep8,pylint,py310,docs envlist = black,pep8,pylint,py3{10,11},docs
[testenv] [testenv]
usedevelop = True usedevelop = True
...@@ -10,8 +10,6 @@ package = wheel ...@@ -10,8 +10,6 @@ package = wheel
wheel_build_env = .pkg wheel_build_env = .pkg
; Python and tox variables are used to access modules and binaries instead of ; Python and tox variables are used to access modules and binaries instead of
; directly. This makes the setup robust for using sitepackages=True. ; directly. This makes the setup robust for using sitepackages=True.
install_command = {envbindir}/pip3 install {opts} {packages}
passenv = HOME
setenv = setenv =
PYTHONWARNINGS=default::DeprecationWarning PYTHONWARNINGS=default::DeprecationWarning
; Share the same envdir with as many jobs as possible due to extensive time it ; Share the same envdir with as many jobs as possible due to extensive time it
...@@ -24,17 +22,12 @@ deps = ...@@ -24,17 +22,12 @@ deps =
allowlist_externals = allowlist_externals =
{work_dir}/.tox/bin/python {work_dir}/.tox/bin/python
commands_pre = commands_pre =
{work_dir}/.tox/bin/python -m tox --version
{envpython} --version {envpython} --version
{work_dir}/.tox/bin/python -m tox --version
commands = commands =
{envpython} -m pytest --version {envpython} -m pytest --version
{envpython} -m pytest -v --log-level=DEBUG --forked test/{posargs} {envpython} -m pytest -v --log-level=DEBUG --forked test/{posargs}
; We can't detect the current Python version for an environment dynamically
; so each Python version specific job needs its own envdir.
[testenv:py310]
envdir = {toxworkdir}/testenvpy310
[testenv:integration] [testenv:integration]
allowlist_externals = allowlist_externals =
{work_dir}/.tox/bin/python {work_dir}/.tox/bin/python
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment