mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-01 16:36:06 +00:00
fix(instances): a failed power cycle says the VM stays off; templates have no autostart
Power Cycle forced the VM off and, when the start failed, showed only libvirt's error: the page now also says the VM was forced off and stays off, and the audit row says so. A template kept the autostart it had as a VM, so the host started it although the panel refuses to: marking a template turns autostart off, and autostart is refused on a template. The autostart switch is disabled, with the reason, on a template and on a VM that exists only while it runs (libvirt refuses autostart there). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
0ce1139c9d
commit
7dd68aaca4
10 changed files with 259 additions and 196 deletions
|
|
@ -51,7 +51,7 @@ class PageControls:
|
|||
UserInstance.objects.create(instance=self.instance, user=user, **owner)
|
||||
return user
|
||||
|
||||
def controls(self, user, status=1, saved=False):
|
||||
def controls(self, user, status=1, saved=False, persistent=True):
|
||||
self.client.force_login(user)
|
||||
with patch("instances.models.wvmInstance") as mock_wvm, patch.object(
|
||||
Compute, "status", new_callable=PropertyMock, return_value=True
|
||||
|
|
@ -76,6 +76,8 @@ class PageControls:
|
|||
proxy.get_console_type.return_value = "vnc"
|
||||
proxy.get_status.return_value = status
|
||||
proxy.get_managed_save_image.return_value = saved
|
||||
proxy.get_autostart.return_value = 0
|
||||
proxy.instance.isPersistent.return_value = persistent
|
||||
response = self.client.get(reverse("instances:instance", args=[self.instance.id]))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.last_page = response.content.decode()
|
||||
|
|
@ -236,6 +238,15 @@ class PageControlsTestCase(PageControls, TestCase):
|
|||
self.assertTrue(fresh.get("data-confirm"))
|
||||
self.assertEqual(fresh.getparent().get("action"), reverse("instances:poweron", args=[self.instance.id]))
|
||||
|
||||
def test_autostart_is_off_for_a_template_and_a_vm_defined_only_while_it_runs(self):
|
||||
admin = get_user_model().objects.create_superuser("autostart_admin", "as@example.com", "x")
|
||||
for template, persistent, disabled in ((False, True, False), (True, True, True), (False, False, True)):
|
||||
with self.subTest(template=template, persistent=persistent):
|
||||
Instance.objects.filter(pk=self.instance.pk).update(is_template=template)
|
||||
self.controls(admin, status=5, persistent=persistent)
|
||||
(switch,) = lxml_html.fromstring(self.last_page).xpath("//input[@id='autostart']")
|
||||
self.assertEqual(switch.get("disabled") is not None, disabled)
|
||||
|
||||
def test_the_power_tab_marks_the_current_state_or_names_another(self):
|
||||
admin = get_user_model().objects.create_superuser("power_strip_admin", "ps@example.com", "x")
|
||||
for status, now in ((1, "Active"), (3, "Suspended"), (5, "Off"), (6, None)):
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ class TemplateFlagTestCase(TestCase):
|
|||
for user in (self.owner, self.staff_owner):
|
||||
UserInstance.objects.create(instance=self.instance, user=user, is_change=True)
|
||||
|
||||
def _post(self, user, action, data, status=302):
|
||||
def _post(self, user, action, data, status=302, autostart=0):
|
||||
self.client.force_login(user)
|
||||
with patch("instances.models.wvmInstance") as wvm:
|
||||
wvm.return_value.get_status.return_value = 5
|
||||
|
|
@ -95,9 +95,11 @@ class TemplateFlagTestCase(TestCase):
|
|||
wvm.return_value.get_vcpu.return_value = 1
|
||||
wvm.return_value.get_memory.return_value = 1024
|
||||
wvm.return_value.get_disk_devices.return_value = []
|
||||
wvm.return_value.get_autostart.return_value = autostart
|
||||
res = self.client.post(reverse(f"instances:{action}", args=[self.instance.id]), data)
|
||||
self.assertEqual(res.status_code, status)
|
||||
self.instance.refresh_from_db()
|
||||
return wvm.return_value
|
||||
|
||||
def _make_template(self):
|
||||
self.instance.is_template = True
|
||||
|
|
@ -122,6 +124,13 @@ class TemplateFlagTestCase(TestCase):
|
|||
self.assertEqual(list(Logs.objects.filter(message__startswith="VM").order_by("id").values_list("message", "success")),
|
||||
[("VM tpl-vm: mark as template", True), ("VM tpl-vm: unmark as template", True)])
|
||||
|
||||
def test_a_template_does_not_start_with_the_host(self):
|
||||
proxy = self._post(self.superuser, "set_template", {"is_template": "True"}, autostart=1)
|
||||
self.assertTrue(self.instance.is_template)
|
||||
proxy.set_autostart.assert_called_once_with(0)
|
||||
proxy = self._post(self.superuser, "set_autostart", {})
|
||||
proxy.set_autostart.assert_not_called()
|
||||
|
||||
def test_superuser_can_unmark_template(self):
|
||||
self._make_template()
|
||||
self._post(self.superuser, "set_template", {})
|
||||
|
|
|
|||
|
|
@ -48,6 +48,15 @@ class VMRowTests(TestCase):
|
|||
self.proxy.start.assert_called_once_with(libvirt.VIR_DOMAIN_START_FORCE_BOOT)
|
||||
self.assertEqual(self.rows(), [("VM vm: power on without the saved state", True)])
|
||||
|
||||
def test_a_power_cycle_that_does_not_start_again_says_the_vm_stays_off(self):
|
||||
self.proxy.start.side_effect = libvirtError("Cannot access storage file")
|
||||
response = self.post("powercycle")
|
||||
self.proxy.force_shutdown.assert_called_once_with()
|
||||
self.assertEqual(self.rows(), [("VM vm: power cycle: forced off, the start failed: Cannot access storage file",
|
||||
False)])
|
||||
self.assertIn("The VM was forced off but did not start again: it stays off.",
|
||||
[m.message for m in get_messages(response.wsgi_request)])
|
||||
|
||||
def test_a_raised_failure_writes_a_failed_row(self):
|
||||
self.proxy.start.side_effect = libvirtError("Requested operation is not valid")
|
||||
self.post("poweron")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue