Fix HTTP 500 raised for getConsoleLog for stopped instance

Stopped instances with pty console will not contain
`source_node` information, and in the current
implementation the pty variable used later will
result in an UnboundLocalError, which results in a
500 error out of the API.

Closes-bug: #1680363

Change-Id: I4dffba959e2292254dc757f22c3f7893d2da72f9
(cherry picked from commit 0c2b73e801)
This commit is contained in:
Kevin_Zheng 2017-04-10 17:28:33 +08:00 committed by Matt Riedemann
parent 6d4f9f4f6c
commit 1c6eab183f
2 changed files with 26 additions and 0 deletions

View File

@ -11078,6 +11078,30 @@ class LibvirtConnTestCase(test.NoDBTestCase):
self.assertEqual(b'67890', output)
def test_get_console_output_pty_not_available(self):
instance = objects.Instance(**self.test_instance)
fake_dom_xml = """
<domain type='kvm'>
<devices>
<disk type='file'>
<source file='filename'/>
</disk>
<console type='pty'>
<target port='0'/>
</console>
</devices>
</domain>
"""
def fake_lookup(id):
return FakeVirtDomain(fake_dom_xml)
self.create_fake_libvirt_mock()
libvirt_driver.LibvirtDriver._conn.lookupByName = fake_lookup
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
self.assertRaises(exception.ConsoleNotAvailable,
drvr.get_console_output, self.context, instance)
@mock.patch('nova.virt.libvirt.host.Host.get_domain')
@mock.patch.object(libvirt_guest.Guest, "get_xml_desc")
def test_get_console_output_not_available(self, mock_get_xml, get_domain):

View File

@ -2783,6 +2783,8 @@ class LibvirtDriver(driver.ComputeDriver):
if not pty:
continue
break
else:
raise exception.ConsoleNotAvailable()
else:
raise exception.ConsoleNotAvailable()