mirror of
https://github.com/dkmstr/openuds.git
synced 2024-12-22 13:34:04 +03:00
Refactors and improvements to fixed_service generic
This commit is contained in:
parent
2290b4f235
commit
a13b4431d0
@ -275,7 +275,7 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called when the service is started
|
||||
"""
|
||||
self.service().start_machine(self, self._vmid)
|
||||
self.service().start(self, self._vmid)
|
||||
|
||||
def op_start_completed(self) -> None:
|
||||
"""
|
||||
@ -288,7 +288,7 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called for stopping the service
|
||||
"""
|
||||
self.service().stop_machine(self, self._vmid)
|
||||
self.service().stop(self, self._vmid)
|
||||
|
||||
def op_stop_completed(self) -> None:
|
||||
"""
|
||||
@ -301,7 +301,7 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called for shutdown the service
|
||||
"""
|
||||
self.service().shutdown_machine(self, self._vmid)
|
||||
self.service().shutdown(self, self._vmid)
|
||||
|
||||
def op_shutdown_completed(self) -> None:
|
||||
"""
|
||||
@ -314,7 +314,7 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
|
||||
This method is called when the service is removed
|
||||
By default, we need a remove machine on the service, use it
|
||||
"""
|
||||
self.service().remove_machine(self, self._vmid)
|
||||
self.service().remove(self, self._vmid)
|
||||
|
||||
def op_remove_completed(self) -> None:
|
||||
"""
|
||||
@ -370,7 +370,7 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called to check if the service is started
|
||||
"""
|
||||
if self.service().is_machine_running(self, self._vmid):
|
||||
if self.service().is_running(self, self._vmid):
|
||||
return types.states.TaskState.FINISHED
|
||||
|
||||
return types.states.TaskState.FINISHED
|
||||
@ -385,7 +385,7 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called to check if the service is stopped
|
||||
"""
|
||||
if self.service().is_machine_running(self, self._vmid) is False:
|
||||
if self.service().is_running(self, self._vmid) is False:
|
||||
return types.states.TaskState.FINISHED
|
||||
|
||||
return types.states.TaskState.FINISHED
|
||||
|
@ -118,7 +118,7 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
return name
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_machine_ip(
|
||||
def get_ip(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> str:
|
||||
"""
|
||||
@ -128,7 +128,7 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_machine_mac(
|
||||
def get_mac(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> str:
|
||||
"""
|
||||
@ -138,7 +138,7 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_machine_running(
|
||||
def is_running(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> bool:
|
||||
"""
|
||||
@ -147,7 +147,7 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def start_machine(
|
||||
def start(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> None:
|
||||
"""
|
||||
@ -157,14 +157,14 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def stop_machine(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
def stop(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
"""
|
||||
Stops the machine
|
||||
Can return a task, or None if no task is returned
|
||||
"""
|
||||
...
|
||||
|
||||
def shutdown_machine(
|
||||
def shutdown(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> None:
|
||||
"""
|
||||
@ -172,9 +172,9 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
Defaults to stop_machine
|
||||
Can return a task, or None if no task is returned
|
||||
"""
|
||||
return self.stop_machine(caller_instance, vmid)
|
||||
return self.stop(caller_instance, vmid)
|
||||
|
||||
def reset_machine(
|
||||
def reset(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> None:
|
||||
"""
|
||||
@ -182,9 +182,9 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
Can return a task, or None if no task is returned
|
||||
"""
|
||||
# Default is to stop "hard"
|
||||
return self.stop_machine(caller_instance, vmid)
|
||||
return self.stop(caller_instance, vmid)
|
||||
|
||||
def suspend_machine(
|
||||
def suspend(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> None:
|
||||
"""
|
||||
@ -192,10 +192,10 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
Defaults to shutdown_machine.
|
||||
Can be overriden if the service supports suspending.
|
||||
"""
|
||||
return self.shutdown_machine(caller_instance, vmid)
|
||||
return self.shutdown(caller_instance, vmid)
|
||||
|
||||
@abc.abstractmethod
|
||||
def remove_machine(
|
||||
def remove(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> None:
|
||||
"""
|
||||
@ -203,12 +203,12 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
|
||||
"""
|
||||
...
|
||||
|
||||
def keep_on_error(self) -> bool:
|
||||
def should_maintain_on_error(self) -> bool:
|
||||
if self.has_field('maintain_on_error'): # If has been defined on own class...
|
||||
return self.maintain_on_error.value
|
||||
return False
|
||||
|
||||
def can_clean_errored_userservices(self) -> bool:
|
||||
def allows_errored_userservice_cleanup(self) -> bool:
|
||||
"""
|
||||
Returns if this service can clean errored services. This is used to check if a service can be cleaned
|
||||
from the stuck cleaner job, for example. By default, this method returns True.
|
||||
|
@ -204,9 +204,9 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
self.do_log(log.LogLevel.ERROR, reason)
|
||||
|
||||
if self._vmid:
|
||||
if self.service().keep_on_error() is False:
|
||||
if self.service().should_maintain_on_error() is False:
|
||||
try:
|
||||
self.service().remove_machine(self, self._vmid)
|
||||
self.service().remove(self, self._vmid)
|
||||
self._vmid = ''
|
||||
except Exception as e:
|
||||
logger.exception('Exception removing machine: %s', e)
|
||||
@ -247,7 +247,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
# Provide self to the service, so it can some of our methods to generate the unique id
|
||||
# (for example, own mac generator, that will autorelease the mac as soon as the machine is removed)
|
||||
if not self._mac:
|
||||
self._mac = self.service().get_machine_mac(self, self._vmid) or ''
|
||||
self._mac = self.service().get_mac(self, self._vmid) or ''
|
||||
return self._mac
|
||||
|
||||
@typing.final
|
||||
@ -256,7 +256,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
try:
|
||||
if self._vmid:
|
||||
# Provide self to the service, so it can use some of our methods for whaterever it needs
|
||||
self._ip = self.service().get_machine_ip(self, self._vmid)
|
||||
self._ip = self.service().get_ip(self, self._vmid)
|
||||
except Exception:
|
||||
logger.warning('Error obtaining IP for %s: %s', self.__class__.__name__, self._vmid, exc_info=True)
|
||||
return self._ip
|
||||
@ -293,7 +293,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
def set_ready(self) -> types.states.TaskState:
|
||||
# If already ready, return finished
|
||||
try:
|
||||
if self.cache.get('ready') == '1' or self.service().is_machine_running(self, self._vmid):
|
||||
if self.cache.get('ready') == '1' or self.service().is_running(self, self._vmid):
|
||||
self._set_queue([Operation.START_COMPLETED, Operation.FINISH])
|
||||
else:
|
||||
self._set_queue([Operation.START, Operation.START_COMPLETED, Operation.FINISH])
|
||||
@ -441,7 +441,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called when the service is started
|
||||
"""
|
||||
self.service().start_machine(self, self._vmid)
|
||||
self.service().start(self, self._vmid)
|
||||
|
||||
def op_start_completed(self) -> None:
|
||||
"""
|
||||
@ -454,7 +454,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called for stopping the service
|
||||
"""
|
||||
self.service().stop_machine(self, self._vmid)
|
||||
self.service().stop(self, self._vmid)
|
||||
|
||||
def op_stop_completed(self) -> None:
|
||||
"""
|
||||
@ -468,12 +468,12 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
This method is called for shutdown the service
|
||||
"""
|
||||
shutdown_stamp = -1
|
||||
is_running = self.service().is_machine_running(self, self._vmid)
|
||||
is_running = self.service().is_running(self, self._vmid)
|
||||
if not is_running:
|
||||
# Already stopped, just finish
|
||||
return
|
||||
|
||||
self.service().shutdown_machine(self, self._vmid)
|
||||
self.service().shutdown(self, self._vmid)
|
||||
shutdown_stamp = sql_stamp_seconds()
|
||||
|
||||
with self.storage.as_dict() as data:
|
||||
@ -504,7 +504,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called when the service is reset
|
||||
"""
|
||||
self.service().reset_machine(self, self._vmid)
|
||||
self.service().reset(self, self._vmid)
|
||||
|
||||
def op_reset_completed(self) -> None:
|
||||
"""
|
||||
@ -517,7 +517,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called when the service is removed
|
||||
"""
|
||||
self.service().remove_machine(self, self._vmid)
|
||||
self.service().remove(self, self._vmid)
|
||||
|
||||
def op_remove_completed(self) -> None:
|
||||
"""
|
||||
@ -581,7 +581,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called to check if the service is started
|
||||
"""
|
||||
if self.service().is_machine_running(self, self._vmid):
|
||||
if self.service().is_running(self, self._vmid):
|
||||
return types.states.TaskState.FINISHED
|
||||
|
||||
return types.states.TaskState.RUNNING
|
||||
@ -596,7 +596,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
"""
|
||||
This method is called to check if the service is stopped
|
||||
"""
|
||||
if self.service().is_machine_running(self, self._vmid) is False:
|
||||
if self.service().is_running(self, self._vmid) is False:
|
||||
return types.states.TaskState.FINISHED
|
||||
return types.states.TaskState.RUNNING
|
||||
|
||||
@ -622,7 +622,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
|
||||
|
||||
logger.debug('Checking State')
|
||||
# Check if machine is already stopped (As soon as it is not running, we will consider it stopped)
|
||||
if self.service().is_machine_running(self, self._vmid) is False:
|
||||
if self.service().is_running(self, self._vmid) is False:
|
||||
return types.states.TaskState.FINISHED
|
||||
|
||||
logger.debug('State is running')
|
||||
|
@ -184,11 +184,14 @@ class FixedService(services.Service, abc.ABC): # pylint: disable=too-many-publi
|
||||
def remove_and_free(self, vmid: str) -> str:
|
||||
try:
|
||||
with self._assigned_access() as assigned:
|
||||
assigned.remove(vmid)
|
||||
# In error situations, due to the "process_snapshot" post runasign, the element could be already removed
|
||||
# So we need to check if it's there
|
||||
if vmid in assigned:
|
||||
assigned.remove(vmid)
|
||||
return types.states.State.FINISHED
|
||||
except Exception as e:
|
||||
logger.warning('Cound not save assigned machines on fixed pool: %s', e)
|
||||
raise
|
||||
logger.error('Error processing remove and free: %s', e)
|
||||
raise Exception(f'Error processing remove and free: {e} on {vmid}') from e
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_first_network_mac(self, vmid: str) -> str:
|
||||
|
@ -104,7 +104,7 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
|
||||
State.ERROR, so we can do "return self._error(reason)"
|
||||
"""
|
||||
reason = str(reason)
|
||||
logger.debug('Setting error state, reason: %s', reason)
|
||||
logger.debug('Setting error state, reason: %s (%s)', reason, self._queue, stack_info=True)
|
||||
self.do_log(log.LogLevel.ERROR, reason)
|
||||
|
||||
if self._vmid:
|
||||
@ -161,7 +161,7 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
|
||||
"""
|
||||
Fixed Userservice does not provided "cached" elements
|
||||
"""
|
||||
return self._error('Cache not supported')
|
||||
return self._error('Cache for fixed userservices not supported')
|
||||
|
||||
@typing.final
|
||||
def assign(self, vmid: str) -> types.states.TaskState:
|
||||
|
@ -270,7 +270,7 @@ class Service(Module):
|
||||
"""
|
||||
return True
|
||||
|
||||
def can_clean_errored_userservices(self) -> bool:
|
||||
def allows_errored_userservice_cleanup(self) -> bool:
|
||||
"""
|
||||
Returns if this service can clean errored services. This is used to check if a service can be cleaned
|
||||
from the stuck cleaner job, for example. By default, this method returns True.
|
||||
|
@ -85,7 +85,7 @@ class StuckCleaner(Job):
|
||||
yield from q.filter(state=State.PREPARING)
|
||||
|
||||
for servicepool in servicePoolswithStucks:
|
||||
if servicepool.service.get_instance().can_clean_errored_userservices() is False:
|
||||
if servicepool.service.get_instance().allows_errored_userservice_cleanup() is False:
|
||||
continue
|
||||
# logger.debug('Searching for stuck states for %s', servicePool.name)
|
||||
for stuck in _retrieve_stuck_user_services(servicepool):
|
||||
|
@ -294,7 +294,7 @@ class OpenStackLiveService(services.Service):
|
||||
vminfo = self.api.get_server(vmid)
|
||||
return vminfo.power_state
|
||||
|
||||
def start_machine(self, machineId: str) -> None:
|
||||
def start_machine(self, vmid: str) -> None:
|
||||
"""
|
||||
Tries to start a machine. No check is done, it is simply requested to OpenStack.
|
||||
|
||||
@ -306,12 +306,12 @@ class OpenStackLiveService(services.Service):
|
||||
Returns:
|
||||
"""
|
||||
# if already running, do nothing
|
||||
if self.get_machine_power_state(machineId) == openstack_types.PowerState.RUNNING:
|
||||
if self.get_machine_power_state(vmid) == openstack_types.PowerState.RUNNING:
|
||||
return
|
||||
|
||||
self.api.start_server(machineId)
|
||||
self.api.start_server(vmid)
|
||||
|
||||
def stop_machine(self, machineId: str) -> None:
|
||||
def stop_machine(self, vmid: str) -> None:
|
||||
"""
|
||||
Tries to stop a machine. No check is done, it is simply requested to OpenStack
|
||||
|
||||
@ -321,12 +321,12 @@ class OpenStackLiveService(services.Service):
|
||||
Returns:
|
||||
"""
|
||||
# If already stopped, do nothing
|
||||
if self.get_machine_power_state(machineId) == openstack_types.PowerState.SHUTDOWN:
|
||||
if self.get_machine_power_state(vmid) == openstack_types.PowerState.SHUTDOWN:
|
||||
return
|
||||
|
||||
self.api.stop_server(machineId)
|
||||
self.api.stop_server(vmid)
|
||||
|
||||
def reset_machine(self, machineId: str) -> None:
|
||||
def reset_machine(self, vmid: str) -> None:
|
||||
"""
|
||||
Tries to stop a machine. No check is done, it is simply requested to OpenStack
|
||||
|
||||
@ -335,9 +335,9 @@ class OpenStackLiveService(services.Service):
|
||||
|
||||
Returns:
|
||||
"""
|
||||
self.api.reset_server(machineId)
|
||||
self.api.reset_server(vmid)
|
||||
|
||||
def suspend_machine(self, machineId: str) -> None:
|
||||
def suspend_machine(self, vmid: str) -> None:
|
||||
"""
|
||||
Tries to suspend a machine. No check is done, it is simply requested to OpenStack
|
||||
|
||||
@ -347,11 +347,11 @@ class OpenStackLiveService(services.Service):
|
||||
Returns:
|
||||
"""
|
||||
# If not running, do nothing
|
||||
if self.get_machine_power_state(machineId) != openstack_types.PowerState.RUNNING:
|
||||
if self.get_machine_power_state(vmid) != openstack_types.PowerState.RUNNING:
|
||||
return
|
||||
self.api.suspend_server(machineId)
|
||||
self.api.suspend_server(vmid)
|
||||
|
||||
def resume_machine(self, machineid: str) -> None:
|
||||
def resume_machine(self, vmid: str) -> None:
|
||||
"""
|
||||
Tries to start a machine. No check is done, it is simply requested to OpenStack
|
||||
|
||||
@ -361,9 +361,9 @@ class OpenStackLiveService(services.Service):
|
||||
Returns:
|
||||
"""
|
||||
# If not suspended, do nothing
|
||||
if self.get_machine_power_state(machineid) != openstack_types.PowerState.SUSPENDED:
|
||||
if self.get_machine_power_state(vmid) != openstack_types.PowerState.SUSPENDED:
|
||||
return
|
||||
self.api.resume_server(machineid)
|
||||
self.api.resume_server(vmid)
|
||||
|
||||
def delete_machine(self, vmid: str) -> None:
|
||||
"""
|
||||
@ -398,7 +398,7 @@ class OpenStackLiveService(services.Service):
|
||||
def is_avaliable(self) -> bool:
|
||||
return self.provider().is_available()
|
||||
|
||||
def can_clean_errored_userservices(self) -> bool:
|
||||
def allows_errored_userservice_cleanup(self) -> bool:
|
||||
return not self.maintain_on_error.value
|
||||
|
||||
def keep_on_error(self) -> bool:
|
||||
|
@ -128,7 +128,7 @@ class ProxmoxPublication(DynamicPublication, autoserializable.AutoSerializable):
|
||||
self.service().provider().create_template(int(self._vmid))
|
||||
|
||||
def op_remove(self) -> None:
|
||||
self.service().remove_machine(self, self._vmid)
|
||||
self.service().remove(self, self._vmid)
|
||||
|
||||
def machine(self) -> int:
|
||||
return int(self._vmid)
|
||||
|
@ -284,42 +284,42 @@ class ProxmoxServiceLinked(DynamicService):
|
||||
def is_avaliable(self) -> bool:
|
||||
return self.provider().is_available()
|
||||
|
||||
def get_machine_ip(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> str:
|
||||
def get_ip(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> str:
|
||||
return self.provider().get_guest_ip_address(int(vmid))
|
||||
|
||||
def get_machine_mac(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> str:
|
||||
def get_mac(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> str:
|
||||
return self.get_nic_mac(int(vmid))
|
||||
|
||||
def start_machine(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
def start(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
if isinstance(caller_instance, ProxmoxUserserviceLinked):
|
||||
if self.is_machine_running(caller_instance, vmid): # If running, skip
|
||||
if self.is_running(caller_instance, vmid): # If running, skip
|
||||
caller_instance._task = ''
|
||||
else:
|
||||
caller_instance._store_task(self.provider().start_machine(int(vmid)))
|
||||
else:
|
||||
raise Exception('Invalid caller instance (publication) for start_machine()')
|
||||
|
||||
def stop_machine(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
def stop(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
if isinstance(caller_instance, ProxmoxUserserviceLinked):
|
||||
if self.is_machine_running(caller_instance, vmid):
|
||||
if self.is_running(caller_instance, vmid):
|
||||
caller_instance._store_task(self.provider().stop_machine(int(vmid)))
|
||||
else:
|
||||
caller_instance._task = ''
|
||||
else:
|
||||
raise Exception('Invalid caller instance (publication) for stop_machine()')
|
||||
|
||||
def shutdown_machine(
|
||||
def shutdown(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> None:
|
||||
if isinstance(caller_instance, ProxmoxUserserviceLinked):
|
||||
if self.is_machine_running(caller_instance, vmid):
|
||||
if self.is_running(caller_instance, vmid):
|
||||
caller_instance._store_task(self.provider().shutdown_machine(int(vmid)))
|
||||
else:
|
||||
caller_instance._task = ''
|
||||
else:
|
||||
raise Exception('Invalid caller instance (publication) for shutdown_machine()')
|
||||
|
||||
def is_machine_running(
|
||||
def is_running(
|
||||
self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str
|
||||
) -> bool:
|
||||
# Raise an exception if fails to get machine info
|
||||
@ -327,7 +327,7 @@ class ProxmoxServiceLinked(DynamicService):
|
||||
|
||||
return vminfo.status != 'stopped'
|
||||
|
||||
def remove_machine(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
def remove(self, caller_instance: 'DynamicUserService | DynamicPublication', vmid: str) -> None:
|
||||
# All removals are deferred, so we can do it async
|
||||
# Try to stop it if already running... Hard stop
|
||||
jobs.ProxmoxDeferredRemoval.remove(self.provider(), int(vmid), self.try_graceful_shutdown())
|
||||
|
@ -440,76 +440,76 @@ class DynamicTestingService(dynamic_service.DynamicService):
|
||||
|
||||
machine_running_flag: bool = False
|
||||
|
||||
def get_machine_ip(
|
||||
def get_ip(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> str:
|
||||
self.mock.get_machine_ip(caller_instance, vmid)
|
||||
self.mock.get_ip(caller_instance, vmid)
|
||||
return '1.2.3.4'
|
||||
|
||||
def get_machine_mac(
|
||||
def get_mac(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> str:
|
||||
self.mock.get_machine_mac(caller_instance, vmid)
|
||||
self.mock.get_mac(caller_instance, vmid)
|
||||
return '02:04:06:08:0A:0C'
|
||||
|
||||
def is_machine_running(
|
||||
def is_running(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> bool:
|
||||
self.mock.is_machine_running(caller_instance, vmid)
|
||||
self.mock.is_running(caller_instance, vmid)
|
||||
return self.machine_running_flag
|
||||
|
||||
def start_machine(
|
||||
def start(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> None:
|
||||
self.mock.start_machine(caller_instance, vmid)
|
||||
self.mock.start(caller_instance, vmid)
|
||||
self.machine_running_flag = True
|
||||
|
||||
def stop_machine(
|
||||
def stop(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> None:
|
||||
self.mock.stop_machine(caller_instance, vmid)
|
||||
self.mock.stop(caller_instance, vmid)
|
||||
self.machine_running_flag = False
|
||||
|
||||
def shutdown_machine(
|
||||
def shutdown(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> None:
|
||||
self.mock.shutdown_machine(caller_instance, vmid)
|
||||
self.mock.shutdown(caller_instance, vmid)
|
||||
self.machine_running_flag = False
|
||||
|
||||
def remove_machine(
|
||||
def remove(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> None:
|
||||
self.mock.remove_machine(caller_instance, vmid)
|
||||
self.mock.remove(caller_instance, vmid)
|
||||
self.machine_running_flag = False
|
||||
|
||||
def reset_machine(
|
||||
def reset(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> None:
|
||||
self.mock.reset_machine(caller_instance, vmid)
|
||||
super().reset_machine(caller_instance, vmid)
|
||||
self.mock.reset(caller_instance, vmid)
|
||||
super().reset(caller_instance, vmid) # Call parent reset, that in order invokes stop
|
||||
|
||||
def suspend_machine(
|
||||
def suspend(
|
||||
self,
|
||||
caller_instance: dynamic_userservice.DynamicUserService | dynamic_publication.DynamicPublication,
|
||||
vmid: str,
|
||||
) -> None:
|
||||
self.mock.suspend_machine(caller_instance, vmid)
|
||||
self.mock.suspend(caller_instance, vmid)
|
||||
self.machine_running_flag = False
|
||||
|
||||
|
||||
|
@ -163,35 +163,35 @@ EXPECTED_DEPLOY_ITERATIONS_INFO: typing.Final[list[DynamicPublicationIterationIn
|
||||
queue=fixtures.PUB_TESTEABLE_OPERATIONS[3:],
|
||||
user_service_calls=[call.create_completed_checker()],
|
||||
service_calls=[
|
||||
call.start_machine(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
call.start(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicPublicationIterationInfo( # 5, START_COMPLETED
|
||||
queue=fixtures.PUB_TESTEABLE_OPERATIONS[4:],
|
||||
user_service_calls=[call.start_completed()],
|
||||
service_calls=[
|
||||
call.is_machine_running(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
call.is_running(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicPublicationIterationInfo( # 6, STOP
|
||||
queue=fixtures.PUB_TESTEABLE_OPERATIONS[5:],
|
||||
user_service_calls=[call.start_completed_checker()],
|
||||
service_calls=[
|
||||
call.stop_machine(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
call.stop(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicPublicationIterationInfo( # 7, STOP_COMPLETED
|
||||
queue=fixtures.PUB_TESTEABLE_OPERATIONS[6:],
|
||||
user_service_calls=[call.stop_completed()],
|
||||
service_calls=[
|
||||
call.is_machine_running(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
call.is_running(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicPublicationIterationInfo( # 8, SHUTDOWN
|
||||
queue=fixtures.PUB_TESTEABLE_OPERATIONS[7:],
|
||||
user_service_calls=[call.stop_completed_checker()],
|
||||
service_calls=[
|
||||
call.shutdown_machine(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str)),
|
||||
call.shutdown(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str)),
|
||||
],
|
||||
),
|
||||
DynamicPublicationIterationInfo( # 9, SHUTDOWN_COMPLETED
|
||||
@ -202,7 +202,7 @@ EXPECTED_DEPLOY_ITERATIONS_INFO: typing.Final[list[DynamicPublicationIterationIn
|
||||
queue=fixtures.PUB_TESTEABLE_OPERATIONS[9:],
|
||||
user_service_calls=[call.shutdown_completed_checker()],
|
||||
service_calls=[
|
||||
call.remove_machine(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
call.remove(MustBeOfType(fixtures.DynamicTestingPublicationQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicPublicationIterationInfo( # 11, REMOVE_COMPLETED
|
||||
|
@ -111,8 +111,8 @@ class DynamicServiceTest(UDSTestCase):
|
||||
state = userservice.check_state()
|
||||
|
||||
self.assertEqual(state, types.states.TaskState.FINISHED)
|
||||
service.mock.start_machine.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.is_machine_running.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.start.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.is_running.assert_called_once_with(userservice, userservice._vmid)
|
||||
|
||||
def test_userservice_deploy_for_cache_l1(self) -> None:
|
||||
service = fixtures.create_dynamic_service()
|
||||
@ -125,8 +125,8 @@ class DynamicServiceTest(UDSTestCase):
|
||||
state = userservice.check_state()
|
||||
|
||||
self.assertEqual(state, types.states.TaskState.FINISHED)
|
||||
service.mock.start_machine.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.is_machine_running.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.start.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.is_running.assert_called_once_with(userservice, userservice._vmid)
|
||||
|
||||
def test_userservice_deploy_for_cache_l2(self) -> None:
|
||||
service = fixtures.create_dynamic_service()
|
||||
@ -143,15 +143,15 @@ class DynamicServiceTest(UDSTestCase):
|
||||
state = userservice.check_state()
|
||||
|
||||
self.assertEqual(state, types.states.TaskState.FINISHED)
|
||||
service.mock.start_machine.assert_called_once_with(userservice, userservice._vmid)
|
||||
# is_machine_running must has been called 3 times:
|
||||
service.mock.start.assert_called_once_with(userservice, userservice._vmid)
|
||||
# is_running must has been called 3 times:
|
||||
# * for start check
|
||||
# * for suspend check before suspend (to ensure it's running)
|
||||
# * for check_suspend after suspend
|
||||
self.assertEqual(service.mock.is_machine_running.call_count, 3)
|
||||
service.mock.is_machine_running.assert_called_with(userservice, userservice._vmid)
|
||||
self.assertEqual(service.mock.is_running.call_count, 3)
|
||||
service.mock.is_running.assert_called_with(userservice, userservice._vmid)
|
||||
# And shutdown must be called once
|
||||
service.mock.shutdown_machine.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.shutdown.assert_called_once_with(userservice, userservice._vmid)
|
||||
|
||||
def test_userservice_removal(self) -> None:
|
||||
service = fixtures.create_dynamic_service()
|
||||
@ -178,15 +178,15 @@ class DynamicServiceTest(UDSTestCase):
|
||||
state = userservice.check_state()
|
||||
|
||||
self.assertEqual(state, types.states.TaskState.FINISHED)
|
||||
service.mock.stop_machine.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.is_machine_running.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.remove_machine.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.stop.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.is_running.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.remove.assert_called_once_with(userservice, userservice._vmid)
|
||||
|
||||
def test_userservice_maintain_on_error_no_created(self) -> None:
|
||||
service = fixtures.create_dynamic_service(maintain_on_error=True)
|
||||
userservice = fixtures.create_dynamic_userservice(service)
|
||||
self.assertFalse(service.can_clean_errored_userservices())
|
||||
self.assertTrue(service.keep_on_error())
|
||||
self.assertFalse(service.allows_errored_userservice_cleanup())
|
||||
self.assertTrue(service.should_maintain_on_error())
|
||||
|
||||
state = userservice.deploy_for_user(models.User())
|
||||
self.assertEqual(state, types.states.TaskState.RUNNING)
|
||||
@ -199,8 +199,8 @@ class DynamicServiceTest(UDSTestCase):
|
||||
def test_userservice_maintain_on_error_created(self) -> None:
|
||||
service = fixtures.create_dynamic_service(maintain_on_error=True)
|
||||
userservice = fixtures.create_dynamic_userservice(service)
|
||||
self.assertFalse(service.can_clean_errored_userservices())
|
||||
self.assertTrue(service.keep_on_error())
|
||||
self.assertFalse(service.allows_errored_userservice_cleanup())
|
||||
self.assertTrue(service.should_maintain_on_error())
|
||||
|
||||
state = userservice.deploy_for_user(models.User())
|
||||
self.assertEqual(state, types.states.TaskState.RUNNING)
|
||||
@ -226,7 +226,7 @@ class DynamicServiceTest(UDSTestCase):
|
||||
for _ in limited_iterator(lambda: state != types.states.TaskState.FINISHED, limit=128):
|
||||
state = userservice.check_state()
|
||||
|
||||
# Now, destroy it. Should call shutdown_machine instead of stop_machine
|
||||
# Now, destroy it. Should call shutdown instead of stop
|
||||
service.mock.reset_mock()
|
||||
userservice.mock.reset_mock()
|
||||
|
||||
@ -236,7 +236,7 @@ class DynamicServiceTest(UDSTestCase):
|
||||
|
||||
self.assertEqual(state, types.states.TaskState.FINISHED)
|
||||
|
||||
service.mock.shutdown_machine.assert_called_once_with(userservice, userservice._vmid)
|
||||
service.mock.shutdown.assert_called_once_with(userservice, userservice._vmid)
|
||||
|
||||
|
||||
|
||||
@ -261,35 +261,35 @@ EXPECTED_DEPLOY_ITERATIONS_INFO: typing.Final[list[DynamicServiceIterationInfo]]
|
||||
queue=fixtures.ALL_TESTEABLE_OPERATIONS[3:],
|
||||
user_service_calls=[call.create_completed_checker()],
|
||||
service_calls=[
|
||||
call.start_machine(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
call.start(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicServiceIterationInfo( # 5, START_COMPLETED
|
||||
queue=fixtures.ALL_TESTEABLE_OPERATIONS[4:],
|
||||
user_service_calls=[call.start_completed()],
|
||||
service_calls=[
|
||||
call.is_machine_running(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
call.is_running(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicServiceIterationInfo( # 6, STOP
|
||||
queue=fixtures.ALL_TESTEABLE_OPERATIONS[5:],
|
||||
user_service_calls=[call.start_completed_checker()],
|
||||
service_calls=[
|
||||
call.stop_machine(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
call.stop(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicServiceIterationInfo( # 7, STOP_COMPLETED
|
||||
queue=fixtures.ALL_TESTEABLE_OPERATIONS[6:],
|
||||
user_service_calls=[call.stop_completed()],
|
||||
service_calls=[
|
||||
call.is_machine_running(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
call.is_running(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicServiceIterationInfo( # 8, SHUTDOWN
|
||||
queue=fixtures.ALL_TESTEABLE_OPERATIONS[7:],
|
||||
user_service_calls=[call.stop_completed_checker()],
|
||||
service_calls=[
|
||||
call.is_machine_running(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str)),
|
||||
call.is_running(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str)),
|
||||
],
|
||||
),
|
||||
DynamicServiceIterationInfo( # 9, SHUTDOWN_COMPLETED
|
||||
@ -310,9 +310,9 @@ EXPECTED_DEPLOY_ITERATIONS_INFO: typing.Final[list[DynamicServiceIterationInfo]]
|
||||
queue=fixtures.ALL_TESTEABLE_OPERATIONS[11:],
|
||||
user_service_calls=[call.suspend_completed_checker()],
|
||||
service_calls=[
|
||||
call.reset_machine(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str)),
|
||||
call.reset(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str)),
|
||||
# In turn, calls stop by default
|
||||
call.stop_machine(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str)),
|
||||
call.stop(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str)),
|
||||
],
|
||||
),
|
||||
DynamicServiceIterationInfo( # 13, RESET_COMPLETED
|
||||
@ -323,7 +323,7 @@ EXPECTED_DEPLOY_ITERATIONS_INFO: typing.Final[list[DynamicServiceIterationInfo]]
|
||||
queue=fixtures.ALL_TESTEABLE_OPERATIONS[13:],
|
||||
user_service_calls=[call.reset_completed_checker()],
|
||||
service_calls=[
|
||||
call.remove_machine(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
call.remove(MustBeOfType(fixtures.DynamicTestingUserServiceQueue), MustBeOfType(str))
|
||||
],
|
||||
),
|
||||
DynamicServiceIterationInfo( # 15, REMOVE_COMPLETED
|
||||
|
@ -113,5 +113,5 @@ class TestOpenstackService(UDSTransactionTestCase):
|
||||
|
||||
self.assertEqual(service.get_basename(), service.basename.value)
|
||||
self.assertEqual(service.get_lenname(), service.lenname.value)
|
||||
self.assertEqual(service.can_clean_errored_userservices(), not service.maintain_on_error.value)
|
||||
self.assertEqual(service.allows_errored_userservice_cleanup(), not service.maintain_on_error.value)
|
||||
self.assertEqual(service.keep_on_error(), service.maintain_on_error.value)
|
||||
|
Loading…
Reference in New Issue
Block a user