Skip to content

Commit

Permalink
Fix os-console-output extension integration
Browse files Browse the repository at this point in the history
Fixes bug 907083

Change-Id: Ia57d316db0c79d7e78ef3225e77cd95589ac68df
  • Loading branch information
Brian Waldon committed Dec 20, 2011
1 parent 733433e commit 47c4c49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
14 changes: 12 additions & 2 deletions nova/api/openstack/v2/contrib/console_output.py
Expand Up @@ -42,10 +42,20 @@ def __init__(self, ext_mgr):
def get_console_output(self, input_dict, req, server_id):
"""Get text console output."""
context = req.environ['nova.context']
length = input_dict['os-getConsoleOutput'].get('length')

try:
instance = self.compute_api.routing_get(context, server_id)
except exception.NotFound:
raise webob.exc.HTTPNotFound(_('Instance not found'))

try:
length = input_dict['os-getConsoleOutput'].get('length')
except (TypeError, KeyError):
raise webob.exc.HTTPBadRequest(_('Malformed request body'))

try:
return self.compute_api.get_console_output(context,
server_id,
instance,
length)
except exception.ApiError, e:
raise webob.exc.HTTPBadRequest(explanation=e.message)
Expand Down
44 changes: 34 additions & 10 deletions nova/tests/api/openstack/v2/contrib/test_console_output.py
Expand Up @@ -18,12 +18,12 @@
import webob

from nova import compute
from nova import exception
from nova import test
from nova.tests.api.openstack import fakes


def fake_text_console_tail(self, method, context, instance_id, params):
tail_length = params['tail_length']
def fake_get_console_output(self, _context, _instance, tail_length):
fixture = [str(i) for i in range(10)]

if tail_length is None:
Expand All @@ -36,15 +36,23 @@ def fake_text_console_tail(self, method, context, instance_id, params):
return '\n'.join(fixture)


def fake_get(self, context, instance_uuid):
return {'uuid': instance_uuid}


def fake_get_not_found(self, context, instance_uuid):
raise exception.NotFound()


class ConsoleOutputExtensionTest(test.TestCase):

def setUp(self):
super(ConsoleOutputExtensionTest, self).setUp()
self.stubs.Set(compute.API, 'get_console_output',
fake_get_console_output)
self.stubs.Set(compute.API, 'get', fake_get)

def test_get_text_console_instance_action(self):
self.stubs.Set(compute.API, '_call_compute_message',
fake_text_console_tail)

body = {'os-getConsoleOutput': {}}
req = webob.Request.blank('/v1.1/123/servers/1/action')
req.method = "POST"
Expand All @@ -55,15 +63,31 @@ def test_get_text_console_instance_action(self):
self.assertEqual(res.status_int, 200)

def test_get_console_output_with_tail(self):
self.stubs.Set(compute.API,
'_call_compute_message',
fake_text_console_tail)

body = {'os-getConsoleOutput': {'length': 3}}
req = webob.Request.blank('/v2/123/servers/1/action')
req.method = "POST"
req.body = json.dumps(body)
req.headers["content-type"] = "application/json"
res = req.get_response(fakes.wsgi_app())

self.assertEqual(res.status_int, 200)

def test_get_text_console_no_instance(self):
self.stubs.Set(compute.API, 'get', fake_get_not_found)
body = {'os-getConsoleOutput': {}}
req = webob.Request.blank('/v1.1/123/servers/1/action')
req.method = "POST"
req.body = json.dumps(body)
req.headers["content-type"] = "application/json"

res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 404)

def test_get_text_console_bad_body(self):
body = {}
req = webob.Request.blank('/v1.1/123/servers/1/action')
req.method = "POST"
req.body = json.dumps(body)
req.headers["content-type"] = "application/json"

res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 400)

0 comments on commit 47c4c49

Please sign in to comment.