David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame^] | 1 | # Copyright 2011 Quanta Research Cambridge, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | """Describe follow-up actions using `PendingAction` class to verify |
| 15 | that nova API calls such as create/delete are completed""" |
| 16 | |
| 17 | |
| 18 | import logging |
| 19 | import time |
| 20 | |
| 21 | |
| 22 | class PendingAction(object): |
| 23 | """ |
| 24 | Initialize and describe actions to verify that a Nova API call |
| 25 | is successful. |
| 26 | """ |
| 27 | |
| 28 | def __init__(self, nova_manager, state, target_server, timeout=600): |
| 29 | """ |
| 30 | `nova_manager` : Manager object. |
| 31 | `state` : externally maintained data structure about |
| 32 | state of VMs or other persistent objects in |
| 33 | the nova cluster |
| 34 | `target_server` : server that actions were performed on |
| 35 | `target_server` : time before we declare a TimeoutException |
| 36 | `pargs` : positional arguments |
| 37 | `kargs` : keyword arguments |
| 38 | """ |
| 39 | self._manager = nova_manager |
| 40 | self._state = state |
| 41 | self._target = target_server |
| 42 | |
| 43 | self._logger = logging.getLogger(self.__class__.__name__) |
| 44 | self._start_time = time.time() |
| 45 | self._timeout = timeout |
| 46 | |
| 47 | def _check_for_status(self, state_string): |
| 48 | """Check to see if the machine has transitioned states""" |
| 49 | t = time.time() # for debugging |
| 50 | target = self._target |
| 51 | _resp, body = self._manager.servers_client.get_server(target['id']) |
| 52 | if body['status'] != state_string: |
| 53 | # grab the actual state as we think it is |
| 54 | temp_obj = self._state.get_instances()[target['id']] |
| 55 | self._logger.debug("machine %s in state %s" % |
| 56 | (target['id'], temp_obj[1])) |
| 57 | self._logger.debug('%s, time: %d' % (temp_obj[1], time.time() - t)) |
| 58 | return temp_obj[1] |
| 59 | self._logger.debug('%s, time: %d' % (state_string, time.time() - t)) |
| 60 | return state_string |
| 61 | |
| 62 | def retry(self): |
| 63 | """Invoked by user of this class to verify completion of""" |
| 64 | """previous TestCase actions""" |
| 65 | return False |