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
19
from lxml import etree
21
from nova.api.openstack import v2
22
from nova.api.openstack.v2.contrib import server_diagnostics
23
from nova.api.openstack.v2 import extensions
24
from nova.api.openstack import wsgi
27
from nova.tests.api.openstack import fakes
31
def fake_get_diagnostics(self, _context, instance_uuid):
32
return {'data': 'Some diagnostic info'}
35
def fake_instance_get(self, _context, instance_uuid):
36
return {'uuid': instance_uuid}
39
class ServerDiagnosticsTest(test.TestCase):
42
super(ServerDiagnosticsTest, self).setUp()
43
self.flags(allow_admin_api=True)
44
self.flags(verbose=True)
45
self.stubs.Set(nova.compute.API, 'get_diagnostics',
47
self.stubs.Set(nova.compute.API, 'get', fake_instance_get)
48
self.compute_api = nova.compute.API()
50
self.router = v2.APIRouter()
51
ext_middleware = extensions.ExtensionMiddleware(self.router)
52
self.app = wsgi.LazySerializationMiddleware(ext_middleware)
54
def test_get_diagnostics(self):
55
uuid = nova.utils.gen_uuid()
56
req = fakes.HTTPRequest.blank('/fake/servers/%s/diagnostics' % uuid)
57
res = req.get_response(self.app)
58
output = json.loads(res.body)
59
self.assertEqual(output, {'data': 'Some diagnostic info'})
62
class TestServerDiagnosticsXMLSerializer(unittest.TestCase):
63
namespace = wsgi.XMLNS_V11
67
self.assertEqual(tagname[0], '{')
68
tmp = tagname.partition('}')
69
namespace = tmp[0][1:]
70
self.assertEqual(namespace, self.namespace)
73
def test_index_serializer(self):
74
serializer = server_diagnostics.ServerDiagnosticsTemplate()
75
exemplar = dict(diag1='foo', diag2='bar')
76
text = serializer.serialize(exemplar)
79
tree = etree.fromstring(text)
81
self.assertEqual('diagnostics', self._tag(tree))
82
self.assertEqual(len(tree), len(exemplar))
84
tag = self._tag(child)
85
self.assertTrue(tag in exemplar)
86
self.assertEqual(child.text, exemplar[tag])