diff --git a/conf/daemon/consolecallback b/conf/daemon/consolecallback index dfb66130..0f396fb0 100755 --- a/conf/daemon/consolecallback +++ b/conf/daemon/consolecallback @@ -47,7 +47,12 @@ class Console: def check(self): """Open the stream while the VM runs, drop it while it does not. The stream may have ended first: the VM's stop is still reported.""" - active = self.domain.state()[0] in (libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_PAUSED) + state, reason = self.domain.state() + # a VM being started is paused until its devices are set up: its + # console has no terminal yet + active = state == libvirt.VIR_DOMAIN_RUNNING or ( + state == libvirt.VIR_DOMAIN_PAUSED and reason != libvirt.VIR_DOMAIN_PAUSED_STARTING_UP + ) if active and self.stream is None: stream = self.connection.newStream(libvirt.VIR_STREAM_NONBLOCK) try: diff --git a/console/tests/test_consolecallback.py b/console/tests/test_consolecallback.py index 5e6b62ad..e6b7238e 100644 --- a/console/tests/test_consolecallback.py +++ b/console/tests/test_consolecallback.py @@ -66,7 +66,7 @@ class ConsoleWorkerTests(TestCase): console.domain = MagicMock() console.stream = None # from_vm dropped it at the stream's end console.active = True - console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_SHUTOFF] + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_SHUTOFF, 0] with patch.object(self.cb, "say") as say: console.check() console.check() @@ -77,7 +77,7 @@ class ConsoleWorkerTests(TestCase): console.domain, console.connection = MagicMock(), MagicMock() console.stream = None console.active = True - console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_RUNNING] + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_RUNNING, 0] console.domain.openConsole.side_effect = self.cb.libvirt.libvirtError("domain is not running") console.domain.isActive.return_value = 0 with patch.object(self.cb, "say") as say: @@ -85,6 +85,21 @@ class ConsoleWorkerTests(TestCase): say.assert_called_once_with("The VM is not running: its console opens again when it starts") self.assertIsNone(console.stream) + def test_a_vm_being_started_is_waited_for(self): + # its terminal is not assigned yet: opening its console then fails + console = self.cb.Console.__new__(self.cb.Console) + console.domain, console.connection = MagicMock(), MagicMock() + console.stream = None + console.active = False + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_PAUSED, + self.cb.libvirt.VIR_DOMAIN_PAUSED_STARTING_UP] + console.check() + console.domain.openConsole.assert_not_called() + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_PAUSED, self.cb.libvirt.VIR_DOMAIN_PAUSED_USER] + console.check() + console.domain.openConsole.assert_called_once() + self.assertTrue(console.active) + def test_a_closed_client_ends_the_worker(self): with patch.object(self.cb.os, "read", return_value=b""): self.cb.from_client(None, 0, None, self.console)