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 | |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 17 | import logging |
| 18 | import time |
Matthew Treinish | 8d6836b | 2012-12-10 10:07:56 -0500 | [diff] [blame] | 19 | |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 20 | from tempest.exceptions import TimeoutException |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class PendingAction(object): |
| 24 | """ |
| 25 | Initialize and describe actions to verify that a Nova API call |
| 26 | is successful. |
| 27 | """ |
| 28 | |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 29 | def __init__(self, nova_manager, timeout=None): |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 30 | """ |
| 31 | `nova_manager` : Manager object. |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 32 | `timeout` : time before we declare a TimeoutException |
| 33 | """ |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 34 | if timeout is None: |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 35 | timeout = nova_manager.config.compute.build_timeout |
| 36 | self._manager = nova_manager |
| 37 | self._logger = logging.getLogger(self.__class__.__name__) |
| 38 | self._start_time = time.time() |
| 39 | self._timeout = timeout |
| 40 | |
| 41 | def retry(self): |
| 42 | """ |
| 43 | Invoked by user of this class to verify completion of |
| 44 | previous TestCase actions |
| 45 | """ |
| 46 | return False |
| 47 | |
| 48 | def check_timeout(self): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 49 | """Check for timeouts of TestCase actions.""" |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 50 | time_diff = time.time() - self._start_time |
| 51 | if time_diff > self._timeout: |
| 52 | self._logger.error('%s exceeded timeout of %d' % |
| 53 | (self.__class__.__name__, self._timeout)) |
| 54 | raise TimeoutException |
| 55 | |
| 56 | def elapsed(self): |
| 57 | return time.time() - self._start_time |
| 58 | |
| 59 | |
| 60 | class PendingServerAction(PendingAction): |
| 61 | """ |
| 62 | Initialize and describe actions to verify that a Nova API call that |
| 63 | changes server state is successful. |
| 64 | """ |
| 65 | |
| 66 | def __init__(self, nova_manager, state, target_server, timeout=None): |
| 67 | """ |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 68 | `state` : externally maintained data structure about |
| 69 | state of VMs or other persistent objects in |
| 70 | the nova cluster |
| 71 | `target_server` : server that actions were performed on |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 72 | """ |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 73 | super(PendingServerAction, self).__init__(nova_manager, |
| 74 | timeout=timeout) |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 75 | self._state = state |
| 76 | self._target = target_server |
| 77 | |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 78 | def _check_for_status(self, state_string): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 79 | """Check to see if the machine has transitioned states.""" |
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 80 | t = time.time() # for debugging |
| 81 | target = self._target |
| 82 | _resp, body = self._manager.servers_client.get_server(target['id']) |
| 83 | if body['status'] != state_string: |
| 84 | # grab the actual state as we think it is |
| 85 | temp_obj = self._state.get_instances()[target['id']] |
| 86 | self._logger.debug("machine %s in state %s" % |
| 87 | (target['id'], temp_obj[1])) |
| 88 | self._logger.debug('%s, time: %d' % (temp_obj[1], time.time() - t)) |
| 89 | return temp_obj[1] |
| 90 | self._logger.debug('%s, time: %d' % (state_string, time.time() - t)) |
| 91 | return state_string |