From 2290b4f2357b8a581953e5d045b899cc19ac143a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Sun, 31 Mar 2024 20:23:19 +0200 Subject: [PATCH] Some renames --- server/tests/REST/servers/test_events_log.py | 4 +- .../REST/servers/test_events_login_logout.py | 2 +- server/tests/REST/servers/test_events_ping.py | 2 +- server/tests/REST/servers/test_test.py | 2 +- .../core/managers/test_servers_managed.py | 2 +- .../core/managers/test_servers_unmanaged.py | 9 ++-- .../generics/test_dynamic_publication.py | 42 +++++++++++++++++++ server/tests/core/util/test_permissions.py | 2 +- server/tests/fixtures/authenticators.py | 1 - server/tests/fixtures/networks.py | 5 +-- server/tests/fixtures/servers.py | 9 ++-- server/tests/fixtures/services.py | 6 --- 12 files changed, 58 insertions(+), 28 deletions(-) diff --git a/server/tests/REST/servers/test_events_log.py b/server/tests/REST/servers/test_events_log.py index d386293d7..6c7fb67e9 100644 --- a/server/tests/REST/servers/test_events_log.py +++ b/server/tests/REST/servers/test_events_log.py @@ -55,7 +55,7 @@ class ServerEventsLogTest(rest.test.RESTTestCase): # 'level': 'debug|info'|'warning'|'error', # 'message': 'message', # } - server = servers_fixtures.createServer() + server = servers_fixtures.create_server() userService = self.user_service_managed with mock.patch('uds.core.managers.log.manager.LogManager.log') as the_log: @@ -91,7 +91,7 @@ class ServerEventsLogTest(rest.test.RESTTestCase): ) def test_event_log_fail(self) -> None: - server = servers_fixtures.createServer() + server = servers_fixtures.create_server() data = { 'token': server.token, 'type': 'log', diff --git a/server/tests/REST/servers/test_events_login_logout.py b/server/tests/REST/servers/test_events_login_logout.py index 74b25e525..9a7b0635e 100644 --- a/server/tests/REST/servers/test_events_login_logout.py +++ b/server/tests/REST/servers/test_events_login_logout.py @@ -55,7 +55,7 @@ class ServerEventsLoginLogoutTest(rest.test.RESTTestCase): def setUp(self) -> None: super().setUp() - self.server = servers_fixtures.createServer() + self.server = servers_fixtures.create_server() def test_login(self) -> None: # REST path: servers/notify (/uds/rest/...) diff --git a/server/tests/REST/servers/test_events_ping.py b/server/tests/REST/servers/test_events_ping.py index 1af651479..7bf2ff004 100644 --- a/server/tests/REST/servers/test_events_ping.py +++ b/server/tests/REST/servers/test_events_ping.py @@ -58,7 +58,7 @@ class ServerEventsPingTest(rest.test.RESTTestCase): def setUp(self) -> None: super().setUp() - self.server = servers_fixtures.createServer() + self.server = servers_fixtures.create_server() def test_event_ping_with_stats(self) -> None: # Ping event diff --git a/server/tests/REST/servers/test_test.py b/server/tests/REST/servers/test_test.py index 1c2d21310..efb569e68 100644 --- a/server/tests/REST/servers/test_test.py +++ b/server/tests/REST/servers/test_test.py @@ -56,7 +56,7 @@ class ServerTestTest(rest.test.RESTTestCase): """ Test server rest api registration """ - server = servers_fixtures.createServer() + server = servers_fixtures.create_server() response = self.client.rest_post( 'servers/test', data={ diff --git a/server/tests/core/managers/test_servers_managed.py b/server/tests/core/managers/test_servers_managed.py index e8a4ff647..08ba951d8 100644 --- a/server/tests/core/managers/test_servers_managed.py +++ b/server/tests/core/managers/test_servers_managed.py @@ -78,7 +78,7 @@ class ServerManagerManagedServersTest(UDSTestCase): # So we have 8 userservices, each one with a different user self.user_services.extend(services_fixtures.create_db_cache_userservices()) - self.registered_servers_group = servers_fixtures.createServerGroup( + self.registered_servers_group = servers_fixtures.create_server_group( type=types.servers.ServerType.SERVER, subtype='test', num_servers=NUM_REGISTEREDSERVERS ) # commodity call to assign diff --git a/server/tests/core/managers/test_servers_unmanaged.py b/server/tests/core/managers/test_servers_unmanaged.py index a91a7abc7..127d7b9db 100644 --- a/server/tests/core/managers/test_servers_unmanaged.py +++ b/server/tests/core/managers/test_servers_unmanaged.py @@ -40,9 +40,8 @@ import functools import logging from uds import models -from uds.core import types, exceptions +from uds.core import types from uds.core.managers import servers -from uds.core.util import storage from ...fixtures import servers as servers_fixtures from ...fixtures import services as services_fixtures @@ -68,11 +67,11 @@ class ServerManagerUnmanagedServersTest(UDSTestCase): # Manager is a singleton, clear counters # self.manager.clearCounters() - for i in range(NUM_USERSERVICES): + for _ in range(NUM_USERSERVICES): # So we have 8 userservices, each one with a different user self.user_services.extend(services_fixtures.create_db_cache_userservices()) - self.registered_servers_group = servers_fixtures.createServerGroup( + self.registered_servers_group = servers_fixtures.create_server_group( type=types.servers.ServerType.UNMANAGED, subtype='test', num_servers=NUM_REGISTEREDSERVERS ) # commodity call to assign @@ -168,7 +167,7 @@ class ServerManagerUnmanagedServersTest(UDSTestCase): # # Remove it, should decrement counter for i in range(32, -1, -1): # Deletes 33 times - res = self.manager.release(userService, self.registered_servers_group) + _res = self.manager.release(userService, self.registered_servers_group) self.assertEqual(len(self.registered_servers_group.properties), 0) diff --git a/server/tests/core/services/generics/test_dynamic_publication.py b/server/tests/core/services/generics/test_dynamic_publication.py index 4730d677f..f7690ec69 100644 --- a/server/tests/core/services/generics/test_dynamic_publication.py +++ b/server/tests/core/services/generics/test_dynamic_publication.py @@ -100,6 +100,48 @@ class DynamicPublicationTest(UDSTestCase): publication = fixtures.create_dynamic_publication_queue(service) self.check_iterations(service, publication, EXPECTED_DEPLOY_ITERATIONS_INFO) + def test_publication_fails_on_initialize(self) -> None: + service = fixtures.create_dynamic_service() + publication = fixtures.create_dynamic_publication(service) + # Mock op_initialize and make it fail with an exception + with mock.patch.object(publication, 'op_initialize', side_effect=Exception('Test')): + state = publication.publish() + self.assertEqual(state, types.states.TaskState.ERROR) + # Check that the reason is the exception + self.assertEqual(publication._reason, 'Test') + # Check that the queue is empty (only ERROR operation) + self.assertEqual(publication._queue, [types.services.Operation.ERROR]) + + def test_publication_fails_on_create(self) -> None: + service = fixtures.create_dynamic_service() + publication = fixtures.create_dynamic_publication(service) + # Mock op_create and make it fail with an exception + with mock.patch.object(publication, 'op_create', side_effect=Exception('Test')): + state = publication.publish() # Firt iteration is INITIALIZE + self.assertEqual(state, types.states.TaskState.RUNNING) # Should work + state = publication.check_state() # Second iteration is CREATE + self.assertEqual(state, types.states.TaskState.ERROR) + # Check that the reason is the exception + self.assertEqual(publication._reason, 'Test') + # Check that the queue is empty (only ERROR operation) + self.assertEqual(publication._queue, [types.services.Operation.ERROR]) + + def test_publication_fails_on_create_completed(self) -> None: + service = fixtures.create_dynamic_service() + publication = fixtures.create_dynamic_publication(service) + # Mock op_create_completed and make it fail with an exception + with mock.patch.object(publication, 'op_create_completed', side_effect=Exception('Test')): + state = publication.publish() + self.assertEqual(state, types.states.TaskState.RUNNING) # Should work + state = publication.check_state() + self.assertEqual(state, types.states.TaskState.RUNNING) # Should work + state = publication.check_state() + self.assertEqual(state, types.states.TaskState.ERROR) + # Check that the reason is the exception + self.assertEqual(publication._reason, 'Test') + # Check that the queue is empty (only ERROR operation) + self.assertEqual(publication._queue, [types.services.Operation.ERROR]) + EXPECTED_DEPLOY_ITERATIONS_INFO: typing.Final[list[DynamicPublicationIterationInfo]] = [ # Initial state for queue diff --git a/server/tests/core/util/test_permissions.py b/server/tests/core/util/test_permissions.py index c67aee0ed..0abe4268e 100644 --- a/server/tests/core/util/test_permissions.py +++ b/server/tests/core/util/test_permissions.py @@ -81,7 +81,7 @@ class PermissionsTest(UDSTestCase): self.service = self.servicePool.service self.provider = self.service.provider - self.network = network_fixtures.createNetwork() + self.network = network_fixtures.create_network() def doTestUserPermissions(self, obj: 'Model', user: models.User) -> None: permissions.add_user_permission(user, obj, uds.core.types.permissions.PermissionType.NONE) diff --git a/server/tests/fixtures/authenticators.py b/server/tests/fixtures/authenticators.py index e023c8faf..342bb0144 100644 --- a/server/tests/fixtures/authenticators.py +++ b/server/tests/fixtures/authenticators.py @@ -29,7 +29,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import random # nosec: testing only from uds import models diff --git a/server/tests/fixtures/networks.py b/server/tests/fixtures/networks.py index 03a6f08d6..c72e565ed 100644 --- a/server/tests/fixtures/networks.py +++ b/server/tests/fixtures/networks.py @@ -28,9 +28,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import typing -import collections.abc - from uds import models @@ -38,7 +35,7 @@ from uds import models glob = {'network_id': 1} -def createNetwork() -> models.Network: +def create_network() -> models.Network: return models.Network.create( 'Network %d' % glob['network_id'], '192.168.{n}.0-192.168.{n}.255'.format(n=glob['network_id']), diff --git a/server/tests/fixtures/servers.py b/server/tests/fixtures/servers.py index 7434911c8..a96612ba4 100644 --- a/server/tests/fixtures/servers.py +++ b/server/tests/fixtures/servers.py @@ -29,7 +29,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from uds import models from uds.core import types @@ -38,7 +37,7 @@ import datetime from ..utils import generators -def createServer( +def create_server( type: 'types.servers.ServerType' = types.servers.ServerType.SERVER, subtype: typing.Optional[str] = None, version: typing.Optional[str] = None, @@ -62,7 +61,7 @@ def createServer( ) -def createServerGroup( +def create_server_group( type: 'types.servers.ServerType' = types.servers.ServerType.SERVER, subtype: typing.Optional[str] = None, version: typing.Optional[str] = None, @@ -80,8 +79,8 @@ def createServerGroup( host=host or '', port=port, ) - for i in range(num_servers): - server = createServer(type, subtype=subtype, version=version, ip=ip, listen_port=listen_port) + for _ in range(num_servers): + server = create_server(type, subtype=subtype, version=version, ip=ip, listen_port=listen_port) rsg.servers.add(server) return rsg diff --git a/server/tests/fixtures/services.py b/server/tests/fixtures/services.py index 0aa6e34ca..6669e2914 100644 --- a/server/tests/fixtures/services.py +++ b/server/tests/fixtures/services.py @@ -30,7 +30,6 @@ """ import datetime import typing -import collections.abc from uds import models from uds.core import environment, types @@ -122,8 +121,6 @@ def create_db_servicepool( transports: typing.Optional[list[models.Transport]] = None, servicePoolGroup: typing.Optional[models.ServicePoolGroup] = None, ) -> models.ServicePool: - from uds.services.Test.service import TestServiceCache, TestServiceNoCache - from uds.osmanagers.Test import TestOSManager service_pool: 'models.ServicePool' = service.deployedServices.create( name='Service pool %d' % (glob['service_pool_id']), @@ -229,9 +226,6 @@ def create_db_one_cache_userservice( groups: list['models.Group'], type_: typing.Union[typing.Literal['managed'], typing.Literal['unmanaged']], ) -> 'models.UserService': - from uds.services.Test.service import TestServiceCache, TestServiceNoCache - from uds.osmanagers.Test import TestOSManager - from uds.transports.Test import TestTransport service = create_db_service(provider)