|
| 1 | +# Copyright 2026 Genesis Corporation. |
| 2 | +# |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +import typing as tp |
| 18 | +import uuid as sys_uuid |
| 19 | + |
| 20 | +from gcl_sdk.infra.dm import models as sdk_models |
| 21 | +import pytest |
| 22 | +from restalchemy.dm import filters as dm_filters |
| 23 | + |
| 24 | +from exordos_core.common import constants as c |
| 25 | +from exordos_core.compute.builders import node as node_builder |
| 26 | +from exordos_core.compute.dm import models |
| 27 | + |
| 28 | + |
| 29 | +class TestNodeBuilderService: |
| 30 | + def setup_method(self) -> None: |
| 31 | + self._service = node_builder.NodeBuilderService() |
| 32 | + |
| 33 | + def _add_node(self, disks: tp.List[dict]) -> models.Node: |
| 34 | + node = models.Node( |
| 35 | + uuid=sys_uuid.uuid4(), |
| 36 | + name="foo-node", |
| 37 | + cores=1, |
| 38 | + ram=1024, |
| 39 | + disk_spec=sdk_models.DisksSpec(disks=disks), |
| 40 | + project_id=c.SERVICE_PROJECT_ID, |
| 41 | + ) |
| 42 | + node.insert() |
| 43 | + return node |
| 44 | + |
| 45 | + def _node_copy_with_disks( |
| 46 | + self, node: models.Node, disks: tp.List[dict] |
| 47 | + ) -> models.Node: |
| 48 | + return models.Node( |
| 49 | + uuid=node.uuid, |
| 50 | + name=node.name, |
| 51 | + cores=node.cores, |
| 52 | + ram=node.ram, |
| 53 | + disk_spec=sdk_models.DisksSpec(disks=disks), |
| 54 | + project_id=node.project_id, |
| 55 | + ) |
| 56 | + |
| 57 | + def _node_volumes(self, node_uuid: sys_uuid.UUID) -> tp.List[models.Volume]: |
| 58 | + return list( |
| 59 | + models.Volume.objects.get_all( |
| 60 | + filters={"node": dm_filters.EQ(node_uuid)}, |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + @pytest.mark.usefixtures("user_api_client", "auth_user_admin") |
| 65 | + def test_add_and_remove_extra_disk(self): |
| 66 | + root_disk = {"size": 8, "image": "ubuntu_24.04"} |
| 67 | + data_disk = {"size": 20, "label": "data"} |
| 68 | + |
| 69 | + actual_node = self._add_node(disks=[root_disk]) |
| 70 | + volumes = self._node_volumes(actual_node.uuid) |
| 71 | + assert len(volumes) == 1 |
| 72 | + |
| 73 | + # Add a data disk to the node |
| 74 | + target_with_data = self._node_copy_with_disks( |
| 75 | + actual_node, disks=[root_disk, data_disk] |
| 76 | + ) |
| 77 | + self._service._update_volumes(target_with_data, actual_node) |
| 78 | + |
| 79 | + volumes = self._node_volumes(actual_node.uuid) |
| 80 | + assert len(volumes) == 2 |
| 81 | + added_volume = next(v for v in volumes if v.label == "data") |
| 82 | + assert added_volume.size == 20 |
| 83 | + assert added_volume.project_id == actual_node.volume_project_id |
| 84 | + |
| 85 | + # Remove the data disk from the node |
| 86 | + target_without_data = self._node_copy_with_disks(actual_node, disks=[root_disk]) |
| 87 | + self._service._update_volumes(target_without_data, target_with_data) |
| 88 | + |
| 89 | + volumes = self._node_volumes(actual_node.uuid) |
| 90 | + assert len(volumes) == 1 |
| 91 | + assert volumes[0].label != "data" |
| 92 | + |
| 93 | + volumes[0].delete() |
| 94 | + actual_node.delete() |
0 commit comments