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

chore: Remove unused machine method in ProxmoxPublication and update references

This commit is contained in:
Adolfo Gómez García 2024-07-11 03:46:46 +02:00
parent 3016b0845c
commit e4d5bef48a
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
11 changed files with 67 additions and 72 deletions

View File

@ -26,10 +26,11 @@
# 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
"""
# pyright: reportUnknownLambdaType=false
import contextlib
import copy
import functools
@ -43,7 +44,8 @@ from uds.core import types, environment
from uds.core.ui.user_interface import gui
import uds.services.Proxmox.proxmox.client
from ...utils.autospec import autospec, AutoSpecMethodInfo
from tests.utils.autospec import autospec, AutoSpecMethodInfo
from tests.utils import search_item_by_attr
from uds.services.Proxmox import (
deployment_linked,
@ -54,7 +56,7 @@ from uds.services.Proxmox import (
service_linked,
)
from uds.services.Proxmox.proxmox import types as prox_types
from uds.services.Proxmox.proxmox import types as prox_types, exceptions as prox_exceptions
DEF_NODES: list[prox_types.Node] = [
prox_types.Node(name='node0', online=True, local=True, nodeid=1, ip='0.0.0.1', level='level', id='id'),
@ -275,7 +277,7 @@ CLUSTER_INFO: prox_types.ClusterInfo = copy.deepcopy(DEF_CLUSTER_INFO)
STORAGES: list[prox_types.StorageInfo] = copy.deepcopy(DEF_STORAGES)
VGPUS: list[prox_types.VGPUInfo] = copy.deepcopy(DEF_VGPUS)
HA_GROUPS: list[str] = copy.deepcopy(DEF_HA_GROUPS)
VMS_INFO: list[prox_types.VMInfo] = copy.deepcopy(DEF_VMS_INFO)
VMINFO_LIST: list[prox_types.VMInfo] = copy.deepcopy(DEF_VMS_INFO)
VMS_CONFIGURATION: list[prox_types.VMConfiguration] = copy.deepcopy(DEF_VMS_CONFIGURATION)
UPID: prox_types.UPID = copy.deepcopy(DEF_UPID)
VM_CREATION_RESULT: prox_types.VmCreationResult = copy.deepcopy(DEF_VM_CREATION_RESULT)
@ -299,7 +301,7 @@ def clear() -> None:
STORAGES[:] = copy.deepcopy(DEF_STORAGES)
VGPUS[:] = copy.deepcopy(DEF_VGPUS)
HA_GROUPS[:] = copy.deepcopy(DEF_HA_GROUPS)
VMS_INFO[:] = copy.deepcopy(DEF_VMS_INFO)
VMINFO_LIST[:] = copy.deepcopy(DEF_VMS_INFO)
VMS_CONFIGURATION[:] = copy.deepcopy(DEF_VMS_CONFIGURATION)
UPID = copy.deepcopy(DEF_UPID) # pyright: ignore
VM_CREATION_RESULT = copy.deepcopy(DEF_VM_CREATION_RESULT) # pyright: ignore
@ -314,11 +316,13 @@ def replace_vm_info(vmid: int, **kwargs: typing.Any) -> prox_types.UPID:
"""
Set the values of VMS_INFO[vmid - 1]
"""
for i in range(len(VMS_INFO)):
if VMS_INFO[i].id == vmid:
for k, v in kwargs.items():
setattr(VMS_INFO[i], k, v)
break
try:
vm = search_item_by_attr(VMINFO_LIST, 'id', vmid)
for k, v in kwargs.items():
setattr(vm, k, v)
except Exception:
raise prox_exceptions.ProxmoxNotFound(f'VM {vmid} not found')
return UPID
@ -381,11 +385,13 @@ CLIENT_METHODS_INFO: list[AutoSpecMethodInfo] = [
# list_snapshots
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.list_snapshots,
returns=lambda *args, **kwargs: SNAPSHOTS_INFO, # pyright: ignore[reportUnknownLambdaType]
returns=lambda *args, **kwargs: SNAPSHOTS_INFO, # pyright: ignore[reportUnknownLambdaType]
),
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.get_current_vm_snapshot,
returns=lambda *args, **kwargs: (SNAPSHOTS_INFO + [None])[0], # pyright: ignore[reportUnknownLambdaType]
returns=lambda *args, **kwargs: (SNAPSHOTS_INFO + [None])[
0
],
),
# supports_snapshot
AutoSpecMethodInfo(uds.services.Proxmox.proxmox.client.ProxmoxClient.supports_snapshot, returns=True),
@ -401,16 +407,16 @@ CLIENT_METHODS_INFO: list[AutoSpecMethodInfo] = [
returns=lambda *args, **kwargs: TASK_STATUS, # pyright: ignore
),
# list_machines
AutoSpecMethodInfo(uds.services.Proxmox.proxmox.client.ProxmoxClient.list_vms, returns=VMS_INFO),
AutoSpecMethodInfo(uds.services.Proxmox.proxmox.client.ProxmoxClient.list_vms, returns=VMINFO_LIST),
# get_vm_pool_info
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.get_vm_pool_info,
returns=lambda vmid, poolid, **kwargs: VMS_INFO[vmid - 1], # pyright: ignore
returns=lambda vmid, poolid, **kwargs: VMINFO_LIST[vmid - 1], # pyright: ignore
),
# get_machine_info
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.get_vm_info,
returns=lambda vmid, *args, **kwargs: VMS_INFO[vmid - 1], # pyright: ignore
returns=lambda vmid, *args, **kwargs: VMINFO_LIST[vmid - 1], # pyright: ignore
),
# get_machine_configuration
AutoSpecMethodInfo(
@ -420,15 +426,18 @@ CLIENT_METHODS_INFO: list[AutoSpecMethodInfo] = [
# enable_machine_ha return None
# start_machine
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.start_vm, returns=replacer_vm_info(status=prox_types.VMStatus.RUNNING)
uds.services.Proxmox.proxmox.client.ProxmoxClient.start_vm,
returns=replacer_vm_info(status=prox_types.VMStatus.RUNNING),
),
# stop_machine
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.stop_vm, returns=replacer_vm_info(status=prox_types.VMStatus.STOPPED)
uds.services.Proxmox.proxmox.client.ProxmoxClient.stop_vm,
returns=replacer_vm_info(status=prox_types.VMStatus.STOPPED),
),
# reset_machine
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.reset_vm, returns=replacer_vm_info(status=prox_types.VMStatus.STOPPED)
uds.services.Proxmox.proxmox.client.ProxmoxClient.reset_vm,
returns=replacer_vm_info(status=prox_types.VMStatus.STOPPED),
),
# suspend_machine
AutoSpecMethodInfo(
@ -461,7 +470,7 @@ CLIENT_METHODS_INFO: list[AutoSpecMethodInfo] = [
AutoSpecMethodInfo(
uds.services.Proxmox.proxmox.client.ProxmoxClient.list_storages,
returns=lambda **kwargs: ( # pyright: ignore[reportUnknownLambdaType]
(list(filter(lambda s: s.node == kwargs.get('node') , STORAGES))) # pyright: ignore
(list(filter(lambda s: s.node == kwargs.get('node'), STORAGES))) # pyright: ignore
if kwargs.get('node') is not None # pyright: ignore
else STORAGES # pyright: ignore
),
@ -510,7 +519,7 @@ SERVICE_LINKED_VALUES_DICT: gui.ValuesDictType = {
'pool': POOLS[0].id,
'ha': HA_GROUPS[0],
'try_soft_shutdown': False,
'machine': VMS_INFO[0].id,
'machine': VMINFO_LIST[0].id,
'datastore': STORAGES[0].storage,
'gpu': VGPUS[0].type,
'basename': 'base',
@ -522,7 +531,7 @@ SERVICE_LINKED_VALUES_DICT: gui.ValuesDictType = {
SERVICE_FIXED_VALUES_DICT: gui.ValuesDictType = {
'token': '',
'pool': POOLS[0].id,
'machines': [str(VMS_INFO[2].id), str(VMS_INFO[3].id), str(VMS_INFO[4].id)],
'machines': [str(VMINFO_LIST[2].id), str(VMINFO_LIST[3].id), str(VMINFO_LIST[4].id)],
'use_snapshots': True,
'prov_uuid': '',
}
@ -649,5 +658,5 @@ def create_userservice_linked(
# Other helpers
def set_all_vm_state(status: prox_types.VMStatus) -> None:
# Set machine state for fixture to stopped
for i in VMS_INFO:
for i in VMINFO_LIST:
i.status = status

View File

@ -43,7 +43,7 @@ from ...utils.test import UDSTransactionTestCase
class TestProxmoxHelpers(UDSTransactionTestCase):
_parameters: dict[str, typing.Any] = {
'prov_uuid': 'test',
'machine': fixtures.VMS_INFO[0].id, # Used on get_storage
'machine': fixtures.VMINFO_LIST[0].id, # Used on get_storage
'pool': fixtures.POOLS[0].id, # Used on get_machines
}

View File

@ -137,10 +137,10 @@ class TestProxmoxProvider(UDSTransactionTestCase):
self.assertEqual(provider.test_connection(), True)
api.test.assert_called_once_with()
self.assertEqual(provider.api.list_vms(force=True), fixtures.VMS_INFO)
self.assertEqual(provider.api.list_vms(), fixtures.VMS_INFO)
self.assertEqual(provider.api.list_vms(force=True), fixtures.VMINFO_LIST)
self.assertEqual(provider.api.list_vms(), fixtures.VMINFO_LIST)
self.assertEqual(provider.api.get_vm_info(1), fixtures.VMS_INFO[0])
self.assertEqual(provider.api.get_vm_info(1), fixtures.VMINFO_LIST[0])
self.assertEqual(provider.api.get_vm_config(1), fixtures.VMS_CONFIGURATION[0])

View File

@ -73,7 +73,7 @@ class TestProxmoxPublication(UDSTransactionTestCase):
# And should end in next call
self.assertEqual(publication.check_state(), types.states.State.FINISHED)
# Must have vmid, and must match machine() result
self.assertEqual(publication.machine(), int(publication._vmid))
self.assertEqual(publication.get_template_id(), publication._vmid)
def test_publication_error(self) -> None:
@ -112,7 +112,7 @@ class TestProxmoxPublication(UDSTransactionTestCase):
def test_publication_destroy(self) -> None:
vmid = str(fixtures.VMS_INFO[0].id)
vmid = str(fixtures.VMINFO_LIST[0].id)
with fixtures.patched_provider() as provider:
api = typing.cast(mock.MagicMock, provider.api)
service = fixtures.create_service_linked(provider=provider)
@ -124,18 +124,18 @@ class TestProxmoxPublication(UDSTransactionTestCase):
publication._vmid = vmid
state = publication.destroy()
self.assertEqual(state, types.states.State.RUNNING)
api.delete_vm.assert_called_once_with(publication.machine())
api.delete_vm.assert_called_once_with(int(publication.get_template_id()))
# Now, destroy again, should do nothing more
state = publication.destroy()
# Should not call again
api.delete_vm.assert_called_once_with(publication.machine())
api.delete_vm.assert_called_once_with(int(publication.get_template_id()))
self.assertEqual(state, types.states.State.RUNNING)
def test_publication_destroy_error(self) -> None:
vmid = str(fixtures.VMS_INFO[0].id)
vmid = str(fixtures.VMINFO_LIST[0].id)
with fixtures.patched_provider() as provider:
api = typing.cast(mock.MagicMock, provider.api)
service = fixtures.create_service_linked(provider=provider)
@ -147,7 +147,7 @@ class TestProxmoxPublication(UDSTransactionTestCase):
api.delete_vm.side_effect = Exception('BOOM!')
publication._vmid = vmid
self.assertEqual(publication.destroy(), types.states.State.RUNNING)
api.delete_vm.assert_called_once_with(publication.machine())
api.delete_vm.assert_called_once_with(int(publication.get_template_id()))
# Ensure cancel calls destroy
with mock.patch.object(publication, 'destroy') as destroy:

View File

@ -70,13 +70,13 @@ class TestProxmoxFixedService(UDSTransactionTestCase):
with fixtures.patched_provider() as provider:
service = fixtures.create_service_fixed(provider=provider)
self.assertEqual(service.get_vm_info(2).name, fixtures.VMS_INFO[1].name)
self.assertEqual(service.get_vm_info(2).name, fixtures.VMINFO_LIST[1].name)
# is_available is already tested, so we will skip it
# Enumerate assignables
locate_vm: typing.Callable[[str], typing.Any] = lambda vmid: next(
(x for x in fixtures.VMS_INFO if x.id == int(vmid)), fixtures.VMS_INFO[0]
(x for x in fixtures.VMINFO_LIST if x.id == int(vmid)), fixtures.VMINFO_LIST[0]
)
self.assertEqual(
@ -117,7 +117,7 @@ class TestProxmoxFixedService(UDSTransactionTestCase):
service = fixtures.create_service_fixed(provider=provider)
# Get machine name
self.assertEqual(service.get_name('1'), fixtures.VMS_INFO[0].name)
self.assertEqual(service.get_name('1'), fixtures.VMINFO_LIST[0].name)
# Get first network mac
self.assertEqual(

View File

@ -109,11 +109,11 @@ class TestProxmovLinkedService(UDSTestCase):
)
# Get machine info
self.assertEqual(service.get_vm_info(1), fixtures.VMS_INFO[0])
self.assertEqual(service.get_vm_info(1), fixtures.VMINFO_LIST[0])
api.get_vm_pool_info.assert_called_with(1, service.pool.value)
# Get nic mac
self.assertEqual(service.get_nic_mac(1), '00:01:02:03:04:05')
self.assertEqual(service.get_mac(None, '1'), '00:01:02:03:04:05')
# remove machine, but this is from provider
self.assertEqual(service.provider().api.delete_vm(1), fixtures.UPID)

View File

@ -30,6 +30,7 @@
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import random
import typing
from unittest import mock
@ -53,12 +54,13 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
"""
Test the user service
"""
vm = random.choice(fixtures.VMINFO_LIST)
with fixtures.patched_provider() as provider:
api = typing.cast(mock.MagicMock, provider.api)
service = fixtures.create_service_linked(provider=provider)
userservice = fixtures.create_userservice_linked(service=service)
publication = userservice.publication()
publication._vmid = '1'
publication._vmid = str(vm.id)
state = userservice.deploy_for_cache(level=types.services.CacheLevel.L1)
@ -76,7 +78,7 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
vmid = int(userservice._vmid)
api.clone_vm.assert_called_with(
publication.machine(),
int(publication.get_template_id()),
mock.ANY,
userservice._name,
mock.ANY,
@ -100,6 +102,7 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
"""
Test the user service
"""
vm = random.choice(fixtures.VMINFO_LIST)
with fixtures.patched_provider() as provider:
api = typing.cast(mock.MagicMock, provider.api)
service = fixtures.create_service_linked(provider=provider)
@ -107,7 +110,7 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
service.ha.value = '__' # Disabled
publication = userservice.publication()
publication._vmid = '1'
publication._vmid = str(vm.id)
state = userservice.deploy_for_cache(level=types.services.CacheLevel.L2)
@ -128,7 +131,7 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
vmid = int(userservice._vmid)
api.clone_vm.assert_called_with(
publication.machine(),
int(publication.get_template_id()),
mock.ANY,
userservice._name,
mock.ANY,
@ -156,13 +159,14 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
"""
Test the user service
"""
vm = random.choice(fixtures.VMINFO_LIST)
with fixtures.patched_provider() as provider:
api = typing.cast(mock.MagicMock, provider.api)
service = fixtures.create_service_linked(provider=provider)
userservice = fixtures.create_userservice_linked(service=service)
publication = userservice.publication()
publication._vmid = '1'
publication._vmid = str(vm.id)
state = userservice.deploy_for_user(models.User())
@ -183,7 +187,7 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
vmid = int(userservice._vmid)
api.clone_vm.assert_called_with(
publication.machine(),
int(publication.get_template_id()),
mock.ANY,
userservice._name,
mock.ANY,
@ -220,6 +224,7 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
"""
Test the user service
"""
vm = random.choice(fixtures.VMINFO_LIST)
with fixtures.patched_provider() as provider:
api = typing.cast(mock.MagicMock, provider.api)
for graceful in [True, False]:
@ -227,12 +232,12 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
userservice = fixtures.create_userservice_linked(service=service)
service.try_soft_shutdown.value = graceful
publication = userservice.publication()
publication._vmid = '1'
publication._vmid = str(vm.id)
service.must_stop_before_deletion = False # Avoid stopping before deletion, not needed for this test
# Set machine state for fixture to started
for vminfo in fixtures.VMS_INFO:
for vminfo in fixtures.VMINFO_LIST:
vminfo.status = prox_types.VMStatus.RUNNING
state = userservice.deploy_for_user(models.User())
@ -280,7 +285,7 @@ class TestProxmoxLinkedUserService(UDSTransactionTestCase):
state = userservice.check_state()
if counter > 5:
# Set machine state for fixture to stopped
for vminfo in fixtures.VMS_INFO:
for vminfo in fixtures.VMINFO_LIST:
vminfo.status = prox_types.VMStatus.STOPPED
self.assertEqual(state, types.states.TaskState.FINISHED, f'Extra info: {userservice._error_debug_info} {userservice._reason} {userservice._queue}')

View File

@ -152,12 +152,12 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
data['exec_count'] = 0
@typing.final
def _inc_checks_counter(self, info: typing.Optional[str] = None) -> typing.Optional[types.states.TaskState]:
def _inc_checks_counter(self, op: types.services.Operation) -> typing.Optional[types.states.TaskState]:
with self.storage.as_dict() as data:
count = data.get('exec_count', 0) + 1
data['exec_count'] = count
if count > self.max_state_checks:
return self.error(f'Max checks reached on {info or "unknown"}')
return self.error(f'Max checks reached on {op}')
return None
@typing.final
@ -450,7 +450,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
if op != types.services.Operation.WAIT:
# All operations except WAIT will check against checks counter
counter_state = self._inc_checks_counter(self._op2str(op))
counter_state = self._inc_checks_counter(op)
if counter_state is not None:
return counter_state # Error, Finished or None (eror can return Finished too)

View File

@ -182,7 +182,7 @@ class ProxmoxUserserviceLinked(DynamicUserService):
# No need for op_reset_checker
def op_create(self) -> None:
template_id = self.publication().machine()
template_id = int(self.publication().get_template_id())
name = self.get_name()
if name == consts.NO_MORE_NAMES:
raise Exception(

View File

@ -126,6 +126,3 @@ class ProxmoxPublication(DynamicPublication, autoserializable.AutoSerializable):
def op_delete(self) -> None:
self.service().delete(self, self._vmid)
def machine(self) -> int:
return int(self._vmid)

View File

@ -233,22 +233,6 @@ class ProxmoxServiceLinked(DynamicService):
def get_vm_info(self, vmid: int) -> 'prox_types.VMInfo':
return self.provider().api.get_vm_pool_info(vmid, self.pool.value.strip())
def get_nic_mac(self, vmid: int) -> str:
config = self.provider().api.get_vm_config(vmid)
return config.networks[0].mac.lower()
# TODO: Remove this method, kept for reference of old code
def _xremove_machine(self, vmid: int) -> 'prox_types.UPID':
# First, remove from HA if needed
try:
self.disable_vm_ha(vmid)
except Exception as e:
logger.warning('Exception disabling HA for vm %s: %s', vmid, e)
self.do_log(level=types.log.LogLevel.WARNING, message=f'Exception disabling HA for vm {vmid}: {e}')
# And remove it
return self.provider().api.delete_vm(vmid)
def enable_vm_ha(self, vmid: int, started: bool = False) -> None:
if self.ha.value == '__':
return
@ -281,7 +265,7 @@ class ProxmoxServiceLinked(DynamicService):
# If vmid is empty, we are requesting a new mac
if not vmid:
return self.mac_generator().get(self.get_macs_range())
return self.get_nic_mac(int(vmid))
return self.provider().api.get_vm_config(int(vmid)).networks[0].mac.lower()
def start(self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str) -> None:
if isinstance(caller_instance, ProxmoxUserserviceLinked):