mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-22 22:03:54 +03:00
Adding tests for userservice manager
This commit is contained in:
parent
207a784b8f
commit
aab94bffbc
@ -76,7 +76,7 @@ class ServerManagerManagedServersTest(UDSTestCase):
|
||||
|
||||
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.user_services.extend(services_fixtures.create_db_assigned_userservices())
|
||||
|
||||
self.registered_servers_group = servers_fixtures.create_server_group(
|
||||
type=types.servers.ServerType.SERVER, subtype='test', num_servers=NUM_REGISTEREDSERVERS
|
||||
|
@ -69,7 +69,7 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
|
||||
|
||||
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.user_services.extend(services_fixtures.create_db_assigned_userservices())
|
||||
|
||||
self.registered_servers_group = servers_fixtures.create_server_group(
|
||||
type=types.servers.ServerType.UNMANAGED, subtype='test', num_servers=NUM_REGISTEREDSERVERS
|
||||
|
98
server/src/tests/core/managers/test_userservice.py
Normal file
98
server/src/tests/core/managers/test_userservice.py
Normal file
@ -0,0 +1,98 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2024 Virtual Cable S.L.U.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
|
||||
# may be used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
"""
|
||||
Author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
import logging
|
||||
|
||||
from uds import models
|
||||
from uds.core import types as core_types
|
||||
|
||||
from uds.core.managers.userservice import UserServiceManager
|
||||
|
||||
|
||||
from tests.fixtures import services as services_fixtures
|
||||
|
||||
from tests.utils.test import UDSTransactionTestCase
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestUserserviceManager(UDSTransactionTestCase):
|
||||
manager: UserServiceManager = UserServiceManager.manager() # For convenience debugging
|
||||
|
||||
def test_forced_mode_assigned_to_l1(self) -> None:
|
||||
# Create an user service, we need
|
||||
userservice = services_fixtures.create_db_assigned_userservices()[0]
|
||||
|
||||
orig_uuid = userservice.uuid
|
||||
orig_src_ip = userservice.src_ip
|
||||
orig_src_hostname = userservice.src_hostname
|
||||
|
||||
self.assertEqual(models.UserService.objects.all().count(), 1)
|
||||
# And uuser service is assigned to an user
|
||||
self.assertIsNotNone(userservice.user)
|
||||
# And cache level is None
|
||||
self.assertEqual(userservice.cache_level, core_types.services.CacheLevel.NONE)
|
||||
|
||||
self.manager.forced_move_assigned_to_cache_l1(userservice)
|
||||
|
||||
# Now, should have 2 user services, one in cache and one in db
|
||||
self.assertEqual(models.UserService.objects.all().count(), 2)
|
||||
# Reload userservice, that should be now in cache
|
||||
userservice = models.UserService.objects.get(uuid=orig_uuid)
|
||||
self.assertEqual(userservice.cache_level, core_types.services.CacheLevel.L1)
|
||||
# Should have no user assigned
|
||||
self.assertIsNone(userservice.user)
|
||||
# Should be usable
|
||||
self.assertTrue(userservice.is_usable())
|
||||
# Should not be in use
|
||||
self.assertFalse(userservice.in_use)
|
||||
# Source ip and hostname should be empty
|
||||
self.assertEqual(userservice.src_ip, '')
|
||||
self.assertEqual(userservice.src_hostname, '')
|
||||
|
||||
|
||||
# Look for the created one (that is the assigned, deleted)
|
||||
assigned = models.UserService.objects.exclude(uuid=orig_uuid).get()
|
||||
self.assertEqual(assigned.cache_level, core_types.services.CacheLevel.NONE)
|
||||
# Should have the user assigned
|
||||
self.assertIsNotNone(assigned.user)
|
||||
# Should be removed
|
||||
self.assertEqual(assigned.state, core_types.states.State.REMOVED)
|
||||
|
||||
# unique_id should be same as the original one
|
||||
self.assertEqual(userservice.unique_id, assigned.unique_id)
|
||||
# src_ip and src_hostname should be the original ones
|
||||
self.assertEqual(assigned.src_ip, orig_src_ip)
|
||||
self.assertEqual(assigned.src_hostname, orig_src_hostname)
|
||||
|
||||
def test_release_from_logout(self) -> None:
|
||||
pass
|
@ -55,7 +55,7 @@ class ModelAccountTest(UDSTestCase):
|
||||
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.user_services = services_fixtures.create_db_cache_userservices(NUM_USERSERVICES)
|
||||
self.user_services = services_fixtures.create_db_assigned_userservices(NUM_USERSERVICES)
|
||||
|
||||
def test_base(self) -> None:
|
||||
acc = models.Account.objects.create(name='Test Account')
|
||||
|
@ -70,7 +70,7 @@ class PermissionsTest(UDSTestCase):
|
||||
self.staffs = authenticators_fixtures.create_db_users(
|
||||
self.authenticator, is_staff=True, groups=self.groups
|
||||
)
|
||||
self.userService = services_fixtures.create_db_one_cache_userservice(
|
||||
self.userService = services_fixtures.create_db_one_assigned_userservice(
|
||||
services_fixtures.create_db_provider(),
|
||||
self.users[0],
|
||||
list(self.users[0].groups.all()),
|
||||
|
@ -49,7 +49,7 @@ class PropertiesTest(UDSTestCase):
|
||||
self.user_services = []
|
||||
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.user_services.extend(services_fixtures.create_db_assigned_userservices())
|
||||
|
||||
def testUserServiceProperty(self) -> None:
|
||||
"""
|
||||
|
@ -49,7 +49,7 @@ class AssignedAndUnusedTest(UDSTestCase):
|
||||
config.GlobalConfig.CHECK_UNUSED_TIME.set('600')
|
||||
AssignedAndUnused.setup()
|
||||
# All created user services has "in_use" to False, os_state and state to USABLE
|
||||
self.userServices = fixtures_services.create_db_cache_userservices(count=32)
|
||||
self.userServices = fixtures_services.create_db_assigned_userservices(count=32)
|
||||
|
||||
def test_assigned_unused(self) -> None:
|
||||
for us in self.userServices: # Update state date to now
|
||||
|
@ -53,7 +53,7 @@ class HangedCleanerTest(UDSTestCase):
|
||||
config.GlobalConfig.MAX_REMOVAL_TIME.set(MAX_INIT)
|
||||
HangedCleaner.setup()
|
||||
# All created user services has "in_use" to False, os_state and state to USABLE
|
||||
self.userServices = fixtures_services.create_db_cache_userservices(
|
||||
self.userServices = fixtures_services.create_db_assigned_userservices(
|
||||
count=TEST_SERVICES
|
||||
)
|
||||
|
||||
|
@ -59,7 +59,7 @@ class ServiceCacheUpdaterTest(UDSTestCase):
|
||||
TestServiceNoCache.userservices_limit = 1000
|
||||
|
||||
ServiceCacheUpdater.setup()
|
||||
userService = services_fixtures.create_db_cache_userservices()[0]
|
||||
userService = services_fixtures.create_db_assigned_userservices()[0]
|
||||
self.servicepool = userService.deployed_service
|
||||
userService.delete() # empty all
|
||||
|
||||
|
@ -53,7 +53,7 @@ class StuckCleanerTest(UDSTestCase):
|
||||
def setUp(self) -> None:
|
||||
StuckCleaner.setup()
|
||||
|
||||
self.userServices = services_fixtures.create_db_cache_userservices(count=128)
|
||||
self.userServices = services_fixtures.create_db_assigned_userservices(count=128)
|
||||
# Set state date of all to 2 days ago
|
||||
for i, us in enumerate(self.userServices):
|
||||
us.state_date = datetime.datetime.now() - datetime.timedelta(days=2)
|
||||
|
6
server/src/tests/fixtures/services.py
vendored
6
server/src/tests/fixtures/services.py
vendored
@ -220,7 +220,7 @@ def create_db_metapool(
|
||||
return meta_pool
|
||||
|
||||
|
||||
def create_db_one_cache_userservice(
|
||||
def create_db_one_assigned_userservice(
|
||||
provider: 'models.Provider',
|
||||
user: 'models.User',
|
||||
groups: list['models.Group'],
|
||||
@ -241,7 +241,7 @@ def create_db_one_cache_userservice(
|
||||
return create_db_userservice(service_pool, publication, user)
|
||||
|
||||
|
||||
def create_db_cache_userservices(
|
||||
def create_db_assigned_userservices(
|
||||
count: int = 1,
|
||||
type_: typing.Literal['managed', 'unmanaged'] = 'managed',
|
||||
user: typing.Optional['models.User'] = None,
|
||||
@ -255,5 +255,5 @@ def create_db_cache_userservices(
|
||||
user = authenticators.create_db_users(auth, 1, groups=groups)[0]
|
||||
user_services: list[models.UserService] = []
|
||||
for _ in range(count):
|
||||
user_services.append(create_db_one_cache_userservice(create_db_provider(), user, groups, type_))
|
||||
user_services.append(create_db_one_assigned_userservice(create_db_provider(), user, groups, type_))
|
||||
return user_services
|
||||
|
@ -92,13 +92,13 @@ class RESTTestCase(test.UDSTransactionTestCase):
|
||||
|
||||
self.provider = services_fixtures.create_db_provider()
|
||||
|
||||
self.user_service_managed = services_fixtures.create_db_one_cache_userservice(
|
||||
self.user_service_managed = services_fixtures.create_db_one_assigned_userservice(
|
||||
self.provider,
|
||||
self.admins[0],
|
||||
self.groups,
|
||||
'managed',
|
||||
)
|
||||
self.user_service_unmanaged = services_fixtures.create_db_one_cache_userservice(
|
||||
self.user_service_unmanaged = services_fixtures.create_db_one_assigned_userservice(
|
||||
self.provider,
|
||||
self.admins[0],
|
||||
self.groups,
|
||||
@ -108,10 +108,10 @@ class RESTTestCase(test.UDSTransactionTestCase):
|
||||
self.user_services = []
|
||||
for user in self.users:
|
||||
self.user_services.append(
|
||||
services_fixtures.create_db_one_cache_userservice(self.provider, user, self.groups, 'managed')
|
||||
services_fixtures.create_db_one_assigned_userservice(self.provider, user, self.groups, 'managed')
|
||||
)
|
||||
self.user_services.append(
|
||||
services_fixtures.create_db_one_cache_userservice(
|
||||
services_fixtures.create_db_one_assigned_userservice(
|
||||
self.provider, user, self.groups, 'unmanaged'
|
||||
)
|
||||
)
|
||||
|
@ -81,7 +81,7 @@ class TestGetServicesData(UDSTransactionTestCase):
|
||||
service_pools: list[models.ServicePool] = []
|
||||
for i in range(110):
|
||||
service_pools.append(
|
||||
fixtures_services.create_db_cache_userservices(
|
||||
fixtures_services.create_db_assigned_userservices(
|
||||
count=1, user=self.user, groups=self.groups
|
||||
)[0].deployed_service
|
||||
)
|
||||
@ -101,7 +101,7 @@ class TestGetServicesData(UDSTransactionTestCase):
|
||||
service_pools: list[models.ServicePool] = []
|
||||
for _i in range(10):
|
||||
service_pools.append(
|
||||
fixtures_services.create_db_cache_userservices(
|
||||
fixtures_services.create_db_assigned_userservices(
|
||||
count=1, user=self.user, groups=self.groups
|
||||
)[0].deployed_service
|
||||
)
|
||||
@ -178,7 +178,7 @@ class TestGetServicesData(UDSTransactionTestCase):
|
||||
service_pools: list[models.ServicePool] = []
|
||||
for i in range(100):
|
||||
service_pools.append(
|
||||
fixtures_services.create_db_cache_userservices(
|
||||
fixtures_services.create_db_assigned_userservices(
|
||||
count=1, user=self.user, groups=self.groups
|
||||
)[0].deployed_service
|
||||
)
|
||||
@ -226,7 +226,7 @@ class TestGetServicesData(UDSTransactionTestCase):
|
||||
user_services: list[models.ServicePool] = []
|
||||
for i in range(110):
|
||||
user_services.append(
|
||||
fixtures_services.create_db_cache_userservices(
|
||||
fixtures_services.create_db_assigned_userservices(
|
||||
count=1, user=self.user, groups=self.groups
|
||||
)[0].deployed_service
|
||||
)
|
||||
@ -255,7 +255,7 @@ class TestGetServicesData(UDSTransactionTestCase):
|
||||
) -> tuple[list[models.ServicePool], models.MetaPool]:
|
||||
service_pools: list[models.ServicePool] = []
|
||||
for _i in range(count):
|
||||
pool = fixtures_services.create_db_cache_userservices(
|
||||
pool = fixtures_services.create_db_assigned_userservices(
|
||||
count=1, user=self.user, groups=self.groups
|
||||
)[0].deployed_service
|
||||
|
||||
|
@ -672,7 +672,7 @@ class Logout(ActorV3Action):
|
||||
osmanagers.OSManager.logged_out(userservice, username)
|
||||
# If does not have osmanager, or has osmanager and is removable, release it
|
||||
if not osmanager or osmanager.is_removable_on_logout(userservice):
|
||||
UserServiceManager.manager().release_on_logout(userservice)
|
||||
UserServiceManager.manager().release_from_logout(userservice)
|
||||
|
||||
def action(self) -> dict[str, typing.Any]:
|
||||
is_managed = self._params.get('type') != consts.actor.UNMANAGED
|
||||
|
@ -307,8 +307,11 @@ class UserServiceManager(metaclass=singleton.Singleton):
|
||||
Clones the record of a user serviceself.
|
||||
For this, the original userservice will ve moved to cache, and a new one will be created
|
||||
to mark it as "REMOVED"
|
||||
|
||||
The reason for creating a new one with cloned data is "conserving" a deleted record, so we can track it
|
||||
as usual
|
||||
"""
|
||||
# Load as new variable to avoid modifying original
|
||||
# Load again to get a copy of the object
|
||||
user_service_copy = UserService.objects.get(id=user_service.id)
|
||||
user_service_copy.pk = None
|
||||
user_service_copy.uuid = generate_uuid()
|
||||
@ -316,7 +319,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
|
||||
user_service_copy.state = State.REMOVED
|
||||
user_service_copy.os_state = State.USABLE
|
||||
|
||||
# Save the new element, for reference
|
||||
# Save the new element.
|
||||
user_service_copy.save()
|
||||
|
||||
# Now, move the original to cache, but do it "hard" way, so we do not need to check for state
|
||||
@ -325,6 +328,8 @@ class UserServiceManager(metaclass=singleton.Singleton):
|
||||
user_service.user = None
|
||||
user_service.cache_level = types.services.CacheLevel.L1
|
||||
user_service.in_use = False
|
||||
user_service.src_hostname = user_service.src_ip = ''
|
||||
user_service.save()
|
||||
|
||||
def get_cache_servicepool_stats(self, servicepool: ServicePool) -> 'types.services.ServicePoolStats':
|
||||
"""
|
||||
@ -489,7 +494,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
|
||||
_('Can\'t remove nor cancel {} cause its state don\'t allow it').format(user_service.name)
|
||||
)
|
||||
|
||||
def release_on_logout(self, userservice: UserService) -> None:
|
||||
def release_from_logout(self, userservice: UserService) -> None:
|
||||
"""
|
||||
In case of logout, this method will take care of removing the service
|
||||
This is so because on logout, may the userservice returns back to cache if ower service
|
||||
|
Loading…
x
Reference in New Issue
Block a user