blob: a2d5a6b989ea1c3aa315787dd1d2e3004d1a0bc5 [file] [log] [blame]
David Kranz6308ec22012-02-22 09:36:48 -05001# 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
15that nova API calls such as create/delete are completed"""
16
17
18import logging
19import time
David Kranz779c7f82012-05-01 16:50:32 -040020from tempest.exceptions import TimeoutException
David Kranz6308ec22012-02-22 09:36:48 -050021
22
23class PendingAction(object):
24 """
25 Initialize and describe actions to verify that a Nova API call
26 is successful.
27 """
28
David Kranz779c7f82012-05-01 16:50:32 -040029 def __init__(self, nova_manager, timeout=None):
David Kranz6308ec22012-02-22 09:36:48 -050030 """
31 `nova_manager` : Manager object.
David Kranz779c7f82012-05-01 16:50:32 -040032 `timeout` : time before we declare a TimeoutException
33 """
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080034 if timeout is None:
David Kranz779c7f82012-05-01 16:50:32 -040035 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):
49 """Check for timeouts of TestCase actions"""
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
60class 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 Kranz6308ec22012-02-22 09:36:48 -050068 `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 Kranz6308ec22012-02-22 09:36:48 -050072 """
David Kranz779c7f82012-05-01 16:50:32 -040073 super(PendingServerAction, self).__init__(nova_manager,
74 timeout=timeout)
David Kranz6308ec22012-02-22 09:36:48 -050075 self._state = state
76 self._target = target_server
77
David Kranz6308ec22012-02-22 09:36:48 -050078 def _check_for_status(self, state_string):
79 """Check to see if the machine has transitioned states"""
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