|
| 1 | +# Copyright 2025 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 | +from unittest import mock |
| 18 | +import uuid as sys_uuid |
| 19 | + |
| 20 | +from restalchemy.storage.sql import orm |
| 21 | + |
| 22 | +from exordos_core.compute import constants as nc |
| 23 | +from exordos_core.compute.builders import node as node_builder |
| 24 | +from exordos_core.compute.dm import models as compute_models |
| 25 | + |
| 26 | + |
| 27 | +class TestNodeBuilderService: |
| 28 | + def test_update_machine_sets_node_status_to_in_progress( |
| 29 | + self, |
| 30 | + monkeypatch, |
| 31 | + ): |
| 32 | + service = node_builder.NodeBuilderService() |
| 33 | + node_uuid = sys_uuid.uuid4() |
| 34 | + |
| 35 | + target_node = mock.MagicMock() |
| 36 | + target_node.uuid = node_uuid |
| 37 | + target_node.cores = 2 |
| 38 | + target_node.ram = 1024 |
| 39 | + target_node.name = "target-name" |
| 40 | + target_node.description = "target-desc" |
| 41 | + target_node.hostname = "host-a" |
| 42 | + target_node.status = nc.NodeStatus.ACTIVE.value |
| 43 | + |
| 44 | + actual_node = mock.MagicMock() |
| 45 | + actual_node.uuid = node_uuid |
| 46 | + actual_node.cores = 4 |
| 47 | + actual_node.ram = 2048 |
| 48 | + actual_node.name = "actual-name" |
| 49 | + actual_node.description = "actual-desc" |
| 50 | + actual_node.hostname = "host-b" |
| 51 | + |
| 52 | + machine = mock.MagicMock() |
| 53 | + machine.cores = 1 |
| 54 | + machine.ram = 512 |
| 55 | + machine.name = "old-name" |
| 56 | + machine.description = "old-desc" |
| 57 | + |
| 58 | + def fake_get_one_or_none(self, *args, **kwargs): |
| 59 | + if self.model_cls is compute_models.Machine: |
| 60 | + return machine |
| 61 | + raise NotImplementedError |
| 62 | + |
| 63 | + monkeypatch.setattr( |
| 64 | + orm.ObjectCollection, "get_one_or_none", fake_get_one_or_none |
| 65 | + ) |
| 66 | + |
| 67 | + service._update_machine(target_node, actual_node) |
| 68 | + |
| 69 | + assert machine.cores == target_node.cores |
| 70 | + assert machine.ram == target_node.ram |
| 71 | + assert machine.name == target_node.name |
| 72 | + assert machine.description == target_node.description |
| 73 | + assert machine.status == nc.MachineStatus.IN_PROGRESS.value |
| 74 | + machine.update.assert_called_once_with(force=True) |
| 75 | + assert target_node.status == nc.NodeStatus.IN_PROGRESS.value |
| 76 | + target_node.save.assert_called_once_with() |
0 commit comments