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 lxml import etree
22
from nova.api.openstack import compute
23
from nova.api.openstack.compute import extensions
24
from nova.api.openstack.compute.contrib import server_action_list
25
from nova.api.openstack import wsgi
28
from nova.tests.api.openstack import fakes
32
dt = datetime.datetime.utcnow()
35
def fake_get_actions(self, _context, instance_uuid):
37
{'action': 'rebuild', 'error': None, 'created_at': dt},
38
{'action': 'reboot', 'error': 'Failed!', 'created_at': dt},
42
def fake_instance_get(self, _context, instance_uuid):
43
return {'uuid': instance_uuid}
46
class ServerActionsTest(test.TestCase):
49
super(ServerActionsTest, self).setUp()
50
self.flags(verbose=True)
51
self.stubs.Set(nova.compute.API, 'get_actions', fake_get_actions)
52
self.stubs.Set(nova.compute.API, 'get', fake_instance_get)
54
self.router = compute.APIRouter()
56
def test_get_actions(self):
57
uuid = nova.utils.gen_uuid()
58
req = fakes.HTTPRequest.blank('/fake/servers/%s/actions' % uuid)
59
res = req.get_response(self.router)
60
output = json.loads(res.body)
61
expected = {'actions': [
62
{'action': 'rebuild', 'error': None, 'created_at': str(dt)},
63
{'action': 'reboot', 'error': 'Failed!', 'created_at': str(dt)},
65
self.assertEqual(output, expected)
68
class TestServerActionsXMLSerializer(unittest.TestCase):
69
namespace = wsgi.XMLNS_V11
73
self.assertEqual(tagname[0], '{')
74
tmp = tagname.partition('}')
75
namespace = tmp[0][1:]
76
self.assertEqual(namespace, self.namespace)
79
def test_index_serializer(self):
80
serializer = server_action_list.ServerActionsTemplate()
82
created_at=datetime.datetime.now(),
86
created_at=datetime.datetime.now(),
89
text = serializer.serialize(dict(actions=exemplar))
92
tree = etree.fromstring(text)
94
self.assertEqual('actions', self._tag(tree))
95
self.assertEqual(len(tree), len(exemplar))
96
for idx, child in enumerate(tree):
97
self.assertEqual('action', self._tag(child))
98
for field in ('created_at', 'action', 'error'):
99
self.assertEqual(str(exemplar[idx][field]), child.get(field))