Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 1 | # Copyright 2016 Hewlett Packard Enterprise Development Company, L.P. |
| 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 | |
| 15 | import time |
| 16 | |
| 17 | from oslo_log import log as logging |
ZhaoBo | ade0492 | 2017-02-10 10:27:29 +0800 | [diff] [blame] | 18 | from tempest.lib.common.utils import test_utils |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 19 | from tempest.lib import exceptions as lib_exc |
| 20 | |
| 21 | LOG = logging.getLogger(__name__) |
| 22 | |
| 23 | |
| 24 | def wait_for_zone_404(client, zone_id): |
| 25 | """Waits for a zone to 404.""" |
| 26 | LOG.info('Waiting for zone %s to 404', zone_id) |
| 27 | start = int(time.time()) |
| 28 | |
| 29 | while True: |
| 30 | time.sleep(client.build_interval) |
| 31 | |
| 32 | try: |
| 33 | _, zone = client.show_zone(zone_id) |
| 34 | except lib_exc.NotFound: |
| 35 | LOG.info('Zone %s is 404ing', zone_id) |
| 36 | return |
| 37 | |
| 38 | if int(time.time()) - start >= client.build_timeout: |
| 39 | message = ('Zone %(zone_id)s failed to 404 within the required ' |
| 40 | 'time (%(timeout)s s). Current status: ' |
| 41 | '%(status_curr)s' % |
| 42 | {'zone_id': zone_id, |
| 43 | 'status_curr': zone['status'], |
| 44 | 'timeout': client.build_timeout}) |
| 45 | |
ZhaoBo | ade0492 | 2017-02-10 10:27:29 +0800 | [diff] [blame] | 46 | caller = test_utils.find_test_caller() |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 47 | |
| 48 | if caller: |
| 49 | message = '(%s) %s' % (caller, message) |
| 50 | |
| 51 | raise lib_exc.TimeoutException(message) |
| 52 | |
| 53 | |
| 54 | def wait_for_zone_status(client, zone_id, status): |
| 55 | """Waits for a zone to reach given status.""" |
| 56 | LOG.info('Waiting for zone %s to reach %s', zone_id, status) |
| 57 | |
| 58 | _, zone = client.show_zone(zone_id) |
| 59 | start = int(time.time()) |
| 60 | |
| 61 | while zone['status'] != status: |
| 62 | time.sleep(client.build_interval) |
| 63 | _, zone = client.show_zone(zone_id) |
| 64 | status_curr = zone['status'] |
| 65 | if status_curr == status: |
| 66 | LOG.info('Zone %s reached %s', zone_id, status) |
| 67 | return |
| 68 | |
| 69 | if int(time.time()) - start >= client.build_timeout: |
| 70 | message = ('Zone %(zone_id)s failed to reach status=%(status)s ' |
| 71 | 'within the required time (%(timeout)s s). Current ' |
| 72 | 'status: %(status_curr)s' % |
| 73 | {'zone_id': zone_id, |
| 74 | 'status': status, |
| 75 | 'status_curr': status_curr, |
| 76 | 'timeout': client.build_timeout}) |
| 77 | |
ZhaoBo | ade0492 | 2017-02-10 10:27:29 +0800 | [diff] [blame] | 78 | caller = test_utils.find_test_caller() |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 79 | |
| 80 | if caller: |
| 81 | message = '(%s) %s' % (caller, message) |
| 82 | |
| 83 | raise lib_exc.TimeoutException(message) |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def wait_for_zone_import_status(client, zone_import_id, status): |
| 87 | """Waits for an imported zone to reach the given status.""" |
| 88 | LOG.info('Waiting for zone import %s to reach %s', zone_import_id, status) |
| 89 | |
| 90 | _, zone_import = client.show_zone_import(zone_import_id) |
| 91 | start = int(time.time()) |
| 92 | |
| 93 | while zone_import['status'] != status: |
| 94 | time.sleep(client.build_interval) |
| 95 | _, zone_import = client.show_zone_import(zone_import_id) |
| 96 | status_curr = zone_import['status'] |
| 97 | if status_curr == status: |
| 98 | LOG.info('Zone import %s reached %s', zone_import_id, status) |
| 99 | return |
| 100 | |
| 101 | if int(time.time()) - start >= client.build_timeout: |
| 102 | message = ('Zone import %(zone_import_id)s failed to reach ' |
| 103 | 'status=%(status)s within the required time ' |
| 104 | '(%(timeout)s s). Current ' |
| 105 | 'status: %(status_curr)s' % |
| 106 | {'zone_import_id': zone_import_id, |
| 107 | 'status': status, |
| 108 | 'status_curr': status_curr, |
| 109 | 'timeout': client.build_timeout}) |
| 110 | |
ZhaoBo | ade0492 | 2017-02-10 10:27:29 +0800 | [diff] [blame] | 111 | caller = test_utils.find_test_caller() |
sonu.kumar | aec952a | 2016-04-20 10:08:46 +0900 | [diff] [blame] | 112 | |
| 113 | if caller: |
| 114 | message = '(%s) %s' % (caller, message) |
| 115 | |
| 116 | raise lib_exc.TimeoutException(message) |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 117 | |
| 118 | |
Paul Glass | 7e2a640 | 2016-06-01 21:50:17 +0000 | [diff] [blame] | 119 | def wait_for_zone_export_status(client, zone_export_id, status): |
| 120 | """Waits for an exported zone to reach the given status.""" |
| 121 | LOG.info('Waiting for zone export %s to reach %s', zone_export_id, status) |
| 122 | |
| 123 | _, zone_export = client.show_zone_export(zone_export_id) |
| 124 | start = int(time.time()) |
| 125 | |
| 126 | while zone_export['status'] != status: |
| 127 | time.sleep(client.build_interval) |
| 128 | _, zone_export = client.show_zone_export(zone_export_id) |
| 129 | status_curr = zone_export['status'] |
| 130 | if status_curr == status: |
| 131 | LOG.info('Zone export %s reached %s', zone_export_id, status) |
| 132 | return |
| 133 | |
| 134 | if int(time.time()) - start >= client.build_timeout: |
| 135 | message = ('Zone export %(zone_export_id)s failed to reach ' |
| 136 | 'status=%(status)s within the required time ' |
| 137 | '(%(timeout)s s). Current ' |
| 138 | 'status: %(status_curr)s' % |
| 139 | {'zone_export_id': zone_export_id, |
| 140 | 'status': status, |
| 141 | 'status_curr': status_curr, |
| 142 | 'timeout': client.build_timeout}) |
| 143 | |
ZhaoBo | ade0492 | 2017-02-10 10:27:29 +0800 | [diff] [blame] | 144 | caller = test_utils.find_test_caller() |
Paul Glass | 7e2a640 | 2016-06-01 21:50:17 +0000 | [diff] [blame] | 145 | |
| 146 | if caller: |
| 147 | message = '(%s) %s' % (caller, message) |
| 148 | |
| 149 | raise lib_exc.TimeoutException(message) |
| 150 | |
| 151 | |
Eric Larson | dc715e1 | 2016-08-25 13:21:35 +0100 | [diff] [blame] | 152 | def wait_for_recordset_status(client, zone_id, recordset_id, status): |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 153 | """Waits for a recordset to reach the given status.""" |
| 154 | LOG.info('Waiting for recordset %s to reach %s', |
| 155 | recordset_id, status) |
| 156 | |
Eric Larson | dc715e1 | 2016-08-25 13:21:35 +0100 | [diff] [blame] | 157 | _, recordset = client.show_recordset(zone_id, recordset_id) |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 158 | start = int(time.time()) |
| 159 | |
| 160 | while recordset['status'] != status: |
| 161 | time.sleep(client.build_interval) |
Eric Larson | dc715e1 | 2016-08-25 13:21:35 +0100 | [diff] [blame] | 162 | _, recordset = client.show_recordset(zone_id, recordset_id) |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 163 | status_curr = recordset['status'] |
| 164 | if status_curr == status: |
| 165 | LOG.info('Recordset %s reached %s', recordset_id, status) |
| 166 | return |
| 167 | |
| 168 | if int(time.time()) - start >= client.build_timeout: |
| 169 | message = ('Recordset %(recordset_id)s failed to reach ' |
Jens Harbott | d8728b4 | 2018-04-16 09:36:00 +0000 | [diff] [blame^] | 170 | 'status=%(status)s within the required time ' |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 171 | '(%(timeout)s s). Current ' |
| 172 | 'status: %(status_curr)s' % |
| 173 | {'recordset_id': recordset_id, |
| 174 | 'status': status, |
| 175 | 'status_curr': status_curr, |
| 176 | 'timeout': client.build_timeout}) |
| 177 | |
ZhaoBo | ade0492 | 2017-02-10 10:27:29 +0800 | [diff] [blame] | 178 | caller = test_utils.find_test_caller() |
sonu.kumar | de24d96 | 2016-05-05 00:28:00 +0900 | [diff] [blame] | 179 | |
| 180 | if caller: |
| 181 | message = '(%s) %s' % (caller, message) |
| 182 | |
Paul Glass | cf98c26 | 2016-05-13 19:34:37 +0000 | [diff] [blame] | 183 | raise lib_exc.TimeoutException(message) |
| 184 | |
| 185 | |
| 186 | def wait_for_query(client, name, rdatatype, found=True): |
| 187 | """Query nameservers until the record of the given name and type is found. |
| 188 | |
| 189 | :param client: A QueryClient |
| 190 | :param name: The record name for which to query |
| 191 | :param rdatatype: The record type for which to query |
| 192 | :param found: If True, wait until the record is found. Else, wait until the |
| 193 | record disappears. |
| 194 | """ |
| 195 | state = "found" if found else "removed" |
| 196 | LOG.info("Waiting for record %s of type %s to be %s on nameservers %s", |
| 197 | name, rdatatype, state, client.nameservers) |
| 198 | start = int(time.time()) |
| 199 | |
| 200 | while True: |
| 201 | time.sleep(client.build_interval) |
| 202 | |
| 203 | responses = client.query(name, rdatatype) |
| 204 | if found: |
| 205 | all_answers_good = all(r.answer for r in responses) |
| 206 | else: |
| 207 | all_answers_good = all(not r.answer for r in responses) |
| 208 | |
| 209 | if not client.nameservers or all_answers_good: |
| 210 | LOG.info("Record %s of type %s was successfully %s on nameservers " |
| 211 | "%s", name, rdatatype, state, client.nameservers) |
| 212 | return |
| 213 | |
| 214 | if int(time.time()) - start >= client.build_timeout: |
| 215 | message = ('Record %(name)s of type %(rdatatype)s not %(state)s ' |
| 216 | 'on nameservers %(nameservers)s within the required ' |
| 217 | 'time (%(timeout)s s)' % |
| 218 | {'name': name, |
| 219 | 'rdatatype': rdatatype, |
| 220 | 'state': state, |
| 221 | 'nameservers': client.nameservers, |
| 222 | 'timeout': client.build_timeout}) |
| 223 | |
ZhaoBo | ade0492 | 2017-02-10 10:27:29 +0800 | [diff] [blame] | 224 | caller = test_utils.find_test_caller() |
Paul Glass | cf98c26 | 2016-05-13 19:34:37 +0000 | [diff] [blame] | 225 | if caller: |
| 226 | message = "(%s) %s" % (caller, message) |
| 227 | |
| 228 | raise lib_exc.TimeoutException(message) |