1
# Copyright 2011 Eldar Nugaev
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
20
from nova import compute
21
from nova import exception
23
from nova.tests.api.openstack import fakes
26
def fake_get_console_output(self, _context, _instance, tail_length):
27
fixture = [str(i) for i in range(5)]
29
if tail_length is None:
31
elif tail_length == 0:
34
fixture = fixture[-int(tail_length):]
36
return '\n'.join(fixture)
39
def fake_get(self, context, instance_uuid):
40
return {'uuid': instance_uuid}
43
def fake_get_not_found(self, context, instance_uuid):
44
raise exception.NotFound()
47
class ConsoleOutputExtensionTest(test.TestCase):
50
super(ConsoleOutputExtensionTest, self).setUp()
51
self.stubs.Set(compute.API, 'get_console_output',
52
fake_get_console_output)
53
self.stubs.Set(compute.API, 'get', fake_get)
55
def test_get_text_console_instance_action(self):
56
body = {'os-getConsoleOutput': {}}
57
req = webob.Request.blank('/v2/fake/servers/1/action')
59
req.body = json.dumps(body)
60
req.headers["content-type"] = "application/json"
62
res = req.get_response(fakes.wsgi_app())
63
output = json.loads(res.body)
64
self.assertEqual(res.status_int, 200)
65
self.assertEqual(output, {'output': '0\n1\n2\n3\n4'})
67
def test_get_console_output_with_tail(self):
68
body = {'os-getConsoleOutput': {'length': 3}}
69
req = webob.Request.blank('/v2/fake/servers/1/action')
71
req.body = json.dumps(body)
72
req.headers["content-type"] = "application/json"
73
res = req.get_response(fakes.wsgi_app())
74
output = json.loads(res.body)
75
self.assertEqual(res.status_int, 200)
76
self.assertEqual(output, {'output': '2\n3\n4'})
78
def test_get_text_console_no_instance(self):
79
self.stubs.Set(compute.API, 'get', fake_get_not_found)
80
body = {'os-getConsoleOutput': {}}
81
req = webob.Request.blank('/v2/fake/servers/1/action')
83
req.body = json.dumps(body)
84
req.headers["content-type"] = "application/json"
86
res = req.get_response(fakes.wsgi_app())
87
self.assertEqual(res.status_int, 404)
89
def test_get_text_console_bad_body(self):
91
req = webob.Request.blank('/v2/fake/servers/1/action')
93
req.body = json.dumps(body)
94
req.headers["content-type"] = "application/json"
96
res = req.get_response(fakes.wsgi_app())
97
self.assertEqual(res.status_int, 400)