1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Justin Santa Barbara
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
21
from nova import flags
23
from nova.log import logging
24
from nova.tests.integrated import integrated_helpers
25
from nova.tests.integrated.api import client
28
LOG = logging.getLogger('nova.tests.integrated')
34
class ServersTest(test.TestCase):
36
super(ServersTest, self).setUp()
38
self.flags(image_service='nova.image.fake.MockImageService')
40
context = integrated_helpers.IntegratedUnitTestContext.startup()
41
self.user = context.test_user
42
self.api = self.user.openstack_api
45
integrated_helpers.IntegratedUnitTestContext.shutdown()
46
super(ServersTest, self).tearDown()
48
def test_get_servers(self):
49
"""Simple check that listing servers works."""
50
servers = self.api.get_servers()
51
for server in servers:
52
LOG.debug("server: %s" % server)
54
def test_create_and_delete_server(self):
55
"""Creates and deletes a server"""
59
# Build the server data gradually, checking errors along the way
61
good_server = self._build_minimal_create_server_request()
63
post = {'server': server}
65
# Without an imageId, this throws 500.
66
# TODO(justinsb): Check whatever the spec says should be thrown here
67
self.assertRaises(client.OpenStackApiException,
68
self.api.post_server, post)
70
# With an invalid imageId, this throws 500.
71
server['imageId'] = self.user.get_invalid_image()
72
# TODO(justinsb): Check whatever the spec says should be thrown here
73
self.assertRaises(client.OpenStackApiException,
74
self.api.post_server, post)
77
server['imageId'] = good_server['imageId']
79
# Without flavorId, this throws 500
80
# TODO(justinsb): Check whatever the spec says should be thrown here
81
self.assertRaises(client.OpenStackApiException,
82
self.api.post_server, post)
84
# Set a valid flavorId
85
server['flavorId'] = good_server['flavorId']
87
# Without a name, this throws 500
88
# TODO(justinsb): Check whatever the spec says should be thrown here
89
self.assertRaises(client.OpenStackApiException,
90
self.api.post_server, post)
92
# Set a valid server name
93
server['name'] = good_server['name']
95
created_server = self.api.post_server(post)
96
LOG.debug("created_server: %s" % created_server)
97
self.assertTrue(created_server['id'])
98
created_server_id = created_server['id']
101
found_server = self.api.get_server(created_server_id)
102
self.assertEqual(created_server_id, found_server['id'])
104
# It should also be in the all-servers list
105
servers = self.api.get_servers()
106
server_ids = [server['id'] for server in servers]
107
self.assertTrue(created_server_id in server_ids)
109
# Wait (briefly) for creation
111
while found_server['status'] == 'build':
112
LOG.debug("found server: %s" % found_server)
114
found_server = self.api.get_server(created_server_id)
115
retries = retries + 1
119
# It should be available...
120
# TODO(justinsb): Mock doesn't yet do this...
121
#self.assertEqual('available', found_server['status'])
123
self._delete_server(created_server_id)
125
def _delete_server(self, server_id):
127
self.api.delete_server(server_id)
129
# Wait (briefly) for deletion
130
for _retries in range(5):
132
found_server = self.api.get_server(server_id)
133
except client.OpenStackApiNotFoundException:
135
LOG.debug("Got 404, proceeding")
138
LOG.debug("Found_server=%s" % found_server)
140
# TODO(justinsb): Mock doesn't yet do accurate state changes
141
#if found_server['status'] != 'deleting':
146
self.assertFalse(found_server)
148
def _build_minimal_create_server_request(self):
151
image = self.user.get_valid_image(create=True)
152
image_id = image['id']
154
#TODO(justinsb): This is FUBAR
155
image_id = abs(hash(image_id))
157
# We now have a valid imageId
158
server['imageId'] = image_id
160
# Set a valid flavorId
161
flavor = self.api.get_flavors()[0]
162
LOG.debug("Using flavor: %s" % flavor)
163
server['flavorId'] = flavor['id']
165
# Set a valid server name
166
server_name = self.user.get_unused_server_name()
167
server['name'] = server_name
171
# TODO(justinsb): Enable this unit test when the metadata bug is fixed
172
# def test_create_server_with_metadata(self):
173
# """Creates a server with metadata"""
175
# # Build the server data gradually, checking errors along the way
176
# server = self._build_minimal_create_server_request()
178
# for metadata_count in range(30):
180
# for i in range(metadata_count):
181
# metadata['key_%s' % i] = 'value_%s' % i
182
# server['metadata'] = metadata
184
# post = {'server': server}
185
# created_server = self.api.post_server(post)
186
# LOG.debug("created_server: %s" % created_server)
187
# self.assertTrue(created_server['id'])
188
# created_server_id = created_server['id']
189
# # Reenable when bug fixed
190
# # self.assertEqual(metadata, created_server.get('metadata'))
193
# found_server = self.api.get_server(created_server_id)
194
# self.assertEqual(created_server_id, found_server['id'])
195
# self.assertEqual(metadata, found_server.get('metadata'))
197
# # The server should also be in the all-servers details list
198
# servers = self.api.get_servers(detail=True)
199
# server_map = dict((server['id'], server) for server in servers)
200
# found_server = server_map.get(created_server_id)
201
# self.assertTrue(found_server)
202
# # Details do include metadata
203
# self.assertEqual(metadata, found_server.get('metadata'))
205
# # The server should also be in the all-servers summary list
206
# servers = self.api.get_servers(detail=False)
207
# server_map = dict((server['id'], server) for server in servers)
208
# found_server = server_map.get(created_server_id)
209
# self.assertTrue(found_server)
210
# # Summary should not include metadata
211
# self.assertFalse(found_server.get('metadata'))
214
# self._delete_server(created_server_id)
217
if __name__ == "__main__":