29
29
class ServersTest(integrated_helpers._IntegratedTestBase):
31
def _wait_for_creation(self, server):
33
while server['status'] == 'BUILD':
31
def _wait_for_state_change(self, server, status):
32
for i in xrange(0, 50):
35
33
server = self.api.get_server(server['id'])
35
if server['status'] != status:
41
def _restart_compute_service(self, periodic_interval=None):
42
"""restart compute service. NOTE: fake driver forgets all instances."""
45
self.compute = self.start_service(
46
'compute', periodic_interval=periodic_interval)
48
self.compute = self.start_service('compute')
42
50
def test_get_servers(self):
43
51
"""Simple check that listing servers works."""
44
52
servers = self.api.get_servers()
115
123
self._delete_server(created_server_id)
117
def _delete_server(self, server_id):
119
self.api.delete_server(server_id)
125
def test_deferred_delete(self):
126
"""Creates, deletes and waits for server to be reclaimed."""
127
self.flags(stub_network=True, reclaim_instance_interval=1)
129
# enforce periodic tasks run in short time to avoid wait for 60s.
130
self._restart_compute_service(periodic_interval=0.3)
133
server = self._build_minimal_create_server_request()
135
created_server = self.api.post_server({'server': server})
136
LOG.debug("created_server: %s" % created_server)
137
self.assertTrue(created_server['id'])
138
created_server_id = created_server['id']
140
# Wait for it to finish being created
141
found_server = self._wait_for_state_change(created_server, 'BUILD')
143
# It should be available...
144
self.assertEqual('ACTIVE', found_server['status'])
146
# Cannot restore unless instance is deleted
147
self.api.post_server_action(created_server_id, {'restore': {}})
149
# Check it's still active
150
found_server = self.api.get_server(created_server_id)
151
self.assertEqual('ACTIVE', found_server['status'])
153
# Cannot forceDelete unless instance is deleted
154
self.api.post_server_action(created_server_id, {'forceDelete': {}})
156
# Check it's still active
157
found_server = self.api.get_server(created_server_id)
158
self.assertEqual('ACTIVE', found_server['status'])
161
self.api.delete_server(created_server_id)
163
# Wait for queued deletion
164
found_server = self._wait_for_state_change(found_server, 'ACTIVE')
165
self.assertEqual('DELETED', found_server['status'])
167
# Wait for real deletion
168
self._wait_for_deletion(created_server_id)
170
def test_deferred_delete_restore(self):
171
"""Creates, deletes and restores a server."""
172
self.flags(stub_network=True, reclaim_instance_interval=1)
175
server = self._build_minimal_create_server_request()
177
created_server = self.api.post_server({'server': server})
178
LOG.debug("created_server: %s" % created_server)
179
self.assertTrue(created_server['id'])
180
created_server_id = created_server['id']
182
# Wait for it to finish being created
183
found_server = self._wait_for_state_change(created_server, 'BUILD')
185
# It should be available...
186
self.assertEqual('ACTIVE', found_server['status'])
189
self.api.delete_server(created_server_id)
191
# Wait for queued deletion
192
found_server = self._wait_for_state_change(found_server, 'ACTIVE')
193
self.assertEqual('DELETED', found_server['status'])
196
self.api.post_server_action(created_server_id, {'restore': {}})
198
# Wait for server to become active again
199
found_server = self._wait_for_state_change(found_server, 'DELETED')
200
self.assertEqual('ACTIVE', found_server['status'])
202
def test_deferred_delete_force(self):
203
"""Creates, deletes and force deletes a server."""
204
self.flags(stub_network=True, reclaim_instance_interval=1)
207
server = self._build_minimal_create_server_request()
209
created_server = self.api.post_server({'server': server})
210
LOG.debug("created_server: %s" % created_server)
211
self.assertTrue(created_server['id'])
212
created_server_id = created_server['id']
214
# Wait for it to finish being created
215
found_server = self._wait_for_state_change(created_server, 'BUILD')
217
# It should be available...
218
self.assertEqual('ACTIVE', found_server['status'])
221
self.api.delete_server(created_server_id)
223
# Wait for queued deletion
224
found_server = self._wait_for_state_change(found_server, 'ACTIVE')
225
self.assertEqual('DELETED', found_server['status'])
227
# Force delete server
228
self.api.post_server_action(created_server_id, {'forceDelete': {}})
230
# Wait for real deletion
231
self._wait_for_deletion(created_server_id)
233
def _wait_for_deletion(self, server_id):
121
234
# Wait (briefly) for deletion
122
for _retries in range(5):
235
for _retries in range(50):
124
237
found_server = self.api.get_server(server_id)
125
238
except client.OpenStackApiNotFoundException: