1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-22 22:03:54 +03:00

Fix service assignments and serialization issues on tests after refactoring storage

This commit is contained in:
Adolfo Gómez García 2024-03-14 23:20:44 +01:00
parent 24c2da1b4b
commit df5d18710a
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
16 changed files with 80 additions and 75 deletions

View File

@ -77,7 +77,7 @@ class OpenStackUserServiceFixed(FixedUserService, autoserializable.AutoSerializa
except Exception as e:
return self._error(f'Machine not found: {e}')
if server_info.status == 'stopped':
if server_info.power_state == openstack_types.PowerState.SHUTDOWN:
self._queue = [Operation.START, Operation.FINISH]
return self._execute_queue()

View File

@ -153,7 +153,8 @@ class OpenStackServiceFixed(FixedService): # pylint: disable=too-many-public-me
return [
gui.choice_item(k, servers[k])
for k in self.machines.as_list()
if k not in assigned_servers and k in servers
if k not in assigned_servers
and k in servers # Only machines not assigned, and that exists on provider will be available
]
def assign_from_assignables(

View File

@ -53,7 +53,6 @@ if typing.TYPE_CHECKING:
logger = logging.getLogger(__name__)
class ProxmoxServiceFixed(FixedService): # pylint: disable=too-many-public-methods
"""
Proxmox fixed machines service.
@ -134,7 +133,7 @@ class ProxmoxServiceFixed(FixedService): # pylint: disable=too-many-public-meth
gui.choice_item(k, vms[int(k)])
for k in self.machines.as_list()
if k not in assigned_vms
and int(k) not in vms
and int(k) in vms # Only machines not assigned, and that exists on provider will be available
]
def assign_from_assignables(

View File

@ -179,7 +179,7 @@ class XenFixedService(FixedService): # pylint: disable=too-many-public-methods
gui.choice_item(k, vms[k])
for k in self.machines.as_list()
if k not in assigned_vms
and k in vms # Only machines not assigned, and that exists on xen will be available
and k in vms # Only machines not assigned, and that exists on provider will be available
]
def assign_from_assignables(

View File

@ -31,15 +31,11 @@
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import collections.abc
import dataclasses
from enum import unique
import typing
from uds.core import environment
from uds.core.util.cache import Cache
from uds.core.util.storage import Storage
from uds.core.util.unique_id_generator import UniqueIDGenerator
from ..utils.test import UDSTransactionTestCase
@ -60,7 +56,7 @@ class TestEnvironment(UDSTransactionTestCase):
self.assertEqual(env.key, expected_key)
env.storage.put('test', 'test')
self.assertEqual(env.storage.get('test'), 'test')
self.assertEqual(env.storage.read('test'), 'test')
env.cache.put('test', 'test')
self.assertEqual(env.cache.get('test'), 'test')
@ -76,10 +72,10 @@ class TestEnvironment(UDSTransactionTestCase):
self.assertEqual(env.key, expected_key)
if is_persistent:
self.assertEqual(env.storage.get('test'), 'test')
self.assertEqual(env.storage.read('test'), 'test')
self.assertEqual(env.cache.get('test'), 'test')
else:
self.assertEqual(env.storage.get('test'), None)
self.assertEqual(env.storage.read('test'), None)
self.assertEqual(env.cache.get('test'), None)
def test_global_environment(self) -> None:
@ -112,7 +108,7 @@ class TestEnvironment(UDSTransactionTestCase):
unique_key = env.key # store for later test
env.storage.put('test', 'test')
self.assertEqual(env.storage.get('test'), 'test')
self.assertEqual(env.storage.read('test'), 'test')
env.cache.put('test', 'test')
self.assertEqual(env.cache.get('test'), 'test')
@ -125,5 +121,5 @@ class TestEnvironment(UDSTransactionTestCase):
self.assertIsInstance(env.storage, Storage)
self.assertIsInstance(env._id_generators, dict)
self.assertEqual(env.key, unique_key)
self.assertEqual(env.storage.get('test'), None)
self.assertEqual(env.storage.read('test'), None)
self.assertEqual(env.cache.get('test'), None)

View File

@ -116,7 +116,7 @@ class OpenGnsysDeploymentSerializationTest(UDSTransactionTestCase):
# queue is kept on "storage", so we need always same environment
environment = Environment.testing_environment()
# Store queue
environment.storage.put_pickle('queue', TEST_QUEUE)
environment.storage.save_pickled('queue', TEST_QUEUE)
def _create_instance(unmarshal_data: 'bytes|None' = None) -> deployment.OpenGnsysUserService:
instance = deployment.OpenGnsysUserService(environment=environment, service=None) # type: ignore

View File

@ -117,7 +117,7 @@ class OpenNebulaDeploymentSerializationTest(UDSTransactionTestCase):
# queue is kept on "storage", so we need always same environment
environment = Environment.testing_environment()
# Store queue
environment.storage.put_pickle('queue', TEST_QUEUE)
environment.storage.save_pickled('queue', TEST_QUEUE)
def _create_instance(unmarshal_data: 'bytes|None' = None) -> deployment.OpenNebulaLiveDeployment:
instance = deployment.OpenNebulaLiveDeployment(environment=environment, service=None) # type: ignore

View File

@ -116,7 +116,7 @@ class OpenStackDeploymentSerializationTest(UDSTransactionTestCase):
# queue is kept on "storage", so we need always same environment
environment = Environment.testing_environment()
# Store queue
environment.storage.put_pickle('queue', TEST_QUEUE)
environment.storage.save_pickled('queue', TEST_QUEUE)
def _create_instance(unmarshal_data: 'bytes|None' = None) -> deployment.OpenStackLiveUserService:
instance = deployment.OpenStackLiveUserService(environment=environment, service=None) # type: ignore

View File

@ -91,14 +91,15 @@ class TestOpenstackLiveDeployment(UDSTransactionTestCase):
self.assertEqual(state, types.states.TaskState.FINISHED, f'Error on {to_test} deployment')
# userservice name is UDS-U-
self.assertEqual(
userservice._name[: len(service.get_basename())],
userservice._name[6: 6+len(service.get_basename())],
service.get_basename(),
f'Error on {to_test} deployment',
)
self.assertEqual(
len(userservice._name),
len(service.get_basename()) + service.get_lenname(),
len(service.get_basename()) + service.get_lenname() + 6, # for UDS-U- prefix
f'Error on {to_test} deployment',
)

View File

@ -42,61 +42,61 @@ from ...utils.generators import limited_iterator
# We use transactions on some related methods (storage access, etc...)
class TestProxmovLinkedService(UDSTransactionTestCase):
class TestOpenstackLiveService(UDSTransactionTestCase):
def test_userservice_fixed_user(self) -> None:
"""
Test the user service
"""
with fixtures.patch_provider_api() as _api:
userservice = fixtures.create_userservice_fixed()
service = userservice.service()
self.assertEqual(service._get_assigned_machines(), set())
for prov in (fixtures.create_provider_legacy(), fixtures.create_provider()):
with fixtures.patch_provider_api(legacy=prov.legacy) as _api:
service = fixtures.create_fixed_service(prov) # Will use provider patched api
userservice = fixtures.create_fixed_userservice(service)
# patch userservice db_obj() method to return a mock
userservice_db = mock.MagicMock()
userservice.db_obj = mock.MagicMock(return_value=userservice_db)
# Test Deploy for cache, should raise Exception due
# to the fact fixed services cannot have cached items
with self.assertRaises(Exception):
userservice.deploy_for_cache(level=1)
# patch userservice db_obj() method to return a mock
userservice_db = mock.MagicMock()
userservice.db_obj = mock.MagicMock(return_value=userservice_db)
# Test Deploy for cache, should raise Exception due
# to the fact fixed services cannot have cached items
with self.assertRaises(Exception):
userservice.deploy_for_cache(level=1)
state = userservice.deploy_for_user(models.User())
state = userservice.deploy_for_user(models.User())
self.assertEqual(state, types.states.TaskState.RUNNING)
self.assertEqual(state, types.states.TaskState.RUNNING)
while state == types.states.TaskState.RUNNING:
state = userservice.check_state()
for _ in limited_iterator(lambda: state == types.states.TaskState.RUNNING, limit=32):
state = userservice.check_state()
self.assertEqual(state, types.states.TaskState.FINISHED)
self.assertEqual(state, types.states.TaskState.FINISHED)
# userservice_db Should have halle set_in_use(True)
userservice_db.set_in_use.assert_called_once_with(True)
# userservice_db Should have halle set_in_use(True)
userservice_db.set_in_use.assert_called_once_with(True)
# vmid should have been assigned, so it must be in the assigned machines
self.assertEqual(set(userservice._vmid), service._get_assigned_machines())
# vmid should have been assigned, so it must be in the assigned machines
with service._assigned_machines_access() as assigned_machines:
self.assertEqual({userservice._vmid}, assigned_machines)
# Now, let's release the service
state = userservice.destroy()
self.assertEqual(state, types.states.TaskState.RUNNING)
while state == types.states.TaskState.RUNNING:
state = userservice.check_state()
self.assertEqual(state, types.states.TaskState.FINISHED)
# must be empty now
self.assertEqual(service._get_assigned_machines(), set())
# set_ready, machine is "stopped" in this test, so must return RUNNING
state = userservice.set_ready()
self.assertEqual(state, types.states.TaskState.RUNNING)
for _ in limited_iterator(lambda: state == types.states.TaskState.RUNNING, limit=32):
state = userservice.check_state()
# Should be finished now
self.assertEqual(state, types.states.TaskState.FINISHED)
# Now, let's release the service
state = userservice.destroy()
self.assertEqual(state, types.states.TaskState.RUNNING)
while state == types.states.TaskState.RUNNING:
state = userservice.check_state()
self.assertEqual(state, types.states.TaskState.FINISHED)
# must be empty now
with service._assigned_machines_access() as assigned_machines:
self.assertEqual(set(), assigned_machines)
# set_ready, machine is "stopped" in this test, so must return RUNNING
state = userservice.set_ready()
self.assertEqual(state, types.states.TaskState.RUNNING)
for _ in limited_iterator(lambda: state == types.states.TaskState.RUNNING, limit=32):
state = userservice.check_state()
# Should be finished now
self.assertEqual(state, types.states.TaskState.FINISHED)

View File

@ -120,7 +120,7 @@ class OvirtDeploymentSerializationTest(UDSTransactionTestCase):
# queue is kept on "storage", so we need always same environment
environment = Environment.testing_environment()
# Store queue
environment.storage.put_pickle('queue', TEST_QUEUE)
environment.storage.save_pickled('queue', TEST_QUEUE)
def _create_instance(unmarshal_data: 'bytes|None' = None) -> Deployment:
instance = Deployment(environment=environment, service=None) # type: ignore

View File

@ -400,7 +400,7 @@ SERVICE_LINKED_VALUES_DICT: typing.Final[gui.ValuesDictType] = {
'lenname': 4,
}
SERVICE_FIXED_VALUES_DICT: typing.Final[gui.ValuesDictType] = {
SERVICE_FIXED_VALUES_DICT: gui.ValuesDictType = {
'token': '',
'pool': POOLS[0].poolid,
'machines': [str(VMS_INFO[2].vmid), str(VMS_INFO[3].vmid), str(VMS_INFO[4].vmid)],

View File

@ -123,7 +123,7 @@ class ProxmoxDeploymentSerializationTest(UDSTransactionTestCase):
# queue is kept on "storage", so we need always same environment
environment = Environment.testing_environment()
# Store queue
environment.storage.put_pickle('queue', TEST_QUEUE)
environment.storage.save_pickled('queue', TEST_QUEUE)
def _create_instance(unmarshal_data: 'bytes|None' = None) -> Deployment:
instance = Deployment(environment=environment, service=fake.fake_service())

View File

@ -104,7 +104,8 @@ class TestProxmovFixedService(UDSTransactionTestCase):
self.assertEqual(service.get_and_assign_machine(), vmid2)
# Now two machies should be assigned
self.assertEqual(service._get_assigned_machines(), set([vmid, vmid2]))
with service._assigned_machines_access() as assigned_machines:
self.assertEqual(assigned_machines, set([vmid, vmid2]))
def test_service_methods_2(self) -> None:
with fixtures.patch_provider_api():
@ -124,9 +125,13 @@ class TestProxmovFixedService(UDSTransactionTestCase):
# Remove and free machine
# Fist, assign a machine
vmid = service.get_and_assign_machine()
self.assertEqual(service._get_assigned_machines(), set([vmid]))
with service._assigned_machines_access() as assigned_machines:
self.assertEqual(assigned_machines, set([vmid]))
# And now free it
self.assertEqual(service.remove_and_free_machine(vmid), types.states.State.FINISHED)
self.assertEqual(service._get_assigned_machines(), set())
with service._assigned_machines_access() as assigned_machines:
self.assertEqual(assigned_machines, set())
def test_process_snapshot(self) -> None:
with fixtures.patch_provider_api() as api:

View File

@ -51,7 +51,8 @@ class TestProxmovLinkedService(UDSTransactionTestCase):
with fixtures.patch_provider_api() as _api:
userservice = fixtures.create_userservice_fixed()
service = userservice.service()
self.assertEqual(service._get_assigned_machines(), set())
with service._assigned_machines_access() as assigned_machines:
self.assertEqual(assigned_machines, set())
# patch userservice db_obj() method to return a mock
userservice_db = mock.MagicMock()
@ -74,8 +75,9 @@ class TestProxmovLinkedService(UDSTransactionTestCase):
userservice_db.set_in_use.assert_called_once_with(True)
# vmid should have been assigned, so it must be in the assigned machines
self.assertEqual(set(userservice._vmid), service._get_assigned_machines())
with service._assigned_machines_access() as assigned_machines:
self.assertEqual({userservice._vmid}, assigned_machines)
# Now, let's release the service
state = userservice.destroy()
@ -87,7 +89,8 @@ class TestProxmovLinkedService(UDSTransactionTestCase):
self.assertEqual(state, types.states.TaskState.FINISHED)
# must be empty now
self.assertEqual(service._get_assigned_machines(), set())
with service._assigned_machines_access() as assigned_machines:
self.assertEqual(assigned_machines, set())
# set_ready, machine is "stopped" in this test, so must return RUNNING
state = userservice.set_ready()

View File

@ -122,7 +122,7 @@ class XenDeploymentSerializationTest(UDSTransactionTestCase):
# queue is kept on "storage", so we need always same environment
environment = Environment.testing_environment()
# Store queue
environment.storage.put_pickle('queue', TEST_QUEUE)
environment.storage.save_pickled('queue', TEST_QUEUE)
def _create_instance(unmarshal_data: 'bytes|None' = None) -> Deployment:
instance = Deployment(environment=environment, service=None) # type: ignore # service is not used