fix: do not spread unplaced VMs once a VM of the cluster is pinned

As soon as any VM of a cluster (in any status) is assigned to one of its
hosts, the placement is treated as managed by hand: VMs without a host
are no longer spread over the hosts and only count towards the cluster
totals. Cluster panel and host tab say which case applies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-29 11:12:15 +01:00
1 parent afa15eb46a
commit 8aa8d3fe1e
6 files changed
+65 -23

No files matched your search

+29 -13
View File
@@ -124,19 +124,35 @@ class UnplacedVMTest(SimpleTestCase):
hardware.Host("small", cores=16, threads=32, memory_gb=256, key=1),
hardware.Host("big", cores=48, threads=96, memory_gb=768, key=2),
]
vms = [(None, 16, 100 * 1024, "active"), (1, 4, 8 * 1024, "active")]
vms = [(None, 16, 100 * 1024, "active"), (None, 4, 8 * 1024, "active")]
capacity = hardware.build_capacity(hosts, vms)
small, big = capacity.hosts
self.assertEqual(capacity.unplaced_vm_count, 1)
self.assertAlmostEqual(small.shared_vcpus, 4)
self.assertAlmostEqual(big.shared_vcpus, 12)
self.assertAlmostEqual(small.shared_memory_gb, 25)
self.assertAlmostEqual(big.shared_memory_gb, 75)
self.assertAlmostEqual(small.total_vcpus, 8)
self.assertAlmostEqual(small.total_vm_memory_gb, 33)
self.assertTrue(capacity.distributed)
self.assertEqual(capacity.unplaced_vm_count, 2)
self.assertAlmostEqual(small.shared_vcpus, 5)
self.assertAlmostEqual(big.shared_vcpus, 15)
self.assertAlmostEqual(small.shared_memory_gb, 27)
self.assertAlmostEqual(big.shared_memory_gb, 81)
# The cluster sums count every VM exactly once.
self.assertEqual((capacity.vcpus, capacity.vm_memory_gb), (20, 108))
def test_no_spreading_once_a_vm_is_pinned(self):
for status in ("active", "offline"):
with self.subTest(pinned_vm_status=status):
hosts = [
hardware.Host("a", cores=32, threads=64, memory_gb=512, key=1),
hardware.Host("b", cores=32, threads=64, memory_gb=512, key=2),
]
vms = [(None, 16, 64 * 1024, "active"), (2, 4, 8 * 1024, status)]
capacity = hardware.build_capacity(hosts, vms)
self.assertTrue(capacity.manual_placement)
self.assertFalse(capacity.distributed)
self.assertEqual([host.shared_vcpus for host in capacity.hosts], [0, 0])
self.assertEqual(capacity.unplaced_vm_count, 1)
self.assertEqual(capacity.vcpus, 20 if status == "active" else 16)
single = hardware.host_capacity(capacity, capacity.hosts[0])
self.assertEqual(single.vcpus, 0)
def test_equal_shares_without_recorded_hardware(self):
hosts = [hardware.Host("a", key=1), hardware.Host("b", key=2)]
capacity = hardware.build_capacity(hosts, [(None, 8, 16 * 1024, "active")])
@@ -148,12 +164,12 @@ class UnplacedVMTest(SimpleTestCase):
hardware.Host("a", cores=32, threads=64, memory_gb=512, key=1),
hardware.Host("b", cores=32, threads=64, memory_gb=512, key=2),
]
cluster = hardware.build_capacity(hosts, [(None, 32, 512 * 1024, "active"), (1, 8, 64 * 1024, "active")])
cluster = hardware.build_capacity(hosts, [(None, 32, 512 * 1024, "active")])
single = hardware.host_capacity(cluster, cluster.hosts[0])
self.assertTrue(single.host_share)
self.assertEqual((single.vm_count, single.vcpus, single.vm_memory_gb), (1, 24, 320))
self.assertEqual(single.memory_percent, 62)
self.assertEqual(single.vcpu_per_core, 0.75)
self.assertTrue(single.host_share and single.distributed)
self.assertEqual((single.vm_count, single.vcpus, single.vm_memory_gb), (0, 16, 256))
self.assertEqual(single.memory_percent, 50)
self.assertEqual(single.vcpu_per_core, 0.5)
def test_hosts_are_matched_by_key_not_name(self):
hosts = [hardware.Host("esx", key=1), hardware.Host("esx", key=2)]