Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 1 | # All Rights Reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # 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, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 15 | import six |
| 16 | from tempest import config |
wangxiyuan | dbd33dc | 2017-02-10 09:40:50 +0800 | [diff] [blame] | 17 | from tempest.lib.common.utils import test_utils |
Lenny Verkhovsky | 8862504 | 2016-03-08 17:44:00 +0200 | [diff] [blame] | 18 | from tempest.lib import exceptions as lib_exc |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 19 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 20 | from ironic_tempest_plugin.common import utils |
| 21 | |
| 22 | CONF = config.CONF |
| 23 | |
| 24 | |
| 25 | def _determine_and_check_timeout_interval(timeout, default_timeout, |
| 26 | interval, default_interval): |
| 27 | if timeout is None: |
| 28 | timeout = default_timeout |
| 29 | if interval is None: |
| 30 | interval = default_interval |
| 31 | if (not isinstance(timeout, six.integer_types) or |
| 32 | not isinstance(interval, six.integer_types) or |
| 33 | timeout < 0 or interval < 0): |
| 34 | raise AssertionError( |
| 35 | 'timeout and interval should be >= 0 or None, current values are: ' |
| 36 | '%(timeout)s, %(interval)s respectively. If timeout and/or ' |
| 37 | 'interval are None, the default_timeout and default_interval are ' |
| 38 | 'used, and they should be integers >= 0, current values are: ' |
| 39 | '%(default_timeout)s, %(default_interval)s respectively.' % dict( |
| 40 | timeout=timeout, interval=interval, |
| 41 | default_timeout=default_timeout, |
| 42 | default_interval=default_interval) |
| 43 | ) |
| 44 | return timeout, interval |
| 45 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 46 | |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 47 | def wait_for_bm_node_status(client, node_id, attr, status, timeout=None, |
| 48 | interval=None): |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 49 | """Waits for a baremetal node attribute to reach given status. |
| 50 | |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 51 | :param client: an instance of tempest plugin BaremetalClient. |
| 52 | :param node_id: identifier of the node. |
| 53 | :param attr: node's API-visible attribute to check status of. |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 54 | :param status: desired status. Can be a list of statuses. |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 55 | :param timeout: the timeout after which the check is considered as failed. |
| 56 | Defaults to client.build_timeout. |
| 57 | :param interval: an interval between show_node calls for status check. |
| 58 | Defaults to client.build_interval. |
| 59 | |
| 60 | The client should have a show_node(node_id) method to get the node. |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 61 | """ |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 62 | timeout, interval = _determine_and_check_timeout_interval( |
| 63 | timeout, client.build_timeout, interval, client.build_interval) |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 64 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 65 | if not isinstance(status, list): |
| 66 | status = [status] |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 67 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 68 | def is_attr_in_status(): |
| 69 | node = utils.get_node(client, node_id=node_id) |
| 70 | if node[attr] in status: |
| 71 | return True |
| 72 | return False |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 73 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 74 | if not test_utils.call_until_true(is_attr_in_status, timeout, |
| 75 | interval): |
| 76 | message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s ' |
| 77 | 'within the required time (%(timeout)s s).' % |
| 78 | {'node_id': node_id, |
| 79 | 'attr': attr, |
| 80 | 'status': status, |
| 81 | 'timeout': timeout}) |
| 82 | caller = test_utils.find_test_caller() |
| 83 | if caller: |
| 84 | message = '(%s) %s' % (caller, message) |
| 85 | raise lib_exc.TimeoutException(message) |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 86 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame^] | 87 | |
| 88 | def wait_node_instance_association(client, instance_uuid, timeout=None, |
| 89 | interval=None): |
| 90 | """Waits for a node to be associated with instance_id. |
| 91 | |
| 92 | :param client: an instance of tempest plugin BaremetalClient. |
| 93 | :param instance_uuid: UUID of the instance. |
| 94 | :param timeout: the timeout after which the check is considered as failed. |
| 95 | Defaults to CONF.baremetal.association_timeout. |
| 96 | :param interval: an interval between show_node calls for status check. |
| 97 | Defaults to client.build_interval. |
| 98 | """ |
| 99 | timeout, interval = _determine_and_check_timeout_interval( |
| 100 | timeout, CONF.baremetal.association_timeout, |
| 101 | interval, client.build_interval) |
| 102 | |
| 103 | def is_some_node_associated(): |
| 104 | node = utils.get_node(client, instance_uuid=instance_uuid) |
| 105 | return node is not None |
| 106 | |
| 107 | if not test_utils.call_until_true(is_some_node_associated, timeout, |
| 108 | interval): |
| 109 | msg = ('Timed out waiting to get Ironic node by instance ID ' |
| 110 | '%(instance_id)s within the required time (%(timeout)s s).' |
| 111 | % {'instance_id': instance_uuid, 'timeout': timeout}) |
| 112 | raise lib_exc.TimeoutException(msg) |